UNPKG

2.53 kBPlain TextView Raw
1import {
2 BinaryWriter,
3 common,
4 createSerializeWire,
5 IOHelper,
6 SerializableWire,
7 SerializeWire,
8 UInt256,
9 UInt256Hex,
10} from '@neo-one/client-common-esnext-esm';
11import BN from 'bn.js';
12import { Equals, Equatable } from './Equatable';
13import { DeserializeWireBaseOptions, DeserializeWireOptions } from './Serializable';
14import { BinaryReader, utils } from './utils';
15
16export interface BlockDataKey {
17 readonly hash: UInt256;
18}
19
20export interface BlockDataAdd {
21 readonly hash: UInt256;
22 readonly lastGlobalTransactionIndex: BN;
23 readonly lastGlobalActionIndex: BN;
24 readonly systemFee: BN;
25}
26
27export class BlockData implements Equatable, SerializableWire<BlockData> {
28 public static deserializeWireBase({ reader }: DeserializeWireBaseOptions): BlockData {
29 const hash = reader.readUInt256();
30 const lastGlobalTransactionIndex = reader.readInt64LE();
31 const lastGlobalActionIndex = reader.readInt64LE();
32 const systemFee = reader.readFixed8();
33
34 return new this({
35 hash,
36 systemFee,
37 lastGlobalTransactionIndex,
38 lastGlobalActionIndex,
39 });
40 }
41
42 public static deserializeWire(options: DeserializeWireOptions): BlockData {
43 return this.deserializeWireBase({
44 context: options.context,
45 reader: new BinaryReader(options.buffer),
46 });
47 }
48
49 public readonly hash: UInt256;
50 public readonly hashHex: UInt256Hex;
51 public readonly lastGlobalTransactionIndex: BN;
52 public readonly lastGlobalActionIndex: BN;
53 public readonly systemFee: BN;
54 public readonly equals: Equals = utils.equals(BlockData, this, (other) => common.uInt256Equal(this.hash, other.hash));
55 public readonly serializeWire: SerializeWire = createSerializeWire(this.serializeWireBase.bind(this));
56 private readonly sizeInternal: () => number;
57
58 public constructor({ hash, lastGlobalTransactionIndex, lastGlobalActionIndex, systemFee }: BlockDataAdd) {
59 this.hash = hash;
60 this.hashHex = common.uInt256ToHex(hash);
61 this.lastGlobalTransactionIndex = lastGlobalTransactionIndex;
62 this.lastGlobalActionIndex = lastGlobalActionIndex;
63 this.systemFee = systemFee;
64 this.sizeInternal = utils.lazy(() => IOHelper.sizeOfUInt256 + IOHelper.sizeOfFixed8);
65 }
66
67 public get size(): number {
68 return this.sizeInternal();
69 }
70
71 public serializeWireBase(writer: BinaryWriter): void {
72 writer.writeUInt256(this.hash);
73 writer.writeInt64LE(this.lastGlobalTransactionIndex);
74 writer.writeInt64LE(this.lastGlobalActionIndex);
75 writer.writeFixed8(this.systemFee);
76 }
77}