Skip to content

Commit

Permalink
fix genesis interop by fixing deposit signature handling; add TRACE-l…
Browse files Browse the repository at this point in the history
…evel compilation to CI
  • Loading branch information
tersec authored and zah committed Jan 6, 2020
1 parent af6510b commit ab06ec3
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions beacon_chain.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ task test, "Run all tests":
buildBinary "test_fixture_ssz_generic_types", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG"

# Consensus object SSZ tests
buildBinary "test_fixture_ssz_consensus_objects", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=minimal"
buildBinary "test_fixture_ssz_consensus_objects", "tests/official/", "-r -d:release -d:chronicles_log_level=TRACE -d:const_preset=minimal"
buildBinary "test_fixture_ssz_consensus_objects", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=mainnet"

buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=minimal"
buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=mainnet"
buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=TRACE -d:const_preset=mainnet"

# State sim; getting into 4th epoch useful to trigger consensus checks
buildBinary "state_sim", "research/", "-r -d:release", "--validators=128 --slots=40"
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/interop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func makeDeposit*(
if skipValidation notin flags:
ret.data.signature =
bls_sign(
privkey, hash_tree_root(ret.data).data, compute_domain(DOMAIN_DEPOSIT))
privkey, hash_tree_root(ret.getDepositMessage).data,
compute_domain(DOMAIN_DEPOSIT))

ret
6 changes: 3 additions & 3 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func process_deposit*(
# Verify the Merkle branch
# TODO enable this check, but don't use doAssert
if not is_valid_merkle_branch(
hash_tree_root(deposit.data),
hash_tree_root(deposit.getDepositMessage),
deposit.proof,
DEPOSIT_CONTRACT_TREE_DEPTH,
state.eth1_deposit_index,
Expand All @@ -80,8 +80,8 @@ func process_deposit*(
if index == -1:
# Verify the deposit signature (proof of possession)
if skipValidation notin flags and not bls_verify(
pubkey, hash_tree_root(deposit.data).data, deposit.data.signature,
compute_domain(DOMAIN_DEPOSIT)):
pubkey, hash_tree_root(deposit.getDepositMessage).data,
deposit.data.signature, compute_domain(DOMAIN_DEPOSIT)):
return false

# Add validator and balance entries
Expand Down
8 changes: 8 additions & 0 deletions beacon_chain/spec/datatypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ macro fieldMaxLen*(x: typed): untyped =
func shortValidatorKey*(state: BeaconState, validatorIdx: int): string =
($state.validators[validatorIdx].pubkey)[0..7]

func getDepositMessage*(depositData: DepositData): DepositMessage =
result.pubkey = depositData.pubkey
result.amount = depositData.amount
result.withdrawal_credentials = depositData.withdrawal_credentials

func getDepositMessage*(deposit: Deposit): DepositMessage =
deposit.data.getDepositMessage

template ethTimeUnit(typ: type) {.dirty.} =
proc `+`*(x: typ, y: uint64): typ {.borrow.}
proc `-`*(x: typ, y: uint64): typ {.borrow.}
Expand Down
2 changes: 1 addition & 1 deletion tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import # Unit test
./test_beaconstate,
./test_block_pool,
./test_helpers,
#./test_interop, TODO check zcli
./test_interop,
./test_ssz,
./test_state_transition,
./test_sync_protocol,
Expand Down
4 changes: 2 additions & 2 deletions tests/mocking/mock_deposits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func signMockDepositData(
# No state --> Genesis
deposit_data.signature = bls_sign(
key = privkey,
msg = deposit_data.hash_tree_root().data,
msg = deposit_data.getDepositMessage().hash_tree_root().data,
domain = compute_domain(
DOMAIN_DEPOSIT,
default(array[4, byte]) # Genesis is fork_version 0
Expand All @@ -39,7 +39,7 @@ func signMockDepositData(
) =
deposit_data.signature = bls_sign(
key = privkey,
msg = deposit_data.hash_tree_root().data,
msg = deposit_data.getDepositMessage().hash_tree_root().data,
domain = get_domain(
state,
DOMAIN_DEPOSIT
Expand Down
7 changes: 3 additions & 4 deletions tests/test_interop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ suite "Interop":
timedTest "Interop genesis":
# Check against https://github.com/protolambda/zcli:
# zcli keys generate --to 64 | zcli genesis mock --genesis-time 1570500000 > /tmp/state.ssz
# zcli hash-tree-root /tmp.state.ssz
# zcli hash-tree-root state /tmp/state.ssz
var deposits: seq[Deposit]

for i in 0..<64:
Expand All @@ -157,11 +157,10 @@ suite "Interop":

let expected =
when const_preset == "minimal":
"75016055f843b92972d647a849168e8c5f559e8d41e05f94fc3f6a9665d1cabb"
"5a3bbcae4ab2b4eafded947689fd7bd8214a616ffffd2521befdfe2a3b2f74c0"
elif const_preset == "mainnet":
"27e4b5dfc67b97fd7d441c60bd5c92851fc1ceebe22435903183d915b3e4e678"
"db0a887acd5e201ac579d6cdc0c4932f2a0adf342d84dc5cd11ce959fbce3760"
else:
"unimplemented"
check:
hash_tree_root(initialState).data.toHex() == expected
true
2 changes: 1 addition & 1 deletion tests/testblockutil.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func makeDeposit(i: int, flags: UpdateFlags): Deposit =

if skipValidation notin flags:
result.data.signature =
bls_sign(privkey, hash_tree_root(result.data).data,
bls_sign(privkey, hash_tree_root(result.getDepositMessage).data,
domain)

func makeInitialDeposits*(
Expand Down

0 comments on commit ab06ec3

Please sign in to comment.