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

Use the new Soroban RPC simulation method when preparing transactions #77

Merged
merged 16 commits into from
May 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

A breaking change should be clearly marked in this log.

#### Pending
updated prepare transaction for new soroban simulation results and fees [#76](https://github.com/stellar/js-soroban-client/issues/76)


#### 0.5.1

* remove params from jsonrpc post payload if empty. [#70](https://github.com/stellar/js-soroban-client/pull/70)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"axios": "0.25.0",
"es6-promise": "^4.2.4",
"lodash": "4.17.21",
"stellar-base": "8.2.2-soroban.12",
"stellar-base": "https://github.com/stellar/js-stellar-base\\#host_functions_ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change this to v9.0.0-soroban.1 when it's out, for now using this slightly newer stellar-base that has tweaks on txbuilder - js-stellar-base#604

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just going to unresolve this so you don't forget after you get approval

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"urijs": "^1.19.1"
}
}
10 changes: 5 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,19 @@ export class Server {
transaction: Transaction | FeeBumpTransaction,
networkPassphrase?: string,
): Promise<Transaction | FeeBumpTransaction> {
const [{ passphrase }, { error, results }] = await Promise.all([
const [{ passphrase }, simResponse] = await Promise.all([
networkPassphrase
? Promise.resolve({ passphrase: networkPassphrase })
: this.getNetwork(),
this.simulateTransaction(transaction),
]);
if (error) {
throw error;
if (simResponse.error) {
throw simResponse.error;
}
if (!results) {
if (!simResponse.results || simResponse.results.length < 1) {
throw new Error("transaction simulation failed");
}
return assembleTransaction(transaction, passphrase, results);
return assembleTransaction(transaction, passphrase, simResponse);
}

/**
Expand Down
25 changes: 13 additions & 12 deletions src/soroban_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export namespace SorobanRpc {
smart: string;
}

export interface Cost {
cpuInsns: string;
memBytes: string;
}

export interface GetHealthResponse {
status: "healthy";
}
Expand Down Expand Up @@ -112,16 +107,22 @@ export namespace SorobanRpc {
latestLedgerCloseTime: number;
}

export interface SimulateHostFunctionResult {
// each string is ContractAuth XDR in base64
auth: string[];
// function response as SCVal XDR in base64
xdr: string;
}

export interface SimulateTransactionResponse {
id: string;
cost: Cost;
results?: Array<{
xdr: string;
footprint: string;
auth: string[];
events: string[];
}>;
error?: jsonrpc.Error;
// this is SorobanTransactionData XDR in base64
transactionData: string;
events: string[];
minResourceFee: string;
suggestedInclusionFee: string;
results: SimulateHostFunctionResult[];
latestLedger: number;
}
}
111 changes: 71 additions & 40 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,54 @@ import {
TransactionBuilder,
xdr,
} from "stellar-base";
import { SorobanRpc } from "./soroban_rpc";

// TODO: Transaction is immutable, so we need to re-build it here. :(
export function assembleTransaction(
raw: Transaction | FeeBumpTransaction,
networkPassphrase: string,
simulated: Array<null | {
footprint: Buffer | string | xdr.LedgerFootprint;
auth: Array<Buffer | string | xdr.ContractAuth>;
}>,
simulation: SorobanRpc.SimulateTransactionResponse,
): Transaction {
if ("innerTransaction" in raw) {
// TODO: Handle feebump transactions
return assembleTransaction(
raw.innerTransaction,
networkPassphrase,
simulated,
simulation,
);
}

if (simulated.length !== raw.operations.length) {
if (
raw.operations.length !== 1 ||
raw.operations[0].type !== "invokeHostFunction"
) {
throw new Error(
"number of simulated operations not equal to number of transaction operations",
"unsupported operation type, must be only one InvokeHostFunctionOp in the transaction.",
);
}

const rawInvokeHostFunctionOp: any = raw.operations[0];

if (
!rawInvokeHostFunctionOp.functions ||
!simulation.results ||
rawInvokeHostFunctionOp.functions.length !== simulation.results.length
) {
throw new Error(
"preflight simulation results do not contain same count of HostFunctions that InvokeHostFunctionOp in the transaction has.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the intricacies of the multi-op host functions yet: why is the length enforced to be 1 above, but that isn't enforced here? I guess it's implicit, but I just want to make sure.

Related: is there no way to simulate transactions with multiple invokeHostOperations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmkozh , can you confirm with the latest changes for multi-function InvokeHostFunctionOp, the limitation for a single InvokeHostFunctionOp in Tx is still being enforced? I thought have seen that mentioned in chat and was assuming that limitation on validation of tx here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh yeah sorry, you're right 🤦 one op per tx, but the op can have many fns. got it!

😆 too many layers 🍰

);
}

// TODO: Figure out a cleaner way to clone this transaction.
const source = new Account(raw.source, `${parseInt(raw.sequence, 10) - 1}`);
const txn = new TransactionBuilder(source, {
fee: raw.fee,
const authDecoratedHostFunctions: xdr.HostFunction[] = [];

const txnBuilder = new TransactionBuilder(source, {
// automatically update the tx fee based on suggested fees from simulation response
fee: Math.max(
parseInt(raw.fee, 10) || 0,
(parseInt(simulation.minResourceFee, 10) || 0) +
(parseInt(simulation.suggestedInclusionFee, 10) || 0),
).toString(),
memo: raw.memo,
networkPassphrase,
timebounds: raw.timeBounds,
Expand All @@ -44,35 +63,47 @@ export function assembleTransaction(
minAccountSequenceLedgerGap: raw.minAccountSequenceLedgerGap,
extraSigners: raw.extraSigners,
});
for (let i = 0; i < raw.operations.length; i++) {
const rawOp = raw.operations[i];
if ("function" in rawOp) {
const sim = simulated[i];
if (!sim) {
throw new Error("missing simulated operation");
}
let footprint = sim.footprint ?? rawOp.footprint;
if (!(footprint instanceof xdr.LedgerFootprint)) {
footprint = xdr.LedgerFootprint.fromXDR(footprint.toString(), "base64");
}
const auth = (sim.auth ?? rawOp.auth).map((a) =>
a instanceof xdr.ContractAuth
? a
: xdr.ContractAuth.fromXDR(a.toString(), "base64"),
);
// TODO: Figure out a cleaner way to clone these operations
txn.addOperation(
Operation.invokeHostFunction({
function: rawOp.function,
parameters: rawOp.parameters,
footprint,
auth,
}),
);
} else {
// TODO: Handle this.
throw new Error("Unsupported operation type");
}

// apply the pre-built Auth from simulation onto each Tx/Op/HostFunction invocation
for (
let hostFnIndex = 0;
hostFnIndex < simulation.results.length;
hostFnIndex++
) {
const rawHostFunction: xdr.HostFunction =
rawInvokeHostFunctionOp.functions[hostFnIndex];
const simHostFunctionResult: SorobanRpc.SimulateHostFunctionResult =
simulation.results[hostFnIndex];
rawHostFunction.auth(buildContractAuth(simHostFunctionResult.auth));
authDecoratedHostFunctions.push(rawHostFunction);
}
sreuland marked this conversation as resolved.
Show resolved Hide resolved

txnBuilder.addOperation(
Operation.invokeHostFunctions({
functions: authDecoratedHostFunctions,
}),
);

// apply the pre-built Soroban Tx Ext from simulation onto the Tx
txnBuilder.setExt(buildExt(simulation.transactionData));

return txnBuilder.build();
}

function buildExt(sorobanTxDataStr: string) {
const sorobanTxData: xdr.SorobanTransactionData = xdr.SorobanTransactionData.fromXDR(
sreuland marked this conversation as resolved.
Show resolved Hide resolved
sorobanTxDataStr,
"base64",
);
const txExt: xdr.TransactionExt = new xdr.TransactionExt(1);
sreuland marked this conversation as resolved.
Show resolved Hide resolved
txExt.sorobanData(sorobanTxData);
return txExt;
}

function buildContractAuth(auths: string[]): xdr.ContractAuth[] {
const contractAuths: xdr.ContractAuth[] = [];
for (const authStr of auths) {
contractAuths.push(xdr.ContractAuth.fromXDR(authStr, "base64"));
}
return txn.build();
return contractAuths;
}
104 changes: 67 additions & 37 deletions test/unit/server/simulate_transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,72 @@ describe("Server#simulateTransaction", function() {
"56199647068161",
);

const simulationResponse = {
transactionData: new SorobanClient.xdr.SorobanTransactionData({
resources: new SorobanClient.xdr.SorobanResources({
footprint: new SorobanClient.xdr.LedgerFootprint({
readOnly: [],
readWrite: [],
}),
instructions: 0,
readBytes: 0,
writeBytes: 0,
extendedMetaDataSizeBytes: 0,
}),
refundableFee: SorobanClient.xdr.Int64.fromString("0"),
ext: new SorobanClient.xdr.ExtensionPoint(0),
}).toXDR("base64"),
events: [],
minResourceFee: "15",
suggestedInclusionFee: "3",
results: [
{
auth: [
new SorobanClient.xdr.ContractAuth({
addressWithNonce: null,
rootInvocation: new SorobanClient.xdr.AuthorizedInvocation({
contractId: Buffer.alloc(32),
functionName: "fn",
args: [],
subInvocations: [],
}),
signatureArgs: [],
}).toXDR("base64"),
],
xdr: SorobanClient.xdr.ScVal.scvU32(0)
.toXDR()
.toString("base64"),
},
],
latestLedger: 3,
};

beforeEach(function() {
this.server = new SorobanClient.Server(serverUrl);
this.axiosMock = sinon.mock(AxiosClient);
let transaction = new SorobanClient.TransactionBuilder(account, {
fee: 100,
networkPassphrase: SorobanClient.Networks.TESTNET,
v1: true,
})
.addOperation(
SorobanClient.Operation.payment({
destination:
"GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW",
asset: SorobanClient.Asset.native(),
amount: "100.50",
}),
)
.setTimeout(SorobanClient.TimeoutInfinite)
.build();
const source = new SorobanClient.Account(
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI",
"1",
);
function emptyContractTransaction() {
return new SorobanClient.TransactionBuilder(source, {
fee: 100,
networkPassphrase: "Test",
v1: true,
})
.addOperation(
SorobanClient.Operation.invokeHostFunction({
args: new SorobanClient.xdr.HostFunctionArgs.hostFunctionTypeInvokeContract(
[],
),
auth: [],
}),
)
.setTimeout(SorobanClient.TimeoutInfinite)
.build();
}

const transaction = emptyContractTransaction();
transaction.sign(keypair);

this.transaction = transaction;
Expand All @@ -38,26 +86,6 @@ describe("Server#simulateTransaction", function() {
this.axiosMock.restore();
});

const result = {
cost: {
cpuInsns: "10000",
memBytes: "10000",
},
results: [
{
xdr: SorobanClient.xdr.ScVal.scvU32(0)
.toXDR()
.toString("base64"),
footprint: new SorobanClient.xdr.LedgerFootprint({
readOnly: [],
readWrite: [],
}).toXDR("base64"),
events: [],
},
],
latestLedger: 1,
};

it("simulates a transaction", function(done) {
this.axiosMock
.expects("post")
Expand All @@ -67,12 +95,14 @@ describe("Server#simulateTransaction", function() {
method: "simulateTransaction",
params: [this.blob],
})
.returns(Promise.resolve({ data: { id: 1, result } }));
.returns(
Promise.resolve({ data: { id: 1, result: simulationResponse } }),
);

this.server
.simulateTransaction(this.transaction)
.then(function(response) {
expect(response).to.be.deep.equal(result);
expect(response).to.be.deep.equal(simulationResponse);
done();
})
.catch(function(err) {
Expand Down
Loading