slightly update/rename verify_merkle_branch(...) to 0.8.2 is_valid_merkle_branch(...); mark check_attestation(...), get_active_validator_indices(...), get_seed(...), and BeaconBlockBody as 0.8.2; update compute_domain(...) 0.8.2 by reversing field order (#356)
This commit is contained in:
parent
60dfa7081b
commit
cc0429a643
|
@ -11,21 +11,21 @@ import
|
|||
../extras, ../ssz, ../beacon_node_types,
|
||||
./crypto, ./datatypes, ./digest, ./helpers, ./validator
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#verify_merkle_branch
|
||||
func verify_merkle_branch(leaf: Eth2Digest, proof: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool =
|
||||
## Verify that the given ``leaf`` is on the merkle branch ``proof``
|
||||
## starting with the given ``root``.
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#is_valid_merkle_branch
|
||||
func is_valid_merkle_branch(leaf: Eth2Digest, branch: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool =
|
||||
## Check if ``leaf`` at ``index`` verifies against the Merkle ``root`` and
|
||||
## ``branch``.
|
||||
var
|
||||
value = leaf
|
||||
buf: array[64, byte]
|
||||
|
||||
for i in 0 ..< depth.int:
|
||||
if (index div (1'u64 shl i)) mod 2 != 0:
|
||||
buf[0..31] = proof[i.int].data
|
||||
buf[0..31] = branch[i.int].data
|
||||
buf[32..63] = value.data
|
||||
else:
|
||||
buf[0..31] = value.data
|
||||
buf[32..63] = proof[i.int].data
|
||||
buf[32..63] = branch[i.int].data
|
||||
value = eth2hash(buf)
|
||||
value == root
|
||||
|
||||
|
@ -53,7 +53,7 @@ func process_deposit*(
|
|||
|
||||
# Verify the Merkle branch
|
||||
# TODO enable this check, but don't use doAssert
|
||||
if not verify_merkle_branch(
|
||||
if not is_valid_merkle_branch(
|
||||
hash_tree_root(deposit.data),
|
||||
deposit.proof,
|
||||
DEPOSIT_CONTRACT_TREE_DEPTH,
|
||||
|
@ -466,8 +466,7 @@ func get_indexed_attestation*(state: BeaconState, attestation: Attestation,
|
|||
signature: attestation.signature,
|
||||
)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.1/specs/core/0_beacon-chain.md#attestations
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#attestations
|
||||
proc check_attestation*(
|
||||
state: BeaconState, attestation: Attestation, flags: UpdateFlags,
|
||||
stateCache: var StateCache): bool =
|
||||
|
|
|
@ -53,7 +53,7 @@ else:
|
|||
{.fatal: "Preset \"" & const_preset ".nim\" is not supported.".}
|
||||
|
||||
const
|
||||
SPEC_VERSION* = "0.8.1" ## \
|
||||
SPEC_VERSION* = "0.8.2" ## \
|
||||
## Spec version we're aiming to be compatible with, right now
|
||||
## TODO: improve this scheme once we can negotiate versions in protocol
|
||||
|
||||
|
@ -239,11 +239,13 @@ type
|
|||
body_root*: Eth2Digest
|
||||
signature*: ValidatorSig
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#beaconblockbody
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#beaconblockbody
|
||||
BeaconBlockBody* = object
|
||||
randao_reveal*: ValidatorSig
|
||||
eth1_data*: Eth1Data
|
||||
graffiti*: Eth2Digest
|
||||
|
||||
# Each of these is a length-bounded list, but enforcing that's elsewhere
|
||||
proposer_slashings*: seq[ProposerSlashing]
|
||||
attester_slashings*: seq[AttesterSlashing]
|
||||
attestations*: seq[Attestation]
|
||||
|
|
|
@ -73,9 +73,10 @@ func is_active_validator*(validator: Validator, epoch: Epoch): bool =
|
|||
validator.activation_epoch <= epoch and epoch < validator.exit_epoch
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_active_validator_indices
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#get_active_validator_indices
|
||||
func get_active_validator_indices*(state: BeaconState, epoch: Epoch):
|
||||
seq[ValidatorIndex] =
|
||||
# Get active validator indices at ``epoch``.
|
||||
# Return the sequence of active validator indices at ``epoch``.
|
||||
for idx, val in state.validators:
|
||||
if is_active_validator(val, epoch):
|
||||
result.add idx.ValidatorIndex
|
||||
|
@ -106,7 +107,6 @@ func get_randao_mix*(state: BeaconState,
|
|||
## LATEST_RANDAO_MIXES_LENGTH, current_epoch].
|
||||
state.randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#bytes_to_int
|
||||
func bytes_to_int*(data: openarray[byte]): uint64 =
|
||||
doAssert data.len == 8
|
||||
|
||||
|
@ -115,7 +115,6 @@ func bytes_to_int*(data: openarray[byte]): uint64 =
|
|||
for i in countdown(7, 0):
|
||||
result = result * 256 + data[i]
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#int_to_bytes1-int_to_bytes2-
|
||||
# Have 1, 4, 8, and 32-byte versions. 1+ more and maybe worth metaprogramming.
|
||||
func int_to_bytes32*(x: uint64): array[32, byte] =
|
||||
## Little-endian data representation
|
||||
|
@ -145,12 +144,12 @@ func int_to_bytes4*(x: uint64): array[4, byte] =
|
|||
result[2] = ((x shr 16) and 0xff).byte
|
||||
result[3] = ((x shr 24) and 0xff).byte
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#bls_domain
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#compute_domain
|
||||
func compute_domain(domain_type: DomainType, fork_version: array[4, byte]):
|
||||
uint64 =
|
||||
var buf: array[8, byte]
|
||||
buf[0..3] = fork_version
|
||||
buf[4..7] = int_to_bytes4(domain_type.uint64)
|
||||
buf[0..3] = int_to_bytes4(domain_type.uint64)
|
||||
buf[4..7] = fork_version
|
||||
bytes_to_int(buf)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#get_domain
|
||||
|
@ -169,7 +168,7 @@ func get_domain*(
|
|||
func get_domain*(state: BeaconState, domain_type: DomainType): Domain =
|
||||
get_domain(state, domain_type, get_current_epoch(state))
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#get_seed
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#get_seed
|
||||
func get_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
|
||||
# Generate a seed for the given ``epoch``.
|
||||
|
||||
|
|
Loading…
Reference in New Issue