Skip to content

Commit

Permalink
Merge pull request #466 from casper-ecosystem/adjust-transaction-runtime
Browse files Browse the repository at this point in the history
Update TransactionRuntime / TransactionTarget serialization
  • Loading branch information
alexmyshchyshyn authored Dec 18, 2024
2 parents 782838c + eae21a9 commit 8ac0f71
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 169 deletions.
13 changes: 8 additions & 5 deletions src/types/AddressableEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { AssociatedKey } from './Account';
import { MessageTopic } from './MessageTopic';
import { EntryPointV1 } from './EntryPoint';
import { AccountHash, URef } from './key';

export type SystemEntityType = string;
export type TransactionRuntime = 'VmCasperV1' | 'VmCasperV2';
import { TransactionRuntime } from './TransactionTarget';

/**
* Defines different kinds of entities within the system, such as system entities,
Expand All @@ -17,7 +15,7 @@ export class EntityKind {
* Represents a system entity type, allowing flexible naming of system-specific entities.
*/
@jsonMember({ name: 'System', constructor: String })
system?: SystemEntityType;
system?: string;

/**
* Represents an account entity, identified by an `AccountHash`.
Expand All @@ -35,7 +33,12 @@ export class EntityKind {
*/
@jsonMember({
name: 'SmartContract',
constructor: String
constructor: TransactionRuntime,
deserializer: json => {
if (!json) return;
return TransactionRuntime.fromJSON(json);
},
serializer: (value: TransactionRuntime) => value.toJSON()
})
smartContract?: TransactionRuntime;
}
Expand Down
17 changes: 17 additions & 0 deletions src/types/ContractWasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { jsonObject, jsonMember } from 'typedjson';

/**
* A container for contract's WASM bytes.
*/
@jsonObject
export class ContractWasm {
/**
* The WASM bytes of the contract as a string.
*/
@jsonMember({ name: 'bytes', constructor: String })
public bytes!: string;

constructor(bytes: string) {
this.bytes = bytes;
}
}
32 changes: 23 additions & 9 deletions src/types/StoredValue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AnyT, jsonArrayMember, jsonMember, jsonObject } from 'typedjson';
import { jsonArrayMember, jsonMember, jsonObject } from 'typedjson';

import { Account } from './Account';
import { TransferV1 } from './Transfer';
import { Transfer } from './Transfer';
import { DeployInfo } from './DeployInfo';
import { EraInfo } from './EraInfo';
import { Bid } from './Bid';
Expand All @@ -17,6 +17,7 @@ import { Contract } from './Contract';
import { ContractPackage } from './ContractPackage';
import { CLValue, CLValueParser } from './clvalue';
import { SystemByteCode } from './ByteCode';
import { ContractWasm } from './ContractWasm';

/**
* Represents a stored value in a decentralized system. The value can be of different types
Expand Down Expand Up @@ -57,8 +58,8 @@ export class StoredValue {
/**
* The WebAssembly (WASM) bytecode for the contract, represented as `AnyT`.
*/
@jsonMember({ name: 'ContractWASM', constructor: AnyT })
contractWASM?: any;
@jsonMember({ name: 'ContractWasm', constructor: ContractWasm })
ContractWasm?: ContractWasm;

/**
* The stored contract package information.
Expand All @@ -67,10 +68,17 @@ export class StoredValue {
contractPackage?: ContractPackage;

/**
* The legacy transfer information, representing a historical transfer.
* The transfer information, representing a historical transfer.
*/
@jsonMember({ name: 'LegacyTransfer', constructor: TransferV1 })
legacyTransfer?: TransferV1;
@jsonMember({
name: 'Transfer',
constructor: Transfer,
deserializer: json => {
if (!json) return;
return Transfer.fromJSON(json);
}
})
transfer?: Transfer;

/**
* The information related to a deploy operation.
Expand Down Expand Up @@ -117,8 +125,8 @@ export class StoredValue {
/**
* The stored package information, typically a contract or executable package.
*/
@jsonMember({ name: 'Package', constructor: Package })
package?: Package;
@jsonMember({ name: 'SmartContract', constructor: Package })
smartContract?: Package;

/**
* The stored bytecode, representing compiled contract or executable code.
Expand Down Expand Up @@ -162,4 +170,10 @@ export class StoredValue {
*/
@jsonMember({ name: 'EntryPoint', constructor: EntryPointValue })
entryPoint?: EntryPointValue;

/**
* Raw bytes. Similar to a [`crate::StoredValue::CLValue`] but does not incur overhead of a [`crate::CLValue`] and [`crate::CLType`].
*/
@jsonMember({ name: 'RawBytes', constructor: String })
rawBytes?: string;
}
13 changes: 6 additions & 7 deletions src/types/Transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { TransactionV1 } from './Transaction';
import { InitiatorAddr } from './InitiatorAddr';
import { FixedMode, PricingMode } from './PricingMode';
import { KeyAlgorithm, PrivateKey, PublicKey } from './keypair';
import { SessionTarget, TransactionTarget } from './TransactionTarget';
import {
SessionTarget,
TransactionRuntime,
TransactionTarget
} from './TransactionTarget';
import {
TransactionEntryPoint,
TransactionEntryPointEnum
Expand All @@ -20,7 +24,6 @@ import {
CLValueUInt64
} from './clvalue';
import { TransactionV1Payload } from './TransactionV1Payload';
import { Hash } from './key';

describe('Test Transaction', () => {
it('should create a TransactionV1 with correct payload instance', async () => {
Expand All @@ -45,13 +48,9 @@ describe('Test Transaction', () => {

const sessionTarget = new SessionTarget();

sessionTarget.runtime = 'VmCasperV1';
sessionTarget.transferredValue = 1000;
sessionTarget.runtime = TransactionRuntime.vmCasperV1();
sessionTarget.moduleBytes = Uint8Array.from([1]);
sessionTarget.isInstallUpgrade = false;
sessionTarget.seed = Hash.fromHex(
'8bf9d406ab901428d43ecd3a6f214b864e7ef8316934e5e0f049650a65b40d73'
);

const transactionTarget = new TransactionTarget(
undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/types/TransactionEntryPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export enum TransactionEntryPointEnum {
* These tags are used to simplify storage and facilitate efficient comparison of entry points.
*/
export enum TransactionEntryPointTag {
Custom = 0,
Call = 1,
Call = 0,
Custom = 1,
Transfer = 2,
AddBid = 3,
WithdrawBid = 4,
Expand Down
Loading

0 comments on commit 8ac0f71

Please sign in to comment.