Skip to content

Commit

Permalink
add support for Soroban Preview 11. (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Sep 18, 2023
1 parent 6c5113a commit 6a99b98
Show file tree
Hide file tree
Showing 61 changed files with 1,497 additions and 1,750 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ xdr/Stellar-internal.x \
xdr/Stellar-contract-config-setting.x

XDRGEN_COMMIT=f0c41458ca0b66b4649b18deddc9f7a11199f1f9
XDRNEXT_COMMIT=e372df9f677961aac04c5a4cc80a3667f310b29f
XDRNEXT_COMMIT=9ac02641139e6717924fdad716f6e958d0168491

.PHONY: xdr xdr-clean xdr-update

Expand Down
33 changes: 16 additions & 17 deletions src/main/java/org/stellar/sdk/InvokeHostFunctionOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import org.stellar.sdk.xdr.Hash;
import org.stellar.sdk.xdr.HostFunction;
import org.stellar.sdk.xdr.HostFunctionType;
import org.stellar.sdk.xdr.InvokeContractArgs;
import org.stellar.sdk.xdr.InvokeHostFunctionOp;
import org.stellar.sdk.xdr.OperationType;
import org.stellar.sdk.xdr.SCSymbol;
import org.stellar.sdk.xdr.SCVal;
import org.stellar.sdk.xdr.SCValType;
import org.stellar.sdk.xdr.SCVec;
import org.stellar.sdk.xdr.SorobanAuthorizationEntry;
import org.stellar.sdk.xdr.Uint256;
import org.stellar.sdk.xdr.XdrString;
Expand Down Expand Up @@ -226,32 +225,25 @@ public static InvokeHostFunctionOperation fromXdr(InvokeHostFunctionOp op) {
* parameter preset, so that you can conveniently build an {@link InvokeHostFunctionOperation} to
* invoke a contract function.
*
* @see org.stellar.sdk.scval.Scv
* @see <a
* href="https://soroban.stellar.org/docs/fundamentals-and-concepts/interacting-with-contracts"
* target="_blank">Interacting with Contracts</a>
* @param contractId The ID of the contract to invoke.
* @param functionName The name of the function to invoke.
* @param parameters The parameters to pass to the method.
* @return {@link InvokeHostFunctionOperationBuilder}
* @see org.stellar.sdk.scval.Scv
* @see <a
* href="https://soroban.stellar.org/docs/fundamentals-and-concepts/interacting-with-contracts"
* target="_blank">Interacting with Contracts</a>
*/
public static InvokeHostFunctionOperationBuilder<?, ?> invokeContractFunctionOperationBuilder(
String contractId, String functionName, @Nullable Collection<SCVal> parameters) {
Address address = new Address(contractId);
if (address.getAddressType() != Address.AddressType.CONTRACT) {
throw new IllegalArgumentException("\"contractId\" must be a contract address");
}
SCVal contractIdScVal = address.toSCVal();
SCVal functionNameScVal =
new SCVal.Builder()
.discriminant(SCValType.SCV_SYMBOL)
.sym(new SCSymbol(new XdrString(functionName)))
.build();

SCSymbol functionNameSCSymbol = new SCSymbol(new XdrString(functionName));
List<SCVal> invokeContractParams =
new ArrayList<>(2 + (parameters != null ? parameters.size() : 0));
invokeContractParams.add(contractIdScVal);
invokeContractParams.add(functionNameScVal);
new ArrayList<>((parameters != null ? parameters.size() : 0));

if (parameters != null) {
for (SCVal parameter : parameters) {
if (parameter == null) {
Expand All @@ -261,10 +253,17 @@ public static InvokeHostFunctionOperation fromXdr(InvokeHostFunctionOp op) {
}
}

InvokeContractArgs invokeContractArgs =
new InvokeContractArgs.Builder()
.contractAddress(address.toSCAddress())
.functionName(functionNameSCSymbol)
.args(invokeContractParams.toArray(new SCVal[0]))
.build();

HostFunction hostFunction =
new HostFunction.Builder()
.discriminant(HostFunctionType.HOST_FUNCTION_TYPE_INVOKE_CONTRACT)
.invokeContract(new SCVec(invokeContractParams.toArray(new SCVal[0])))
.invokeContract(invokeContractArgs)
.build();
return builder().hostFunction(hostFunction);
}
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/stellar/sdk/SorobanDataBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public SorobanDataBuilder() {
.instructions(new Uint32(new XdrUnsignedInteger(0)))
.readBytes(new Uint32(new XdrUnsignedInteger(0)))
.writeBytes(new Uint32(new XdrUnsignedInteger(0)))
.extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(0)))
.build())
.refundableFee(new Int64(0L))
.ext(new ExtensionPoint.Builder().discriminant(0).build())
Expand Down Expand Up @@ -99,9 +98,6 @@ public SorobanDataBuilder setResources(Resources resources) {
data.getResources().setReadBytes(new Uint32(new XdrUnsignedInteger(resources.getReadBytes())));
data.getResources()
.setWriteBytes(new Uint32(new XdrUnsignedInteger(resources.getWriteBytes())));
data.getResources()
.setExtendedMetaDataSizeBytes(
new Uint32(new XdrUnsignedInteger(resources.getMetadataBytes())));
return this;
}

Expand Down Expand Up @@ -160,7 +156,5 @@ public static class Resources {
@NonNull Long readBytes;
// number of bytes being written (uint32)
@NonNull Long writeBytes;
// number of extended metadata bytes (uint32)
@NonNull Long metadataBytes;
}
}
20 changes: 9 additions & 11 deletions src/main/java/org/stellar/sdk/SorobanServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SorobanRpcResponse;
import org.stellar.sdk.xdr.ContractDataDurability;
import org.stellar.sdk.xdr.ContractEntryBodyType;
import org.stellar.sdk.xdr.LedgerEntry;
import org.stellar.sdk.xdr.LedgerEntryType;
import org.stellar.sdk.xdr.LedgerKey;
Expand Down Expand Up @@ -179,7 +178,6 @@ public Optional<GetLedgerEntriesResponse.LedgerEntryResult> getContractData(
.contract(address.toSCAddress())
.key(key)
.durability(contractDataDurability)
.bodyType(ContractEntryBodyType.DATA_ENTRY)
.build();
LedgerKey ledgerKey =
new LedgerKey.Builder()
Expand Down Expand Up @@ -376,12 +374,6 @@ public Transaction prepareTransaction(Transaction transaction)
"simulation transaction failed, the response contains error information.",
simulateTransactionResponse);
}
if (simulateTransactionResponse.getResults() == null
|| simulateTransactionResponse.getResults().size() != 1) {
throw new PrepareTransactionException(
"simulation transaction failed, the \"results\" field contains multiple records, but it should only contain one.",
simulateTransactionResponse);
}
return assembleTransaction(transaction, simulateTransactionResponse);
}

