-
Notifications
You must be signed in to change notification settings - Fork 63
/
block.py
63 lines (52 loc) · 1.62 KB
/
block.py
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
from typing import ( # noqa: F401
Any,
Dict,
)
from ssz import (
serialize,
)
from beacon_chain.utils.blake import blake
from beacon_chain.state.constants import (
ZERO_HASH32,
)
from .attestation_record import AttestationRecord
class Block():
fields = {
# Hash of the parent block
'parent_hash': 'hash32',
# Slot number (for the PoS mechanism)
'slot_number': 'uint64',
# Randao commitment reveal
'randao_reveal': 'hash32',
# Attestations
'attestations': [AttestationRecord],
# Reference to PoW chain block
'pow_chain_ref': 'hash32',
# Hash of the active state
'active_state_root': 'hash32',
# Hash of the crystallized state
'crystallized_state_root': 'hash32',
} # type: Dict[str, Any]
defaults = {
'parent_hash': ZERO_HASH32,
'slot_number': 0,
'randao_reveal': ZERO_HASH32,
'attestations': [],
'pow_chain_ref': ZERO_HASH32,
'active_state_root': ZERO_HASH32,
'crystallized_state_root': ZERO_HASH32,
}
def __init__(self, **kwargs):
for k in self.fields.keys():
assert k in kwargs or k in self.defaults
setattr(self, k, kwargs.get(k, self.defaults.get(k)))
def __setattr__(self, name: str, value: Any) -> None:
super().__setattr__(name, value)
def __getattribute__(self, name: str) -> Any:
return super().__getattribute__(name)
@property
def hash(self):
return blake(serialize(self))
@property
def num_attestations(self):
return len(self.attestations)