-
Notifications
You must be signed in to change notification settings - Fork 458
/
transaction_context.ts
175 lines (164 loc) · 5.31 KB
/
transaction_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
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
/*
* 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 { codec, Schema } from '@liskhq/lisk-codec';
import { Logger } from '../logger';
import { createMethodContext, createImmutableMethodContext } from './method_context';
import { EventQueue } from './event_queue';
import { PrefixedStateReadWriter } from './prefixed_state_read_writer';
import {
CommandExecuteContext,
CommandVerifyContext,
TransactionExecuteContext,
TransactionVerifyContext,
BlockHeader,
BlockAssets,
} from './types';
import { EVENT_TOPIC_TRANSACTION_EXECUTION } from './constants';
interface ContextParams {
chainID: Buffer;
stateStore: PrefixedStateReadWriter;
contextStore: Map<string, unknown>;
logger: Logger;
eventQueue: EventQueue;
transaction: Transaction;
header: BlockHeader;
assets?: BlockAssets;
}
export class TransactionContext {
private readonly _stateStore: PrefixedStateReadWriter;
private readonly _contextStore: Map<string, unknown>;
private readonly _chainID: Buffer;
private readonly _logger: Logger;
private readonly _eventQueue: EventQueue;
private readonly _transaction: Transaction;
private readonly _header: BlockHeader;
private readonly _assets?: BlockAssets;
public constructor(params: ContextParams) {
this._stateStore = params.stateStore;
this._contextStore = params.contextStore;
this._logger = params.logger;
this._header = params.header;
this._eventQueue = params.eventQueue;
this._chainID = params.chainID;
this._transaction = params.transaction;
this._assets = params.assets;
}
public createTransactionVerifyContext(): TransactionVerifyContext {
return {
logger: this._logger,
chainID: this._chainID,
stateStore: this._stateStore,
header: { height: this._header.height, timestamp: this._header.timestamp },
contextStore: this._contextStore,
getMethodContext: () => createImmutableMethodContext(this._stateStore),
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
this._stateStore.getStore(moduleID, storePrefix),
transaction: this._transaction,
};
}
public createTransactionExecuteContext(): TransactionExecuteContext {
if (!this._assets) {
throw new Error('Transaction Execution requires block assets in the context.');
}
const childQueue = this._eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_TRANSACTION_EXECUTION, this._transaction.id]),
);
return {
logger: this._logger,
chainID: this._chainID,
eventQueue: childQueue,
stateStore: this._stateStore,
contextStore: this._contextStore,
getMethodContext: () =>
createMethodContext({
stateStore: this._stateStore,
eventQueue: childQueue,
contextStore: this._contextStore,
}),
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
this._stateStore.getStore(moduleID, storePrefix),
header: this._header,
transaction: this._transaction,
assets: this._assets,
};
}
public createCommandVerifyContext<T = undefined>(paramsSchema?: Schema): CommandVerifyContext<T> {
return {
logger: this._logger,
chainID: this._chainID,
stateStore: this._stateStore,
contextStore: this._contextStore,
header: {
height: this._header.height,
timestamp: this._header.timestamp,
},
getMethodContext: () =>
createMethodContext({
stateStore: this._stateStore,
eventQueue: this._eventQueue,
contextStore: this._contextStore,
}),
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
this._stateStore.getStore(moduleID, storePrefix),
transaction: this._transaction,
params: (paramsSchema
? codec.decode(paramsSchema, this._transaction.params)
: undefined) as T,
};
}
public createCommandExecuteContext<T = undefined>(
paramsSchema?: Schema,
): CommandExecuteContext<T> {
if (!this._header) {
throw new Error('Transaction Execution requires block header in the context.');
}
if (!this._assets) {
throw new Error('Transaction Execution requires block assets in the context.');
}
const childQueue = this._eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_TRANSACTION_EXECUTION, this._transaction.id]),
);
return {
logger: this._logger,
chainID: this._chainID,
eventQueue: childQueue,
stateStore: this._stateStore,
contextStore: this._contextStore,
getMethodContext: () =>
createMethodContext({
stateStore: this._stateStore,
eventQueue: childQueue,
contextStore: this._contextStore,
}),
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
this._stateStore.getStore(moduleID, storePrefix),
header: this._header,
transaction: this._transaction,
assets: this._assets,
params: (paramsSchema
? codec.decode(paramsSchema, this._transaction.params)
: undefined) as T,
};
}
public get transaction(): Transaction {
return this._transaction;
}
public get eventQueue(): EventQueue {
return this._eventQueue;
}
public get stateStore(): PrefixedStateReadWriter {
return this._stateStore;
}
}