-
Notifications
You must be signed in to change notification settings - Fork 0
/
engineApi.js
111 lines (99 loc) · 4.55 KB
/
engineApi.js
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
const { toQuantity, calculateWithdrawals, kzgCommitmentToVersionedHash } = require('./utils');
function prepareNewPayloadCall(beacon_block, fork) {
const execution_payload = beacon_block.body.execution_payload;
switch (fork) {
case 'capella':
return {
method: 'engine_newPayloadV2',
params: [
{
parentHash: execution_payload.parent_hash,
feeRecipient: execution_payload.fee_recipient,
stateRoot: execution_payload.state_root,
receiptsRoot: execution_payload.receipts_root,
logsBloom: execution_payload.logs_bloom,
prevRandao: execution_payload.prev_randao,
blockNumber: toQuantity(execution_payload.block_number),
gasLimit: toQuantity(execution_payload.gas_limit),
gasUsed: toQuantity(execution_payload.gas_used),
timestamp: toQuantity(execution_payload.timestamp),
extraData: execution_payload.extra_data,
baseFeePerGas: toQuantity(execution_payload.base_fee_per_gas),
blockHash: execution_payload.block_hash,
transactions: execution_payload.transactions,
withdrawals: calculateWithdrawals(execution_payload.withdrawals)
}
]
};
case 'deneb':
const blob_kzg_commitments = beacon_block.body.blob_kzg_commitments;
return {
method: 'engine_newPayloadV3',
params: [
{
parentHash: execution_payload.parent_hash,
feeRecipient: execution_payload.fee_recipient,
stateRoot: execution_payload.state_root,
receiptsRoot: execution_payload.receipts_root,
logsBloom: execution_payload.logs_bloom,
prevRandao: execution_payload.prev_randao,
blockNumber: toQuantity(execution_payload.block_number),
gasLimit: toQuantity(execution_payload.gas_limit),
gasUsed: toQuantity(execution_payload.gas_used),
timestamp: toQuantity(execution_payload.timestamp),
extraData: execution_payload.extra_data,
baseFeePerGas: toQuantity(execution_payload.base_fee_per_gas),
blockHash: execution_payload.block_hash,
transactions: execution_payload.transactions,
withdrawals: calculateWithdrawals(execution_payload.withdrawals),
blobGasUsed: toQuantity(execution_payload.blob_gas_used),
excessBlobGas: toQuantity(execution_payload.excess_blob_gas)
},
blob_kzg_commitments.map(kzgCommitmentToVersionedHash),
beacon_block.parent_root
]
};
}
}
function prepareForkchoiceUpdatedCall(fork_choice, headBlockHash, fork) {
const justifiedCheckpointRoot = fork_choice.justified_checkpoint.root;
const finalizedCheckpointRoot = fork_choice.finalized_checkpoint.root;
const forkChoiceNodes = fork_choice.fork_choice_nodes;
const safeBlockHash = executionBlockHashLookup(justifiedCheckpointRoot, forkChoiceNodes);
const finalizedBlockHash = executionBlockHashLookup(finalizedCheckpointRoot, forkChoiceNodes);
switch (fork) {
case 'capella':
return {
method: 'engine_forkchoiceUpdatedV2',
params: [
{
headBlockHash,
safeBlockHash,
finalizedBlockHash,
}
]
};
case 'deneb':
return {
method: 'engine_forkchoiceUpdatedV3',
params: [
{
headBlockHash,
safeBlockHash,
finalizedBlockHash,
}
]
};
}
}
function executionBlockHashLookup(beaconBlockRoot, forkChoiceNodes) {
const node = forkChoiceNodes.find(node => node.block_root === beaconBlockRoot);
if (node) {
return node.execution_block_hash;
}
return null;
}
module.exports = {
prepareNewPayloadCall,
prepareForkchoiceUpdatedCall
};