UNPKG

4.37 kBPlain TextView Raw
1import { BinaryWriter, IOHelper, UInt256 } from '@neo-one/client-common-esnext-esm';
2import { BaseState } from '@neo-one/client-full-common-esnext-esm';
3import BN from 'bn.js';
4import {
5 createSerializeWire,
6 DeserializeWireBaseOptions,
7 DeserializeWireOptions,
8 SerializableWire,
9 SerializeWire,
10} from './Serializable';
11import { BinaryReader, utils } from './utils';
12
13export interface TransactionDataAdd {
14 readonly version?: number;
15 readonly hash: UInt256;
16 readonly blockHash: UInt256;
17 readonly startHeight: number;
18 readonly index: number;
19 readonly globalIndex: BN;
20 readonly endHeights?: { readonly [index: number]: number };
21 readonly claimed?: { readonly [index: number]: boolean };
22}
23
24export interface TransactionDataUpdate {
25 readonly endHeights?: { readonly [index: number]: number };
26 readonly claimed?: { readonly [index: number]: boolean };
27}
28
29export interface TransactionDataKey {
30 readonly hash: UInt256;
31}
32
33export class TransactionData extends BaseState implements SerializableWire<TransactionData> {
34 public static deserializeWireBase({ reader }: DeserializeWireBaseOptions): TransactionData {
35 const version = reader.readUInt8();
36 const hash = reader.readUInt256();
37 const blockHash = reader.readUInt256();
38 const startHeight = reader.readUInt32LE();
39 const index = reader.readUInt32LE();
40 const globalIndex = reader.readUInt64LE();
41 const endHeights = reader.readObject(() => {
42 const key = reader.readUInt32LE();
43 const value = reader.readUInt32LE();
44
45 return { key, value };
46 });
47 const claimed = reader.readObject(() => {
48 const key = reader.readUInt32LE();
49 const value = reader.readBoolean();
50
51 return { key, value };
52 });
53
54 return new this({
55 version,
56 hash,
57 blockHash,
58 startHeight,
59 index,
60 globalIndex,
61 endHeights,
62 claimed,
63 });
64 }
65
66 public static deserializeWire(options: DeserializeWireOptions): TransactionData {
67 return this.deserializeWireBase({
68 context: options.context,
69 reader: new BinaryReader(options.buffer),
70 });
71 }
72
73 public readonly hash: UInt256;
74 public readonly blockHash: UInt256;
75 public readonly startHeight: number;
76 public readonly index: number;
77 public readonly globalIndex: BN;
78 public readonly endHeights: {
79 readonly [index: number]: number;
80 };
81 public readonly claimed: {
82 readonly [index: number]: boolean;
83 };
84 public readonly serializeWire: SerializeWire = createSerializeWire(this.serializeWireBase.bind(this));
85 private readonly sizeInternal: () => number;
86
87 public constructor({
88 version,
89 hash,
90 blockHash,
91 startHeight,
92 index,
93 globalIndex,
94 endHeights = {},
95 claimed = {},
96 }: TransactionDataAdd) {
97 super({ version });
98 this.hash = hash;
99 this.blockHash = blockHash;
100 this.startHeight = startHeight;
101 this.index = index;
102 this.globalIndex = globalIndex;
103 this.endHeights = endHeights;
104 this.claimed = claimed;
105 this.sizeInternal = utils.lazy(
106 () =>
107 IOHelper.sizeOfUInt8 +
108 IOHelper.sizeOfUInt256 +
109 IOHelper.sizeOfUInt32LE +
110 IOHelper.sizeOfObject(this.endHeights, () => IOHelper.sizeOfUInt32LE + IOHelper.sizeOfUInt32LE) +
111 IOHelper.sizeOfObject(this.claimed, () => IOHelper.sizeOfUInt32LE + IOHelper.sizeOfBoolean),
112 );
113 }
114
115 public get size(): number {
116 return this.sizeInternal();
117 }
118
119 public update({ endHeights = this.endHeights, claimed = this.claimed }: TransactionDataUpdate): TransactionData {
120 return new TransactionData({
121 version: this.version,
122 hash: this.hash,
123 blockHash: this.blockHash,
124 startHeight: this.startHeight,
125 index: this.index,
126 globalIndex: this.globalIndex,
127 endHeights,
128 claimed,
129 });
130 }
131
132 public serializeWireBase(writer: BinaryWriter): void {
133 writer.writeUInt8(this.version);
134 writer.writeUInt256(this.hash);
135 writer.writeUInt256(this.blockHash);
136 writer.writeUInt32LE(this.startHeight);
137 writer.writeUInt32LE(this.index);
138 writer.writeUInt64LE(this.globalIndex);
139 writer.writeObject(this.endHeights, (key, value) => {
140 writer.writeUInt32LE(key);
141 writer.writeUInt32LE(value);
142 });
143 writer.writeObject(this.claimed, (key, value) => {
144 writer.writeUInt32LE(key);
145 writer.writeBoolean(value);
146 });
147 }
148}