1 | import {
|
2 | BinaryWriter,
|
3 | common,
|
4 | IOHelper,
|
5 | JSONHelper,
|
6 | StorageItemJSON,
|
7 | toJSONStorageFlags,
|
8 | UInt160,
|
9 | } from '@neo-one/client-common-esnext-esm';
|
10 | import { Equals, Equatable } from './Equatable';
|
11 | import {
|
12 | createSerializeWire,
|
13 | DeserializeWireBaseOptions,
|
14 | DeserializeWireOptions,
|
15 | SerializableJSON,
|
16 | SerializableWire,
|
17 | SerializeJSONContext,
|
18 | SerializeWire,
|
19 | } from './Serializable';
|
20 | import { StorageFlags } from './StorageFlags';
|
21 | import { BinaryReader, utils } from './utils';
|
22 |
|
23 | export interface StorageItemAdd {
|
24 | readonly hash: UInt160;
|
25 | readonly key: Buffer;
|
26 | readonly value: Buffer;
|
27 | readonly flags: StorageFlags;
|
28 | }
|
29 |
|
30 | export interface StorageItemUpdate {
|
31 | readonly value: Buffer;
|
32 | readonly flags: StorageFlags;
|
33 | }
|
34 |
|
35 | export interface StorageItemsKey {
|
36 | readonly hash?: UInt160;
|
37 | readonly prefix?: Buffer;
|
38 | }
|
39 |
|
40 | export interface StorageItemKey {
|
41 | readonly hash: UInt160;
|
42 | readonly key: Buffer;
|
43 | }
|
44 |
|
45 | export class StorageItem implements SerializableWire<StorageItem>, Equatable, SerializableJSON<StorageItemJSON> {
|
46 | public static deserializeWireBase({ reader }: DeserializeWireBaseOptions): StorageItem {
|
47 | const hash = reader.readUInt160();
|
48 | const key = reader.readVarBytesLE();
|
49 | const value = reader.readVarBytesLE();
|
50 | const flags = reader.readUInt8();
|
51 |
|
52 | return new this({
|
53 | hash,
|
54 | key,
|
55 | value,
|
56 | flags,
|
57 | });
|
58 | }
|
59 |
|
60 | public static deserializeWire(options: DeserializeWireOptions): StorageItem {
|
61 | return this.deserializeWireBase({
|
62 | context: options.context,
|
63 | reader: new BinaryReader(options.buffer),
|
64 | });
|
65 | }
|
66 |
|
67 | public readonly hash: UInt160;
|
68 | public readonly key: Buffer;
|
69 | public readonly value: Buffer;
|
70 | public readonly flags: StorageFlags;
|
71 | public readonly equals: Equals = utils.equals(
|
72 | StorageItem,
|
73 | this,
|
74 | (other) =>
|
75 | common.uInt160Equal(this.hash, other.hash) &&
|
76 | this.key.equals(other.key) &&
|
77 | this.value.equals(other.value) &&
|
78 | this.flags === other.flags,
|
79 | );
|
80 | public readonly serializeWire: SerializeWire = createSerializeWire(this.serializeWireBase.bind(this));
|
81 | private readonly sizeInternal: () => number;
|
82 |
|
83 | public constructor({ hash, key, value, flags }: StorageItemAdd) {
|
84 | this.hash = hash;
|
85 | this.key = key;
|
86 | this.value = value;
|
87 | this.flags = flags;
|
88 | this.sizeInternal = utils.lazy(
|
89 | () =>
|
90 | IOHelper.sizeOfUInt160 +
|
91 | IOHelper.sizeOfVarBytesLE(this.key) +
|
92 | IOHelper.sizeOfVarBytesLE(this.value) +
|
93 | IOHelper.sizeOfUInt8,
|
94 | );
|
95 | }
|
96 |
|
97 | public get size(): number {
|
98 | return this.sizeInternal();
|
99 | }
|
100 |
|
101 | public update({ value, flags }: StorageItemUpdate): StorageItem {
|
102 | return new StorageItem({
|
103 | hash: this.hash,
|
104 | key: this.key,
|
105 | value,
|
106 | flags,
|
107 | });
|
108 | }
|
109 |
|
110 | public serializeWireBase(writer: BinaryWriter): void {
|
111 | writer.writeUInt160(this.hash);
|
112 | writer.writeVarBytesLE(this.key);
|
113 | writer.writeVarBytesLE(this.value);
|
114 | writer.writeUInt8(this.flags);
|
115 | }
|
116 |
|
117 | public serializeJSON(_context: SerializeJSONContext): StorageItemJSON {
|
118 | return {
|
119 | hash: JSONHelper.writeUInt160(this.hash),
|
120 | key: JSONHelper.writeBuffer(this.key),
|
121 | value: JSONHelper.writeBuffer(this.value),
|
122 | flags: toJSONStorageFlags(this.flags),
|
123 | };
|
124 | }
|
125 | }
|