-
Notifications
You must be signed in to change notification settings - Fork 458
/
genesis_block_context.ts
136 lines (128 loc) · 3.91 KB
/
genesis_block_context.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
/*
* 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 { Logger } from '../logger';
import { MethodContext } from './method_context';
import { EVENT_INDEX_FINALIZE_GENESIS_STATE, EVENT_INDEX_INIT_GENESIS_STATE } from './constants';
import { EventQueue } from './event_queue';
import { PrefixedStateReadWriter } from './prefixed_state_read_writer';
import { BlockAssets, BlockHeader, GenesisBlockExecuteContext, Validator } from './types';
export interface ContextParams {
logger: Logger;
stateStore: PrefixedStateReadWriter;
header: BlockHeader;
assets: BlockAssets;
eventQueue: EventQueue;
chainID: Buffer;
}
export class GenesisBlockContext {
private readonly _stateStore: PrefixedStateReadWriter;
private readonly _contextStore: Map<string, unknown>;
private readonly _logger: Logger;
private readonly _header: BlockHeader;
private readonly _assets: BlockAssets;
private readonly _eventQueue: EventQueue;
private readonly _chainID: Buffer;
private _nextValidators?: {
precommitThreshold: bigint;
certificateThreshold: bigint;
validators: Validator[];
};
public constructor(params: ContextParams) {
this._logger = params.logger;
this._stateStore = params.stateStore;
this._eventQueue = params.eventQueue;
this._header = params.header;
this._assets = params.assets;
this._chainID = params.chainID;
this._contextStore = new Map<string, unknown>();
}
public createInitGenesisStateContext(): GenesisBlockExecuteContext {
const childQueue = this._eventQueue.getChildQueue(EVENT_INDEX_INIT_GENESIS_STATE);
return {
eventQueue: childQueue,
getMethodContext: () =>
new MethodContext({
stateStore: this._stateStore,
eventQueue: childQueue,
contextStore: this._contextStore,
}),
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
this._stateStore.getStore(moduleID, storePrefix),
stateStore: this._stateStore,
header: this._header,
logger: this._logger,
assets: this._assets,
chainID: this._chainID,
setNextValidators: (
precommitThreshold: bigint,
certificateThreshold: bigint,
validators: Validator[],
) => {
if (this._nextValidators) {
throw new Error('Next validators can be set only once');
}
this._nextValidators = {
precommitThreshold,
certificateThreshold,
validators: [...validators],
};
},
};
}
public createFinalizeGenesisStateContext(): GenesisBlockExecuteContext {
const childQueue = this._eventQueue.getChildQueue(EVENT_INDEX_FINALIZE_GENESIS_STATE);
return {
eventQueue: childQueue,
getMethodContext: () =>
new MethodContext({
stateStore: this._stateStore,
eventQueue: childQueue,
contextStore: this._contextStore,
}),
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
this._stateStore.getStore(moduleID, storePrefix),
stateStore: this._stateStore,
header: this._header,
logger: this._logger,
assets: this._assets,
chainID: this._chainID,
setNextValidators: (
precommitThreshold: bigint,
certificateThreshold: bigint,
validators: Validator[],
) => {
if (this._nextValidators) {
throw new Error('Next validators can be set only once');
}
this._nextValidators = {
precommitThreshold,
certificateThreshold,
validators: [...validators],
};
},
};
}
public get eventQueue(): EventQueue {
return this._eventQueue;
}
public get nextValidators() {
return (
this._nextValidators ?? {
certificateThreshold: BigInt(0),
precommitThreshold: BigInt(0),
validators: [],
}
);
}
}