Skip to content

Commit a70f374

Browse files
Make speculative result backward compatible with Casper 1.5.x and Casper 2.0
1 parent ab32b69 commit a70f374

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

src/rpc/response.ts

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
EntryPointValue,
2626
EraSummary,
2727
ExecutionInfo,
28+
ExecutionResultV1,
2829
Hash,
2930
InitiatorAddr,
3031
MinimalBlockInfo,
@@ -831,14 +832,71 @@ export class SpeculativeExecResult {
831832
})
832833
blockHash?: Hash;
833834

834-
@jsonMember({
835-
name: 'execution_result',
836-
constructor: SpeculativeExecutionResult,
837-
preserveNull: true
838-
})
835+
/**
836+
* Execution result for Casper v2.0 (speculative execution format).
837+
*/
839838
executionResult?: SpeculativeExecutionResult;
840839

840+
/**
841+
* Execution result for Casper v1.5.x (legacy execution format).
842+
*/
843+
executionResultV1?: ExecutionResultV1;
844+
845+
/**
846+
* Stores the raw json object if version could not be parsed.
847+
*/
841848
rawJSON?: any;
849+
850+
/**
851+
* True if the parsed execution result matches Casper v1.5.x format.
852+
*/
853+
get isV1(): boolean {
854+
return !!this.executionResultV1;
855+
}
856+
857+
/**
858+
* True if the parsed execution result matches Casper v2.0 format.
859+
*/
860+
get isV2(): boolean {
861+
return !!this.executionResult;
862+
}
863+
864+
/**
865+
* Parses and returns a version-aware SpeculativeExecResult from raw JSON.
866+
* Supports both Casper v1.5.x and v2.0 formats.
867+
*/
868+
static fromJSON(json: any): SpeculativeExecResult {
869+
const result = new SpeculativeExecResult();
870+
871+
result.apiVersion = json?.version;
872+
const execJson = json?.result?.execution_result;
873+
result.rawJSON = json;
874+
875+
result.blockHash = execJson?.block_hash
876+
? Hash.fromHex(execJson.block_hash)
877+
: undefined;
878+
879+
if (
880+
execJson &&
881+
(execJson.block_hash ||
882+
execJson.limit !== undefined ||
883+
execJson.consumed !== undefined)
884+
) {
885+
// Casper v2.0
886+
result.executionResult = new TypedJSON(SpeculativeExecutionResult).parse(
887+
execJson
888+
);
889+
} else if (execJson && ('Success' in execJson || 'Failure' in execJson)) {
890+
// Casper v1.5.x
891+
result.executionResultV1 = new TypedJSON(ExecutionResultV1).parse(
892+
execJson
893+
);
894+
} else {
895+
console.warn('Unknown execution_result format:', execJson);
896+
}
897+
898+
return result;
899+
}
842900
}
843901

844902
@jsonObject

src/rpc/speculative_client.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { RpcResponse, SpeculativeExecResult } from './response';
1010
import { IHandler } from './client';
1111
import { IDValue } from './id_value';
12-
import { Deploy, Hash, SpeculativeExecutionResult } from '../types';
12+
import { Deploy } from '../types';
1313

1414
/**
1515
* A client for interacting with the speculative execution endpoint in the Casper Network.
@@ -85,16 +85,7 @@ export class SpeculativeClient {
8585
throw new Error(`Error parsing JSON`);
8686
}
8787

88-
const result = new SpeculativeExecResult();
89-
result.apiVersion = data?.version;
90-
result.executionResult = new TypedJSON(SpeculativeExecutionResult).parse(
91-
data?.result?.execution_result
92-
);
93-
result.blockHash = data?.result?.execution_result?.block_hash
94-
? Hash.fromHex(data?.result?.execution_result?.block_hash)
95-
: undefined;
96-
result.rawJSON = data?.result;
97-
return result;
88+
return SpeculativeExecResult.fromJSON(data);
9889
} catch (error) {
9990
throw new Error(`Error parsing JSON, details: ${error}`);
10091
}

0 commit comments

Comments
 (0)