Skip to content

Commit

Permalink
[TypeScript SDK] Fix backward-compatibility for expiration field (Mys…
Browse files Browse the repository at this point in the history
…tenLabs#8551)

The TS e2e test is broken due to change in
MystenLabs#8499 . The e2e test failed to
catch the error before merging because the devnet was still at 0.26.0 at
the time but the culrprit PR did not go into 0.27.0
  • Loading branch information
666lcz authored Feb 22, 2023
1 parent 99067c0 commit f2e713b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-donuts-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mysten/sui.js": minor
---

Add TransactionExpiration to TransactionData
34 changes: 0 additions & 34 deletions sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
SuiEventEnvelope,
SuiEventFilter,
SuiExecuteTransactionResponse,
SuiExecuteTransactionResponse_v26,
SuiMoveFunctionArgTypes,
SuiMoveNormalizedFunction,
SuiMoveNormalizedModule,
Expand All @@ -38,7 +37,6 @@ import {
TransactionEffects,
DevInspectResults,
CoinMetadata,
toSuiTransactionData,
isValidTransactionDigest,
isValidSuiAddress,
isValidSuiObjectId,
Expand Down Expand Up @@ -689,38 +687,6 @@ export class JsonRpcProvider extends Provider {
signature: SerializedSignature,
requestType: ExecuteTransactionRequestType = 'WaitForEffectsCert',
): Promise<SuiExecuteTransactionResponse> {
const version = await this.getRpcApiVersion();
if (version?.major === 0 && version?.minor <= 26) {
try {
let resp = await this.client.requestWithType(
'sui_executeTransactionSerializedSig',
[
typeof txnBytes === 'string' ? txnBytes : toB64(txnBytes),
signature,
requestType,
],
SuiExecuteTransactionResponse_v26,
this.options.skipDataValidation,
);
let certificate = resp.certificate
? {
transactionDigest: resp.certificate!.transactionDigest,
data: toSuiTransactionData(resp.certificate!.data),
txSignatures: [resp.certificate!.txSignature],
authSignInfo: resp.certificate!.authSignInfo,
}
: undefined;
return {
certificate: certificate,
effects: resp.effects,
confirmed_local_execution: resp.confirmed_local_execution,
};
} catch (err) {
throw new Error(
`Error executing transaction with request type: ${err}`,
);
}
}
try {
return await this.client.requestWithType(
'sui_executeTransactionSerializedSig',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import {
normalizeSuiObjectId,
bcsForVersion,
GasData,
TransactionData_v26,
RpcApiVersion,
toTransactionData,
} from '../../types';
import {
MoveCallTransaction,
Expand Down Expand Up @@ -247,7 +245,7 @@ export class LocalTxnDataSerializer implements TxnDataSerializer {
async constructTransactionData(
signerAddress: string,
unserializedTxn: UnserializedSignableTransaction,
): Promise<TransactionData | TransactionData_v26> {
): Promise<TransactionData> {
const [tx, gasPayment] = await this.constructTransactionKindAndPayment(
signerAddress,
unserializedTxn,
Expand Down Expand Up @@ -337,7 +335,7 @@ export class LocalTxnDataSerializer implements TxnDataSerializer {
originalTx: UnserializedSignableTransaction,
gasObjectId: ObjectId | undefined,
signerAddress: SuiAddress,
): Promise<TransactionData | TransactionData_v26> {
): Promise<TransactionData> {
// TODO: Allow people to add tip to the reference gas price by using originalTx.data.gasPrice
originalTx.data.gasPrice = await this.provider.getReferenceGasPrice();
if (gasObjectId === undefined) {
Expand All @@ -357,16 +355,6 @@ export class LocalTxnDataSerializer implements TxnDataSerializer {
'Must provide a valid gas budget for contructing TransactionData',
);
}
let version = await this.provider.getRpcApiVersion();
if (version?.major === 0 && version?.minor <= 26) {
return {
kind: tx,
sender: signerAddress,
gasPayment: gasPayment!,
gasPrice: originalTx.data.gasPrice!,
gasBudget: originalTx.data.gasBudget!,
};
}
return {
kind: tx,
sender: signerAddress,
Expand All @@ -384,7 +372,7 @@ export class LocalTxnDataSerializer implements TxnDataSerializer {
* Serialize `TransactionData` into BCS encoded bytes
*/
public async serializeTransactionData(
tx: TransactionData | TransactionData_v26,
tx: TransactionData,
// TODO: derive the buffer size automatically
size: number = 8192,
): Promise<Uint8Array> {
Expand Down Expand Up @@ -427,11 +415,10 @@ export class LocalTxnDataSerializer implements TxnDataSerializer {
* Deserialize `TransactionData` to `SignableTransaction`
*/
public async transformTransactionDataToSignableTransaction(
tx: TransactionData | TransactionData_v26,
tx_data: TransactionData,
): Promise<
UnserializedSignableTransaction | UnserializedSignableTransaction[]
> {
let tx_data = toTransactionData(tx);
if ('Single' in tx_data.kind) {
return this.transformTransactionToSignableTransaction(
tx_data.kind.Single,
Expand Down
4 changes: 2 additions & 2 deletions sdk/typescript/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
union,
unknown,
} from 'superstruct';
import { CallArg, TransactionData, TransactionData_v26 } from './sui-bcs';
import { CallArg, TransactionData } from './sui-bcs';
import { sha256Hash } from '../cryptography/hash';
import { BCS, fromB58, toB58 } from '@mysten/bcs';

Expand Down Expand Up @@ -123,7 +123,7 @@ export function normalizeSuiObjectId(
* @param publicKey public key
*/
export function generateTransactionDigest(
data: TransactionData | TransactionData_v26,
data: TransactionData,
bcs: BCS,
): string {
const txBytes = bcs.ser('TransactionData', data).toBytes();
Expand Down
52 changes: 8 additions & 44 deletions sdk/typescript/src/types/sui-bcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,44 +264,10 @@ export const TRANSACTION_DATA_TYPE_TAG = Array.from('TransactionData::').map(
export function deserializeTransactionBytesToTransactionData(
bcs: BCS,
bytes: Uint8Array,
): TransactionData | TransactionData_v26 {
return bcs.de('TransactionData', bytes);
}

export function toTransactionData(
tx_data: TransactionData_v26 | TransactionData,
): TransactionData {
if ('gasData' in tx_data) {
return tx_data;
}
return {
sender: tx_data.sender,
kind: tx_data.kind,
gasData: {
payment: tx_data.gasPayment,
owner: tx_data.sender!,
budget: tx_data.gasBudget,
price: tx_data.gasPrice,
},
expiration: { None: null },
};
return bcs.de('TransactionData', bytes);
}

/* TransactionData <= v26 */
/**
* The TransactionData to be signed and sent to the RPC service.
*
* Field `sender` is made optional as it can be added during the signing
* process and there's no need to define it sooner.
*/
export type TransactionData_v26 = {
sender?: string; //
gasBudget: number;
gasPrice: number;
kind: TransactionKind;
gasPayment: SuiObjectRef;
};

const BCS_SPEC = {
enums: {
'Option<T>': {
Expand Down Expand Up @@ -420,16 +386,14 @@ const BCS_SPEC = {
},
};

// for version <= 0.26.0
const BCS_0_26_SPEC = {
// for version <= 0.27.0
const BCS_0_27_SPEC = {
structs: {
...BCS_SPEC.structs,
TransactionData: {
kind: 'TransactionKind',
sender: BCS.ADDRESS,
gasPayment: 'SuiObjectRef',
gasPrice: BCS.U64,
gasBudget: BCS.U64,
gasData: 'GasData',
},
SenderSignedData: {
data: 'TransactionData',
Expand All @@ -446,12 +410,12 @@ const bcs = new BCS({ ...getSuiMoveConfig(), types: BCS_SPEC });
registerUTF8String(bcs);

// ========== Backward Compatibility (remove after v0.24 deploys) ===========
const bcs_0_26 = new BCS({ ...getSuiMoveConfig(), types: BCS_0_26_SPEC });
registerUTF8String(bcs_0_26);
const bcs_0_27 = new BCS({ ...getSuiMoveConfig(), types: BCS_0_27_SPEC });
registerUTF8String(bcs_0_27);

export function bcsForVersion(v?: RpcApiVersion) {
if (v?.major === 0 && v?.minor <= 26) {
return bcs_0_26;
if (v?.major === 0 && v?.minor <= 27) {
return bcs_0_27;
}

return bcs;
Expand Down
68 changes: 4 additions & 64 deletions sdk/typescript/src/types/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,49 +256,6 @@ export const SuiFinalizedEffects = object({
});
export type SuiFinalizedEffects = Infer<typeof SuiFinalizedEffects>;

export const SuiTransactionData_v26 = object({
transactions: array(SuiTransactionKind),
sender: SuiAddress,
gasPayment: SuiObjectRef,
// TODO: remove optional after 0.21.0 is released
gasPrice: optional(number()),
gasBudget: number(),
});
export type SuiTransactionData_v26 = Infer<typeof SuiTransactionData_v26>;

export function toSuiTransactionData(
tx_data: SuiTransactionData_v26,
): SuiTransactionData {
return {
transactions: tx_data.transactions,
sender: tx_data.sender,
gasData: {
payment: tx_data.gasPayment,
owner: tx_data.sender,
budget: tx_data.gasBudget,
price: tx_data.gasPrice!,
},
};
}

export const CertifiedTransaction_v26 = object({
transactionDigest: TransactionDigest,
data: SuiTransactionData_v26,
txSignature: string(),
authSignInfo: AuthorityQuorumSignInfo,
});
export type CertifiedTransaction_v26 = Infer<typeof CertifiedTransaction_v26>;

export const SuiExecuteTransactionResponse_v26 = object({
certificate: optional(CertifiedTransaction_v26),
effects: SuiFinalizedEffects,
confirmed_local_execution: boolean(),
});

export type SuiExecuteTransactionResponse_v26 = Infer<
typeof SuiExecuteTransactionResponse_v26
>;

// TODO: Remove after devnet 0.28.0

export type GatewayTxSeqNumber = number;
Expand Down Expand Up @@ -388,9 +345,7 @@ export const SuiTransactionResponse = object({
// TODO: Remove optional after devnet 0.28.0
transaction: optional(SuiTransaction),
// TODO: Remove after devnet 0.28.0
certificate: optional(
union([CertifiedTransaction, CertifiedTransaction_v26]),
),
certificate: optional(CertifiedTransaction),
effects: TransactionEffects,
// TODO: Remove after devnet 0.28.0
timestamp_ms: optional(union([number(), literal(null)])),
Expand Down Expand Up @@ -433,7 +388,7 @@ export type SuiExecuteTransactionResponse = Infer<

export function getCertifiedTransaction(
tx: SuiTransactionResponse | SuiExecuteTransactionResponse,
): CertifiedTransaction | CertifiedTransaction_v26 | undefined {
): CertifiedTransaction | undefined {
if ('certificate' in tx) {
return tx.certificate;
} else if ('EffectsCert' in tx) {
Expand All @@ -445,7 +400,6 @@ export function getCertifiedTransaction(
export function getTransactionDigest(
tx:
| CertifiedTransaction
| CertifiedTransaction_v26
| SuiTransactionResponse
| SuiExecuteTransactionResponse,
): TransactionDigest {
Expand All @@ -457,7 +411,7 @@ export function getTransactionDigest(
}

export function getTransactionSignature(
tx: SuiTransactionResponse | CertifiedTransaction | CertifiedTransaction_v26,
tx: SuiTransactionResponse | CertifiedTransaction,
): string[] {
const certificateOrTx =
'certificate' in tx
Expand All @@ -470,10 +424,6 @@ export function getTransactionSignature(
return certificateOrTx.txSignatures;
}

if ('txSignature' in certificateOrTx) {
return [certificateOrTx.txSignature];
}

return [];
}

Expand All @@ -499,17 +449,7 @@ export function getGasData(
}

if ('certificate' in tx) {
const data = tx.certificate!.data;
if ('gasData' in data) {
return data.gasData;
} else {
return {
payment: data.gasPayment,
budget: data.gasBudget,
owner: data.sender,
price: data.gasPrice!,
};
}
return tx.certificate!.data.gasData;
}

return tx.transaction!.data.gasData;
Expand Down
Loading

0 comments on commit f2e713b

Please sign in to comment.