-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathBeaconChainLikeWithdrawalsModule.php
More file actions
140 lines (113 loc) · 5.2 KB
/
BeaconChainLikeWithdrawalsModule.php
File metadata and controls
140 lines (113 loc) · 5.2 KB
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
<?php declare(strict_types = 1);
/* Idea (c) 2023 Nikita Zhavoronkov, nikzh@nikzh.com
* Copyright (c) 2023 3xpl developers, 3@3xpl.com, see CONTRIBUTORS.md
* Distributed under the MIT software license, see LICENSE.md */
/* This module processes withdrawals happening on the Beacon Chain.
* It requires a Prysm-like node to run. */
abstract class BeaconChainLikeWithdrawalsModule extends CoreModule
{
use BeaconChainLikeTraits;
public ?BlockHashFormat $block_hash_format = BlockHashFormat::HexWithout0x;
public ?AddressFormat $address_format = AddressFormat::AlphaNumeric;
public ?TransactionHashFormat $transaction_hash_format = TransactionHashFormat::AlphaNumeric;
public ?TransactionRenderModel $transaction_render_model = TransactionRenderModel::Mixed;
public ?FeeRenderModel $fee_render_model = FeeRenderModel::None;
public ?PrivacyModel $privacy_model = PrivacyModel::Transparent;
public ?array $events_table_fields = ['block', 'transaction', 'sort_key', 'time', 'address', 'effect', 'extra_indexed'];
public ?array $events_table_nullable_fields = [];
public ?bool $should_return_events = true;
public ?bool $should_return_currencies = false;
public ?bool $allow_empty_return_events = true;
public ?bool $mempool_implemented = false;
public ?bool $forking_implemented = false;
public ?bool $ignore_sum_of_all_effects = true; // Essentially, all events here always come in pair with `the-void`, but we don't
// put that into the database to save space.
public string $block_entity_name = 'epoch';
public string $transaction_entity_name = 'slot';
public string $address_entity_name = 'validator';
public array $chain_config = [];
final public function pre_initialize()
{
$this->version = 1;
}
final public function post_post_initialize()
{
if (!isset($this->chain_config['BELLATRIX_FORK_EPOCH'])) throw new DeveloperError("`BELLATRIX_FORK_EPOCH` is not set");
if (!isset($this->chain_config['CAPELLA_FORK_EPOCH'])) throw new DeveloperError("`CAPELLA_FORK_EPOCH` is not set");
if (!isset($this->chain_config['SLOT_PER_EPOCH'])) throw new DeveloperError("`SLOT_PER_EPOCH` is not set");
if (!isset($this->chain_config['DELAY'])) throw new DeveloperError("`DELAY` is not set");
}
final public function pre_process_block($block) // $block here is an epoch number
{
$events = [];
$rq_slot_time = [];
$withdrawals = []; // [i] -> [validator, address, amount, slot]
$slot_data = [];
$proposers = requester_single($this->select_node(),
endpoint: "eth/v1/validator/duties/proposer/{$block}",
timeout: $this->timeout,
result_in: 'data',
);
foreach ($proposers as $proposer)
$slots[$proposer['slot']] = null;
foreach ($slots as $slot => $tm)
$rq_slot_time[] = requester_multi_prepare($this->select_node(), endpoint: "eth/v1/beacon/blocks/{$slot}");
$rq_slot_time_multi = requester_multi($rq_slot_time,
limit: envm($this->module, 'REQUESTER_THREADS'),
timeout: $this->timeout,
valid_codes: [200, 404],
);
foreach ($rq_slot_time_multi as $slot)
$slot_data[] = requester_multi_process($slot);
foreach ($slot_data as $slot_info)
{
$withdrawal = [];
if (isset($slot_info['code']) && $slot_info['code'] === '404')
continue;
elseif (isset($slot_info['code']))
throw new ModuleError('Unexpected response code');
$slot_id = (string)$slot_info['data']['message']['slot'];
if (isset($slot_info['data']['message']['body']['execution_payload']))
{
$timestamp = (int)$slot_info['data']['message']['body']['execution_payload']['timestamp'];
if ($block >= $this->chain_config['CAPELLA_FORK_EPOCH'])
{
$withdrawal = $slot_info['data']['message']['body']['execution_payload']['withdrawals'];
}
}
else
{
$timestamp = 0;
$withdrawal = [];
}
$slots[$slot_id] = $timestamp;
foreach ($withdrawal as $w)
{
$withdrawals[] = [
$w['validator_index'],
$w['address'],
$w['amount'],
$slot_id,
];
}
}
usort($withdrawals, function ($a, $b) {
return (strcmp($a[3], $b[3]));
});
$this->get_epoch_time($block, $slots);
$key_tes = 0;
foreach ($withdrawals as [$index, $address, $amount, $slot])
{
$events[] = [
'block' => $block,
'transaction' => $slot,
'sort_key' => $key_tes++,
'time' => $this->block_time,
'address' => (string)$index,
'effect' => '-' . $amount,
'extra_indexed' => (string)$address
];
}
$this->set_return_events($events);
}
}