-
Notifications
You must be signed in to change notification settings - Fork 767
/
runBlock.ts
320 lines (293 loc) · 8.66 KB
/
runBlock.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import { BN, toBuffer, bufferToInt } from 'ethereumjs-util'
import { encode } from 'rlp'
import VM from './index'
import Bloom from './bloom'
import { RunTxResult } from './runTx'
import { StateManager } from './state/index'
/**
* Options for running a block.
*/
export interface RunBlockOpts {
/**
* The [`Block`](https://github.com/ethereumjs/ethereumjs-block) to process
*/
block: any
/**
* Root of the state trie
*/
root?: Buffer
/**
* Whether to generate the stateRoot. If false `runBlock` will check the
* stateRoot of the block against the Trie
*/
generate?: boolean
/**
* If true, will skip block validation
*/
skipBlockValidation?: boolean
/**
* If true, skips the nonce check
*/
skipNonce?: boolean
/**
* If true, skips the balance check
*/
skipBalance?: boolean
}
/**
* Result of [[runBlock]]
*/
export interface RunBlockResult {
/**
* Receipts generated for transactions in the block
*/
receipts: (PreByzantiumTxReceipt | PostByzantiumTxReceipt)[]
/**
* Results of executing the transactions in the block
*/
results: RunTxResult[]
}
/**
* Abstract interface with common transaction receipt fields
*/
interface TxReceipt {
/**
* Gas used
*/
gasUsed: Buffer
/**
* Bloom bitvector
*/
bitvector: Buffer
/**
* Logs emitted
*/
logs: any[]
}
/**
* Pre-Byzantium receipt type with a field
* for the intermediary state root
*/
export interface PreByzantiumTxReceipt extends TxReceipt {
/**
* Intermediary state root
*/
stateRoot: Buffer
}
/**
* Receipt type for Byzantium and beyond replacing the intermediary
* state root field with a status code field (EIP-658)
*/
export interface PostByzantiumTxReceipt extends TxReceipt {
/**
* Status of transaction, `1` if successful, `0` if an exception occured
*/
status: 0 | 1
}
/**
* @ignore
*/
export default async function runBlock(this: VM, opts: RunBlockOpts): Promise<RunBlockResult> {
if (opts === undefined) {
throw new Error('invalid input, opts must be provided')
}
if (!opts.block) {
throw new Error('invalid input, block must be provided')
}
const state = this.stateManager
const block = opts.block
const generateStateRoot = !!opts.generate
/**
* The `beforeBlock` event.
*
* @event Event: beforeBlock
* @type {Object}
* @property {Block} block emits the block that is about to be processed
*/
await this._emit('beforeBlock', opts.block)
// Set state root if provided
if (opts.root) {
await state.setStateRoot(opts.root)
}
// Checkpoint state
await state.checkpoint()
let result
try {
result = await applyBlock.bind(this)(block, opts)
} catch (err) {
await state.revert()
throw err
}
// Persist state
await state.commit()
const stateRoot = await state.getStateRoot()
// Given the generate option, either set resulting header
// values to the current block, or validate the resulting
// header values against the current block.
if (generateStateRoot) {
block.header.stateRoot = stateRoot
block.header.bloom = result.bloom.bitvector
} else {
if (
result.receiptRoot &&
result.receiptRoot.toString('hex') !== block.header.receiptTrie.toString('hex')
) {
throw new Error('invalid receiptTrie ')
}
if (result.bloom.bitvector.toString('hex') !== block.header.bloom.toString('hex')) {
throw new Error('invalid bloom ')
}
if (bufferToInt(block.header.gasUsed) !== Number(result.gasUsed)) {
throw new Error('invalid gasUsed ')
}
if (stateRoot.toString('hex') !== block.header.stateRoot.toString('hex')) {
throw new Error('invalid block stateRoot ')
}
}
/**
* The `afterBlock` event
*
* @event Event: afterBlock
* @type {Object}
* @property {Object} result emits the results of processing a block
*/
await this._emit('afterBlock', {
receipts: result.receipts,
results: result.results,
})
return { receipts: result.receipts, results: result.results }
}
/**
* Validates and applies a block, computing the results of
* applying its transactions. This method doesn't modify the
* block itself. It computes the block rewards and puts
* them on state (but doesn't persist the changes).
* @param {Block} block
* @param {Boolean} [skipBlockValidation=false]
*/
async function applyBlock(this: VM, block: any, opts: RunBlockOpts) {
// Validate block
if (!opts.skipBlockValidation) {
if (new BN(block.header.gasLimit).gte(new BN('8000000000000000', 16))) {
throw new Error('Invalid block with gas limit greater than (2^63 - 1)')
} else {
await block.validate(this.blockchain)
}
}
// Apply transactions
const txResults = await applyTransactions.bind(this)(block, opts)
// Pay ommers and miners
await assignBlockRewards.bind(this)(block)
return txResults
}
/**
* Applies the transactions in a block, computing the receipts
* as well as gas usage and some relevant data. This method is
* side-effect free (it doesn't modify the block nor the state).
* @param {Block} block
*/
async function applyTransactions(this: VM, block: any, opts: RunBlockOpts) {
const bloom = new Bloom()
// the total amount of gas used processing these transactions
let gasUsed = new BN(0)
const receiptTrie = new Trie()
const receipts = []
const txResults = []
/*
* Process transactions
*/
for (let txIdx = 0; txIdx < block.transactions.length; txIdx++) {
const tx = block.transactions[txIdx]
const gasLimitIsHigherThanBlock = new BN(block.header.gasLimit).lt(
new BN(tx.gasLimit).add(gasUsed),
)
if (gasLimitIsHigherThanBlock) {
throw new Error('tx has a higher gas limit than the block')
}
// Run the tx through the VM
const txRes = await this.runTx({
tx: tx,
block: block,
skipBalance: opts.skipBalance,
skipNonce: opts.skipNonce,
})
txResults.push(txRes)
// Add to total block gas usage
gasUsed = gasUsed.add(txRes.gasUsed)
// Combine blooms via bitwise OR
bloom.or(txRes.bloom)
const abstractTxReceipt: TxReceipt = {
gasUsed: gasUsed.toArrayLike(Buffer),
bitvector: txRes.bloom.bitvector,
logs: txRes.execResult.logs || [],
}
let txReceipt
if (this._common.gteHardfork('byzantium')) {
txReceipt = {
status: txRes.execResult.exceptionError ? 0 : 1, // Receipts have a 0 as status on error
...abstractTxReceipt,
} as PostByzantiumTxReceipt
} else {
// This is just using a dummy place holder for the state root right now.
// Giving the correct intermediary state root would need a too depp intervention
// into the current checkpointing mechanism which hasn't been considered
// to be worth it on a HF backport, 2020-06-26
txReceipt = {
stateRoot: Buffer.alloc(32),
...abstractTxReceipt,
} as PreByzantiumTxReceipt
}
receipts.push(txReceipt)
// Add receipt to trie to later calculate receipt root
await receiptTrie.put(encode(txIdx), encode(Object.values(txReceipt)))
}
return {
bloom,
gasUsed,
receiptRoot: receiptTrie.root,
receipts,
results: txResults,
}
}
/**
* Calculates block rewards for miner and ommers and puts
* the updated balances of their accounts to state.
*/
async function assignBlockRewards(this: VM, block: any): Promise<void> {
const state = this.stateManager
const minerReward = new BN(this._common.param('pow', 'minerReward'))
const ommers = block.uncleHeaders
// Reward ommers
for (const ommer of ommers) {
const reward = calculateOmmerReward(
new BN(ommer.number),
new BN(block.header.number),
minerReward,
)
await rewardAccount(state, ommer.coinbase, reward)
}
// Reward miner
const reward = calculateMinerReward(minerReward, ommers.length)
await rewardAccount(state, block.header.coinbase, reward)
}
function calculateOmmerReward(ommerBlockNumber: BN, blockNumber: BN, minerReward: BN): BN {
const heightDiff = blockNumber.sub(ommerBlockNumber)
let reward = new BN(8).sub(heightDiff).mul(minerReward.divn(8))
if (reward.ltn(0)) {
reward = new BN(0)
}
return reward
}
function calculateMinerReward(minerReward: BN, ommersNum: number): BN {
// calculate nibling reward
const niblingReward = minerReward.divn(32)
const totalNiblingReward = niblingReward.muln(ommersNum)
const reward = minerReward.add(totalNiblingReward)
return reward
}
async function rewardAccount(state: StateManager, address: Buffer, reward: BN): Promise<void> {
const account = await state.getAccount(address)
account.balance = toBuffer(new BN(account.balance).add(reward))
await state.putAccount(address, account)
}