This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 454
/
state_store_mock.ts
138 lines (126 loc) · 4.71 KB
/
state_store_mock.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
/*
* Copyright © 2020 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.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-return */
// eslint-disable-next-line @typescript-eslint/no-require-imports
import { objects } from '@liskhq/lisk-utils';
import { BlockHeader, Account, AccountDefaultProps } from '../../types';
interface AccountState {
get<T = any>(address: Buffer): Promise<Account<T>>;
getOrDefault<T = any>(address: Buffer): Promise<Account<T>>;
set<T = any>(address: Buffer, account: T): Promise<void>;
del(address: Buffer): Promise<void>;
getUpdated<T = any>(): Account<T>[];
}
interface ChainState {
lastBlockHeaders: BlockHeader[];
lastBlockReward: bigint;
networkIdentifier: Buffer;
get(key: string): Promise<Buffer | undefined>;
set(address: string, value: Buffer): Promise<void>;
}
interface ConsensusState {
get(key: string): Promise<Buffer | undefined>;
set(address: string, value: Buffer): Promise<void>;
}
export interface MockInput {
accounts?: Account<any>[];
defaultAccount?: AccountDefaultProps;
chain?: { [key: string]: Buffer };
consensus?: { [key: string]: Buffer };
lastBlockHeaders?: Partial<BlockHeader>[];
networkIdentifier?: Buffer;
lastBlockReward?: bigint;
}
const defaultNetworkIdentifier = Buffer.from('', 'hex');
export class StateStoreMock {
public accountData: { address: Buffer }[];
public chainData: { [key: string]: Buffer };
public consensusData: { [key: string]: Buffer };
public account: AccountState;
public chain: ChainState;
public consensus: ConsensusState;
private readonly _defaultAccount: AccountDefaultProps;
public constructor({
accounts,
chain,
consensus,
defaultAccount,
lastBlockHeaders,
lastBlockReward,
networkIdentifier,
}: MockInput = {}) {
// Make sure to be deep copy
this.accountData = accounts?.map(a => ({ ...a } as { address: Buffer })) ?? [];
this.chainData = chain ?? {};
this.consensusData = consensus ?? {};
this._defaultAccount = defaultAccount ?? {};
this.account = {
// eslint-disable-next-line @typescript-eslint/require-await
get: async <T = any>(address: Buffer): Promise<Account<T>> => {
const account = this.accountData.find(acc => acc.address.equals(address));
if (!account) {
throw new Error('Account not defined');
}
return objects.cloneDeep(account) as Account<T>;
},
// eslint-disable-next-line @typescript-eslint/require-await
getOrDefault: async <T = any>(address: Buffer): Promise<Account<T>> => {
const account = this.accountData.find(acc => acc.address.equals(address));
if (!account) {
return objects.cloneDeep({ ...this._defaultAccount, address }) as Account<T>;
}
return objects.cloneDeep(account) as Account<T>;
},
getUpdated: <T = any>() => this.accountData as Account<T>[],
// eslint-disable-next-line @typescript-eslint/require-await
set: async <T = any>(address: Buffer, account: Account<T>): Promise<void> => {
const index = this.accountData.findIndex(acc => acc.address.equals(address));
if (index > -1) {
this.accountData[index] = account;
return;
}
this.accountData.push(account);
},
// eslint-disable-next-line @typescript-eslint/require-await
del: async (address: Buffer): Promise<void> => {
const index = this.accountData.findIndex(acc => acc.address.equals(address));
if (index < 0) {
throw new Error('Cannot delete not existing account');
}
this.accountData.splice(index, 1);
},
};
this.chain = {
networkIdentifier: networkIdentifier ?? defaultNetworkIdentifier,
lastBlockHeaders: (lastBlockHeaders as BlockHeader[]) ?? [],
lastBlockReward: lastBlockReward ?? BigInt(0),
get: async (key: string): Promise<Buffer | undefined> =>
Promise.resolve(objects.cloneDeep(this.chainData[key])),
// eslint-disable-next-line @typescript-eslint/require-await
set: async (key: string, value: Buffer): Promise<void> => {
this.chainData[key] = value;
},
};
this.consensus = {
get: async (key: string): Promise<Buffer | undefined> =>
Promise.resolve(objects.cloneDeep(this.consensusData[key])),
// eslint-disable-next-line @typescript-eslint/require-await
set: async (key: string, value: Buffer): Promise<void> => {
this.consensusData[key] = value;
},
};
}
}