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

Correct all breaking changes in RPC for Protocol 22 #1084

Merged
merged 10 commits into from
Nov 8, 2024
31 changes: 20 additions & 11 deletions src/rpc/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import { Contract, SorobanDataBuilder, xdr } from '@stellar/stellar-base';

/* tslint:disable-next-line:no-namespace */
export namespace Api {

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

export interface GetHealthResponse {
status: 'healthy';
}
Expand Down Expand Up @@ -72,6 +66,7 @@ export namespace Api {

interface GetAnyTransactionResponse {
status: GetTransactionStatus;
txHash: string;
latestLedger: number;
latestLedgerCloseTime: number;
oldestLedger: number;
Expand Down Expand Up @@ -119,15 +114,17 @@ export namespace Api {
latestLedgerCloseTime: number;
oldestLedger: number;
oldestLedgerCloseTime: number;
txHash: string;

// the fields below are set if status is SUCCESS
applicationOrder?: number;
feeBump?: boolean;
ledger?: number;
createdAt?: number;

envelopeXdr?: string;
resultXdr?: string;
resultMetaXdr?: string;
ledger?: number;
createdAt?: number;
diagnosticEventsXdr?: string[];
}

Expand All @@ -143,6 +140,8 @@ export namespace Api {
createdAt: number;
applicationOrder: number;
feeBump: boolean;
txHash: string;

envelopeXdr?: string;
resultXdr?: string;
resultMetaXdr?: string;
Expand All @@ -155,6 +154,8 @@ export namespace Api {
createdAt: number;
applicationOrder: number;
feeBump: boolean;
txHash: string;

envelopeXdr: xdr.TransactionEnvelope;
resultXdr: xdr.TransactionResult;
resultMetaXdr: xdr.TransactionMeta;
Expand Down Expand Up @@ -209,6 +210,7 @@ export namespace Api {
type: EventType;
ledger: number;
ledgerClosedAt: string;
cursor: string;
pagingToken: string;
inSuccessfulContractCall: boolean;
txHash: string;
Expand Down Expand Up @@ -317,7 +319,6 @@ export namespace Api {
extends BaseSimulateTransactionResponse {
transactionData: SorobanDataBuilder;
minResourceFee: string;
cost: Cost;

/** present only for invocation simulation */
result?: SimulateHostFunctionResult;
Expand Down Expand Up @@ -403,7 +404,6 @@ export namespace Api {
* invokeHostFunctionOperation is supported per transaction.
* */
results?: RawSimulateHostFunctionResult[];
cost?: Cost;
/** Present if succeeded but has expired ledger entries */
restorePreamble?: {
minResourceFee: string;
Expand All @@ -416,9 +416,18 @@ export namespace Api {

export interface GetVersionInfoResponse {
version: string;
commitHash: string;
buildTimestamp: string;
captiveCoreVersion: string;
protocolVersion: number; // uint32

/// @deprecated
commit_hash: string;
build_time_stamp: string;
/// @deprecated
build_timestamp: string;
/// @deprecated
captive_core_version: string;
/// @deprecated
protocol_version: number; // uint32
}

Expand Down
12 changes: 7 additions & 5 deletions src/rpc/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ export function parseRawSendTransaction(
return { ...raw } as Api.BaseSendTransactionResponse;
}

export function parseTransactionInfo(raw: Api.RawTransactionInfo | Api.RawGetTransactionResponse): Omit<Api.TransactionInfo, 'status'> {
export function parseTransactionInfo(
raw: Api.RawTransactionInfo | Api.RawGetTransactionResponse
): Omit<Api.TransactionInfo, 'status' | 'txHash'> {
const meta = xdr.TransactionMeta.fromXDR(raw.resultMetaXdr!, 'base64');
const info: Omit<Api.TransactionInfo, 'status'> = {
const info: Omit<Api.TransactionInfo, 'status' | 'txHash'> = {
ledger: raw.ledger!,
createdAt: raw.createdAt!,
applicationOrder: raw.applicationOrder!,
Expand Down Expand Up @@ -64,6 +66,7 @@ export function parseRawTransactions(
): Api.TransactionInfo {
return {
status: r.status,
txHash: r.txHash,
...parseTransactionInfo(r),
};
}
Expand Down Expand Up @@ -147,11 +150,10 @@ function parseSuccessful(
...partial,
transactionData: new SorobanDataBuilder(sim.transactionData!),
minResourceFee: sim.minResourceFee!,
cost: sim.cost!,
...// coalesce 0-or-1-element results[] list into a single result struct
// coalesce 0-or-1-element results[] list into a single result struct
// with decoded fields if present
// eslint-disable-next-line no-self-compare
((sim.results?.length ?? 0 > 0) && {
...((sim.results?.length ?? 0 > 0) && {
result: sim.results!.map((row) => ({
auth: (row.auth ?? []).map((entry) =>
xdr.SorobanAuthorizationEntry.fromXDR(entry, 'base64')
Expand Down
5 changes: 3 additions & 2 deletions src/rpc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ export class RpcServer {
): Promise<Api.GetTransactionResponse> {
return this._getTransaction(hash).then((raw) => {
const foundInfo: Omit<
Api.GetSuccessfulTransactionResponse,
keyof Api.GetMissingTransactionResponse
Api.GetSuccessfulTransactionResponse,
keyof Api.GetMissingTransactionResponse
> = {} as any;

if (raw.status !== Api.GetTransactionStatus.NOT_FOUND) {
Expand All @@ -490,6 +490,7 @@ export class RpcServer {

const result: Api.GetTransactionResponse = {
status: raw.status,
txHash: hash,
latestLedger: raw.latestLedger,
latestLedgerCloseTime: raw.latestLedgerCloseTime,
oldestLedger: raw.oldestLedger,
Expand Down
8 changes: 4 additions & 4 deletions test/unit/server/soroban/get_events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ let getEventsResponseFixture = [
ledgerClosedAt: "2022-11-16T16:10:41Z",
contractId: "",
id: "0164090849041387521-0000000003",
pagingToken: "164090849041387521-3",
cursor: "164090849041387521-3",
inSuccessfulContractCall: true,
topic: topicVals.slice(0, 2),
value: eventVal,
Expand All @@ -254,7 +254,7 @@ let getEventsResponseFixture = [
ledgerClosedAt: "2022-11-16T16:10:41Z",
contractId,
id: "0164090849041387521-0000000003",
pagingToken: "164090849041387521-3",
cursor: "164090849041387521-3",
inSuccessfulContractCall: true,
topic: topicVals.slice(0, 2),
value: eventVal,
Expand All @@ -266,7 +266,7 @@ let getEventsResponseFixture = [
ledgerClosedAt: "2022-11-16T16:10:41Z",
contractId,
id: "0164090849041387521-0000000003",
pagingToken: "164090849041387521-3",
cursor: "164090849041387521-3",
inSuccessfulContractCall: true,
topic: [topicVals[0]],
value: eventVal,
Expand All @@ -278,7 +278,7 @@ let getEventsResponseFixture = [
ledgerClosedAt: "2022-12-14T01:01:20Z",
contractId,
id: "0000000171798695936-0000000001",
pagingToken: "0000000171798695936-0000000001",
cursor: "0000000171798695936-0000000001",
inSuccessfulContractCall: true,
topic: topicVals,
value: eventVal,
Expand Down
10 changes: 5 additions & 5 deletions test/unit/server/soroban/get_transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe("Server#getTransaction", function () {
transaction.sign(keypair);

this.transaction = transaction;
this.hash = this.transaction.hash().toString("hex");
this.blob = transaction.toEnvelope().toXDR().toString("base64");
this.prepareAxios = (result) => {
this.axiosMock
Expand All @@ -42,7 +41,7 @@ describe("Server#getTransaction", function () {
jsonrpc: "2.0",
id: 1,
method: "getTransaction",
params: { hash: this.hash },
params: { hash: result.txHash },
})
.returns(Promise.resolve({ data: { id: 1, result } }));
};
Expand All @@ -58,7 +57,7 @@ describe("Server#getTransaction", function () {
this.prepareAxios(result);

this.server
.getTransaction(this.hash)
.getTransaction(result.txHash)
.then(function (response) {
expect(response).to.be.deep.equal(result);
done();
Expand All @@ -84,7 +83,7 @@ describe("Server#getTransaction", function () {
.returnValue();

this.server
.getTransaction(this.hash)
.getTransaction(result.txHash)
.then((resp) => {
expect(Object.keys(resp)).to.eql(Object.keys(expected));
expect(resp).to.eql(expected);
Expand All @@ -99,7 +98,7 @@ describe("Server#getTransaction", function () {
this.prepareAxios(result);

this.server
.getTransaction(this.hash)
.getTransaction(result.txHash)
.then((resp) => {
expect(resp).to.be.deep.equal(result);
done();
Expand Down Expand Up @@ -152,6 +151,7 @@ function makeTxResult(status, addSoroban = true) {

return {
status,
txHash: "ae9f315c048d87a5f853bc15bf284a2c3c89eb0e1cb38c10409b77a877b830a8",
latestLedger: 100,
latestLedgerCloseTime: 12345,
oldestLedger: 50,
Expand Down
1 change: 1 addition & 0 deletions test/unit/server/soroban/get_transactions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function makeTxResult(ledger, applicationOrder, status) {
return {
status: status,
ledger: ledger,
txHash: "ae9f315c048d87a5f853bc15bf284a2c3c89eb0e1cb38c10409b77a877b830a8",
createdAt: ledger * 25 + 100,
applicationOrder: applicationOrder,
feeBump: false,
Expand Down
11 changes: 0 additions & 11 deletions test/unit/server/soroban/simulate_transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ describe("Server#simulateTransaction", async function (done) {
),
retval: xdr.ScVal.fromXDR(simulationResponse.results[0].xdr, "base64"),
},
cost: simulationResponse.cost,
stateChanges: [
{
type: 2,
Expand Down Expand Up @@ -242,7 +241,6 @@ describe("Server#simulateTransaction", async function (done) {
const expected = cloneSimulation(parsedSimulationResponse);
// drop fields that go away with errors
delete expected.result;
delete expected.cost;
delete expected.transactionData;
delete expected.minResourceFee;
delete expected.stateChanges;
Expand Down Expand Up @@ -270,7 +268,6 @@ function cloneSimulation(sim) {
),
retval: xdr.ScVal.fromXDR(sim.result.retval.toXDR()),
},
cost: sim.cost,
stateChanges: sim.stateChanges,
_parsed: sim._parsed,
};
Expand Down Expand Up @@ -332,10 +329,6 @@ function baseSimulationResponse(results) {
minResourceFee: "15",
transactionData: new SorobanDataBuilder().build().toXDR("base64"),
...(results !== undefined && { results }),
cost: {
cpuInsns: "1",
memBytes: "2",
},
stateChanges: [
{
type: 2,
Expand Down Expand Up @@ -440,10 +433,6 @@ describe("works with real responses", function () {
xdr: "AAAAEAAAAAEAAAACAAAADwAAAAVIZWxsbwAAAAAAAA8AAAAFQWxvaGEAAAA=",
},
],
cost: {
cpuInsns: "1322134",
memBytes: "1207047",
},
restorePreamble: {
transactionData: "",
minResourceFee: "0",
Expand Down
Loading