1 | import { BinaryWriter, common, ECPoint, IOHelper, UInt160, UInt256 } from '@neo-one/client-common-esnext-esm';
|
2 | import BN from 'bn.js';
|
3 | import { deserializeInvocationResultWireBase, InvocationResult } from './invocationResult';
|
4 | import {
|
5 | createSerializeWire,
|
6 | DeserializeWireBaseOptions,
|
7 | DeserializeWireOptions,
|
8 | SerializableWire,
|
9 | SerializeWire,
|
10 | } from './Serializable';
|
11 | import { BinaryReader, utils } from './utils';
|
12 |
|
13 | export interface InvocationDataAdd {
|
14 | readonly hash: UInt256;
|
15 | readonly assetHash?: UInt256;
|
16 | readonly contractHashes: ReadonlyArray<UInt160>;
|
17 | readonly deletedContractHashes: ReadonlyArray<UInt160>;
|
18 | readonly migratedContractHashes: ReadonlyArray<[UInt160, UInt160]>;
|
19 | readonly voteUpdates: ReadonlyArray<[UInt160, ReadonlyArray<ECPoint>]>;
|
20 | readonly blockIndex: number;
|
21 | readonly transactionIndex: number;
|
22 | readonly actionIndexStart: BN;
|
23 | readonly actionIndexStop: BN;
|
24 | readonly result: InvocationResult;
|
25 | }
|
26 |
|
27 | export interface InvocationDataKey {
|
28 | readonly hash: UInt256;
|
29 | }
|
30 |
|
31 | export class InvocationData implements SerializableWire<InvocationData> {
|
32 | public static deserializeWireBase(options: DeserializeWireBaseOptions): InvocationData {
|
33 | const { reader } = options;
|
34 | const hash = reader.readUInt256();
|
35 | const assetHash = reader.readUInt256();
|
36 | const contractHashes = reader.readArray(() => reader.readUInt160());
|
37 | const deletedContractHashes = reader.readArray(() => reader.readUInt160());
|
38 | const migratedContractHashes = reader.readArray<[UInt160, UInt160]>(() => {
|
39 | const from = reader.readUInt160();
|
40 | const to = reader.readUInt160();
|
41 |
|
42 | return [from, to];
|
43 | });
|
44 | const voteUpdates = reader.readArray<[UInt160, ReadonlyArray<ECPoint>]>(() => {
|
45 | const address = reader.readUInt160();
|
46 | const votes = reader.readArray<ECPoint>(() => reader.readECPoint());
|
47 |
|
48 | return [address, votes];
|
49 | });
|
50 | const blockIndex = reader.readUInt32LE();
|
51 | const transactionIndex = reader.readUInt32LE();
|
52 | const actionIndexStart = reader.readUInt64LE();
|
53 | const actionIndexStop = reader.readUInt64LE();
|
54 | const result = deserializeInvocationResultWireBase(options);
|
55 |
|
56 | return new this({
|
57 | hash,
|
58 | assetHash: common.uInt256Equal(assetHash, common.ZERO_UINT256) ? undefined : assetHash,
|
59 | contractHashes,
|
60 | deletedContractHashes,
|
61 | migratedContractHashes,
|
62 | voteUpdates,
|
63 | blockIndex,
|
64 | transactionIndex,
|
65 | actionIndexStart,
|
66 | actionIndexStop,
|
67 | result,
|
68 | });
|
69 | }
|
70 |
|
71 | public static deserializeWire(options: DeserializeWireOptions): InvocationData {
|
72 | return this.deserializeWireBase({
|
73 | context: options.context,
|
74 | reader: new BinaryReader(options.buffer),
|
75 | });
|
76 | }
|
77 |
|
78 | public readonly hash: UInt256;
|
79 | public readonly assetHash: UInt256 | undefined;
|
80 | public readonly contractHashes: ReadonlyArray<UInt160>;
|
81 | public readonly deletedContractHashes: ReadonlyArray<UInt160>;
|
82 | public readonly migratedContractHashes: ReadonlyArray<[UInt160, UInt160]>;
|
83 | public readonly voteUpdates: ReadonlyArray<[UInt160, ReadonlyArray<ECPoint>]>;
|
84 | public readonly blockIndex: number;
|
85 | public readonly transactionIndex: number;
|
86 | public readonly actionIndexStart: BN;
|
87 | public readonly actionIndexStop: BN;
|
88 | public readonly result: InvocationResult;
|
89 | public readonly serializeWire: SerializeWire = createSerializeWire(this.serializeWireBase.bind(this));
|
90 | private readonly sizeInternal: () => number;
|
91 |
|
92 | public constructor({
|
93 | hash,
|
94 | assetHash,
|
95 | contractHashes,
|
96 | deletedContractHashes,
|
97 | migratedContractHashes,
|
98 | voteUpdates,
|
99 | blockIndex,
|
100 | transactionIndex,
|
101 | actionIndexStart,
|
102 | actionIndexStop,
|
103 | result,
|
104 | }: InvocationDataAdd) {
|
105 | this.hash = hash;
|
106 | this.assetHash = assetHash;
|
107 | this.contractHashes = contractHashes;
|
108 | this.deletedContractHashes = deletedContractHashes;
|
109 | this.migratedContractHashes = migratedContractHashes;
|
110 | this.voteUpdates = voteUpdates;
|
111 | this.blockIndex = blockIndex;
|
112 | this.transactionIndex = transactionIndex;
|
113 | this.actionIndexStart = actionIndexStart;
|
114 | this.actionIndexStop = actionIndexStop;
|
115 | this.result = result;
|
116 | this.sizeInternal = utils.lazy(
|
117 | () =>
|
118 | IOHelper.sizeOfUInt256 +
|
119 | IOHelper.sizeOfUInt256 +
|
120 | IOHelper.sizeOfArray(this.contractHashes, () => IOHelper.sizeOfUInt160) +
|
121 | IOHelper.sizeOfArray(this.deletedContractHashes, () => IOHelper.sizeOfUInt160) +
|
122 | IOHelper.sizeOfArray(this.migratedContractHashes, () => IOHelper.sizeOfUInt160) +
|
123 | IOHelper.sizeOfArray(
|
124 | this.voteUpdates,
|
125 | (value) => IOHelper.sizeOfUInt160 + IOHelper.sizeOfArray(value[1], (val) => IOHelper.sizeOfECPoint(val)),
|
126 | ) +
|
127 | IOHelper.sizeOfUInt32LE +
|
128 | IOHelper.sizeOfUInt32LE +
|
129 | IOHelper.sizeOfUInt64LE +
|
130 | IOHelper.sizeOfUInt64LE +
|
131 | this.result.size,
|
132 | );
|
133 | }
|
134 |
|
135 | public get size(): number {
|
136 | return this.sizeInternal();
|
137 | }
|
138 |
|
139 | public serializeWireBase(writer: BinaryWriter): void {
|
140 | writer.writeUInt256(this.hash);
|
141 | writer.writeUInt256(this.assetHash === undefined ? common.ZERO_UINT256 : this.assetHash);
|
142 | writer.writeArray(this.contractHashes, (contractHash) => {
|
143 | writer.writeUInt160(contractHash);
|
144 | });
|
145 | writer.writeArray(this.deletedContractHashes, (contractHash) => {
|
146 | writer.writeUInt160(contractHash);
|
147 | });
|
148 | writer.writeArray(this.migratedContractHashes, ([from, to]) => {
|
149 | writer.writeUInt160(from);
|
150 | writer.writeUInt160(to);
|
151 | });
|
152 | writer.writeArray(this.voteUpdates, ([address, votes]) => {
|
153 | writer.writeUInt160(address);
|
154 | writer.writeArray(votes, (vote) => {
|
155 | writer.writeECPoint(vote);
|
156 | });
|
157 | });
|
158 | writer.writeUInt32LE(this.blockIndex);
|
159 | writer.writeUInt32LE(this.transactionIndex);
|
160 | writer.writeUInt64LE(this.actionIndexStart);
|
161 | writer.writeUInt64LE(this.actionIndexStop);
|
162 | this.result.serializeWireBase(writer);
|
163 | }
|
164 | }
|