Expand Down Expand Up @@ -416,9 +408,6 @@ private Transaction assembleTransaction(
"unsupported transaction: must contain exactly one InvokeHostFunctionOperation, BumpSequenceOperation, or RestoreFootprintOperation");
}

SimulateTransactionResponse.SimulateHostFunctionResult simulateHostFunctionResult =
simulateTransactionResponse.getResults().get(0);

long classicFeeNum = transaction.getFee();
long minResourceFeeNum =
Optional.ofNullable(simulateTransactionResponse.getMinResourceFee()).orElse(0L);
Expand All @@ -428,6 +417,15 @@ private Transaction assembleTransaction(
if (operation instanceof InvokeHostFunctionOperation) {
// If the operation is an InvokeHostFunctionOperation, we need to update the auth entries if
// existing entries are empty and the simulation result contains auth entries.
if (simulateTransactionResponse.getResults() == null
|| simulateTransactionResponse.getResults().size() != 1) {
throw new IllegalArgumentException(
"invalid simulateTransactionResponse: results must contain exactly one element if the operation is an InvokeHostFunctionOperation");
}

SimulateTransactionResponse.SimulateHostFunctionResult simulateHostFunctionResult =
simulateTransactionResponse.getResults().get(0);

Collection<SorobanAuthorizationEntry> existingEntries =
((InvokeHostFunctionOperation) operation).getAuth();
if (existingEntries.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
/**
* Response for JSON-RPC method simulateTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/simulateTransaction#returns"
* target="_blank">simulateTransaction documentation</a>
* <p>Note - The simulation response will have different model representations with different
* members present or absent depending on type of response that it is conveying. For example, the
* simulation response for invoke host function, could be one of three types: error, success, or
* restore operation needed.
*
* <p>Please refer to the latest <a
* href="https://soroban.stellar.org/api/methods/simulateTransaction#returns"
* target="_blank">Soroban simulateTransaction documentation</a> for details on which members of the
* simulation response model are keyed to each type of response.
*/
@AllArgsConstructor
@Value
Expand All @@ -23,10 +30,16 @@ public class SimulateTransactionResponse {

Long minResourceFee;

// An array of the individual host function call results.
// This will only contain a single element if present, because only a single
// invokeHostFunctionOperation
// is supported per transaction.
ImmutableList<SimulateHostFunctionResult> results;

SimulateTransactionCost cost;

RestorePreamble restorePreamble;

Long latestLedger;

@AllArgsConstructor
Expand All @@ -46,4 +59,12 @@ public static class SimulateTransactionCost {
@SerializedName("memBytes")
BigInteger memoryBytes;
}

@AllArgsConstructor
@Value
public static class RestorePreamble {
String transactionData;

Long minResourceFee;
}
}
5 changes: 1 addition & 4 deletions src/main/java/org/stellar/sdk/scval/ScvError.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ class ScvError {
private static final SCValType TYPE = SCValType.SCV_ERROR;

static SCVal toSCVal(SCError value) {
return new SCVal.Builder()
.discriminant(TYPE)
.error(new SCError.Builder().code(value.getCode()).type(value.getType()).build())
.build();
return new SCVal.Builder().discriminant(TYPE).error(value).build();
}

static SCError fromSCVal(SCVal scVal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// void;
// case BUMP_FOOTPRINT_EXPIRATION_MALFORMED:
// case BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED:
// case BUMP_FOOTPRINT_EXPIRATION_INSUFFICIENT_REFUNDABLE_FEE:
// void;
// };

Expand Down Expand Up @@ -63,6 +64,7 @@ public static void encode(
break;
case BUMP_FOOTPRINT_EXPIRATION_MALFORMED:
case BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED:
case BUMP_FOOTPRINT_EXPIRATION_INSUFFICIENT_REFUNDABLE_FEE:
break;
}
}
Expand All @@ -82,6 +84,7 @@ public static BumpFootprintExpirationResult decode(XdrDataInputStream stream) th
break;
case BUMP_FOOTPRINT_EXPIRATION_MALFORMED:
case BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED:
case BUMP_FOOTPRINT_EXPIRATION_INSUFFICIENT_REFUNDABLE_FEE:
break;
}
return decodedBumpFootprintExpirationResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
//
// // codes considered as "failure" for the operation
// BUMP_FOOTPRINT_EXPIRATION_MALFORMED = -1,
// BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED = -2
// BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED = -2,
// BUMP_FOOTPRINT_EXPIRATION_INSUFFICIENT_REFUNDABLE_FEE = -3
// };

// ===========================================================================
public enum BumpFootprintExpirationResultCode implements XdrElement {
BUMP_FOOTPRINT_EXPIRATION_SUCCESS(0),
BUMP_FOOTPRINT_EXPIRATION_MALFORMED(-1),
BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED(-2),
BUMP_FOOTPRINT_EXPIRATION_INSUFFICIENT_REFUNDABLE_FEE(-3),
;
private int mValue;

Expand All @@ -48,6 +50,8 @@ public static BumpFootprintExpirationResultCode decode(XdrDataInputStream stream
return BUMP_FOOTPRINT_EXPIRATION_MALFORMED;
case -2:
return BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED;
case -3:
return BUMP_FOOTPRINT_EXPIRATION_INSUFFICIENT_REFUNDABLE_FEE;
default:
throw new RuntimeException("Unknown enum value: " + value);
}
Expand Down
Loading

0 comments on commit 6a99b98

Please sign in to comment.