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 9 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: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: yarn install
run: |
yarn cache clean
yarn install --network-concurrency 1

- name: Build
run: gulp
Expand Down
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"
}
}
23 changes: 15 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,11 @@ export class Server {
}

/**
* Submit a trial contract invocation, then add the expected ledger footprint
* and auth into the transaction so it is ready for signing & sending.
* Submit a trial contract invocation, first run a simulation of the contract
* invocation, and use the results to set the ledger footprint
* and auth into the returned transaction so it is ready for signing & sending.
* The return transaction will also have an updated fee if the contract resource
* fees estimated from simulation exceed the fee from input transaction.
*
* @example
* const contractId = '0000000000000000000000000000000000000000000000000000000000000001';
Expand Down Expand Up @@ -419,25 +422,29 @@ export class Server {
* passphrase. If not passed, the current network passphrase will be requested
* from the server via `getNetwork`.
* @returns {Promise<Transaction | FeeBumpTransaction>} Returns a copy of the
* transaction, with the expected ledger footprint added.
* transaction, with the expected ledger footprint and authorizations added
* and the transaction fee will automatically be adjusted if it was lower
* than the soroban contract minimum resource fees discovered from the simulation,
* it will be set to that minimum resource fees value instead.
*
*/
public async prepareTransaction(
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
20 changes: 13 additions & 7 deletions src/soroban_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,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;
results: SimulateHostFunctionResult[];
latestLedger: number;
cost: Cost;
}
}
110 changes: 70 additions & 40 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,53 @@ 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 classicFeeNum = parseInt(raw.fee, 10) || 0;
const minResourceFeeNum = parseInt(simulation.minResourceFee, 10) || 0;
const txnBuilder = new TransactionBuilder(source, {
// automatically update the 'classic' tx fee if min resource fees from simulation response
// surpass that value.
// 'classic' fees are measured as the product of tx.fee * 'number of operations', In soroban contract tx,
Copy link
Contributor

Choose a reason for hiding this comment

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

classic fees are classicFeeNum * operations num right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, on classic tx's that would be the way classic fees would result, with the limitation of only one operation in the soroban tx, have reduced to just classic fees = classicFeeNum(the parsed numeric value of user specified tx.fee).

// there can only be single operation in the tx, so can safely make simplification of classic fee = tx.fee.
fee: Math.max(classicFeeNum + minResourceFeeNum, classicFeeNum).toString(),
Copy link
Contributor

Choose a reason for hiding this comment

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

wait, when will this ever return the second case of the max? it seems like the first arg is strictly larger. I guess minResourceFeeNum could be negative, but that's... adversarial on the client's end?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the second argument to the Math.max function was supposed to be raw.fee

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, good point, max is un-needed at this point, the incoming tx.fee has really become the 'suggested inclusion fee' when using server.prepareTransaction(tx, networkPassphrase).

I think we can only append minResourceFee to what user provides on incoming tx.fee and return the sum as the updated tx.fee on the returned transaction from server.prepareTransaction(tx, networkPassphrase). I've updated the js docs on that method to illustrate further.

memo: raw.memo,
networkPassphrase,
timebounds: raw.timeBounds,
Expand All @@ -44,35 +62,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
const authDecoratedHostFunctions = simulation.results.map(
(functionSimulationResult, i) => {
const hostFn: xdr.HostFunction = rawInvokeHostFunctionOp.functions[i];
hostFn.auth(buildContractAuth(functionSimulationResult.auth));
return hostFn;
},
);

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",
);

// TODO - remove this workaround to use constructor on js-xdr union
// and use the typescript generated static factory method once fixed
// https://github.com/stellar/dts-xdr/issues/5
// @ts-ignore
const txExt = new xdr.TransactionExt(1, sorobanTxData);
return txExt;
}
sreuland marked this conversation as resolved.
Show resolved Hide resolved

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;
}
107 changes: 70 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,75 @@ 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",
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,
cost: {
cpuInsns: "0",
memBytes: "0",
},
};

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 +89,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 +98,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