Skip to content

Commit 801b52e

Browse files
Bhargavasomuhwwhww
authored andcommitted
Implements validate_proposer_signature function
1 parent d82b10a commit 801b52e

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from eth_utils import (
2+
ValidationError,
3+
)
4+
5+
from eth.beacon.enums import (
6+
SignatureDomain,
7+
)
8+
from eth.beacon.helpers import (
9+
get_beacon_proposer_index,
10+
get_domain,
11+
)
12+
from eth.beacon.types.blocks import (
13+
BaseBeaconBlock,
14+
)
15+
from eth.beacon.types.proposal_signed_data import (
16+
ProposalSignedData,
17+
)
18+
from eth.beacon.types.states import (
19+
BeaconState,
20+
)
21+
22+
from eth._utils.bls import (
23+
verify,
24+
)
25+
26+
27+
def validate_proposer_signature(state: BeaconState,
28+
block: BaseBeaconBlock,
29+
beacon_chain_shard_number: int,
30+
epoch_length: int) -> None:
31+
block_without_signature_root = BaseBeaconBlock.create_block_without_signature_root(block).root
32+
33+
proposal_root = ProposalSignedData(
34+
state.slot,
35+
beacon_chain_shard_number,
36+
block_without_signature_root
37+
).root
38+
39+
# Get the public key of proposer
40+
beacon_proposer_index = get_beacon_proposer_index(state, state.slot, epoch_length)
41+
proposer_pubkey = state.validator_registry[beacon_proposer_index].pubkey
42+
43+
if not verify(
44+
pubkey=proposer_pubkey,
45+
message=proposal_root,
46+
signature=block.signature,
47+
domain=get_domain(state.fork_data, state.slot, SignatureDomain.DOMAIN_PROPOSAL)):
48+
raise ValidationError("Invalid Proposer Signature on block")

eth/beacon/types/blocks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
uint64,
2020
uint384,
2121
)
22+
23+
from eth.beacon.constants import EMPTY_SIGNATURE
2224
from eth.beacon._utils.hash import hash_eth2
2325

2426
from .attestations import Attestation
@@ -111,3 +113,17 @@ def root(self) -> Hash32:
111113
@property
112114
def num_attestations(self) -> int:
113115
return len(self.body.attestations)
116+
117+
@classmethod
118+
def create_block_without_signature_root(
119+
cls,
120+
block: 'BaseBeaconBlock') -> 'BaseBeaconBlock':
121+
return cls(
122+
slot=block.slot,
123+
parent_root=block.parent_root,
124+
state_root=block.state_root,
125+
randao_reveal=block.randao_reveal,
126+
candidate_pow_receipt_root=block.candidate_pow_receipt_root,
127+
signature=EMPTY_SIGNATURE,
128+
body=block.body,
129+
)

eth/beacon/types/proposal_signed_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
)
44
import rlp
55

6+
from eth.beacon._utils.hash import hash_eth2
7+
68
from eth.rlp.sedes import (
79
uint64,
810
hash32,
@@ -31,3 +33,17 @@ def __init__(self,
3133
shard,
3234
block_root,
3335
)
36+
37+
_hash = None
38+
39+
@property
40+
def hash(self) -> Hash32:
41+
if self._hash is None:
42+
self._hash = hash_eth2(rlp.encode(self))
43+
return self._hash
44+
45+
@property
46+
def root(self) -> Hash32:
47+
# Alias of `hash`.
48+
# Using flat hash, might change to SSZ tree hash.
49+
return self.hash

0 commit comments

Comments
 (0)