Skip to content

Commit

Permalink
Feat/ra 26 implement construction submit endpoint (#101)
Browse files Browse the repository at this point in the history
* chore: node version bump

* feat: initial submit implementation

* feat: initial submit implementation

* chore: added postman tests

* chore: fixed property and cleaned up unused property files

* refactor: extracted Exception handling from controller & added javadoc

* refactor
  • Loading branch information
Kammerlo authored Apr 2, 2024
1 parent 976dcfd commit b3425e7
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 166 deletions.
3 changes: 2 additions & 1 deletion .env.IntegrationTest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ JDBC_BATCH_SIZE=1000
SCHEMA=${NETWORK}
CARDANO_NODE_HOST="yaci-cli" # Service name in docker-compose or local cardano node
CARDANO_NODE_PORT="9999" # Uncomment if you are using local cardano node
CARDANO_NODE_VERSION="8.1.2"
CARDANO_NODE_VERSION="8.9.0"
NODE_SUBMIT_API_PORT=8090
LOG=INFO

#api env
Expand Down
3 changes: 2 additions & 1 deletion .env.docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ JDBC_BATCH_SIZE=1000
SCHEMA=${NETWORK}
CARDANO_NODE_HOST="cardano-node" # Service name in docker-compose or local cardano node
CARDANO_NODE_PORT="3001" # Uncomment if you are using local cardano node
CARDANO_NODE_VERSION="8.1.2"
CARDANO_NODE_VERSION="8.9.0"
NODE_SUBMIT_API_PORT=8090
LOG=INFO

#api env
Expand Down
3 changes: 2 additions & 1 deletion .env.h2
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ JDBC_BATCH_SIZE=1000
SCHEMA=${NETWORK}
CARDANO_NODE_HOST=localhost # Service name in docker-compose or local cardano node
CARDANO_NODE_PORT=3001 # Uncomment if you are using local cardano node
CARDANO_NODE_VERSION=8.1.2
CARDANO_NODE_VERSION=8.9.0
NODE_SUBMIT_API_PORT=8090
LOG=INFO

#api env
Expand Down
3 changes: 2 additions & 1 deletion .envPreprod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ DB_SCHEMA="preprod"
MAXIMUM_POOL_SIZE=80
JDBC_BATCH_SIZE=1000
SCHEMA=preprod
CARDANO_NODE_VERSION="8.1.2"
CARDANO_NODE_VERSION="8.9.0"
NODE_SUBMIT_API_PORT=8090
CARDANO_NODE_HOST="cardano-node"
LOG=INFO

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ to fetch the data from the node.
- [x] /construction/combine
- [x] /construction/parse
- [x] /construction/hash
- [ ] /construction/submit
- [x] /construction/submit

## Getting Started

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public TransactionIdentifierResponse constructionHashService(
@Override
public TransactionIdentifierResponse constructionSubmitService(
ConstructionSubmitRequest constructionSubmitRequest) {
return null;
String signedTransaction = constructionSubmitRequest.getSignedTransaction();
log.info("[constructionSubmit] About to submit signed transaction");
String txHash = cardanoService.submitTransaction(signedTransaction);

return new TransactionIdentifierResponse(new TransactionIdentifier(txHash), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,7 @@ public static ApiException invalidNetworkError() {
return new ApiException(RosettaErrorType.INVALID_NETWORK.toRosettaError(false));
}

public static ApiException sendTransactionError(String error) {
return new ApiException(RosettaErrorType.SEND_TRANSACTION_ERROR.toRosettaError(false, null, error));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ ProcessOperations convertRosettaOperations(NetworkIdentifierType networkIdentifi
List<Operation> operations) throws IOException;

UnsignedTransaction createUnsignedTransaction(NetworkIdentifierType networkIdentifier, List<Operation> operations, int ttl, DepositParameters depositParameters) throws IOException, CborSerializationException, AddressExcepion, CborException;

String submitTransaction(String signedTransaction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@
import com.bloxbean.cardano.client.transaction.spec.TransactionWitnessSet;
import com.bloxbean.cardano.client.transaction.spec.VkeyWitness;
import com.bloxbean.cardano.client.util.HexUtil;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.lang3.ObjectUtils;
import org.cardanofoundation.rosetta.api.block.model.domain.ProcessOperations;
import org.cardanofoundation.rosetta.api.block.model.domain.ProcessOperationsReturn;
import org.cardanofoundation.rosetta.common.enumeration.AddressType;
import org.cardanofoundation.rosetta.common.enumeration.EraAddressType;
import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType;
import org.cardanofoundation.rosetta.common.exception.ApiException;
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.entity.ProtocolParams;
Expand All @@ -47,6 +56,7 @@
import org.openapitools.client.model.SignatureType;
import org.openapitools.client.model.SigningPayload;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
import java.util.ArrayList;
Expand All @@ -64,7 +74,11 @@
@RequiredArgsConstructor
public class CardanoServiceImpl implements CardanoService {

private final LedgerDataProviderService ledgerDataProviderService;
private final LedgerDataProviderService ledgerDataProviderService;
@Value("${cardano.rosetta.NODE_SUBMIT_API_PORT}")
private int NODE_SUBMIT_API_PORT;
@Value("${cardano.rosetta.CARDANO_NODE_HOST}")
private String CARDANO_NODE_HOST;

@Override
public TransactionParsed parseTransaction(NetworkIdentifierType networkIdentifierType,
Expand Down Expand Up @@ -451,4 +465,48 @@ public ProcessOperations convertRosettaOperations(NetworkIdentifierType networkI
return processor;
}

/**
* Submits the signed transaction to the preconfigured SubmitAPI. If successfull the transaction hash is returned.
* @param signedTransaction signed transaction in hex format
* @return transaction hash
* @throws ApiException if the transaction submission fails, additional information is provided in the exception message
*/
@Override
public String submitTransaction(String signedTransaction) throws ApiException {
String submitURL = Constants.PROTOCOL + CARDANO_NODE_HOST + ":" + NODE_SUBMIT_API_PORT
+ Constants.SUBMIT_API_PATH;
log.info("[submitTransaction] About to submit transaction to {}", submitURL);
Request request = null;
request = new Builder()
.url(submitURL)
.addHeader(Constants.CONTENT_TYPE_HEADER_KEY, Constants.CBOR_CONTENT_TYPE)
.post(RequestBody.create(MediaType.parse(Constants.CBOR_CONTENT_TYPE),
HexUtil.decodeHexString(signedTransaction)))
.build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();

if (response.code() == Constants.SUCCESS_SUBMIT_TX_HTTP_CODE) {
String txHash = response.body().string();
// removing leading and trailing quotes returned from node API
if (txHash.length() == Constants.TX_HASH_LENGTH + 2) {
txHash = txHash.substring(1, txHash.length() - 1);
}
return txHash;
} else {
throw ExceptionFactory.sendTransactionError(response.body().string());
}
} catch (IOException e) {
log.error(e.getMessage() + "[submitTransaction] There was an error submitting transaction");
throw ExceptionFactory.sendTransactionError(e.getMessage());
} finally {
if (response != null) {
response.close();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public class Constants {
public static final String RELATIVE_TTL = "relative_ttl";
public static final String TRANSACTION_SIZE = "transaction_size";
public static final int HEX_PREFIX_AND_REWARD_ACCOUNT_LENGTH = 58;
public static final String SUBMIT_API_PATH = "/api/submit/tx";
public static final int SUCCESS_SUBMIT_TX_HTTP_CODE = 202;
public static final String CBOR_CONTENT_TYPE = "application/cbor";
public static final String CONTENT_TYPE_HEADER_KEY = "Content-Type";
public static final int TX_HASH_LENGTH = 64;
public static final String PROTOCOL = "http://";

private Constants() {
}
Expand Down
6 changes: 0 additions & 6 deletions api/src/main/resources/config/application-cluster.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions api/src/main/resources/config/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ cardano:
GENESIS_SHELLEY_PATH: ${GENESIS_SHELLEY_PATH}
CARDANO_NODE_VERSION: ${CARDANO_NODE_VERSION}
EXEMPTION_TYPES_PATH: ${EXEMPTION_TYPES_PATH}
CARDANO_NODE_HOST: ${CARDANO_NODE_HOST}
NODE_SUBMIT_API_PORT: ${NODE_SUBMIT_API_PORT}

page-size: ${PAGE_SIZE:5}

Expand Down
2 changes: 2 additions & 0 deletions api/src/main/resources/config/application-h2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ cardano:
GENESIS_SHELLEY_PATH: ${GENESIS_SHELLEY_PATH}
CARDANO_NODE_VERSION: ${CARDANO_NODE_VERSION}
EXEMPTION_TYPES_PATH: ${EXEMPTION_TYPES_PATH}
CARDANO_NODE_HOST: ${CARDANO_NODE_HOST}
NODE_SUBMIT_API_PORT: ${NODE_SUBMIT_API_PORT}

page-size: ${PAGE_SIZE:5}

Expand Down
14 changes: 0 additions & 14 deletions api/src/main/resources/config/application-sentinel.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions api/src/main/resources/config/application-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ cardano:
GENESIS_SHELLEY_PATH: ${GENESIS_SHELLEY_PATH}
CARDANO_NODE_PATH: ${CARDANO_NODE_PATH}
EXEMPTION_TYPES_PATH: ${EXEMPTION_TYPES_PATH}
CARDANO_NODE_HOST: ${CARDANO_NODE_HOST}
NODE_SUBMIT_API_PORT: ${NODE_SUBMIT_API_PORT}

page-size: ${PAGE_SIZE:5}

Expand Down
6 changes: 0 additions & 6 deletions api/src/main/resources/config/application-standalone.yaml

This file was deleted.

129 changes: 0 additions & 129 deletions api/src/main/resources/config/application-test.yaml

This file was deleted.

4 changes: 3 additions & 1 deletion api/src/test/resources/application-test-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ cardano:
'1.0.0-SNAPSHOT'
TOPOLOGY_FILEPATH: ../config/devkit/topology.json
GENESIS_SHELLEY_PATH: ../config/devkit/shelley-genesis.json
CARDANO_NODE_VERSION: "8.1.2"
CARDANO_NODE_VERSION: "8.9.0"
CARDANO_NODE_HOST: localhost
NODE_SUBMIT_API_PORT: 8090

page-size: ${PAGE_SIZE:5}
cardano-transaction-submitter:
Expand Down
2 changes: 2 additions & 0 deletions docker-api-indexer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ services:
TRANSACTION_TTL: ${TRANSACTION_TTL}
TOPOLOGY_FILEPATH: ${TOPOLOGY_FILEPATH}
GENESIS_SHELLEY_PATH: ${GENESIS_SHELLEY_PATH}
NODE_SUBMIT_API_PORT: ${NODE_SUBMIT_API_PORT}
CARDANO_NODE_HOST: ${CARDANO_NODE_HOST}
depends_on:
db:
condition: service_healthy
Expand Down
Loading

0 comments on commit b3425e7

Please sign in to comment.