-
Notifications
You must be signed in to change notification settings - Fork 458
/
types.ts
261 lines (240 loc) · 7.67 KB
/
types.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { Transaction } from '@liskhq/lisk-chain';
import { Schema } from '@liskhq/lisk-codec';
import { IterateOptions } from '@liskhq/lisk-db';
import { TransactionVerifyResult } from '../abi';
import { Logger } from '../logger';
import { EventQueue } from './event_queue';
export interface ImmutableSubStore {
get(key: Buffer): Promise<Buffer>;
getWithSchema<T>(key: Buffer, schema: Schema): Promise<T>;
has(key: Buffer): Promise<boolean>;
iterate(input: IterateOptions): Promise<{ key: Buffer; value: Buffer }[]>;
iterateWithSchema<T>(input: IterateOptions, schema: Schema): Promise<{ key: Buffer; value: T }[]>;
}
export interface SubStore extends ImmutableSubStore {
del(key: Buffer): Promise<void>;
set(key: Buffer, value: Buffer): Promise<void>;
// eslint-disable-next-line @typescript-eslint/ban-types
setWithSchema(key: Buffer, value: object, schema: Schema): Promise<void>;
createSnapshot(): number;
restoreSnapshot(snapshotID: number): void;
}
export interface ImmutableStateStore {
getStore: (moduleID: Buffer, storePrefix: Buffer) => ImmutableSubStore;
}
/** State store interface. */
export interface StateStore {
getStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
createSnapshot(): number;
restoreSnapshot(snapshotID: number): void;
}
/** Context for immutable methods */
export interface ImmutableMethodContext {
getStore: (moduleID: Buffer, storePrefix: Buffer) => ImmutableSubStore;
}
/** Context for methods */
export interface MethodContext {
/**
* Returns a module store based on module ID and store prefix
* @param moduleID The ID of a module
* @param storePrefix The prefix of a module store
*
* @returns The requested module store.
*/
getStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
eventQueue: EventQueue;
contextStore: Map<string, unknown>;
}
export enum VerifyStatus {
FAIL = TransactionVerifyResult.INVALID,
OK = TransactionVerifyResult.OK,
PENDING = TransactionVerifyResult.PENDING,
}
/** The block header. */
export interface BlockHeader {
version: number;
height: number;
timestamp: number;
previousBlockID: Buffer;
generatorAddress: Buffer;
maxHeightPrevoted: number;
maxHeightGenerated: number;
impliesMaxPrevotes: boolean;
aggregateCommit: {
height: number;
aggregationBits: Buffer;
certificateSignature: Buffer;
};
}
/** The block assets. */
export interface BlockAssets {
getAsset: (module: string) => Buffer | undefined;
}
export interface WritableBlockAssets extends BlockAssets {
setAsset: (module: string, value: Buffer) => void;
}
export interface VerificationResult {
status: VerifyStatus;
error?: Error;
}
// eventQueue is not present here because event cannot be emitted
export interface TransactionVerifyContext {
chainID: Buffer;
logger: Logger;
header: { timestamp: number; height: number };
transaction: Transaction;
stateStore: ImmutableStateStore;
contextStore: Map<string, unknown>;
getMethodContext: () => ImmutableMethodContext;
getStore: (moduleID: Buffer, storePrefix: Buffer) => ImmutableSubStore;
}
/** Context for the command verification. */
export interface CommandVerifyContext<T = undefined> {
/** Logger interface, to create log messages. */
logger: Logger;
/** The identifier of the blockchain network, in which this command is executed. */
chainID: Buffer;
/** Timestamp and height when the transaction was sent. */
header: { timestamp: number; height: number };
/** The transaction to verify. */
transaction: Transaction; // without decoding params
/** The command-specific parameters. */
params: T;
/** TBD */
getMethodContext: () => ImmutableMethodContext;
/** State store interface to get data from the module stores. */
getStore: (moduleID: Buffer, storePrefix: Buffer) => ImmutableSubStore;
/** TBD */
stateStore: ImmutableStateStore;
/** TBD */
contextStore: Map<string, unknown>;
}
/**
* Context for the command execution.
*/
export interface CommandExecuteContext<T = undefined> {
/** Logger interface, to create log messages. */
logger: Logger;
/** The identifier of the blockchain network, in which this command is executed. */
chainID: Buffer;
/** Event interface to ad blockchain events to the event queue. */
eventQueue: EventQueue;
/** State store interface. */
stateStore: StateStore;
/** TBD */
contextStore: Map<string, unknown>;
/** The block header. */
header: BlockHeader;
/** The block assets. */
assets: BlockAssets;
/** The transaction to execute. */
transaction: Transaction; // without decoding params
/** The command-specific parameters. */
params: T;
/** Method interface to call methods */
getMethodContext: () => MethodContext;
/** State store interface to get data from the module stores. */
getStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
}
/**
* Context for the genesis block execution hook.
*/
export interface GenesisBlockExecuteContext {
logger: Logger;
eventQueue: EventQueue;
stateStore: StateStore;
getMethodContext: () => MethodContext;
/** State store interface to get data from the module stores. */
getStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
header: BlockHeader;
assets: BlockAssets;
/**
* Sets the next active validators to generate blocks after the genesis block.
*
* @param preCommitThreshold
* @param certificateThreshold
* @param validators
*/
setNextValidators: (
preCommitThreshold: bigint,
certificateThreshold: bigint,
validators: Validator[],
) => void;
/** The identifier of the blockchain, the genesis block belongs to. */
chainID: Buffer;
}
export interface TransactionExecuteContext {
logger: Logger;
chainID: Buffer;
eventQueue: EventQueue;
stateStore: StateStore;
contextStore: Map<string, unknown>;
getMethodContext: () => MethodContext;
getStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
header: BlockHeader;
assets: BlockAssets;
transaction: Transaction;
}
export interface BlockVerifyContext {
logger: Logger;
chainID: Buffer;
stateStore: ImmutableStateStore;
contextStore: Map<string, unknown>;
getMethodContext: () => ImmutableMethodContext;
getStore: (moduleID: Buffer, storePrefix: Buffer) => ImmutableSubStore;
header: BlockHeader;
assets: BlockAssets;
}
export interface BlockExecuteContext {
logger: Logger;
chainID: Buffer;
eventQueue: EventQueue;
stateStore: StateStore;
getMethodContext: () => MethodContext;
getStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
contextStore: Map<string, unknown>;
header: BlockHeader;
assets: BlockAssets;
}
export interface Validator {
address: Buffer;
bftWeight: bigint;
generatorKey: Buffer;
blsKey: Buffer;
}
export interface NextValidatorsSetter {
setNextValidators: (
preCommitThreshold: bigint,
certificateThreshold: bigint,
validators: Validator[],
) => void;
}
export type BlockAfterExecuteContext = BlockExecuteContext &
NextValidatorsSetter & {
transactions: ReadonlyArray<Transaction>;
};
export interface InsertAssetContext {
logger: Logger;
chainID: Buffer;
getMethodContext: () => MethodContext;
stateStore: ImmutableStateStore;
contextStore: Map<string, unknown>;
getStore: (moduleID: Buffer, storePrefix: Buffer) => ImmutableSubStore;
header: BlockHeader;
assets: WritableBlockAssets;
getOffchainStore: (moduleID: Buffer, storePrefix: Buffer) => SubStore;
getFinalizedHeight(): number;
}