Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Soroban Preview 11. #530

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
sreuland marked this conversation as resolved.
Show resolved Hide resolved
|| 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 @@ -23,10 +23,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 +52,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@

// struct ConfigSettingContractBandwidthV0
// {
// // Maximum size in bytes to propagate per ledger
// uint32 ledgerMaxPropagateSizeBytes;
// // Maximum sum of all transaction sizes in the ledger in bytes
// uint32 ledgerMaxTxsSizeBytes;
// // Maximum size in bytes for a transaction
// uint32 txMaxSizeBytes;
//
// // Fee for propagating 1KB of data
// int64 feePropagateData1KB;
// // Fee for 1 KB of transaction size
// int64 feeTxSize1KB;
// };

// ===========================================================================
public class ConfigSettingContractBandwidthV0 implements XdrElement {
public ConfigSettingContractBandwidthV0() {}

private Uint32 ledgerMaxPropagateSizeBytes;
private Uint32 ledgerMaxTxsSizeBytes;

public Uint32 getLedgerMaxPropagateSizeBytes() {
return this.ledgerMaxPropagateSizeBytes;
public Uint32 getLedgerMaxTxsSizeBytes() {
return this.ledgerMaxTxsSizeBytes;
}

public void setLedgerMaxPropagateSizeBytes(Uint32 value) {
this.ledgerMaxPropagateSizeBytes = value;
public void setLedgerMaxTxsSizeBytes(Uint32 value) {
this.ledgerMaxTxsSizeBytes = value;
}

private Uint32 txMaxSizeBytes;
Expand All @@ -48,23 +48,23 @@ public void setTxMaxSizeBytes(Uint32 value) {
this.txMaxSizeBytes = value;
}

private Int64 feePropagateData1KB;
private Int64 feeTxSize1KB;

public Int64 getFeePropagateData1KB() {
return this.feePropagateData1KB;
public Int64 getFeeTxSize1KB() {
return this.feeTxSize1KB;
}

public void setFeePropagateData1KB(Int64 value) {
this.feePropagateData1KB = value;
public void setFeeTxSize1KB(Int64 value) {
this.feeTxSize1KB = value;
}

public static void encode(
XdrDataOutputStream stream,
ConfigSettingContractBandwidthV0 encodedConfigSettingContractBandwidthV0)
throws IOException {
Uint32.encode(stream, encodedConfigSettingContractBandwidthV0.ledgerMaxPropagateSizeBytes);
Uint32.encode(stream, encodedConfigSettingContractBandwidthV0.ledgerMaxTxsSizeBytes);
Uint32.encode(stream, encodedConfigSettingContractBandwidthV0.txMaxSizeBytes);
Int64.encode(stream, encodedConfigSettingContractBandwidthV0.feePropagateData1KB);
Int64.encode(stream, encodedConfigSettingContractBandwidthV0.feeTxSize1KB);
}

public void encode(XdrDataOutputStream stream) throws IOException {
Expand All @@ -75,16 +75,15 @@ public static ConfigSettingContractBandwidthV0 decode(XdrDataInputStream stream)
throws IOException {
ConfigSettingContractBandwidthV0 decodedConfigSettingContractBandwidthV0 =
new ConfigSettingContractBandwidthV0();
decodedConfigSettingContractBandwidthV0.ledgerMaxPropagateSizeBytes = Uint32.decode(stream);
decodedConfigSettingContractBandwidthV0.ledgerMaxTxsSizeBytes = Uint32.decode(stream);
decodedConfigSettingContractBandwidthV0.txMaxSizeBytes = Uint32.decode(stream);
decodedConfigSettingContractBandwidthV0.feePropagateData1KB = Int64.decode(stream);
decodedConfigSettingContractBandwidthV0.feeTxSize1KB = Int64.decode(stream);
return decodedConfigSettingContractBandwidthV0;
}

@Override
public int hashCode() {
return Objects.hash(
this.ledgerMaxPropagateSizeBytes, this.txMaxSizeBytes, this.feePropagateData1KB);
return Objects.hash(this.ledgerMaxTxsSizeBytes, this.txMaxSizeBytes, this.feeTxSize1KB);
}

@Override
Expand All @@ -94,9 +93,9 @@ public boolean equals(Object object) {
}

ConfigSettingContractBandwidthV0 other = (ConfigSettingContractBandwidthV0) object;
return Objects.equals(this.ledgerMaxPropagateSizeBytes, other.ledgerMaxPropagateSizeBytes)
return Objects.equals(this.ledgerMaxTxsSizeBytes, other.ledgerMaxTxsSizeBytes)
&& Objects.equals(this.txMaxSizeBytes, other.txMaxSizeBytes)
&& Objects.equals(this.feePropagateData1KB, other.feePropagateData1KB);
&& Objects.equals(this.feeTxSize1KB, other.feeTxSize1KB);
}

@Override
Expand Down Expand Up @@ -124,12 +123,12 @@ public static ConfigSettingContractBandwidthV0 fromXdrByteArray(byte[] xdr) thro
}

public static final class Builder {
private Uint32 ledgerMaxPropagateSizeBytes;
private Uint32 ledgerMaxTxsSizeBytes;
private Uint32 txMaxSizeBytes;
private Int64 feePropagateData1KB;
private Int64 feeTxSize1KB;

public Builder ledgerMaxPropagateSizeBytes(Uint32 ledgerMaxPropagateSizeBytes) {
this.ledgerMaxPropagateSizeBytes = ledgerMaxPropagateSizeBytes;
public Builder ledgerMaxTxsSizeBytes(Uint32 ledgerMaxTxsSizeBytes) {
this.ledgerMaxTxsSizeBytes = ledgerMaxTxsSizeBytes;
return this;
}

Expand All @@ -138,16 +137,16 @@ public Builder txMaxSizeBytes(Uint32 txMaxSizeBytes) {
return this;
}

public Builder feePropagateData1KB(Int64 feePropagateData1KB) {
this.feePropagateData1KB = feePropagateData1KB;
public Builder feeTxSize1KB(Int64 feeTxSize1KB) {
this.feeTxSize1KB = feeTxSize1KB;
return this;
}

public ConfigSettingContractBandwidthV0 build() {
ConfigSettingContractBandwidthV0 val = new ConfigSettingContractBandwidthV0();
val.setLedgerMaxPropagateSizeBytes(this.ledgerMaxPropagateSizeBytes);
val.setLedgerMaxTxsSizeBytes(this.ledgerMaxTxsSizeBytes);
val.setTxMaxSizeBytes(this.txMaxSizeBytes);
val.setFeePropagateData1KB(this.feePropagateData1KB);
val.setFeeTxSize1KB(this.feeTxSize1KB);
return val;
}
}
Expand Down
Loading