-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathpublic_data_update_request.ts
103 lines (85 loc) · 2.97 KB
/
public_data_update_request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { inspect } from 'util';
import { computePublicDataTreeLeafSlot } from '../hash/hash.js';
import { type ContractStorageUpdateRequest } from './contract_storage_update_request.js';
// TO BE REMOVED.
/**
* Write operations on the public data tree including the previous value.
*/
export class PublicDataUpdateRequest {
constructor(
/**
* Index of the leaf in the public data tree which is to be updated.
*/
public readonly leafSlot: Fr,
/**
* New value of the leaf.
*/
public readonly newValue: Fr,
/**
* Side effect counter tracking position of this event in tx execution.
*/
public readonly sideEffectCounter: number,
) {}
static from(args: {
/**
* Index of the leaf in the public data tree which is to be updated.
*/
leafIndex: Fr;
/**
* New value of the leaf.
*/
newValue: Fr;
/**
* Side effect counter tracking position of this event in tx execution.
*/
sideEffectCounter: number;
}) {
return new PublicDataUpdateRequest(args.leafIndex, args.newValue, args.sideEffectCounter);
}
get counter() {
return this.sideEffectCounter;
}
get position() {
return this.leafSlot;
}
toBuffer() {
return serializeToBuffer(this.leafSlot, this.newValue, this.sideEffectCounter);
}
isEmpty() {
return this.leafSlot.isZero() && this.newValue.isZero();
}
static fromFields(fields: Fr[] | FieldReader) {
const reader = FieldReader.asReader(fields);
return new PublicDataUpdateRequest(reader.readField(), reader.readField(), reader.readU32());
}
static isEmpty(x: PublicDataUpdateRequest) {
return x.isEmpty();
}
equals(other: PublicDataUpdateRequest) {
return this.leafSlot.equals(other.leafSlot) && this.newValue.equals(other.newValue);
}
static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new PublicDataUpdateRequest(Fr.fromBuffer(reader), Fr.fromBuffer(reader), reader.readNumber());
}
static fromContractStorageUpdateRequest(contractAddress: AztecAddress, updateRequest: ContractStorageUpdateRequest) {
const leafSlot = computePublicDataTreeLeafSlot(contractAddress, updateRequest.storageSlot);
return new PublicDataUpdateRequest(leafSlot, updateRequest.newValue, updateRequest.counter);
}
static empty() {
return new PublicDataUpdateRequest(Fr.ZERO, Fr.ZERO, 0);
}
toFriendlyJSON() {
return `Leaf=${this.leafSlot.toFriendlyJSON()}: ${this.newValue.toFriendlyJSON()}, SideEffectCounter=${
this.sideEffectCounter
}`;
}
[inspect.custom]() {
return `PublicDataUpdateRequest { leafSlot: ${this.leafSlot.toFriendlyJSON()}, newValue: ${this.newValue.toFriendlyJSON()}, sideEffectCounter: ${
this.sideEffectCounter
} }`;
}
}