From a0fd345a77e633da86a7ae99330d8c2cb56efabe Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Fri, 31 May 2019 13:49:15 -0600 Subject: [PATCH 01/39] enforce order of attester slashing indicies. replicates #1126 --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 46c811fed..1ca1e9d4e 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1681,7 +1681,7 @@ def process_attester_slashing(state: BeaconState, slashed_any = False attesting_indices_1 = attestation_1.custody_bit_0_indices + attestation_1.custody_bit_1_indices attesting_indices_2 = attestation_2.custody_bit_0_indices + attestation_2.custody_bit_1_indices - for index in set(attesting_indices_1).intersection(attesting_indices_2): + for index in sorted(set(attesting_indices_1).intersection(attesting_indices_2)): if is_slashable_validator(state.validator_registry[index], get_current_epoch(state)): slash_validator(state, index) slashed_any = True From 8b65b95bd44e58012acb0e9905644d743f452371 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Fri, 31 May 2019 13:54:58 -0600 Subject: [PATCH 02/39] port deposit test from #1133 --- .../block_processing/test_process_deposit.py | 16 ++++++++++++++++ .../pyspec/eth2spec/test/helpers/deposits.py | 18 ++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py b/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py index 336af3bf7..95b13c779 100644 --- a/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py @@ -93,6 +93,22 @@ def test_invalid_sig_top_up(state): yield from run_deposit_processing(state, deposit, validator_index, valid=True, effective=True) +@spec_state_test +def test_invalid_withdrawal_credentials_top_up(state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(b"junk")[1:] + deposit = prepare_state_and_deposit( + state, + validator_index, + amount, + withdrawal_credentials=withdrawal_credentials, + ) + + # inconsistent withdrawal credentials, in top-ups, are allowed! + yield from run_deposit_processing(state, deposit, validator_index, valid=True, effective=True) + + @spec_state_test def test_wrong_index(state): validator_index = len(state.validator_registry) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index c5deb124e..d28d0bcb8 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -8,11 +8,10 @@ from eth2spec.utils.merkle_minimal import calc_merkle_tree_from_leaves, get_merk from eth2spec.utils.minimal_ssz import signing_root -def build_deposit_data(state, pubkey, privkey, amount, signed=False): +def build_deposit_data(state, pubkey, privkey, amount, withdrawal_credentials, signed=False): deposit_data = DepositData( pubkey=pubkey, - # insecurely use pubkey as withdrawal key as well - withdrawal_credentials=spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(pubkey)[1:], + withdrawal_credentials=withdrawal_credentials, amount=amount, ) if signed: @@ -37,8 +36,9 @@ def build_deposit(state, pubkey, privkey, amount, + withdrawal_credentials, signed): - deposit_data = build_deposit_data(state, pubkey, privkey, amount, signed) + deposit_data = build_deposit_data(state, pubkey, privkey, amount, withdrawal_credentials, signed) item = deposit_data.hash_tree_root() index = len(deposit_data_leaves) @@ -57,7 +57,7 @@ def build_deposit(state, return deposit, root, deposit_data_leaves -def prepare_state_and_deposit(state, validator_index, amount, signed=False): +def prepare_state_and_deposit(state, validator_index, amount, withdrawal_credentials=None, signed=False): """ Prepare the state for the deposit, and create a deposit for the given validator, depositing the given amount. """ @@ -67,13 +67,19 @@ def prepare_state_and_deposit(state, validator_index, amount, signed=False): pubkey = pubkeys[validator_index] privkey = privkeys[validator_index] + + # insecurely use pubkey as withdrawal key if no credentials provided + if withdrawal_credentials is None: + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(pubkey)[1:] + deposit, root, deposit_data_leaves = build_deposit( state, deposit_data_leaves, pubkey, privkey, amount, - signed + withdrawal_credentials, + signed, ) state.latest_eth1_data.deposit_root = root From d1e589f11fd854a7ec8ac7fee05e91c88c199d1d Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sat, 8 Jun 2019 19:00:50 +0100 Subject: [PATCH 03/39] Remove eth2 genesis in favour of genesis trigger --- .../contracts/validator_registration.json | 2 +- .../contracts/validator_registration.v.py | 22 -------- deposit_contract/tests/contracts/conftest.py | 26 ++-------- .../tests/contracts/test_deposit.py | 50 ------------------ specs/core/0_beacon-chain.md | 51 ++++++++++++++----- specs/core/0_deposit-contract.md | 20 -------- 6 files changed, 42 insertions(+), 129 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json index 08d57f80a..a885ef0a1 100644 --- a/deposit_contract/contracts/validator_registration.json +++ b/deposit_contract/contracts/validator_registration.json @@ -1 +1 @@ -{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}, {"type": "bytes", "name": "merkle_tree_index", "indexed": false}], "anonymous": false, "type": "event"}, {"name": "Eth2Genesis", "inputs": [{"type": "bytes32", "name": "deposit_root", "indexed": false}, {"type": "bytes", "name": "deposit_count", "indexed": false}, {"type": "bytes", "name": "time", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 7077}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 11026}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 445994}, {"name": "chainStarted", "outputs": [{"type": "bool", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 603}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600060c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600060c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600060c052602060c02001555b81516001018083528114156100aa575b505061140756600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561026b57602060046101403734156100b457600080fd5b67ffffffffffffffff6101405111156100cc57600080fd5b60006101605261014051610180526101a060006008818352015b6101605160086000811215610103578060000360020a820461010a565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561013557600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8600081121561017e578060000360020a8204610185565b8060020a82025b90509050610180525b81516001018083528114156100e6575b505060186008602082066101e001602082840111156101bc57600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101f957600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b8261030051111561022b57610247565b6000610300516102c001535b815160010180835281141561021b575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103c357341561028457600080fd5b6000610140526002546101605261018060006020818352015b600160016101605116141561031e57600061018051602081106102bf57600080fd5b600160c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af161031057600080fd5b60c05190506101405261038c565b6000610140516020826101a0010152602081019050610180516020811061034457600080fd5b600060c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161038257600080fd5b60c0519050610140525b610160600261039a57600080fd5b60028151048152505b815160010180835281141561029d575b50506101405160005260206000f3005b63621fd13060005114156104995734156103dc57600080fd5b60606101c060246380673289610140526002546101605261015c6000305af161040457600080fd5b6101e0805160200180610260828460006004600a8704601201f161042757600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c051111561045957610475565b60006102c05161028001535b8151600101808352811415610449575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d600051141561125657606060046101403760506004356004016101a03760306004356004013511156104cf57600080fd5b60406024356004016102203760206024356004013511156104ef57600080fd5b608060443560040161028037606060443560040135111561050f57600080fd5b63ffffffff6002541061052157600080fd5b60306101a0511461053157600080fd5b6020610220511461054157600080fd5b6060610280511461055157600080fd5b633b9aca00610340526103405161056757600080fd5b61034051340461032052633b9aca0061032051101561058557600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af16105ae57600080fd5b610460805160200180610360828460006004600a8704601201f16105d157600080fd5b50506002546104a05260006104c0526104a05160016104a0510110156105f657600080fd5b60016104a051016104e05261050060006020818352015b600160016104e05116141561062157610674565b6104c060605160018251018060405190131561063c57600080fd5b809190121561064a57600080fd5b8152506104e0600261065b57600080fd5b60028151048152505b815160010180835281141561060d575b505060006101a06030806020846105e001018260208501600060046016f1505080518201915050600060106020820661056001602082840111156106b757600080fd5b60208061058082610520600060046015f15050818152809050905090506010806020846105e001018260208501600060046013f1505080518201915050806105e0526105e09050602060c0825160208401600060025af161071757600080fd5b60c051905061054052600060006040602082066106800161028051828401111561074057600080fd5b6060806106a0826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af161078057600080fd5b60c051905060208261088001015260208101905060006040602060208206610740016102805182840111156107b457600080fd5b606080610760826020602088068803016102800160006004601bf150508181528090509050905060208060208461080001018260208501600060046015f15050805182019150506105205160208261080001015260208101905080610800526108009050602060c0825160208401600060025af161083157600080fd5b60c051905060208261088001015260208101905080610880526108809050602060c0825160208401600060025af161086857600080fd5b60c051905061066052600060006105405160208261092001015260208101905061022060208060208461092001018260208501600060046015f150508051820191505080610920526109209050602060c0825160208401600060025af16108ce57600080fd5b60c0519050602082610aa00101526020810190506000610360600880602084610a2001018260208501600060046012f150508051820191505060006018602082066109a0016020828401111561092357600080fd5b6020806109c082610520600060046015f1505081815280905090509050601880602084610a2001018260208501600060046014f150508051820191505061066051602082610a2001015260208101905080610a2052610a209050602060c0825160208401600060025af161099657600080fd5b60c0519050602082610aa001015260208101905080610aa052610aa09050602060c0825160208401600060025af16109cd57600080fd5b60c051905061090052610b2060006020818352015b6104c051610b20511215610a62576000610b205160208110610a0357600080fd5b600160c052602060c0200154602082610b4001015260208101905061090051602082610b4001015260208101905080610b4052610b409050602060c0825160208401600060025af1610a5457600080fd5b60c051905061090052610a67565b610a78565b5b81516001018083528114156109e2575b5050610900516104c05160208110610a8f57600080fd5b600160c052602060c02001556002805460018254011015610aaf57600080fd5b60018154018155506060610c4060246380673289610bc0526104a051610be052610bdc6000305af1610ae057600080fd5b610c60805160200180610ca0828460006004600a8704601201f1610b0357600080fd5b505060a0610d2052610d2051610d60526101a0805160200180610d2051610d6001828460006004600a8704601201f1610b3b57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516040818352015b83610d0051101515610b7957610b96565b6000610d00516020850101535b8151600101808352811415610b68575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610d8052610220805160200180610d2051610d6001828460006004600a8704601201f1610bed57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610c2b57610c48565b6000610d00516020850101535b8151600101808352811415610c1a575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610da052610360805160200180610d2051610d6001828460006004600a8704601201f1610c9f57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610cdd57610cfa565b6000610d00516020850101535b8151600101808352811415610ccc575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610dc052610280805160200180610d2051610d6001828460006004600a8704601201f1610d5157600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516060818352015b83610d0051101515610d8f57610dac565b6000610d00516020850101535b8151600101808352811415610d7e575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610de052610ca0805160200180610d2051610d6001828460006004600a8704601201f1610e0357600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610e4157610e5e565b6000610d00516020850101535b8151600101808352811415610e30575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d20527fdc5fc95703516abd38fa03c3737ff3b52dc52347055c8028460fdf5bbe2f12ce610d2051610d60a164077359400061032051101515611254576003805460018254011015610ed357600080fd5b60018154018155506201000060035414156112535742610e205242610e405262015180610eff57600080fd5b62015180610e405106610e20511015610f1757600080fd5b42610e405262015180610f2957600080fd5b62015180610e405106610e2051036202a30042610e205242610e405262015180610f5257600080fd5b62015180610e405106610e20511015610f6a57600080fd5b42610e405262015180610f7c57600080fd5b62015180610e405106610e205103011015610f9657600080fd5b6202a30042610e205242610e405262015180610fb157600080fd5b62015180610e405106610e20511015610fc957600080fd5b42610e405262015180610fdb57600080fd5b62015180610e405106610e20510301610e00526020610ee0600463c5f2892f610e8052610e9c6000305af161100f57600080fd5b610ee051610e60526060610f8060246380673289610f0052600254610f2052610f1c6000305af161103f57600080fd5b610fa0805160200180610fe0828460006004600a8704601201f161106257600080fd5b505060606110c06024638067328961104052610e00516110605261105c6000305af161108d57600080fd5b6110e0805160200180611120828460006004600a8704601201f16110b057600080fd5b5050610e60516111e05260606111a0526111a05161120052610fe08051602001806111a0516111e001828460006004600a8704601201f16110f057600080fd5b50506111a0516111e0015160206001820306601f82010390506111a0516111e00161118081516020818352015b836111805110151561112e5761114b565b6000611180516020850101535b815160010180835281141561111d575b5050505060206111a0516111e0015160206001820306601f82010390506111a05101016111a0526111a051611220526111208051602001806111a0516111e001828460006004600a8704601201f16111a257600080fd5b50506111a0516111e0015160206001820306601f82010390506111a0516111e00161118081516020818352015b83611180511015156111e0576111fd565b6000611180516020850101535b81516001018083528114156111cf575b5050505060206111a0516111e0015160206001820306601f82010390506111a05101016111a0527f08b71ef3f1b58f7a23ffb82e27f12f0888c8403f1ceb0ea7ea26b274e2189d4c6111a0516111e0a160016004555b5b005b63845980e8600051141561127c57341561126f57600080fd5b60045460005260206000f3005b60006000fd5b61018561140703610185600039610185611407036000f3"} \ No newline at end of file +{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}, {"type": "bytes", "name": "merkle_tree_index", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 7077}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 11026}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 255244}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600060c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600060c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600060c052602060c02001555b81516001018083528114156100aa575b505061103b56600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561026b57602060046101403734156100b457600080fd5b67ffffffffffffffff6101405111156100cc57600080fd5b60006101605261014051610180526101a060006008818352015b6101605160086000811215610103578060000360020a820461010a565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561013557600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8600081121561017e578060000360020a8204610185565b8060020a82025b90509050610180525b81516001018083528114156100e6575b505060186008602082066101e001602082840111156101bc57600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101f957600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b8261030051111561022b57610247565b6000610300516102c001535b815160010180835281141561021b575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103c357341561028457600080fd5b6000610140526002546101605261018060006020818352015b600160016101605116141561031e57600061018051602081106102bf57600080fd5b600160c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af161031057600080fd5b60c05190506101405261038c565b6000610140516020826101a0010152602081019050610180516020811061034457600080fd5b600060c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161038257600080fd5b60c0519050610140525b610160600261039a57600080fd5b60028151048152505b815160010180835281141561029d575b50506101405160005260206000f3005b63621fd13060005114156104995734156103dc57600080fd5b60606101c060246380673289610140526002546101605261015c6000305af161040457600080fd5b6101e0805160200180610260828460006004600a8704601201f161042757600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c051111561045957610475565b60006102c05161028001535b8151600101808352811415610449575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d6000511415610eb057606060046101403760506004356004016101a03760306004356004013511156104cf57600080fd5b60406024356004016102203760206024356004013511156104ef57600080fd5b608060443560040161028037606060443560040135111561050f57600080fd5b63ffffffff6002541061052157600080fd5b60306101a0511461053157600080fd5b6020610220511461054157600080fd5b6060610280511461055157600080fd5b633b9aca00610340526103405161056757600080fd5b61034051340461032052633b9aca0061032051101561058557600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af16105ae57600080fd5b610460805160200180610360828460006004600a8704601201f16105d157600080fd5b50506002546104a05260006104c0526104a05160016104a0510110156105f657600080fd5b60016104a051016104e05261050060006020818352015b600160016104e05116141561062157610674565b6104c060605160018251018060405190131561063c57600080fd5b809190121561064a57600080fd5b8152506104e0600261065b57600080fd5b60028151048152505b815160010180835281141561060d575b505060006101a06030806020846105e001018260208501600060046016f1505080518201915050600060106020820661056001602082840111156106b757600080fd5b60208061058082610520600060046015f15050818152809050905090506010806020846105e001018260208501600060046013f1505080518201915050806105e0526105e09050602060c0825160208401600060025af161071757600080fd5b60c051905061054052600060006040602082066106800161028051828401111561074057600080fd5b6060806106a0826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af161078057600080fd5b60c051905060208261088001015260208101905060006040602060208206610740016102805182840111156107b457600080fd5b606080610760826020602088068803016102800160006004601bf150508181528090509050905060208060208461080001018260208501600060046015f15050805182019150506105205160208261080001015260208101905080610800526108009050602060c0825160208401600060025af161083157600080fd5b60c051905060208261088001015260208101905080610880526108809050602060c0825160208401600060025af161086857600080fd5b60c051905061066052600060006105405160208261092001015260208101905061022060208060208461092001018260208501600060046015f150508051820191505080610920526109209050602060c0825160208401600060025af16108ce57600080fd5b60c0519050602082610aa00101526020810190506000610360600880602084610a2001018260208501600060046012f150508051820191505060006018602082066109a0016020828401111561092357600080fd5b6020806109c082610520600060046015f1505081815280905090509050601880602084610a2001018260208501600060046014f150508051820191505061066051602082610a2001015260208101905080610a2052610a209050602060c0825160208401600060025af161099657600080fd5b60c0519050602082610aa001015260208101905080610aa052610aa09050602060c0825160208401600060025af16109cd57600080fd5b60c051905061090052610b2060006020818352015b6104c051610b20511215610a62576000610b205160208110610a0357600080fd5b600160c052602060c0200154602082610b4001015260208101905061090051602082610b4001015260208101905080610b4052610b409050602060c0825160208401600060025af1610a5457600080fd5b60c051905061090052610a67565b610a78565b5b81516001018083528114156109e2575b5050610900516104c05160208110610a8f57600080fd5b600160c052602060c02001556002805460018254011015610aaf57600080fd5b60018154018155506060610c4060246380673289610bc0526104a051610be052610bdc6000305af1610ae057600080fd5b610c60805160200180610ca0828460006004600a8704601201f1610b0357600080fd5b505060a0610d2052610d2051610d60526101a0805160200180610d2051610d6001828460006004600a8704601201f1610b3b57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516040818352015b83610d0051101515610b7957610b96565b6000610d00516020850101535b8151600101808352811415610b68575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610d8052610220805160200180610d2051610d6001828460006004600a8704601201f1610bed57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610c2b57610c48565b6000610d00516020850101535b8151600101808352811415610c1a575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610da052610360805160200180610d2051610d6001828460006004600a8704601201f1610c9f57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610cdd57610cfa565b6000610d00516020850101535b8151600101808352811415610ccc575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610dc052610280805160200180610d2051610d6001828460006004600a8704601201f1610d5157600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516060818352015b83610d0051101515610d8f57610dac565b6000610d00516020850101535b8151600101808352811415610d7e575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610de052610ca0805160200180610d2051610d6001828460006004600a8704601201f1610e0357600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610e4157610e5e565b6000610d00516020850101535b8151600101808352811415610e30575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d20527fdc5fc95703516abd38fa03c3737ff3b52dc52347055c8028460fdf5bbe2f12ce610d2051610d60a1005b60006000fd5b61018561103b0361018560003961018561103b036000f3"} \ No newline at end of file diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index 1d475311a..c67ed2841 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -1,8 +1,5 @@ MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei -FULL_DEPOSIT_AMOUNT: constant(uint256) = 32000000000 # Gwei -CHAIN_START_FULL_DEPOSIT_THRESHOLD: constant(uint256) = 65536 # 2**16 DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32 -SECONDS_PER_DAY: constant(uint256) = 86400 MAX_64_BIT_VALUE: constant(uint256) = 18446744073709551615 # 2**64 - 1 PUBKEY_LENGTH: constant(uint256) = 48 # bytes WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes @@ -16,13 +13,10 @@ Deposit: event({ signature: bytes[96], merkle_tree_index: bytes[8], }) -Eth2Genesis: event({deposit_root: bytes32, deposit_count: bytes[8], time: bytes[8]}) zerohashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] deposit_count: uint256 -full_deposit_count: uint256 -chainStarted: public(bool) @public @@ -30,7 +24,6 @@ def __init__(): for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1): self.zerohashes[i+1] = sha256(concat(self.zerohashes[i], self.zerohashes[i])) - @public @constant def to_little_endian_64(value: uint256) -> bytes[8]: @@ -47,7 +40,6 @@ def to_little_endian_64(value: uint256) -> bytes[8]: return slice(convert(y, bytes32), start=24, len=8) - @public @constant def get_deposit_root() -> bytes32: @@ -124,17 +116,3 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], signature, self.to_little_endian_64(index), ) - - if deposit_amount >= FULL_DEPOSIT_AMOUNT: - self.full_deposit_count += 1 - if self.full_deposit_count == CHAIN_START_FULL_DEPOSIT_THRESHOLD: - timestamp_day_boundary: uint256 = ( - as_unitless_number(block.timestamp) - - as_unitless_number(block.timestamp) % SECONDS_PER_DAY + - 2 * SECONDS_PER_DAY - ) - new_deposit_root: bytes32 = self.get_deposit_root() - log.Eth2Genesis(new_deposit_root, - self.to_little_endian_64(self.deposit_count), - self.to_little_endian_64(timestamp_day_boundary)) - self.chainStarted = True diff --git a/deposit_contract/tests/contracts/conftest.py b/deposit_contract/tests/contracts/conftest.py index 69ece247d..b8b488abe 100644 --- a/deposit_contract/tests/contracts/conftest.py +++ b/deposit_contract/tests/contracts/conftest.py @@ -26,7 +26,6 @@ from .utils import ( # Constants MIN_DEPOSIT_AMOUNT = 1000000000 # Gwei FULL_DEPOSIT_AMOUNT = 32000000000 # Gwei -CHAIN_START_FULL_DEPOSIT_THRESHOLD = 65536 # 2**16 DEPOSIT_CONTRACT_TREE_DEPTH = 32 TWO_TO_POWER_OF_TREE_DEPTH = 2**DEPOSIT_CONTRACT_TREE_DEPTH @@ -63,28 +62,14 @@ def registration_contract(w3, tester): return registration_deployed -@pytest.fixture(scope="session") -def chain_start_full_deposit_thresholds(): - return [randint(1, 5), randint(6, 10), randint(11, 15)] - - @pytest.fixture(params=[0, 1, 2]) def modified_registration_contract( request, w3, - tester, - chain_start_full_deposit_thresholds): - # Set CHAIN_START_FULL_DEPOSIT_THRESHOLD to different threshold t + tester): registration_code = get_deposit_contract_code() - t = str(chain_start_full_deposit_thresholds[request.param]) - modified_registration_code = re.sub( - r'CHAIN_START_FULL_DEPOSIT_THRESHOLD: constant\(uint256\) = [0-9]+', - 'CHAIN_START_FULL_DEPOSIT_THRESHOLD: constant(uint256) = ' + t, - registration_code, - ) - assert modified_registration_code != registration_code - contract_bytecode = compiler.compile_code(modified_registration_code)['bytecode'] - contract_abi = compiler.mk_full_signature(modified_registration_code) + contract_bytecode = compiler.compile_code(registration_code)['bytecode'] + contract_abi = compiler.mk_full_signature(registration_code) registration = w3.eth.contract( abi=contract_abi, bytecode=contract_bytecode) @@ -94,11 +79,6 @@ def modified_registration_contract( address=tx_receipt.contractAddress, abi=contract_abi ) - setattr( - registration_deployed, - 'chain_start_full_deposit_threshold', - chain_start_full_deposit_thresholds[request.param] - ) return registration_deployed diff --git a/deposit_contract/tests/contracts/test_deposit.py b/deposit_contract/tests/contracts/test_deposit.py index 8492d6347..25c1a73bb 100644 --- a/deposit_contract/tests/contracts/test_deposit.py +++ b/deposit_contract/tests/contracts/test_deposit.py @@ -184,53 +184,3 @@ def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input leaf_nodes.append(hash_tree_root_result) root = compute_merkle_root(leaf_nodes) assert root == registration_contract.functions.get_deposit_root().call() - - -def test_chain_start(modified_registration_contract, w3, assert_tx_failed, deposit_input): - t = getattr(modified_registration_contract, 'chain_start_full_deposit_threshold') - # CHAIN_START_FULL_DEPOSIT_THRESHOLD is set to t - min_deposit_amount = MIN_DEPOSIT_AMOUNT * eth_utils.denoms.gwei # in wei - full_deposit_amount = FULL_DEPOSIT_AMOUNT * eth_utils.denoms.gwei - log_filter = modified_registration_contract.events.Eth2Genesis.createFilter( - fromBlock='latest', - ) - - index_not_full_deposit = randint(0, t - 1) - for i in range(t): - if i == index_not_full_deposit: - # Deposit with value below FULL_DEPOSIT_AMOUNT - modified_registration_contract.functions.deposit( - *deposit_input, - ).transact({"value": min_deposit_amount}) - logs = log_filter.get_new_entries() - # Eth2Genesis event should not be triggered - assert len(logs) == 0 - else: - # Deposit with value FULL_DEPOSIT_AMOUNT - modified_registration_contract.functions.deposit( - *deposit_input, - ).transact({"value": full_deposit_amount}) - logs = log_filter.get_new_entries() - # Eth2Genesis event should not be triggered - assert len(logs) == 0 - - # Make 1 more deposit with value FULL_DEPOSIT_AMOUNT to trigger Eth2Genesis event - modified_registration_contract.functions.deposit( - *deposit_input, - ).transact({"value": full_deposit_amount}) - logs = log_filter.get_new_entries() - assert len(logs) == 1 - timestamp = int(w3.eth.getBlock(w3.eth.blockNumber)['timestamp']) - timestamp_day_boundary = timestamp + (86400 - timestamp % 86400) + 86400 - log = logs[0]['args'] - assert log['deposit_root'] == modified_registration_contract.functions.get_deposit_root().call() - assert int.from_bytes(log['time'], byteorder='little') == timestamp_day_boundary - assert modified_registration_contract.functions.chainStarted().call() is True - - # Make 1 deposit with value FULL_DEPOSIT_AMOUNT and - # check that Eth2Genesis event is not triggered - modified_registration_contract.functions.deposit( - *deposit_input, - ).transact({"value": full_deposit_amount}) - logs = log_filter.get_new_entries() - assert len(logs) == 0 diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index a6d9d23c5..70e9e2b27 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -95,7 +95,7 @@ - [`initiate_validator_exit`](#initiate_validator_exit) - [`slash_validator`](#slash_validator) - [Genesis](#genesis) - - [`Eth2Genesis`](#eth2genesis) + - [Genesis trigger](#genesis-trigger) - [Genesis state](#genesis-state) - [Genesis block](#genesis-block) - [Beacon chain state transition function](#beacon-chain-state-transition-function) @@ -370,12 +370,12 @@ class PendingAttestation(Container): ```python class Eth1Data(Container): + # Block hash + block_hash: Bytes32 # Root of the deposit tree deposit_root: Bytes32 # Total number of deposits deposit_count: uint64 - # Block hash - block_hash: Bytes32 ``` #### `HistoricalBatch` @@ -1156,20 +1156,45 @@ def slash_validator(state: BeaconState, ## Genesis -### `Eth2Genesis` +### Genesis trigger -When enough deposits of size `MAX_EFFECTIVE_BALANCE` have been made to the deposit contract an `Eth2Genesis` log is emitted triggering the genesis of the beacon chain. Let: +Whenever the deposit contract emits a `Deposit` log call the function `is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool` where: -* `eth2genesis` be the object corresponding to `Eth2Genesis` -* `genesis_eth1_data` be object of type `Eth1Data` where - * `genesis_eth1_data.deposit_root = eth2genesis.deposit_root` - * `genesis_eth1_data.deposit_count = eth2genesis.deposit_count` - * `genesis_eth1_data.block_hash` is the hash of the Ethereum 1.0 block that emitted the `Eth2Genesis` log -* `genesis_deposits` be the object of type `List[Deposit]` with deposits ordered chronologically up to and including the deposit that triggered the `Eth2Genesis` log +* `deposits` is the list of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log +* `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log + +When `is_genesis_trigger(deposits, timestamp) == True` for the first time let: + +* `genesis_deposits = deposits` +* `genesis_time = timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY` where `SECONDS_PER_DAY = 86400` +* `genesis_eth1_data` be the object of type `Eth1Data` where: + * `genesis_eth1_data.block_hash` is the block hash corresponding to `deposits` + * `genesis_eth1_data.deposit_root` is the deposit root corresponding to `deposits` + * `genesis_eth1_data.deposit_count = len(genesis_deposits)` + +*Note*: The function `is_genesis_trigger` has yet to be agreed by the community, and can be updated as necessary. We define the following testing placeholder: + +```python +def is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool: + # Process deposits + state = BeaconState() + for deposit in deposits: + process_deposit(state, deposit) + + # Count active validators at genesis + active_validator_count = 0 + for validator in state.validator_registry: + if validator.effective_balance >= MAX_EFFECTIVE_BALANCE: + active_validator_count += 1 + + # Check effective balance to trigger genesis + GENESIS_EFFECTIVE_BALANCE = 2**21 + return active_validator_count * MAX_EFFECTIVE_BALANCE == GENESIS_EFFECTIVE_BALANCE +``` ### Genesis state -Let `genesis_state = get_genesis_beacon_state(genesis_deposits, eth2genesis.genesis_time, genesis_eth1_data)`. +Let `genesis_state = get_genesis_beacon_state(genesis_deposits, genesis_time, genesis_eth1_data)`. ```python def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis_eth1_data: Eth1Data) -> BeaconState: @@ -1246,7 +1271,7 @@ def process_slot(state: BeaconState) -> None: ### Epoch processing -Note: the `# @LabelHere` lines below are placeholders to show that code will be inserted here in a future phase. +*Note*: the `# @LabelHere` lines below are placeholders to show that code will be inserted here in a future phase. ```python def process_epoch(state: BeaconState) -> None: diff --git a/specs/core/0_deposit-contract.md b/specs/core/0_deposit-contract.md index 5d9398589..047e992c0 100644 --- a/specs/core/0_deposit-contract.md +++ b/specs/core/0_deposit-contract.md @@ -9,7 +9,6 @@ - [Table of contents](#table-of-contents) - [Introduction](#introduction) - [Constants](#constants) - - [Gwei values](#gwei-values) - [Contract](#contract) - [Ethereum 1.0 deposit contract](#ethereum-10-deposit-contract) - [Arguments](#arguments) @@ -17,7 +16,6 @@ - [Amount](#amount) - [Event logs](#event-logs) - [`Deposit` logs](#deposit-logs) - - [`Eth2Genesis` log](#eth2genesis-log) - [Vyper code](#vyper-code) @@ -28,19 +26,12 @@ This document represents the specification for the beacon chain deposit contract ## Constants -### Gwei values - -| Name | Value | Unit | -| - | - | - | -| `FULL_DEPOSIT_AMOUNT` | `32 * 10**9` | Gwei | - ### Contract | Name | Value | | - | - | | `DEPOSIT_CONTRACT_ADDRESS` | **TBD** | | `DEPOSIT_CONTRACT_TREE_DEPTH` | `2**5` (= 32) | -| `CHAIN_START_FULL_DEPOSIT_THRESHOLD` | `2**16` (= 65,536) | ## Ethereum 1.0 deposit contract @@ -62,7 +53,6 @@ The private key corresponding to `withdrawal_pubkey` will be required to initiat #### Amount * A valid deposit amount should be at least `MIN_DEPOSIT_AMOUNT` in Gwei. -* A deposit with an amount greater than or equal to `FULL_DEPOSIT_AMOUNT` in Gwei is considered as a full deposit. ## Event logs @@ -70,16 +60,6 @@ The private key corresponding to `withdrawal_pubkey` will be required to initiat Every Ethereum 1.0 deposit, of size at least `MIN_DEPOSIT_AMOUNT`, emits a `Deposit` log for consumption by the beacon chain. The deposit contract does little validation, pushing most of the validator onboarding logic to the beacon chain. In particular, the proof of possession (a BLS12-381 signature) is not verified by the deposit contract. -### `Eth2Genesis` log - -When `CHAIN_START_FULL_DEPOSIT_THRESHOLD` of full deposits have been made, the deposit contract emits the `Eth2Genesis` log. The beacon chain state may then be initialized by calling the `get_genesis_beacon_state` function (defined [here](./0_beacon-chain.md#genesis-state)) where: - -* `genesis_time` equals `time` in the `Eth2Genesis` log -* `latest_eth1_data.deposit_root` equals `deposit_root` in the `Eth2Genesis` log -* `latest_eth1_data.deposit_count` equals `deposit_count` in the `Eth2Genesis` log -* `latest_eth1_data.block_hash` equals the hash of the block that included the log -* `genesis_validator_deposits` is a list of `Deposit` objects built according to the `Deposit` logs up to the deposit that triggered the `Eth2Genesis` log, processed in the order in which they were emitted (oldest to newest) - ## Vyper code The source for the Vyper contract lives in a [separate repository](https://github.com/ethereum/deposit_contract) at [https://github.com/ethereum/deposit_contract/blob/master/deposit_contract/contracts/validator_registration.v.py](https://github.com/ethereum/deposit_contract/blob/master/deposit_contract/contracts/validator_registration.v.py). From 4ee00c9cbddd0872b2878897a1b56b43c0e56094 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sun, 9 Jun 2019 11:03:38 +0100 Subject: [PATCH 04/39] Address HW's comments --- .../contracts/validator_registration.v.py | 4 ++ specs/core/0_beacon-chain.md | 12 +++--- specs/core/0_deposit-contract.md | 37 +++++++------------ 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index c67ed2841..0411bdb78 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -24,6 +24,7 @@ def __init__(): for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1): self.zerohashes[i+1] = sha256(concat(self.zerohashes[i], self.zerohashes[i])) + @public @constant def to_little_endian_64(value: uint256) -> bytes[8]: @@ -40,6 +41,7 @@ def to_little_endian_64(value: uint256) -> bytes[8]: return slice(convert(y, bytes32), start=24, len=8) + @public @constant def get_deposit_root() -> bytes32: @@ -53,11 +55,13 @@ def get_deposit_root() -> bytes32: size /= 2 return root + @public @constant def get_deposit_count() -> bytes[8]: return self.to_little_endian_64(self.deposit_count) + @payable @public def deposit(pubkey: bytes[PUBKEY_LENGTH], diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 70e9e2b27..8cacde746 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1163,13 +1163,13 @@ Whenever the deposit contract emits a `Deposit` log call the function `is_genesi * `deposits` is the list of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log * `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log -When `is_genesis_trigger(deposits, timestamp) == True` for the first time let: +When `is_genesis_trigger(deposits, timestamp) is True` for the first time let: * `genesis_deposits = deposits` * `genesis_time = timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY` where `SECONDS_PER_DAY = 86400` * `genesis_eth1_data` be the object of type `Eth1Data` where: - * `genesis_eth1_data.block_hash` is the block hash corresponding to `deposits` - * `genesis_eth1_data.deposit_root` is the deposit root corresponding to `deposits` + * `genesis_eth1_data.block_hash` is the block hash for the last deposit in `deposits` + * `genesis_eth1_data.deposit_root` is the deposit root for the last deposit in `deposits` * `genesis_eth1_data.deposit_count = len(genesis_deposits)` *Note*: The function `is_genesis_trigger` has yet to be agreed by the community, and can be updated as necessary. We define the following testing placeholder: @@ -1184,12 +1184,12 @@ def is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool: # Count active validators at genesis active_validator_count = 0 for validator in state.validator_registry: - if validator.effective_balance >= MAX_EFFECTIVE_BALANCE: + if validator.effective_balance == MAX_EFFECTIVE_BALANCE: active_validator_count += 1 # Check effective balance to trigger genesis - GENESIS_EFFECTIVE_BALANCE = 2**21 - return active_validator_count * MAX_EFFECTIVE_BALANCE == GENESIS_EFFECTIVE_BALANCE + GENESIS_ACTIVE_VALIDATOR_COUNT = 2**16 + return active_validator_count == GENESIS_ACTIVE_VALIDATOR_COUNT ``` ### Genesis state diff --git a/specs/core/0_deposit-contract.md b/specs/core/0_deposit-contract.md index 047e992c0..50f52fd37 100644 --- a/specs/core/0_deposit-contract.md +++ b/specs/core/0_deposit-contract.md @@ -11,11 +11,10 @@ - [Constants](#constants) - [Contract](#contract) - [Ethereum 1.0 deposit contract](#ethereum-10-deposit-contract) - - [Arguments](#arguments) + - [`deposit` function](#deposit-function) + - [Deposit amount](#deposit-amount) - [Withdrawal credentials](#withdrawal-credentials) - - [Amount](#amount) - - [Event logs](#event-logs) - - [`Deposit` logs](#deposit-logs) + - [`Deposit` log](#deposit-log) - [Vyper code](#vyper-code) @@ -37,37 +36,29 @@ This document represents the specification for the beacon chain deposit contract The initial deployment phases of Ethereum 2.0 are implemented without consensus changes to Ethereum 1.0. A deposit contract at address `DEPOSIT_CONTRACT_ADDRESS` is added to Ethereum 1.0 for deposits of ETH to the beacon chain. Validator balances will be withdrawable to the shards in Phase 2 (i.e. when the EVM 2.0 is deployed and the shards have state). -### Arguments +### `deposit` function -The deposit contract has a `deposit` function which takes the amount in Ethereum 1.0 transaction, and arguments `pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96]` corresponding to `DepositData`. +The deposit contract has a public `deposit` function to make deposits. It takes as arguments `pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96]` corresponding to a `DepositData` object. + +#### Deposit amount + +The ETH sent to the deposit contract, i.e. the deposit amount, is burnt on Ethereum 1.0. The minimum deposit amount is `MIN_DEPOSIT_AMOUNT` Gwei. #### Withdrawal credentials -One of the `DepositData` fields is `withdrawal_credentials`. It is a commitment to credentials for withdrawals to shards. The first byte of `withdrawal_credentials` is a version number. As of now, the only expected format is as follows: +One of the `DepositData` fields is `withdrawal_credentials`. It is a commitment to credentials for withdrawing validator balance (e.g. to another validator, or to shards). The first byte of `withdrawal_credentials` is a version number. As of now, the only expected format is as follows: * `withdrawal_credentials[:1] == BLS_WITHDRAWAL_PREFIX_BYTE` * `withdrawal_credentials[1:] == hash(withdrawal_pubkey)[1:]` where `withdrawal_pubkey` is a BLS pubkey The private key corresponding to `withdrawal_pubkey` will be required to initiate a withdrawal. It can be stored separately until a withdrawal is required, e.g. in cold storage. -#### Amount +#### `Deposit` log -* A valid deposit amount should be at least `MIN_DEPOSIT_AMOUNT` in Gwei. - -## Event logs - -### `Deposit` logs - -Every Ethereum 1.0 deposit, of size at least `MIN_DEPOSIT_AMOUNT`, emits a `Deposit` log for consumption by the beacon chain. The deposit contract does little validation, pushing most of the validator onboarding logic to the beacon chain. In particular, the proof of possession (a BLS12-381 signature) is not verified by the deposit contract. +Every Ethereum 1.0 deposit emits a `Deposit` log for consumption by the beacon chain. The deposit contract does little validation, pushing most of the validator onboarding logic to the beacon chain. In particular, the proof of possession (a BLS12-381 signature) is not verified by the deposit contract. ## Vyper code -The source for the Vyper contract lives in a [separate repository](https://github.com/ethereum/deposit_contract) at [https://github.com/ethereum/deposit_contract/blob/master/deposit_contract/contracts/validator_registration.v.py](https://github.com/ethereum/deposit_contract/blob/master/deposit_contract/contracts/validator_registration.v.py). +The deposit contract source code, written in Vyper, is available [here](https://github.com/ethereum/eth2.0-specs/blob/dev/deposit_contract/contracts/validator_registration.v.py). -*Note*: To save ~10x on gas, this contract uses a somewhat unintuitive progressive Merkle root calculation algo that requires only O(log(n)) storage. See https://github.com/ethereum/research/blob/master/beacon_chain_impl/progressive_merkle_tree.py for an implementation of the same algo in Python tested for correctness. - -For convenience, we provide the interface to the contract here: - -* `__init__()`: initializes the contract -* `get_deposit_root() -> bytes32`: returns the current root of the deposit tree -* `deposit(pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96])`: adds a deposit instance to the deposit tree, incorporating the input arguments and the value transferred in the given call. *Note*: The amount of value transferred *must* be at least `MIN_DEPOSIT_AMOUNT`. Each of these constants are specified in units of Gwei. +*Note*: To save on gas the deposit contract uses a progressive Merkle root calculation algorithm that requires only O(log(n)) storage. See [here](https://github.com/ethereum/research/blob/master/beacon_chain_impl/progressive_merkle_tree.py) for a Python implementation, and [here](https://github.com/runtimeverification/verified-smart-contracts/blob/master/deposit/formal-incremental-merkle-tree-algorithm.pdf) for a formal correctness proof. From c293b9dcef4d25a3a0ae4ebfe2a0405811b0d86d Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sun, 9 Jun 2019 11:29:22 +0100 Subject: [PATCH 05/39] Cleanups --- .../contracts/validator_registration.json | 2 +- .../contracts/validator_registration.v.py | 91 +++++++------------ .../tests/contracts/test_deposit.py | 5 +- 3 files changed, 36 insertions(+), 62 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json index a885ef0a1..9da4a015c 100644 --- a/deposit_contract/contracts/validator_registration.json +++ b/deposit_contract/contracts/validator_registration.json @@ -1 +1 @@ -{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}, {"type": "bytes", "name": "merkle_tree_index", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 7077}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 11026}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 255244}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600060c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600060c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600060c052602060c02001555b81516001018083528114156100aa575b505061103b56600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561026b57602060046101403734156100b457600080fd5b67ffffffffffffffff6101405111156100cc57600080fd5b60006101605261014051610180526101a060006008818352015b6101605160086000811215610103578060000360020a820461010a565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561013557600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8600081121561017e578060000360020a8204610185565b8060020a82025b90509050610180525b81516001018083528114156100e6575b505060186008602082066101e001602082840111156101bc57600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101f957600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b8261030051111561022b57610247565b6000610300516102c001535b815160010180835281141561021b575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103c357341561028457600080fd5b6000610140526002546101605261018060006020818352015b600160016101605116141561031e57600061018051602081106102bf57600080fd5b600160c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af161031057600080fd5b60c05190506101405261038c565b6000610140516020826101a0010152602081019050610180516020811061034457600080fd5b600060c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161038257600080fd5b60c0519050610140525b610160600261039a57600080fd5b60028151048152505b815160010180835281141561029d575b50506101405160005260206000f3005b63621fd13060005114156104995734156103dc57600080fd5b60606101c060246380673289610140526002546101605261015c6000305af161040457600080fd5b6101e0805160200180610260828460006004600a8704601201f161042757600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c051111561045957610475565b60006102c05161028001535b8151600101808352811415610449575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d6000511415610eb057606060046101403760506004356004016101a03760306004356004013511156104cf57600080fd5b60406024356004016102203760206024356004013511156104ef57600080fd5b608060443560040161028037606060443560040135111561050f57600080fd5b63ffffffff6002541061052157600080fd5b60306101a0511461053157600080fd5b6020610220511461054157600080fd5b6060610280511461055157600080fd5b633b9aca00610340526103405161056757600080fd5b61034051340461032052633b9aca0061032051101561058557600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af16105ae57600080fd5b610460805160200180610360828460006004600a8704601201f16105d157600080fd5b50506002546104a05260006104c0526104a05160016104a0510110156105f657600080fd5b60016104a051016104e05261050060006020818352015b600160016104e05116141561062157610674565b6104c060605160018251018060405190131561063c57600080fd5b809190121561064a57600080fd5b8152506104e0600261065b57600080fd5b60028151048152505b815160010180835281141561060d575b505060006101a06030806020846105e001018260208501600060046016f1505080518201915050600060106020820661056001602082840111156106b757600080fd5b60208061058082610520600060046015f15050818152809050905090506010806020846105e001018260208501600060046013f1505080518201915050806105e0526105e09050602060c0825160208401600060025af161071757600080fd5b60c051905061054052600060006040602082066106800161028051828401111561074057600080fd5b6060806106a0826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af161078057600080fd5b60c051905060208261088001015260208101905060006040602060208206610740016102805182840111156107b457600080fd5b606080610760826020602088068803016102800160006004601bf150508181528090509050905060208060208461080001018260208501600060046015f15050805182019150506105205160208261080001015260208101905080610800526108009050602060c0825160208401600060025af161083157600080fd5b60c051905060208261088001015260208101905080610880526108809050602060c0825160208401600060025af161086857600080fd5b60c051905061066052600060006105405160208261092001015260208101905061022060208060208461092001018260208501600060046015f150508051820191505080610920526109209050602060c0825160208401600060025af16108ce57600080fd5b60c0519050602082610aa00101526020810190506000610360600880602084610a2001018260208501600060046012f150508051820191505060006018602082066109a0016020828401111561092357600080fd5b6020806109c082610520600060046015f1505081815280905090509050601880602084610a2001018260208501600060046014f150508051820191505061066051602082610a2001015260208101905080610a2052610a209050602060c0825160208401600060025af161099657600080fd5b60c0519050602082610aa001015260208101905080610aa052610aa09050602060c0825160208401600060025af16109cd57600080fd5b60c051905061090052610b2060006020818352015b6104c051610b20511215610a62576000610b205160208110610a0357600080fd5b600160c052602060c0200154602082610b4001015260208101905061090051602082610b4001015260208101905080610b4052610b409050602060c0825160208401600060025af1610a5457600080fd5b60c051905061090052610a67565b610a78565b5b81516001018083528114156109e2575b5050610900516104c05160208110610a8f57600080fd5b600160c052602060c02001556002805460018254011015610aaf57600080fd5b60018154018155506060610c4060246380673289610bc0526104a051610be052610bdc6000305af1610ae057600080fd5b610c60805160200180610ca0828460006004600a8704601201f1610b0357600080fd5b505060a0610d2052610d2051610d60526101a0805160200180610d2051610d6001828460006004600a8704601201f1610b3b57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516040818352015b83610d0051101515610b7957610b96565b6000610d00516020850101535b8151600101808352811415610b68575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610d8052610220805160200180610d2051610d6001828460006004600a8704601201f1610bed57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610c2b57610c48565b6000610d00516020850101535b8151600101808352811415610c1a575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610da052610360805160200180610d2051610d6001828460006004600a8704601201f1610c9f57600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610cdd57610cfa565b6000610d00516020850101535b8151600101808352811415610ccc575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610dc052610280805160200180610d2051610d6001828460006004600a8704601201f1610d5157600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516060818352015b83610d0051101515610d8f57610dac565b6000610d00516020850101535b8151600101808352811415610d7e575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d2052610d2051610de052610ca0805160200180610d2051610d6001828460006004600a8704601201f1610e0357600080fd5b5050610d2051610d60015160206001820306601f8201039050610d2051610d6001610d0081516020818352015b83610d0051101515610e4157610e5e565b6000610d00516020850101535b8151600101808352811415610e30575b505050506020610d2051610d60015160206001820306601f8201039050610d20510101610d20527fdc5fc95703516abd38fa03c3737ff3b52dc52347055c8028460fdf5bbe2f12ce610d2051610d60a1005b60006000fd5b61018561103b0361018560003961018561103b036000f3"} \ No newline at end of file +{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 6973}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10922}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1318334}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b5050610e9956600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561025357602060046101403734156100b457600080fd5b60006101605261014051610180526101a060006008818352015b61016051600860008112156100eb578060000360020a82046100f2565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561011d57600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610166578060000360020a820461016d565b8060020a82025b90509050610180525b81516001018083528114156100ce575b505060186008602082066101e001602082840111156101a457600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101e157600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b826103005111156102135761022f565b6000610300516102c001535b8151600101808352811415610203575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103ab57341561026c57600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561030657600061018051602081106102a757600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af16102f857600080fd5b60c051905061014052610374565b6000610140516020826101a0010152602081019050610180516020811061032c57600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161036a57600080fd5b60c0519050610140525b610160600261038257600080fd5b60028151048152505b8151600101808352811415610285575b50506101405160005260206000f3005b63621fd13060005114156104815734156103c457600080fd5b60606101c060246380673289610140526001546101605261015c6000305af16103ec57600080fd5b6101e0805160200180610260828460006004600a8704601201f161040f57600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c05111156104415761045d565b60006102c05161028001535b8151600101808352811415610431575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d6000511415610d0e57606060046101403760506004356004016101a03760306004356004013511156104b757600080fd5b60406024356004016102203760206024356004013511156104d757600080fd5b60806044356004016102803760606044356004013511156104f757600080fd5b63ffffffff6001541061050957600080fd5b633b9aca00610340526103405161051f57600080fd5b61034051340461032052633b9aca0061032051101561053d57600080fd5b60306101a0511461054d57600080fd5b6020610220511461055d57600080fd5b6060610280511461056d57600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af161059657600080fd5b610460805160200180610360828460006004600a8704601201f16105b957600080fd5b505060806104c0526104c051610500526101a08051602001806104c05161050001828460006004600a8704601201f16105f157600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516040818352015b836104a05110151561062f5761064c565b60006104a0516020850101535b815160010180835281141561061e575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0526104c051610520526102208051602001806104c05161050001828460006004600a8704601201f16106a357600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516020818352015b836104a0511015156106e1576106fe565b60006104a0516020850101535b81516001018083528114156106d0575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0526104c051610540526103608051602001806104c05161050001828460006004600a8704601201f161075557600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516020818352015b836104a051101515610793576107b0565b60006104a0516020850101535b8151600101808352811415610782575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0526104c051610560526102808051602001806104c05161050001828460006004600a8704601201f161080757600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516060818352015b836104a05110151561084557610862565b60006104a0516020850101535b8151600101808352811415610834575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0527f9d0a206c338cfcaf9198c04fe61b39a988e26b623ef97cb2f72bafcfbf8bb93e6104c051610500a160006101a060308060208461064001018260208501600060046016f150508051820191505060006010602082066105c001602082840111156108f357600080fd5b6020806105e082610580600060046015f150508181528090509050905060108060208461064001018260208501600060046013f150508051820191505080610640526106409050602060c0825160208401600060025af161095357600080fd5b60c05190506105a052600060006040602082066106e00161028051828401111561097c57600080fd5b606080610700826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af16109bc57600080fd5b60c05190506020826108e0010152602081019050600060406020602082066107a0016102805182840111156109f057600080fd5b6060806107c0826020602088068803016102800160006004601bf150508181528090509050905060208060208461086001018260208501600060046015f15050805182019150506105805160208261086001015260208101905080610860526108609050602060c0825160208401600060025af1610a6d57600080fd5b60c05190506020826108e0010152602081019050806108e0526108e09050602060c0825160208401600060025af1610aa457600080fd5b60c05190506106c052600060006105a05160208261098001015260208101905061022060208060208461098001018260208501600060046015f150508051820191505080610980526109809050602060c0825160208401600060025af1610b0a57600080fd5b60c0519050602082610b000101526020810190506000610360600880602084610a8001018260208501600060046012f15050805182019150506000601860208206610a000160208284011115610b5f57600080fd5b602080610a2082610580600060046015f1505081815280905090509050601880602084610a8001018260208501600060046014f15050805182019150506106c051602082610a8001015260208101905080610a8052610a809050602060c0825160208401600060025af1610bd257600080fd5b60c0519050602082610b0001015260208101905080610b0052610b009050602060c0825160208401600060025af1610c0957600080fd5b60c0519050610960526001805460018254011015610c2657600080fd5b6001815401815550600154610b8052610ba060006020818352015b60016001610b8051161415610c765761096051610ba05160208110610c6557600080fd5b600060c052602060c0200155610d0a565b6000610ba05160208110610c8957600080fd5b600060c052602060c0200154602082610bc001015260208101905061096051602082610bc001015260208101905080610bc052610bc09050602060c0825160208401600060025af1610cda57600080fd5b60c051905061096052610b806002610cf157600080fd5b60028151048152505b8151600101808352811415610c41575b5050005b60006000fd5b610185610e9903610185600039610185610e99036000f3"} \ No newline at end of file diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index 0411bdb78..6c4bd7dc7 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -1,59 +1,54 @@ MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32 -MAX_64_BIT_VALUE: constant(uint256) = 18446744073709551615 # 2**64 - 1 PUBKEY_LENGTH: constant(uint256) = 48 # bytes WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes +AMOUNT_LENGTH: constant(uint256) = 8 # bytes SIGNATURE_LENGTH: constant(uint256) = 96 # bytes -MAX_DEPOSIT_COUNT: constant(uint256) = 4294967295 # 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1 Deposit: event({ pubkey: bytes[48], withdrawal_credentials: bytes[32], amount: bytes[8], signature: bytes[96], - merkle_tree_index: bytes[8], }) -zerohashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] deposit_count: uint256 - +# Compute hashes in empty sparse Merkle tree +zero_hashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] @public def __init__(): for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1): - self.zerohashes[i+1] = sha256(concat(self.zerohashes[i], self.zerohashes[i])) + self.zero_hashes[i + 1] = sha256(concat(self.zero_hashes[i], self.zero_hashes[i])) @public @constant def to_little_endian_64(value: uint256) -> bytes[8]: - assert value <= MAX_64_BIT_VALUE - - # array access for bytes[] not currently supported in vyper so - # reversing bytes using bitwise uint256 manipulations + # Reversing bytes using bitwise uint256 manipulations + # (array accesses of bytes[] are not currently supported in Vyper) y: uint256 = 0 x: uint256 = value - for i in range(8): + for _ in range(8): y = shift(y, 8) y = y + bitwise_and(x, 255) x = shift(x, -8) - return slice(convert(y, bytes32), start=24, len=8) @public @constant def get_deposit_root() -> bytes32: - root: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 + node: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 size: uint256 = self.deposit_count - for h in range(DEPOSIT_CONTRACT_TREE_DEPTH): - if bitwise_and(size, 1) == 1: - root = sha256(concat(self.branch[h], root)) + for height in range(DEPOSIT_CONTRACT_TREE_DEPTH): + if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1` + node = sha256(concat(self.branch[height], node)) else: - root = sha256(concat(root, self.zerohashes[h])) + node = sha256(concat(node, self.zero_hashes[height])) size /= 2 - return root + return node @public @@ -67,56 +62,38 @@ def get_deposit_count() -> bytes[8]: def deposit(pubkey: bytes[PUBKEY_LENGTH], withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH], signature: bytes[SIGNATURE_LENGTH]): - # Prevent edge case in computing `self.branch` when `self.deposit_count == MAX_DEPOSIT_COUNT` - # NOTE: reaching this point with the constants as currently defined is impossible due to the - # uni-directional nature of transfers from eth1 to eth2 and the total ether supply (< 130M). - assert self.deposit_count < MAX_DEPOSIT_COUNT + # Avoid overflowing the Merkle tree + assert self.deposit_count < 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1 + # Validate deposit data + deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") + assert deposit_amount >= MIN_DEPOSIT_AMOUNT assert len(pubkey) == PUBKEY_LENGTH assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH assert len(signature) == SIGNATURE_LENGTH - deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") - assert deposit_amount >= MIN_DEPOSIT_AMOUNT + # Emit `Deposit` log amount: bytes[8] = self.to_little_endian_64(deposit_amount) + log.Deposit(pubkey, withdrawal_credentials, amount, signature) - index: uint256 = self.deposit_count - - # add deposit to merkle tree - i: int128 = 0 - size: uint256 = index + 1 - for _ in range(DEPOSIT_CONTRACT_TREE_DEPTH): - if bitwise_and(size, 1) == 1: - break - i += 1 - size /= 2 - - zero_bytes_32: bytes32 - pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes_32, start=0, len=16))) + # Compute `DepositData` root + zero_bytes32: bytes32 + pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH))) signature_root: bytes32 = sha256(concat( sha256(slice(signature, start=0, len=64)), - sha256(concat(slice(signature, start=64, len=32), zero_bytes_32)) + sha256(concat(slice(signature, start=64, len=SIGNATURE_LENGTH - 64), zero_bytes32)), )) - value: bytes32 = sha256(concat( + node: bytes32 = sha256(concat( sha256(concat(pubkey_root, withdrawal_credentials)), - sha256(concat( - amount, - slice(zero_bytes_32, start=0, len=24), - signature_root, - )) + sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)), )) - for j in range(DEPOSIT_CONTRACT_TREE_DEPTH): - if j < i: - value = sha256(concat(self.branch[j], value)) - else: - break - self.branch[i] = value + # Add `DepositData` root to Merkle tree (update a single `branch` node) self.deposit_count += 1 - log.Deposit( - pubkey, - withdrawal_credentials, - amount, - signature, - self.to_little_endian_64(index), - ) + size: uint256 = self.deposit_count + for height in range(DEPOSIT_CONTRACT_TREE_DEPTH): + if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1` + self.branch[height] = node + break + node = sha256(concat(self.branch[height], node)) + size /= 2 diff --git a/deposit_contract/tests/contracts/test_deposit.py b/deposit_contract/tests/contracts/test_deposit.py index 25c1a73bb..c1c07aac9 100644 --- a/deposit_contract/tests/contracts/test_deposit.py +++ b/deposit_contract/tests/contracts/test_deposit.py @@ -56,7 +56,7 @@ def deposit_input(): (10, True), (55555, True), (2**64 - 1, True), - (2**64, False), + (2**64, True), # Note that all calls to `to_little_endian_64` have an input less than 2**64 ] ) def test_to_little_endian_64(registration_contract, value, success, assert_tx_failed): @@ -151,7 +151,6 @@ def test_deposit_log(registration_contract, a0, w3, deposit_input): assert log['withdrawal_credentials'] == deposit_input[1] assert log['amount'] == deposit_amount_list[i].to_bytes(8, 'little') assert log['signature'] == deposit_input[2] - assert log['merkle_tree_index'] == i.to_bytes(8, 'little') def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input): @@ -172,8 +171,6 @@ def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input assert len(logs) == 1 log = logs[0]['args'] - assert log["merkle_tree_index"] == i.to_bytes(8, 'little') - deposit_data = DepositData( pubkey=deposit_input[0], withdrawal_credentials=deposit_input[1], From 29129d06cf762889312ed6eeeec783751d76cda7 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sun, 9 Jun 2019 17:03:35 +0100 Subject: [PATCH 06/39] Fix tests --- deposit_contract/contracts/validator_registration.json | 2 +- deposit_contract/contracts/validator_registration.v.py | 8 ++++---- deposit_contract/tests/contracts/test_deposit.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json index 9da4a015c..676bf470a 100644 --- a/deposit_contract/contracts/validator_registration.json +++ b/deposit_contract/contracts/validator_registration.json @@ -1 +1 @@ -{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 6973}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10922}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1318334}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b5050610e9956600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561025357602060046101403734156100b457600080fd5b60006101605261014051610180526101a060006008818352015b61016051600860008112156100eb578060000360020a82046100f2565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561011d57600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610166578060000360020a820461016d565b8060020a82025b90509050610180525b81516001018083528114156100ce575b505060186008602082066101e001602082840111156101a457600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101e157600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b826103005111156102135761022f565b6000610300516102c001535b8151600101808352811415610203575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103ab57341561026c57600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561030657600061018051602081106102a757600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af16102f857600080fd5b60c051905061014052610374565b6000610140516020826101a0010152602081019050610180516020811061032c57600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161036a57600080fd5b60c0519050610140525b610160600261038257600080fd5b60028151048152505b8151600101808352811415610285575b50506101405160005260206000f3005b63621fd13060005114156104815734156103c457600080fd5b60606101c060246380673289610140526001546101605261015c6000305af16103ec57600080fd5b6101e0805160200180610260828460006004600a8704601201f161040f57600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c05111156104415761045d565b60006102c05161028001535b8151600101808352811415610431575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d6000511415610d0e57606060046101403760506004356004016101a03760306004356004013511156104b757600080fd5b60406024356004016102203760206024356004013511156104d757600080fd5b60806044356004016102803760606044356004013511156104f757600080fd5b63ffffffff6001541061050957600080fd5b633b9aca00610340526103405161051f57600080fd5b61034051340461032052633b9aca0061032051101561053d57600080fd5b60306101a0511461054d57600080fd5b6020610220511461055d57600080fd5b6060610280511461056d57600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af161059657600080fd5b610460805160200180610360828460006004600a8704601201f16105b957600080fd5b505060806104c0526104c051610500526101a08051602001806104c05161050001828460006004600a8704601201f16105f157600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516040818352015b836104a05110151561062f5761064c565b60006104a0516020850101535b815160010180835281141561061e575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0526104c051610520526102208051602001806104c05161050001828460006004600a8704601201f16106a357600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516020818352015b836104a0511015156106e1576106fe565b60006104a0516020850101535b81516001018083528114156106d0575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0526104c051610540526103608051602001806104c05161050001828460006004600a8704601201f161075557600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516020818352015b836104a051101515610793576107b0565b60006104a0516020850101535b8151600101808352811415610782575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0526104c051610560526102808051602001806104c05161050001828460006004600a8704601201f161080757600080fd5b50506104c051610500015160206001820306601f82010390506104c051610500016104a081516060818352015b836104a05110151561084557610862565b60006104a0516020850101535b8151600101808352811415610834575b5050505060206104c051610500015160206001820306601f82010390506104c05101016104c0527f9d0a206c338cfcaf9198c04fe61b39a988e26b623ef97cb2f72bafcfbf8bb93e6104c051610500a160006101a060308060208461064001018260208501600060046016f150508051820191505060006010602082066105c001602082840111156108f357600080fd5b6020806105e082610580600060046015f150508181528090509050905060108060208461064001018260208501600060046013f150508051820191505080610640526106409050602060c0825160208401600060025af161095357600080fd5b60c05190506105a052600060006040602082066106e00161028051828401111561097c57600080fd5b606080610700826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af16109bc57600080fd5b60c05190506020826108e0010152602081019050600060406020602082066107a0016102805182840111156109f057600080fd5b6060806107c0826020602088068803016102800160006004601bf150508181528090509050905060208060208461086001018260208501600060046015f15050805182019150506105805160208261086001015260208101905080610860526108609050602060c0825160208401600060025af1610a6d57600080fd5b60c05190506020826108e0010152602081019050806108e0526108e09050602060c0825160208401600060025af1610aa457600080fd5b60c05190506106c052600060006105a05160208261098001015260208101905061022060208060208461098001018260208501600060046015f150508051820191505080610980526109809050602060c0825160208401600060025af1610b0a57600080fd5b60c0519050602082610b000101526020810190506000610360600880602084610a8001018260208501600060046012f15050805182019150506000601860208206610a000160208284011115610b5f57600080fd5b602080610a2082610580600060046015f1505081815280905090509050601880602084610a8001018260208501600060046014f15050805182019150506106c051602082610a8001015260208101905080610a8052610a809050602060c0825160208401600060025af1610bd257600080fd5b60c0519050602082610b0001015260208101905080610b0052610b009050602060c0825160208401600060025af1610c0957600080fd5b60c0519050610960526001805460018254011015610c2657600080fd5b6001815401815550600154610b8052610ba060006020818352015b60016001610b8051161415610c765761096051610ba05160208110610c6557600080fd5b600060c052602060c0200155610d0a565b6000610ba05160208110610c8957600080fd5b600060c052602060c0200154602082610bc001015260208101905061096051602082610bc001015260208101905080610bc052610bc09050602060c0825160208401600060025af1610cda57600080fd5b60c051905061096052610b806002610cf157600080fd5b60028151048152505b8151600101808352811415610c41575b5050005b60006000fd5b610185610e9903610185600039610185610e99036000f3"} \ No newline at end of file +{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 6973}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10922}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1318334}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b5050610e9956600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561025357602060046101403734156100b457600080fd5b60006101605261014051610180526101a060006008818352015b61016051600860008112156100eb578060000360020a82046100f2565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561011d57600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610166578060000360020a820461016d565b8060020a82025b90509050610180525b81516001018083528114156100ce575b505060186008602082066101e001602082840111156101a457600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101e157600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b826103005111156102135761022f565b6000610300516102c001535b8151600101808352811415610203575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103ab57341561026c57600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561030657600061018051602081106102a757600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af16102f857600080fd5b60c051905061014052610374565b6000610140516020826101a0010152602081019050610180516020811061032c57600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161036a57600080fd5b60c0519050610140525b610160600261038257600080fd5b60028151048152505b8151600101808352811415610285575b50506101405160005260206000f3005b63621fd13060005114156104815734156103c457600080fd5b60606101c060246380673289610140526001546101605261015c6000305af16103ec57600080fd5b6101e0805160200180610260828460006004600a8704601201f161040f57600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c05111156104415761045d565b60006102c05161028001535b8151600101808352811415610431575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d6000511415610d0e57606060046101403760506004356004016101a03760306004356004013511156104b757600080fd5b60406024356004016102203760206024356004013511156104d757600080fd5b60806044356004016102803760606044356004013511156104f757600080fd5b63ffffffff6001541061050957600080fd5b633b9aca00610340526103405161051f57600080fd5b61034051340461032052633b9aca0061032051101561053d57600080fd5b60306101a0511461054d57600080fd5b6020610220511461055d57600080fd5b6060610280511461056d57600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af161059657600080fd5b610460805160200180610360828460006004600a8704601201f16105b957600080fd5b505060006101a060308060208461056001018260208501600060046016f150508051820191505060006010602082066104e001602082840111156105fc57600080fd5b602080610500826104a0600060046015f150508181528090509050905060108060208461056001018260208501600060046013f150508051820191505080610560526105609050602060c0825160208401600060025af161065c57600080fd5b60c05190506104c052600060006040602082066106000161028051828401111561068557600080fd5b606080610620826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af16106c557600080fd5b60c0519050602082610800010152602081019050600060406020602082066106c0016102805182840111156106f957600080fd5b6060806106e0826020602088068803016102800160006004601bf150508181528090509050905060208060208461078001018260208501600060046015f15050805182019150506104a05160208261078001015260208101905080610780526107809050602060c0825160208401600060025af161077657600080fd5b60c051905060208261080001015260208101905080610800526108009050602060c0825160208401600060025af16107ad57600080fd5b60c05190506105e052600060006104c0516020826108a00101526020810190506102206020806020846108a001018260208501600060046015f1505080518201915050806108a0526108a09050602060c0825160208401600060025af161081357600080fd5b60c0519050602082610a2001015260208101905060006103606008806020846109a001018260208501600060046012f15050805182019150506000601860208206610920016020828401111561086857600080fd5b602080610940826104a0600060046015f15050818152809050905090506018806020846109a001018260208501600060046014f15050805182019150506105e0516020826109a0010152602081019050806109a0526109a09050602060c0825160208401600060025af16108db57600080fd5b60c0519050602082610a2001015260208101905080610a2052610a209050602060c0825160208401600060025af161091257600080fd5b60c051905061088052600180546001825401101561092f57600080fd5b6001815401815550600154610aa052610ac060006020818352015b60016001610aa05116141561097f5761088051610ac0516020811061096e57600080fd5b600060c052602060c0200155610a13565b6000610ac0516020811061099257600080fd5b600060c052602060c0200154602082610ae001015260208101905061088051602082610ae001015260208101905080610ae052610ae09050602060c0825160208401600060025af16109e357600080fd5b60c051905061088052610aa060026109fa57600080fd5b60028151048152505b815160010180835281141561094a575b50506080610b8052610b8051610bc0526101a0805160200180610b8051610bc001828460006004600a8704601201f1610a4b57600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516040818352015b83610b6051101515610a8957610aa6565b6000610b60516020850101535b8151600101808352811415610a78575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b8052610b8051610be052610220805160200180610b8051610bc001828460006004600a8704601201f1610afd57600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516020818352015b83610b6051101515610b3b57610b58565b6000610b60516020850101535b8151600101808352811415610b2a575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b8052610b8051610c0052610360805160200180610b8051610bc001828460006004600a8704601201f1610baf57600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516020818352015b83610b6051101515610bed57610c0a565b6000610b60516020850101535b8151600101808352811415610bdc575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b8052610b8051610c2052610280805160200180610b8051610bc001828460006004600a8704601201f1610c6157600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516060818352015b83610b6051101515610c9f57610cbc565b6000610b60516020850101535b8151600101808352811415610c8e575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b80527f9d0a206c338cfcaf9198c04fe61b39a988e26b623ef97cb2f72bafcfbf8bb93e610b8051610bc0a1005b60006000fd5b610185610e9903610185600039610185610e99036000f3"} \ No newline at end of file diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index 6c4bd7dc7..b99a30784 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -72,11 +72,8 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH assert len(signature) == SIGNATURE_LENGTH - # Emit `Deposit` log - amount: bytes[8] = self.to_little_endian_64(deposit_amount) - log.Deposit(pubkey, withdrawal_credentials, amount, signature) - # Compute `DepositData` root + amount: bytes[8] = self.to_little_endian_64(deposit_amount) zero_bytes32: bytes32 pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH))) signature_root: bytes32 = sha256(concat( @@ -97,3 +94,6 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], break node = sha256(concat(self.branch[height], node)) size /= 2 + + # Emit `Deposit` log + log.Deposit(pubkey, withdrawal_credentials, amount, signature) diff --git a/deposit_contract/tests/contracts/test_deposit.py b/deposit_contract/tests/contracts/test_deposit.py index c1c07aac9..12f3e7f8b 100644 --- a/deposit_contract/tests/contracts/test_deposit.py +++ b/deposit_contract/tests/contracts/test_deposit.py @@ -56,7 +56,7 @@ def deposit_input(): (10, True), (55555, True), (2**64 - 1, True), - (2**64, True), # Note that all calls to `to_little_endian_64` have an input less than 2**64 + # (2**64, True), # Note that all calls to `to_little_endian_64` have an input less than 2**64 ] ) def test_to_little_endian_64(registration_contract, value, success, assert_tx_failed): From 8b54c90fd6c6111bf6c027b2009e89d99c8a6d83 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 9 Jun 2019 12:09:54 -0700 Subject: [PATCH 07/39] Update 0_beacon-chain-validator.md --- specs/validator/0_beacon-chain-validator.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specs/validator/0_beacon-chain-validator.md b/specs/validator/0_beacon-chain-validator.md index 2f5aa4264..e7a3a1a9d 100644 --- a/specs/validator/0_beacon-chain-validator.md +++ b/specs/validator/0_beacon-chain-validator.md @@ -297,11 +297,11 @@ Set `attestation_data.beacon_block_root = signing_root(head_block)`. * Set `attestation_data.source_epoch = head_state.current_justified_epoch`. * Set `attestation_data.source_root = head_state.current_justified_root`. * Set `attestation_data.target_epoch = get_current_epoch(head_state)` -* Set `attestation_data.target_root = signing_root(epoch_boundary_block)` where `epoch_boundary_block` is the block at the most recent epoch boundary. +* Set `attestation_data.target_root = epoch_boundary_block_root` where `epoch_boundary_block_root` is the root of block at the most recent epoch boundary. -*Note*: `epoch_boundary_block` can be looked up in the state using: +*Note*: `epoch_boundary_block_root` can be looked up in the state using: * Let `epoch_start_slot = get_epoch_start_slot(get_current_epoch(head_state))`. -* Let `epoch_boundary_block = head if epoch_start_slot == head_state.slot else get_block_root(state, epoch_start_slot)`. +* Let `epoch_boundary_block_root = signing_root(head_block) if epoch_start_slot == head_state.slot else get_block_root(state, epoch_start_slot)`. ##### Crosslink vote From 565f61dfaa520708cc83a0a593e8eb2df9e5d516 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sun, 9 Jun 2019 20:41:21 +0100 Subject: [PATCH 08/39] Cleanup containers --- configs/constant_presets/mainnet.yaml | 6 +- configs/constant_presets/minimal.yaml | 6 +- scripts/build_spec.py | 11 +- specs/core/0_beacon-chain.md | 756 ++++++++---------- specs/core/0_deposit-contract.md | 6 +- specs/core/0_fork-choice.md | 12 +- specs/core/1_custody-game.md | 20 +- specs/core/1_shard-data-chains.md | 12 +- specs/light_client/sync_protocol.md | 8 +- specs/networking/rpc-interface.md | 8 +- specs/validator/0_beacon-chain-validator.md | 16 +- .../pyspec/eth2spec/test/helpers/block.py | 4 +- .../pyspec/eth2spec/test/helpers/deposits.py | 6 +- .../pyspec/eth2spec/test/helpers/genesis.py | 12 +- .../test/helpers/proposer_slashings.py | 2 +- .../pyspec/eth2spec/test/helpers/state.py | 2 +- .../pyspec/eth2spec/test/helpers/transfers.py | 2 +- .../test_process_attestation.py | 8 +- .../test_process_attester_slashing.py | 4 +- .../test_process_block_header.py | 2 +- .../block_processing/test_process_deposit.py | 28 +- .../test_process_proposer_slashing.py | 10 +- .../block_processing/test_process_transfer.py | 20 +- .../test_process_voluntary_exit.py | 34 +- .../test_process_crosslinks.py | 8 +- .../test_process_registry_updates.py | 26 +- ...est_process_early_derived_secret_reveal.py | 4 +- .../eth2spec/test/sanity/test_blocks.py | 46 +- 28 files changed, 515 insertions(+), 564 deletions(-) diff --git a/configs/constant_presets/mainnet.yaml b/configs/constant_presets/mainnet.yaml index 6ac3f422f..d72b276c1 100644 --- a/configs/constant_presets/mainnet.yaml +++ b/configs/constant_presets/mainnet.yaml @@ -80,11 +80,11 @@ MIN_EPOCHS_TO_INACTIVITY_PENALTY: 4 # State list lengths # --------------------------------------------------------------- # 2**13 (= 8,192) epochs ~36 days -LATEST_RANDAO_MIXES_LENGTH: 8192 +RANDAO_MIXES_LENGTH: 8192 # 2**13 (= 8,192) epochs ~36 days -LATEST_ACTIVE_INDEX_ROOTS_LENGTH: 8192 +ACTIVE_INDEX_ROOTS_LENGTH: 8192 # 2**13 (= 8,192) epochs ~36 days -LATEST_SLASHED_EXIT_LENGTH: 8192 +SLASHED_EXIT_LENGTH: 8192 # Reward and penalty quotients diff --git a/configs/constant_presets/minimal.yaml b/configs/constant_presets/minimal.yaml index 73448c3c6..25f1e8419 100644 --- a/configs/constant_presets/minimal.yaml +++ b/configs/constant_presets/minimal.yaml @@ -81,11 +81,11 @@ EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS: 4096 # State list lengths # --------------------------------------------------------------- # [customized] smaller state -LATEST_RANDAO_MIXES_LENGTH: 64 +RANDAO_MIXES_LENGTH: 64 # [customized] smaller state -LATEST_ACTIVE_INDEX_ROOTS_LENGTH: 64 +ACTIVE_INDEX_ROOTS_LENGTH: 64 # [customized] smaller state -LATEST_SLASHED_EXIT_LENGTH: 64 +SLASHED_EXIT_LENGTH: 64 # Reward and penalty quotients diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 7a51970e3..e25147a7f 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -62,6 +62,7 @@ from eth2spec.utils.bls import ( from eth2spec.utils.hash_function import hash ''' +BYTE_TYPES = [4, 32, 48, 96] NEW_TYPES = { 'Slot': 'int', 'Epoch': 'int', @@ -69,7 +70,6 @@ NEW_TYPES = { 'ValidatorIndex': 'int', 'Gwei': 'int', } -BYTE_TYPES = [4, 32, 48, 96] SUNDRY_FUNCTIONS = ''' def get_ssz_type_by_name(name: str) -> Container: return globals()[name] @@ -130,9 +130,10 @@ def objects_to_spec(functions: Dict[str, str], """ Given all the objects that constitute a spec, combine them into a single pyfile. """ - new_type_definitions = \ - '\n'.join(['''%s = NewType('%s', %s)''' % (key, key, value) for key, value in new_types.items()]) - new_type_definitions += '\n' + '\n'.join(['Bytes%s = BytesN[%s]' % (n, n) for n in byte_types]) + new_type_definitions = '\n'.join(['Bytes%s = BytesN[%s]' % (n, n) for n in byte_types]) + new_type_definitions += '\n' + '\n'.join(['Hash = Bytes32', 'BLSPubkey = Bytes48', 'BLSSignature = Bytes96']) + new_type_definitions += \ + '\n' + '\n'.join(['''%s = NewType('%s', %s)''' % (key, key, value) for key, value in new_types.items()]) functions_spec = '\n\n'.join(functions.values()) constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, constants[x]), constants)) ssz_objects_instantiation_spec = '\n\n'.join(ssz_objects.values()) @@ -177,7 +178,7 @@ def dependency_order_ssz_objects(objects: Dict[str, str]) -> None: items = list(objects.items()) for key, value in items: dependencies = re.findall(r'(: [A-Z][\w[]*)', value) - dependencies = map(lambda x: re.sub(r'\W|Vector|List|Container|uint\d+|Bytes\d+|bytes', '', x), dependencies) + dependencies = map(lambda x: re.sub(r'\W|Vector|List|Container|Hash|BLSPubkey|BLSSignature|uint\d+|Bytes\d+|bytes', '', x), dependencies) for dep in dependencies: if dep in NEW_TYPES or len(dep) == 0: continue diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index a6d9d23c5..9fd38d7db 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -20,7 +20,8 @@ - [Rewards and penalties](#rewards-and-penalties) - [Max operations per block](#max-operations-per-block) - [Signature domains](#signature-domains) - - [Data structures](#data-structures) + - [Custom types](#custom-types) + - [Containers](#containers) - [Misc dependencies](#misc-dependencies) - [`Fork`](#fork) - [`Validator`](#validator) @@ -45,7 +46,6 @@ - [`BeaconBlock`](#beaconblock) - [Beacon state](#beacon-state) - [`BeaconState`](#beaconstate) - - [Custom types](#custom-types) - [Helper functions](#helper-functions) - [`xor`](#xor) - [`hash`](#hash) @@ -216,9 +216,9 @@ These configurations are updated for releases, but may be out of sync during `de | Name | Value | Unit | Duration | | - | - | :-: | :-: | -| `LATEST_RANDAO_MIXES_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | -| `LATEST_ACTIVE_INDEX_ROOTS_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | -| `LATEST_SLASHED_EXIT_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | +| `RANDAO_MIXES_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | +| `ACTIVE_INDEX_ROOTS_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | +| `SLASHED_EXIT_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | ### Rewards and penalties @@ -255,314 +255,6 @@ These configurations are updated for releases, but may be out of sync during `de | `DOMAIN_VOLUNTARY_EXIT` | `4` | | `DOMAIN_TRANSFER` | `5` | -## Data structures - -The following data structures are defined as [SimpleSerialize (SSZ)](../simple-serialize.md) objects. - -The types are defined topologically to aid in facilitating an executable version of the spec. - -### Misc dependencies - -#### `Fork` - -```python -class Fork(Container): - # Previous fork version - previous_version: Bytes4 - # Current fork version - current_version: Bytes4 - # Fork epoch number - epoch: uint64 -``` - -#### `Validator` - -```python -class Validator(Container): - # BLS public key - pubkey: Bytes48 - # Withdrawal credentials - withdrawal_credentials: Bytes32 - # Epoch when became eligible for activation - activation_eligibility_epoch: uint64 - # Epoch when validator activated - activation_epoch: uint64 - # Epoch when validator exited - exit_epoch: uint64 - # Epoch when validator is eligible to withdraw - withdrawable_epoch: uint64 - # Was the validator slashed - slashed: bool - # Effective balance - effective_balance: uint64 -``` - -#### `Crosslink` - -```python -class Crosslink(Container): - # Shard number - shard: uint64 - # Crosslinking data from epochs [start....end-1] - start_epoch: uint64 - end_epoch: uint64 - # Root of the previous crosslink - parent_root: Bytes32 - # Root of the crosslinked shard data since the previous crosslink - data_root: Bytes32 -``` - -#### `AttestationData` - -```python -class AttestationData(Container): - # LMD GHOST vote - beacon_block_root: Bytes32 - - # FFG vote - source_epoch: uint64 - source_root: Bytes32 - target_epoch: uint64 - target_root: Bytes32 - - # Crosslink vote - crosslink: Crosslink -``` - -#### `AttestationDataAndCustodyBit` - -```python -class AttestationDataAndCustodyBit(Container): - # Attestation data - data: AttestationData - # Custody bit - custody_bit: bool -``` - -#### `IndexedAttestation` - -```python -class IndexedAttestation(Container): - # Validator indices - custody_bit_0_indices: List[uint64] - custody_bit_1_indices: List[uint64] - # Attestation data - data: AttestationData - # Aggregate signature - signature: Bytes96 -``` - -#### `PendingAttestation` - -```python -class PendingAttestation(Container): - # Attester aggregation bitfield - aggregation_bitfield: bytes - # Attestation data - data: AttestationData - # Inclusion delay - inclusion_delay: uint64 - # Proposer index - proposer_index: uint64 -``` - -#### `Eth1Data` - -```python -class Eth1Data(Container): - # Root of the deposit tree - deposit_root: Bytes32 - # Total number of deposits - deposit_count: uint64 - # Block hash - block_hash: Bytes32 -``` - -#### `HistoricalBatch` - -```python -class HistoricalBatch(Container): - # Block roots - block_roots: Vector[Bytes32, SLOTS_PER_HISTORICAL_ROOT] - # State roots - state_roots: Vector[Bytes32, SLOTS_PER_HISTORICAL_ROOT] -``` - -#### `DepositData` - -```python -class DepositData(Container): - # BLS pubkey - pubkey: Bytes48 - # Withdrawal credentials - withdrawal_credentials: Bytes32 - # Amount in Gwei - amount: uint64 - # Container self-signature - signature: Bytes96 -``` - -#### `BeaconBlockHeader` - -```python -class BeaconBlockHeader(Container): - slot: uint64 - parent_root: Bytes32 - state_root: Bytes32 - body_root: Bytes32 - signature: Bytes96 -``` - -### Beacon operations - -#### `ProposerSlashing` - -```python -class ProposerSlashing(Container): - # Proposer index - proposer_index: uint64 - # First block header - header_1: BeaconBlockHeader - # Second block header - header_2: BeaconBlockHeader -``` - -#### `AttesterSlashing` - -```python -class AttesterSlashing(Container): - # First attestation - attestation_1: IndexedAttestation - # Second attestation - attestation_2: IndexedAttestation -``` - -#### `Attestation` - -```python -class Attestation(Container): - # Attester aggregation bitfield - aggregation_bitfield: bytes - # Attestation data - data: AttestationData - # Custody bitfield - custody_bitfield: bytes - # BLS aggregate signature - signature: Bytes96 -``` - -#### `Deposit` - -```python -class Deposit(Container): - # Branch in the deposit tree - proof: Vector[Bytes32, DEPOSIT_CONTRACT_TREE_DEPTH] - # Data - data: DepositData -``` - -#### `VoluntaryExit` - -```python -class VoluntaryExit(Container): - # Minimum epoch for processing exit - epoch: uint64 - # Index of the exiting validator - validator_index: uint64 - # Validator signature - signature: Bytes96 -``` - -#### `Transfer` - -```python -class Transfer(Container): - # Sender index - sender: uint64 - # Recipient index - recipient: uint64 - # Amount in Gwei - amount: uint64 - # Fee in Gwei for block proposer - fee: uint64 - # Inclusion slot - slot: uint64 - # Sender withdrawal pubkey - pubkey: Bytes48 - # Sender signature - signature: Bytes96 -``` - -### Beacon blocks - -#### `BeaconBlockBody` - -```python -class BeaconBlockBody(Container): - randao_reveal: Bytes96 - eth1_data: Eth1Data - graffiti: Bytes32 - proposer_slashings: List[ProposerSlashing] - attester_slashings: List[AttesterSlashing] - attestations: List[Attestation] - deposits: List[Deposit] - voluntary_exits: List[VoluntaryExit] - transfers: List[Transfer] -``` - -#### `BeaconBlock` - -```python -class BeaconBlock(Container): - # Header - slot: uint64 - parent_root: Bytes32 - state_root: Bytes32 - body: BeaconBlockBody - signature: Bytes96 -``` - -### Beacon state - -#### `BeaconState` - -```python -class BeaconState(Container): - # Misc - slot: uint64 - genesis_time: uint64 - fork: Fork # For versioning hard forks - # Validator registry - validator_registry: List[Validator] - balances: List[uint64] - # Randomness and committees - latest_randao_mixes: Vector[Bytes32, LATEST_RANDAO_MIXES_LENGTH] - latest_start_shard: uint64 - # Finality - previous_epoch_attestations: List[PendingAttestation] - current_epoch_attestations: List[PendingAttestation] - previous_justified_epoch: uint64 - current_justified_epoch: uint64 - previous_justified_root: Bytes32 - current_justified_root: Bytes32 - justification_bitfield: uint64 - finalized_epoch: uint64 - finalized_root: Bytes32 - # Recent state - current_crosslinks: Vector[Crosslink, SHARD_COUNT] - previous_crosslinks: Vector[Crosslink, SHARD_COUNT] - latest_block_roots: Vector[Bytes32, SLOTS_PER_HISTORICAL_ROOT] - latest_state_roots: Vector[Bytes32, SLOTS_PER_HISTORICAL_ROOT] - latest_active_index_roots: Vector[Bytes32, LATEST_ACTIVE_INDEX_ROOTS_LENGTH] - latest_slashed_balances: Vector[uint64, LATEST_SLASHED_EXIT_LENGTH] - latest_block_header: BeaconBlockHeader - historical_roots: List[Bytes32] - # Ethereum 1.0 chain data - latest_eth1_data: Eth1Data - eth1_data_votes: List[Eth1Data] - deposit_index: uint64 -``` - ## Custom types We define the following Python custom types for type hinting and readability: @@ -577,6 +269,264 @@ We define the following Python custom types for type hinting and readability: | `BLSPubkey` | `Bytes48` | a BLS12-381 public key | | `BLSSignature` | `Bytes96` | a BLS12-381 signature | +## Containers + +The following types are [SimpleSerialize (SSZ)](../simple-serialize.md) containers. + +*Note*: The definitions are ordered topologically to facilitate execution of the spec. + +### Misc dependencies + +#### `Fork` + +```python +class Fork(Container): + previous_version: Bytes4 + current_version: Bytes4 + epoch: Epoch +``` + +#### `Validator` + +```python +class Validator(Container): + pubkey: BLSPubkey + withdrawal_credentials: Hash + effective_balance: Gwei + slashed: bool + # Status epochs + activation_eligibility_epoch: Epoch + activation_epoch: Epoch + exit_epoch: Epoch + withdrawable_epoch: Epoch +``` + +#### `Crosslink` + +```python +class Crosslink(Container): + shard: Shard + parent_root: Hash + # Crosslinking data + start_epoch: Epoch + end_epoch: Epoch + data_root: Hash +``` + +#### `AttestationData` + +```python +class AttestationData(Container): + # LMD GHOST vote + beacon_block_root: Hash + # FFG vote + source_epoch: Epoch + source_root: Hash + target_epoch: Epoch + target_root: Hash + # Crosslink vote + crosslink: Crosslink +``` + +#### `AttestationDataAndCustodyBit` + +```python +class AttestationDataAndCustodyBit(Container): + data: AttestationData + custody_bit: bool +``` + +#### `IndexedAttestation` + +```python +class IndexedAttestation(Container): + custody_bit_0_indices: List[ValidatorIndex] # Indices with custody bit equal to 0 + custody_bit_1_indices: List[ValidatorIndex] # Indices with custody bit equal to 1 + data: AttestationData + signature: BLSSignature +``` + +#### `PendingAttestation` + +```python +class PendingAttestation(Container): + aggregation_bitfield: bytes + data: AttestationData + inclusion_delay: Slot + proposer_index: ValidatorIndex +``` + +#### `Eth1Data` + +```python +class Eth1Data(Container): + deposit_root: Hash + deposit_count: uint64 + block_hash: Hash +``` + +#### `HistoricalBatch` + +```python +class HistoricalBatch(Container): + block_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] + state_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] +``` + +#### `DepositData` + +```python +class DepositData(Container): + pubkey: BLSPubkey + withdrawal_credentials: Hash + amount: Gwei + signature: BLSSignature +``` + +#### `BeaconBlockHeader` + +```python +class BeaconBlockHeader(Container): + slot: Slot + parent_root: Hash + state_root: Hash + body_root: Hash + signature: BLSSignature +``` + +### Beacon operations + +#### `ProposerSlashing` + +```python +class ProposerSlashing(Container): + proposer_index: ValidatorIndex + header_1: BeaconBlockHeader + header_2: BeaconBlockHeader +``` + +#### `AttesterSlashing` + +```python +class AttesterSlashing(Container): + attestation_1: IndexedAttestation + attestation_2: IndexedAttestation +``` + +#### `Attestation` + +```python +class Attestation(Container): + aggregation_bitfield: bytes + data: AttestationData + custody_bitfield: bytes + signature: BLSSignature +``` + +#### `Deposit` + +```python +class Deposit(Container): + proof: Vector[Hash, DEPOSIT_CONTRACT_TREE_DEPTH] + data: DepositData +``` + +#### `VoluntaryExit` + +```python +class VoluntaryExit(Container): + epoch: Epoch + validator_index: ValidatorIndex + signature: BLSSignature +``` + +#### `Transfer` + +```python +class Transfer(Container): + sender: ValidatorIndex + recipient: ValidatorIndex + amount: Gwei + fee: Gwei + slot: Slot + pubkey: BLSPubkey + signature: BLSSignature +``` + +### Beacon blocks + +#### `BeaconBlockBody` + +```python +class BeaconBlockBody(Container): + randao_reveal: BLSSignature + eth1_data: Eth1Data + graffiti: Bytes32 + # Operations + proposer_slashings: List[ProposerSlashing] + attester_slashings: List[AttesterSlashing] + attestations: List[Attestation] + deposits: List[Deposit] + voluntary_exits: List[VoluntaryExit] + transfers: List[Transfer] +``` + +#### `BeaconBlock` + +```python +class BeaconBlock(Container): + slot: Slot + parent_root: Hash + state_root: Hash + body: BeaconBlockBody + signature: BLSSignature +``` + +### Beacon state + +#### `BeaconState` + +```python +class BeaconState(Container): + # Versioning + genesis_time: uint64 + slot: Slot + fork: Fork + # History + block_header: BeaconBlockHeader + block_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] + state_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] + historical_roots: List[Hash] + # Eth1 + eth1_data: Eth1Data + eth1_data_votes: List[Eth1Data] + eth1_deposit_index: uint64 + # Registry + validators: List[Validator] + balances: List[Gwei] + # Shuffling + start_shard: Shard + randao_mixes: Vector[Hash, RANDAO_MIXES_LENGTH] + active_index_roots: Vector[Hash, ACTIVE_INDEX_ROOTS_LENGTH] + # Slashings + slashed_balances: Vector[Gwei, SLASHED_EXIT_LENGTH] + # Attestations + previous_attestations: List[PendingAttestation] + current_attestations: List[PendingAttestation] + # Crosslinks + previous_crosslinks: Vector[Crosslink, SHARD_COUNT] + current_crosslinks: Vector[Crosslink, SHARD_COUNT] + # Justification + previous_justified_epoch: Epoch + previous_justified_root: Hash + current_justified_epoch: Epoch + current_justified_root: Hash + justification_bitfield: uint64 + # Finality + finalized_epoch: Epoch + finalized_root: Hash +``` + ## Helper functions *Note*: The definitions below are for specification purposes and are not necessarily optimal implementations. @@ -596,11 +546,11 @@ The `hash` function is SHA256. ### `hash_tree_root` -`def hash_tree_root(object: SSZSerializable) -> Bytes32` is a function for hashing objects into a single root utilizing a hash tree structure. `hash_tree_root` is defined in the [SimpleSerialize spec](../simple-serialize.md#merkleization). +`def hash_tree_root(object: SSZSerializable) -> Hash` is a function for hashing objects into a single root utilizing a hash tree structure. `hash_tree_root` is defined in the [SimpleSerialize spec](../simple-serialize.md#merkleization). ### `signing_root` -`def signing_root(object: Container) -> Bytes32` is a function defined in the [SimpleSerialize spec](../simple-serialize.md#self-signed-containers) to compute signing messages. +`def signing_root(object: Container) -> Hash` is a function defined in the [SimpleSerialize spec](../simple-serialize.md#self-signed-containers) to compute signing messages. ### `bls_domain` @@ -681,7 +631,7 @@ def get_active_validator_indices(state: BeaconState, epoch: Epoch) -> List[Valid """ Get active validator indices at ``epoch``. """ - return [i for i, v in enumerate(state.validator_registry) if is_active_validator(v, epoch)] + return [i for i, v in enumerate(state.validators) if is_active_validator(v, epoch)] ``` ### `increase_balance` @@ -726,7 +676,7 @@ def get_epoch_committee_count(state: BeaconState, epoch: Epoch) -> int: ```python def get_shard_delta(state: BeaconState, epoch: Epoch) -> int: """ - Return the number of shards to increment ``state.latest_start_shard`` during ``epoch``. + Return the number of shards to increment ``state.start_shard`` during ``epoch``. """ return min(get_epoch_committee_count(state, epoch), SHARD_COUNT - SHARD_COUNT // SLOTS_PER_EPOCH) ``` @@ -737,7 +687,7 @@ def get_shard_delta(state: BeaconState, epoch: Epoch) -> int: def get_epoch_start_shard(state: BeaconState, epoch: Epoch) -> Shard: assert epoch <= get_current_epoch(state) + 1 check_epoch = get_current_epoch(state) + 1 - shard = (state.latest_start_shard + get_shard_delta(state, get_current_epoch(state))) % SHARD_COUNT + shard = (state.start_shard + get_shard_delta(state, get_current_epoch(state))) % SHARD_COUNT while check_epoch > epoch: check_epoch -= 1 shard = (shard + SHARD_COUNT - get_shard_delta(state, check_epoch)) % SHARD_COUNT @@ -757,19 +707,19 @@ def get_attestation_data_slot(state: BeaconState, data: AttestationData) -> Slot ```python def get_block_root_at_slot(state: BeaconState, - slot: Slot) -> Bytes32: + slot: Slot) -> Hash: """ Return the block root at a recent ``slot``. """ assert slot < state.slot <= slot + SLOTS_PER_HISTORICAL_ROOT - return state.latest_block_roots[slot % SLOTS_PER_HISTORICAL_ROOT] + return state.block_roots[slot % SLOTS_PER_HISTORICAL_ROOT] ``` ### `get_block_root` ```python def get_block_root(state: BeaconState, - epoch: Epoch) -> Bytes32: + epoch: Epoch) -> Hash: """ Return the block root at a recent ``epoch``. """ @@ -780,37 +730,37 @@ def get_block_root(state: BeaconState, ```python def get_randao_mix(state: BeaconState, - epoch: Epoch) -> Bytes32: + epoch: Epoch) -> Hash: """ Return the randao mix at a recent ``epoch``. - ``epoch`` expected to be between (current_epoch - LATEST_RANDAO_MIXES_LENGTH, current_epoch]. + ``epoch`` expected to be between (current_epoch - RANDAO_MIXES_LENGTH, current_epoch]. """ - return state.latest_randao_mixes[epoch % LATEST_RANDAO_MIXES_LENGTH] + return state.randao_mixes[epoch % RANDAO_MIXES_LENGTH] ``` ### `get_active_index_root` ```python def get_active_index_root(state: BeaconState, - epoch: Epoch) -> Bytes32: + epoch: Epoch) -> Hash: """ Return the index root at a recent ``epoch``. ``epoch`` expected to be between - (current_epoch - LATEST_ACTIVE_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY]. + (current_epoch - ACTIVE_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY]. """ - return state.latest_active_index_roots[epoch % LATEST_ACTIVE_INDEX_ROOTS_LENGTH] + return state.active_index_roots[epoch % ACTIVE_INDEX_ROOTS_LENGTH] ``` ### `generate_seed` ```python def generate_seed(state: BeaconState, - epoch: Epoch) -> Bytes32: + epoch: Epoch) -> Hash: """ Generate a seed for the given ``epoch``. """ return hash( - get_randao_mix(state, epoch + LATEST_RANDAO_MIXES_LENGTH - MIN_SEED_LOOKAHEAD) + + get_randao_mix(state, epoch + RANDAO_MIXES_LENGTH - MIN_SEED_LOOKAHEAD) + get_active_index_root(state, epoch) + int_to_bytes(epoch, length=32) ) @@ -834,7 +784,7 @@ def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex: while True: candidate_index = first_committee[(epoch + i) % len(first_committee)] random_byte = hash(seed + int_to_bytes(i // 32, length=8))[i % 32] - effective_balance = state.validator_registry[candidate_index].effective_balance + effective_balance = state.validators[candidate_index].effective_balance if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte: return candidate_index i += 1 @@ -843,7 +793,7 @@ def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex: ### `verify_merkle_branch` ```python -def verify_merkle_branch(leaf: Bytes32, proof: List[Bytes32], depth: int, index: int, root: Bytes32) -> bool: +def verify_merkle_branch(leaf: Hash, proof: List[Hash], depth: int, index: int, root: Hash) -> bool: """ Verify that the given ``leaf`` is on the merkle branch ``proof`` starting with the given ``root``. @@ -860,7 +810,7 @@ def verify_merkle_branch(leaf: Bytes32, proof: List[Bytes32], depth: int, index: ### `get_shuffled_index` ```python -def get_shuffled_index(index: ValidatorIndex, index_count: int, seed: Bytes32) -> ValidatorIndex: +def get_shuffled_index(index: ValidatorIndex, index_count: int, seed: Hash) -> ValidatorIndex: """ Return the shuffled validator index corresponding to ``seed`` (and ``index_count``). """ @@ -884,7 +834,7 @@ def get_shuffled_index(index: ValidatorIndex, index_count: int, seed: Bytes32) - ### `compute_committee` ```python -def compute_committee(indices: List[ValidatorIndex], seed: Bytes32, index: int, count: int) -> List[ValidatorIndex]: +def compute_committee(indices: List[ValidatorIndex], seed: Hash, index: int, count: int) -> List[ValidatorIndex]: start = (len(indices) * index) // count end = (len(indices) * (index + 1)) // count return [indices[get_shuffled_index(i, len(indices), seed)] for i in range(start, end)] @@ -937,7 +887,7 @@ def get_total_balance(state: BeaconState, indices: List[ValidatorIndex]) -> Gwei """ Return the combined effective balance of the ``indices``. (1 Gwei minimum to avoid divisions by zero.) """ - return max(sum([state.validator_registry[index].effective_balance for index in indices]), 1) + return max(sum([state.validators[index].effective_balance for index in indices]), 1) ``` ### `get_domain` @@ -1022,8 +972,8 @@ def validate_indexed_attestation(state: BeaconState, indexed_attestation: Indexe # Verify aggregate signature assert bls_verify_multiple( pubkeys=[ - bls_aggregate_pubkeys([state.validator_registry[i].pubkey for i in bit_0_indices]), - bls_aggregate_pubkeys([state.validator_registry[i].pubkey for i in bit_1_indices]), + bls_aggregate_pubkeys([state.validators[i].pubkey for i in bit_0_indices]), + bls_aggregate_pubkeys([state.validators[i].pubkey for i in bit_1_indices]), ], message_hashes=[ hash_tree_root(AttestationDataAndCustodyBit(data=indexed_attestation.data, custody_bit=0b0)), @@ -1112,14 +1062,14 @@ def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None: Initiate the exit of the validator of the given ``index``. """ # Return if validator already initiated exit - validator = state.validator_registry[index] + validator = state.validators[index] if validator.exit_epoch != FAR_FUTURE_EPOCH: return # Compute exit queue epoch - exit_epochs = [v.exit_epoch for v in state.validator_registry if v.exit_epoch != FAR_FUTURE_EPOCH] + exit_epochs = [v.exit_epoch for v in state.validators if v.exit_epoch != FAR_FUTURE_EPOCH] exit_queue_epoch = max(exit_epochs + [get_delayed_activation_exit_epoch(get_current_epoch(state))]) - exit_queue_churn = len([v for v in state.validator_registry if v.exit_epoch == exit_queue_epoch]) + exit_queue_churn = len([v for v in state.validators if v.exit_epoch == exit_queue_epoch]) if exit_queue_churn >= get_churn_limit(state): exit_queue_epoch += 1 @@ -1139,10 +1089,10 @@ def slash_validator(state: BeaconState, """ current_epoch = get_current_epoch(state) initiate_validator_exit(state, slashed_index) - state.validator_registry[slashed_index].slashed = True - state.validator_registry[slashed_index].withdrawable_epoch = current_epoch + LATEST_SLASHED_EXIT_LENGTH - slashed_balance = state.validator_registry[slashed_index].effective_balance - state.latest_slashed_balances[current_epoch % LATEST_SLASHED_EXIT_LENGTH] += slashed_balance + state.validators[slashed_index].slashed = True + state.validators[slashed_index].withdrawable_epoch = current_epoch + SLASHED_EXIT_LENGTH + slashed_balance = state.validators[slashed_index].effective_balance + state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] += slashed_balance proposer_index = get_beacon_proposer_index(state) if whistleblower_index is None: @@ -1175,8 +1125,8 @@ Let `genesis_state = get_genesis_beacon_state(genesis_deposits, eth2genesis.gene def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis_eth1_data: Eth1Data) -> BeaconState: state = BeaconState( genesis_time=genesis_time, - latest_eth1_data=genesis_eth1_data, - latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), + eth1_data=genesis_eth1_data, + block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), ) # Process genesis deposits @@ -1184,15 +1134,15 @@ def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis process_deposit(state, deposit) # Process genesis activations - for validator in state.validator_registry: + for validator in state.validators: if validator.effective_balance >= MAX_EFFECTIVE_BALANCE: validator.activation_eligibility_epoch = GENESIS_EPOCH validator.activation_epoch = GENESIS_EPOCH - # Populate latest_active_index_roots + # Populate active_index_roots genesis_active_index_root = hash_tree_root(get_active_validator_indices(state, GENESIS_EPOCH)) - for index in range(LATEST_ACTIVE_INDEX_ROOTS_LENGTH): - state.latest_active_index_roots[index] = genesis_active_index_root + for index in range(ACTIVE_INDEX_ROOTS_LENGTH): + state.active_index_roots[index] = genesis_active_index_root return state ``` @@ -1233,15 +1183,15 @@ def process_slots(state: BeaconState, slot: Slot) -> None: def process_slot(state: BeaconState) -> None: # Cache state root previous_state_root = hash_tree_root(state) - state.latest_state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_state_root + state.state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_state_root # Cache latest block header state root - if state.latest_block_header.state_root == ZERO_HASH: - state.latest_block_header.state_root = previous_state_root + if state.block_header.state_root == ZERO_HASH: + state.block_header.state_root = previous_state_root # Cache block root - previous_block_root = signing_root(state.latest_block_header) - state.latest_block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root + previous_block_root = signing_root(state.block_header) + state.block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root ``` ### Epoch processing @@ -1271,7 +1221,7 @@ def get_total_active_balance(state: BeaconState) -> Gwei: ```python def get_matching_source_attestations(state: BeaconState, epoch: Epoch) -> List[PendingAttestation]: assert epoch in (get_current_epoch(state), get_previous_epoch(state)) - return state.current_epoch_attestations if epoch == get_current_epoch(state) else state.previous_epoch_attestations + return state.current_attestations if epoch == get_current_epoch(state) else state.previous_attestations ``` ```python @@ -1296,7 +1246,7 @@ def get_unslashed_attesting_indices(state: BeaconState, output = set() for a in attestations: output = output.union(get_attesting_indices(state, a.data, a.aggregation_bitfield)) - return sorted(filter(lambda index: not state.validator_registry[index].slashed, list(output))) + return sorted(filter(lambda index: not state.validators[index].slashed, list(output))) ``` ```python @@ -1391,7 +1341,7 @@ def process_crosslinks(state: BeaconState) -> None: ```python def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei: total_balance = get_total_active_balance(state) - effective_balance = state.validator_registry[index].effective_balance + effective_balance = state.validators[index].effective_balance return effective_balance * BASE_REWARD_FACTOR // integer_squareroot(total_balance) // BASE_REWARDS_PER_EPOCH ``` @@ -1399,10 +1349,10 @@ def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei: def get_attestation_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: previous_epoch = get_previous_epoch(state) total_balance = get_total_active_balance(state) - rewards = [0 for _ in range(len(state.validator_registry))] - penalties = [0 for _ in range(len(state.validator_registry))] + rewards = [0 for _ in range(len(state.validators))] + penalties = [0 for _ in range(len(state.validators))] eligible_validator_indices = [ - index for index, v in enumerate(state.validator_registry) + index for index, v in enumerate(state.validators) if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch) ] @@ -1436,7 +1386,7 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: penalties[index] += BASE_REWARDS_PER_EPOCH * get_base_reward(state, index) if index not in matching_target_attesting_indices: penalties[index] += ( - state.validator_registry[index].effective_balance * finality_delay // INACTIVITY_PENALTY_QUOTIENT + state.validators[index].effective_balance * finality_delay // INACTIVITY_PENALTY_QUOTIENT ) return rewards, penalties @@ -1444,8 +1394,8 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: ```python def get_crosslink_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: - rewards = [0 for index in range(len(state.validator_registry))] - penalties = [0 for index in range(len(state.validator_registry))] + rewards = [0 for index in range(len(state.validators))] + penalties = [0 for index in range(len(state.validators))] epoch = get_previous_epoch(state) for offset in range(get_epoch_committee_count(state, epoch)): shard = (get_epoch_start_shard(state, epoch) + offset) % SHARD_COUNT @@ -1469,7 +1419,7 @@ def process_rewards_and_penalties(state: BeaconState) -> None: rewards1, penalties1 = get_attestation_deltas(state) rewards2, penalties2 = get_crosslink_deltas(state) - for i in range(len(state.validator_registry)): + for i in range(len(state.validators)): increase_balance(state, i, rewards1[i] + rewards2[i]) decrease_balance(state, i, penalties1[i] + penalties2[i]) ``` @@ -1479,7 +1429,7 @@ def process_rewards_and_penalties(state: BeaconState) -> None: ```python def process_registry_updates(state: BeaconState) -> None: # Process activation eligibility and ejections - for index, validator in enumerate(state.validator_registry): + for index, validator in enumerate(state.validators): if ( validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH and validator.effective_balance >= MAX_EFFECTIVE_BALANCE @@ -1491,13 +1441,13 @@ def process_registry_updates(state: BeaconState) -> None: # Queue validators eligible for activation and not dequeued for activation prior to finalized epoch activation_queue = sorted([ - index for index, validator in enumerate(state.validator_registry) if + index for index, validator in enumerate(state.validators) if validator.activation_eligibility_epoch != FAR_FUTURE_EPOCH and validator.activation_epoch >= get_delayed_activation_exit_epoch(state.finalized_epoch) - ], key=lambda index: state.validator_registry[index].activation_eligibility_epoch) + ], key=lambda index: state.validators[index].activation_eligibility_epoch) # Dequeued validators for activation up to churn limit (without resetting activation epoch) for index in activation_queue[:get_churn_limit(state)]: - validator = state.validator_registry[index] + validator = state.validators[index] if validator.activation_epoch == FAR_FUTURE_EPOCH: validator.activation_epoch = get_delayed_activation_exit_epoch(get_current_epoch(state)) ``` @@ -1510,12 +1460,12 @@ def process_slashings(state: BeaconState) -> None: total_balance = get_total_active_balance(state) # Compute slashed balances in the current epoch - total_at_start = state.latest_slashed_balances[(current_epoch + 1) % LATEST_SLASHED_EXIT_LENGTH] - total_at_end = state.latest_slashed_balances[current_epoch % LATEST_SLASHED_EXIT_LENGTH] + total_at_start = state.slashed_balances[(current_epoch + 1) % SLASHED_EXIT_LENGTH] + total_at_end = state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] total_penalties = total_at_end - total_at_start - for index, validator in enumerate(state.validator_registry): - if validator.slashed and current_epoch == validator.withdrawable_epoch - LATEST_SLASHED_EXIT_LENGTH // 2: + for index, validator in enumerate(state.validators): + if validator.slashed and current_epoch == validator.withdrawable_epoch - SLASHED_EXIT_LENGTH // 2: penalty = max( validator.effective_balance * min(total_penalties * 3, total_balance) // total_balance, validator.effective_balance // MIN_SLASHING_PENALTY_QUOTIENT @@ -1533,34 +1483,34 @@ def process_final_updates(state: BeaconState) -> None: if (state.slot + 1) % SLOTS_PER_ETH1_VOTING_PERIOD == 0: state.eth1_data_votes = [] # Update effective balances with hysteresis - for index, validator in enumerate(state.validator_registry): + for index, validator in enumerate(state.validators): balance = state.balances[index] HALF_INCREMENT = EFFECTIVE_BALANCE_INCREMENT // 2 if balance < validator.effective_balance or validator.effective_balance + 3 * HALF_INCREMENT < balance: validator.effective_balance = min(balance - balance % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE) # Update start shard - state.latest_start_shard = (state.latest_start_shard + get_shard_delta(state, current_epoch)) % SHARD_COUNT + state.start_shard = (state.start_shard + get_shard_delta(state, current_epoch)) % SHARD_COUNT # Set active index root - index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % LATEST_ACTIVE_INDEX_ROOTS_LENGTH - state.latest_active_index_roots[index_root_position] = hash_tree_root( + index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % ACTIVE_INDEX_ROOTS_LENGTH + state.active_index_roots[index_root_position] = hash_tree_root( get_active_validator_indices(state, next_epoch + ACTIVATION_EXIT_DELAY) ) # Set total slashed balances - state.latest_slashed_balances[next_epoch % LATEST_SLASHED_EXIT_LENGTH] = ( - state.latest_slashed_balances[current_epoch % LATEST_SLASHED_EXIT_LENGTH] + state.slashed_balances[next_epoch % SLASHED_EXIT_LENGTH] = ( + state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] ) # Set randao mix - state.latest_randao_mixes[next_epoch % LATEST_RANDAO_MIXES_LENGTH] = get_randao_mix(state, current_epoch) + state.randao_mixes[next_epoch % RANDAO_MIXES_LENGTH] = get_randao_mix(state, current_epoch) # Set historical root accumulator if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: historical_batch = HistoricalBatch( - block_roots=state.latest_block_roots, - state_roots=state.latest_state_roots, + block_roots=state.block_roots, + state_roots=state.state_roots, ) state.historical_roots.append(hash_tree_root(historical_batch)) # Rotate current/previous epoch attestations - state.previous_epoch_attestations = state.current_epoch_attestations - state.current_epoch_attestations = [] + state.previous_attestations = state.current_attestations + state.current_attestations = [] ``` ### Block processing @@ -1580,15 +1530,15 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None: # Verify that the slots match assert block.slot == state.slot # Verify that the parent matches - assert block.parent_root == signing_root(state.latest_block_header) + assert block.parent_root == signing_root(state.block_header) # Save current block as the new latest block - state.latest_block_header = BeaconBlockHeader( + state.block_header = BeaconBlockHeader( slot=block.slot, parent_root=block.parent_root, body_root=hash_tree_root(block.body), ) # Verify proposer is not slashed - proposer = state.validator_registry[get_beacon_proposer_index(state)] + proposer = state.validators[get_beacon_proposer_index(state)] assert not proposer.slashed # Verify proposer signature assert bls_verify(proposer.pubkey, signing_root(block), block.signature, get_domain(state, DOMAIN_BEACON_PROPOSER)) @@ -1598,7 +1548,7 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None: ```python def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: - proposer = state.validator_registry[get_beacon_proposer_index(state)] + proposer = state.validators[get_beacon_proposer_index(state)] # Verify that the provided randao value is valid assert bls_verify( proposer.pubkey, @@ -1607,7 +1557,7 @@ def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: get_domain(state, DOMAIN_RANDAO), ) # Mix it in - state.latest_randao_mixes[get_current_epoch(state) % LATEST_RANDAO_MIXES_LENGTH] = ( + state.randao_mixes[get_current_epoch(state) % RANDAO_MIXES_LENGTH] = ( xor(get_randao_mix(state, get_current_epoch(state)), hash(body.randao_reveal)) ) @@ -1619,7 +1569,7 @@ def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: def process_eth1_data(state: BeaconState, body: BeaconBlockBody) -> None: state.eth1_data_votes.append(body.eth1_data) if state.eth1_data_votes.count(body.eth1_data) * 2 > SLOTS_PER_ETH1_VOTING_PERIOD: - state.latest_eth1_data = body.eth1_data + state.eth1_data = body.eth1_data ``` #### Operations @@ -1627,7 +1577,7 @@ def process_eth1_data(state: BeaconState, body: BeaconBlockBody) -> None: ```python def process_operations(state: BeaconState, body: BeaconBlockBody) -> None: # Verify that outstanding deposits are processed up to the maximum number of deposits - assert len(body.deposits) == min(MAX_DEPOSITS, state.latest_eth1_data.deposit_count - state.deposit_index) + assert len(body.deposits) == min(MAX_DEPOSITS, state.eth1_data.deposit_count - state.eth1_deposit_index) # Verify that there are no duplicate transfers assert len(body.transfers) == len(set(body.transfers)) @@ -1651,7 +1601,7 @@ def process_proposer_slashing(state: BeaconState, proposer_slashing: ProposerSla """ Process ``ProposerSlashing`` operation. """ - proposer = state.validator_registry[proposer_slashing.proposer_index] + proposer = state.validators[proposer_slashing.proposer_index] # Verify that the epoch is the same assert slot_to_epoch(proposer_slashing.header_1.slot) == slot_to_epoch(proposer_slashing.header_2.slot) # But the headers are different @@ -1683,7 +1633,7 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla attesting_indices_1 = attestation_1.custody_bit_0_indices + attestation_1.custody_bit_1_indices attesting_indices_2 = attestation_2.custody_bit_0_indices + attestation_2.custody_bit_1_indices for index in sorted(set(attesting_indices_1).intersection(attesting_indices_2)): - if is_slashable_validator(state.validator_registry[index], get_current_epoch(state)): + if is_slashable_validator(state.validators[index], get_current_epoch(state)): slash_validator(state, index) slashed_any = True assert slashed_any @@ -1711,11 +1661,11 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: if data.target_epoch == get_current_epoch(state): ffg_data = (state.current_justified_epoch, state.current_justified_root, get_current_epoch(state)) parent_crosslink = state.current_crosslinks[data.crosslink.shard] - state.current_epoch_attestations.append(pending_attestation) + state.current_attestations.append(pending_attestation) else: ffg_data = (state.previous_justified_epoch, state.previous_justified_root, get_previous_epoch(state)) parent_crosslink = state.previous_crosslinks[data.crosslink.shard] - state.previous_epoch_attestations.append(pending_attestation) + state.previous_attestations.append(pending_attestation) # Check FFG data, crosslink data, and signature assert ffg_data == (data.source_epoch, data.source_root, data.target_epoch) @@ -1738,16 +1688,16 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None: leaf=hash_tree_root(deposit.data), proof=deposit.proof, depth=DEPOSIT_CONTRACT_TREE_DEPTH, - index=state.deposit_index, - root=state.latest_eth1_data.deposit_root, + index=state.eth1_deposit_index, + root=state.eth1_data.deposit_root, ) # Deposits must be processed in order - state.deposit_index += 1 + state.eth1_deposit_index += 1 pubkey = deposit.data.pubkey amount = deposit.data.amount - validator_pubkeys = [v.pubkey for v in state.validator_registry] + validator_pubkeys = [v.pubkey for v in state.validators] if pubkey not in validator_pubkeys: # Verify the deposit signature (proof of possession). # Invalid signatures are allowed by the deposit contract, @@ -1759,7 +1709,7 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None: return # Add validator and balance entries - state.validator_registry.append(Validator( + state.validators.append(Validator( pubkey=pubkey, withdrawal_credentials=deposit.data.withdrawal_credentials, activation_eligibility_epoch=FAR_FUTURE_EPOCH, @@ -1782,7 +1732,7 @@ def process_voluntary_exit(state: BeaconState, exit: VoluntaryExit) -> None: """ Process ``VoluntaryExit`` operation. """ - validator = state.validator_registry[exit.validator_index] + validator = state.validators[exit.validator_index] # Verify the validator is active assert is_active_validator(validator, get_current_epoch(state)) # Verify the validator has not yet exited @@ -1811,13 +1761,13 @@ def process_transfer(state: BeaconState, transfer: Transfer) -> None: assert state.slot == transfer.slot # Sender must be not yet eligible for activation, withdrawn, or transfer balance over MAX_EFFECTIVE_BALANCE assert ( - state.validator_registry[transfer.sender].activation_eligibility_epoch == FAR_FUTURE_EPOCH or - get_current_epoch(state) >= state.validator_registry[transfer.sender].withdrawable_epoch or + state.validators[transfer.sender].activation_eligibility_epoch == FAR_FUTURE_EPOCH or + get_current_epoch(state) >= state.validators[transfer.sender].withdrawable_epoch or transfer.amount + transfer.fee + MAX_EFFECTIVE_BALANCE <= state.balances[transfer.sender] ) # Verify that the pubkey is valid assert ( - state.validator_registry[transfer.sender].withdrawal_credentials == + state.validators[transfer.sender].withdrawal_credentials == int_to_bytes(BLS_WITHDRAWAL_PREFIX, length=1) + hash(transfer.pubkey)[1:] ) # Verify that the signature is valid diff --git a/specs/core/0_deposit-contract.md b/specs/core/0_deposit-contract.md index e80dad1c5..d40553f43 100644 --- a/specs/core/0_deposit-contract.md +++ b/specs/core/0_deposit-contract.md @@ -75,9 +75,9 @@ Every Ethereum 1.0 deposit, of size at least `MIN_DEPOSIT_AMOUNT`, emits a `Depo When `CHAIN_START_FULL_DEPOSIT_THRESHOLD` of full deposits have been made, the deposit contract emits the `Eth2Genesis` log. The beacon chain state may then be initialized by calling the `get_genesis_beacon_state` function (defined [here](./0_beacon-chain.md#genesis-state)) where: * `genesis_time` equals `time` in the `Eth2Genesis` log -* `latest_eth1_data.deposit_root` equals `deposit_root` in the `Eth2Genesis` log -* `latest_eth1_data.deposit_count` equals `deposit_count` in the `Eth2Genesis` log -* `latest_eth1_data.block_hash` equals the hash of the block that included the log +* `eth1_data.deposit_root` equals `deposit_root` in the `Eth2Genesis` log +* `eth1_data.deposit_count` equals `deposit_count` in the `Eth2Genesis` log +* `eth1_data.block_hash` equals the hash of the block that included the log * `genesis_validator_deposits` is a list of `Deposit` objects built according to the `Deposit` logs up to the deposit that triggered the `Eth2Genesis` log, processed in the order in which they were emitted (oldest to newest) ## Vyper code diff --git a/specs/core/0_fork-choice.md b/specs/core/0_fork-choice.md index 91c3e27ee..739c06d59 100644 --- a/specs/core/0_fork-choice.md +++ b/specs/core/0_fork-choice.md @@ -39,7 +39,7 @@ All terminology, constants, functions, and protocol mechanics defined in the [Ph Processing the beacon chain is similar to processing the Ethereum 1.0 chain. Clients download and process blocks and maintain a view of what is the current "canonical chain", terminating at the current "head". For a beacon block, `block`, to be processed by a node, the following conditions must be met: * The parent block with root `block.parent_root` has been processed and accepted. -* An Ethereum 1.0 block pointed to by the `state.latest_eth1_data.block_hash` has been processed and accepted. +* An Ethereum 1.0 block pointed to by the `state.eth1_data.block_hash` has been processed and accepted. * The node's Unix time is greater than or equal to `state.genesis_time + block.slot * SECONDS_PER_SLOT`. *Note*: Leap seconds mean that slots will occasionally last `SECONDS_PER_SLOT + 1` or `SECONDS_PER_SLOT - 1` seconds, possibly several times a year. @@ -68,8 +68,8 @@ def get_ancestor(store: Store, block: BeaconBlock, slot: Slot) -> BeaconBlock: return get_ancestor(store, store.get_parent(block), slot) ``` -* Let `get_latest_attestation(store: Store, index: ValidatorIndex) -> Attestation` be the attestation with the highest slot number in `store` from the validator with the given `index`. If several such attestations exist, use the one the validator `v` observed first. -* Let `get_latest_attestation_target(store: Store, index: ValidatorIndex) -> BeaconBlock` be the target block in the attestation `get_latest_attestation(store, index)`. +* Let `get_attestation(store: Store, index: ValidatorIndex) -> Attestation` be the attestation with the highest slot number in `store` from the validator with the given `index`. If several such attestations exist, use the one the validator `v` observed first. +* Let `get_attestation_target(store: Store, index: ValidatorIndex) -> BeaconBlock` be the target block in the attestation `get_attestation(store, index)`. * Let `get_children(store: Store, block: BeaconBlock) -> List[BeaconBlock]` return the child blocks of the given `block`. * Let `justified_head_state` be the resulting `BeaconState` object from processing the chain up to the `justified_head`. * The `head` is `lmd_ghost(store, justified_head_state, justified_head)` where the function `lmd_ghost` is defined below. Note that the implementation below is suboptimal; there are implementations that compute the head in time logarithmic in slot count. @@ -79,16 +79,16 @@ def lmd_ghost(store: Store, start_state: BeaconState, start_block: BeaconBlock) """ Execute the LMD-GHOST algorithm to find the head ``BeaconBlock``. """ - validators = start_state.validator_registry + validators = start_state.validators active_validator_indices = get_active_validator_indices(validators, slot_to_epoch(start_state.slot)) - attestation_targets = [(i, get_latest_attestation_target(store, i)) for i in active_validator_indices] + attestation_targets = [(i, get_attestation_target(store, i)) for i in active_validator_indices] # Use the rounded-balance-with-hysteresis supplied by the protocol for fork # choice voting. This reduces the number of recomputations that need to be # made for optimized implementations that precompute and save data def get_vote_count(block: BeaconBlock) -> int: return sum( - start_state.validator_registry[validator_index].effective_balance + start_state.validators[validator_index].effective_balance for validator_index, target in attestation_targets if get_ancestor(store, target, block.slot) == block ) diff --git a/specs/core/1_custody-game.md b/specs/core/1_custody-game.md index 6c89ef853..75ab36609 100644 --- a/specs/core/1_custody-game.md +++ b/specs/core/1_custody-game.md @@ -339,7 +339,7 @@ def process_custody_key_reveal(state: BeaconState, Note that this function mutates ``state``. """ - revealer = state.validator_registry[reveal.revealer_index] + revealer = state.validators[reveal.revealer_index] epoch_to_sign = get_randao_epoch_for_custody_period(revealer.next_custody_reveal_period, reveal.revealed_index) assert revealer.next_custody_reveal_period < get_validators_custody_reveal_period(state, reveal.revealed_index) @@ -389,8 +389,8 @@ def process_early_derived_secret_reveal(state: BeaconState, Note that this function mutates ``state``. """ - revealed_validator = state.validator_registry[reveal.revealed_index] - masker = state.validator_registry[reveal.masker_index] + revealed_validator = state.validators[reveal.revealed_index] + masker = state.validators[reveal.masker_index] derived_secret_location = reveal.epoch % EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS assert reveal.epoch >= get_current_epoch(state) + RANDAO_PENALTY_EPOCHS @@ -399,7 +399,7 @@ def process_early_derived_secret_reveal(state: BeaconState, assert reveal.revealed_index not in state.exposed_derived_secrets[derived_secret_location] # Verify signature correctness - masker = state.validator_registry[reveal.masker_index] + masker = state.validators[reveal.masker_index] pubkeys = [revealed_validator.pubkey, masker.pubkey] message_hashes = [ hash_tree_root(reveal.epoch), @@ -465,7 +465,7 @@ def process_chunk_challenge(state: BeaconState, validate_indexed_attestation(state, convert_to_indexed(state, challenge.attestation)) # Verify it is not too late to challenge assert slot_to_epoch(challenge.attestation.data.slot) >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY - responder = state.validator_registry[challenge.responder_index] + responder = state.validators[challenge.responder_index] assert responder.exit_epoch >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY # Verify the responder participated in the attestation attesters = get_attesting_indices(state, challenge.attestation.data, challenge.attestation.aggregation_bitfield) @@ -507,7 +507,7 @@ def process_bit_challenge(state: BeaconState, challenge: CustodyBitChallenge) -> None: # Verify challenge signature - challenger = state.validator_registry[challenge.challenger_index] + challenger = state.validators[challenge.challenger_index] assert bls_verify( pubkey=challenger.pubkey, message_hash=signing_root(challenge), @@ -520,7 +520,7 @@ def process_bit_challenge(state: BeaconState, attestation = challenge.attestation validate_indexed_attestation(state, convert_to_indexed(state, attestation)) # Verify the attestation is eligible for challenging - responder = state.validator_registry[challenge.responder_index] + responder = state.validators[challenge.responder_index] assert (slot_to_epoch(attestation.data.slot) + responder.max_reveal_lateness <= get_validators_custody_reveal_period(state, challenge.responder_index)) @@ -630,7 +630,7 @@ def process_bit_challenge_response(state: BeaconState, # Verify chunk index assert response.chunk_index < challenge.chunk_count # Verify responder has not been slashed - responder = state.validator_registry[challenge.responder_index] + responder = state.validators[challenge.responder_index] assert not responder.slashed # Verify the chunk matches the crosslink data root assert verify_merkle_branch( @@ -669,7 +669,7 @@ Run `process_reveal_deadlines(state)` immediately after `process_registry_update process_reveal_deadlines(state) # end insert @process_reveal_deadlines def process_reveal_deadlines(state: BeaconState) -> None: - for index, validator in enumerate(state.validator_registry): + for index, validator in enumerate(state.validators): deadline = validator.next_custody_reveal_period + (CUSTODY_RESPONSE_DEADLINE // EPOCHS_PER_CUSTODY_PERIOD) if get_validators_custody_reveal_period(state, index) > deadline: slash_validator(state, index) @@ -710,7 +710,7 @@ def after_process_final_updates(state: BeaconState) -> None: validator_indices_in_records = set( [record.challenger_index for record in records] + [record.responder_index for record in records] ) - for index, validator in enumerate(state.validator_registry): + for index, validator in enumerate(state.validators): if index not in validator_indices_in_records: if validator.exit_epoch != FAR_FUTURE_EPOCH and validator.withdrawable_epoch == FAR_FUTURE_EPOCH: validator.withdrawable_epoch = validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY diff --git a/specs/core/1_shard-data-chains.md b/specs/core/1_shard-data-chains.md index 21e08e7c9..a895b3c38 100644 --- a/specs/core/1_shard-data-chains.md +++ b/specs/core/1_shard-data-chains.md @@ -158,9 +158,9 @@ def get_persistent_committee(state: BeaconState, later_start_epoch = epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD committee_count = max( - len(get_active_validator_indices(state.validator_registry, earlier_start_epoch)) // + len(get_active_validator_indices(state.validators, earlier_start_epoch)) // (SHARD_COUNT * TARGET_COMMITTEE_SIZE), - len(get_active_validator_indices(state.validator_registry, later_start_epoch)) // + len(get_active_validator_indices(state.validators, later_start_epoch)) // (SHARD_COUNT * TARGET_COMMITTEE_SIZE), ) + 1 @@ -190,7 +190,7 @@ def get_shard_proposer_index(state: BeaconState, # Search for an active proposer for index in persistent_committee: - if is_active_validator(state.validator_registry[index], get_current_epoch(state)): + if is_active_validator(state.validators[index], get_current_epoch(state)): return index # No block can be proposed if no validator is active @@ -224,7 +224,7 @@ def verify_shard_attestation_signature(state: BeaconState, pubkeys = [] for i, index in enumerate(persistent_committee): if get_bitfield_bit(attestation.aggregation_bitfield, i) == 0b1: - validator = state.validator_registry[index] + validator = state.validators[index] assert is_active_validator(validator, get_current_epoch(state)) pubkeys.append(validator.pubkey) assert bls_verify( @@ -325,7 +325,7 @@ def is_valid_shard_block(beacon_blocks: List[BeaconBlock], proposer_index = get_shard_proposer_index(beacon_state, candidate.shard, candidate.slot) assert proposer_index is not None assert bls_verify( - pubkey=beacon_state.validator_registry[proposer_index].pubkey, + pubkey=beacon_state.validators[proposer_index].pubkey, message_hash=signing_root(block), signature=candidate.signature, domain=get_domain(beacon_state, slot_to_epoch(candidate.slot), DOMAIN_SHARD_PROPOSER), @@ -395,7 +395,7 @@ def is_valid_beacon_attestation(shard: Shard, assert candidate.data.previous_attestation.epoch < slot_to_epoch(candidate.data.slot) # Check crosslink data root - start_epoch = beacon_state.latest_crosslinks[shard].epoch + start_epoch = beacon_state.crosslinks[shard].epoch end_epoch = min(slot_to_epoch(candidate.data.slot) - CROSSLINK_LOOKBACK, start_epoch + MAX_EPOCHS_PER_CROSSLINK) blocks = [] for slot in range(start_epoch * SLOTS_PER_EPOCH, end_epoch * SLOTS_PER_EPOCH): diff --git a/specs/light_client/sync_protocol.md b/specs/light_client/sync_protocol.md index 8501c5869..eb4bf09eb 100644 --- a/specs/light_client/sync_protocol.md +++ b/specs/light_client/sync_protocol.md @@ -31,7 +31,7 @@ We define an "expansion" of an object as an object where a field in an object th We define two expansions: -* `ExtendedBeaconState`, which is identical to a `BeaconState` except `latest_active_index_roots: List[Bytes32]` is replaced by `latest_active_indices: List[List[ValidatorIndex]]`, where `BeaconState.latest_active_index_roots[i] = hash_tree_root(ExtendedBeaconState.latest_active_indices[i])`. +* `ExtendedBeaconState`, which is identical to a `BeaconState` except `active_index_roots: List[Bytes32]` is replaced by `active_indices: List[List[ValidatorIndex]]`, where `BeaconState.active_index_roots[i] = hash_tree_root(ExtendedBeaconState.active_indices[i])`. * `ExtendedBeaconBlock`, which is identical to a `BeaconBlock` except `state_root` is replaced with the corresponding `state: ExtendedBeaconState`. ### `get_active_validator_indices` @@ -40,10 +40,10 @@ Note that there is now a new way to compute `get_active_validator_indices`: ```python def get_active_validator_indices(state: ExtendedBeaconState, epoch: Epoch) -> List[ValidatorIndex]: - return state.latest_active_indices[epoch % LATEST_ACTIVE_INDEX_ROOTS_LENGTH] + return state.active_indices[epoch % ACTIVE_INDEX_ROOTS_LENGTH] ``` -Note that it takes `state` instead of `state.validator_registry` as an argument. This does not affect its use in `get_shuffled_committee`, because `get_shuffled_committee` has access to the full `state` as one of its arguments. +Note that it takes `state` instead of `state.validators` as an argument. This does not affect its use in `get_shuffled_committee`, because `get_shuffled_committee` has access to the full `state` as one of its arguments. ### `MerklePartial` @@ -85,7 +85,7 @@ def get_period_data(block: ExtendedBeaconBlock, shard_id: Shard, later: bool) -> return PeriodData( validator_count, generate_seed(block.state, period_start), - [block.state.validator_registry[i] for i in indices], + [block.state.validators[i] for i in indices], ) ``` diff --git a/specs/networking/rpc-interface.md b/specs/networking/rpc-interface.md index b81f78408..be154075c 100644 --- a/specs/networking/rpc-interface.md +++ b/specs/networking/rpc-interface.md @@ -95,8 +95,8 @@ Since some clients are waiting for `libp2p` implementations in their respective ( network_id: uint8 chain_id: uint64 - latest_finalized_root: bytes32 - latest_finalized_epoch: uint64 + finalized_root: bytes32 + finalized_epoch: uint64 best_root: bytes32 best_slot: uint64 ) @@ -107,7 +107,7 @@ Clients exchange `hello` messages upon connection, forming a two-phase handshake Clients SHOULD immediately disconnect from one another following the handshake above under the following conditions: 1. If `network_id` belongs to a different chain, since the client definitionally cannot sync with this client. -2. If the `latest_finalized_root` shared by the peer is not in the client's chain at the expected epoch. For example, if Peer 1 in the diagram below has `(root, epoch)` of `(A, 5)` and Peer 2 has `(B, 3)`, Peer 1 would disconnect because it knows that `B` is not the root in their chain at epoch 3: +2. If the `finalized_root` shared by the peer is not in the client's chain at the expected epoch. For example, if Peer 1 in the diagram below has `(root, epoch)` of `(A, 5)` and Peer 2 has `(B, 3)`, Peer 1 would disconnect because it knows that `B` is not the root in their chain at epoch 3: ``` Root A @@ -136,7 +136,7 @@ Root B ^ +---+ ``` -Once the handshake completes, the client with the higher `latest_finalized_epoch` or `best_slot` (if the clients have equal `latest_finalized_epoch`s) SHOULD request beacon block roots from its counterparty via `beacon_block_roots` (i.e. RPC method `10`). +Once the handshake completes, the client with the higher `finalized_epoch` or `best_slot` (if the clients have equal `finalized_epoch`s) SHOULD request beacon block roots from its counterparty via `beacon_block_roots` (i.e. RPC method `10`). ### Goodbye diff --git a/specs/validator/0_beacon-chain-validator.md b/specs/validator/0_beacon-chain-validator.md index 2f5aa4264..d062e1d3b 100644 --- a/specs/validator/0_beacon-chain-validator.md +++ b/specs/validator/0_beacon-chain-validator.md @@ -101,15 +101,15 @@ To submit a deposit: * Let `signature` be the result of `bls_sign` of the `signing_root(deposit_data)` with `domain=bls_domain(DOMAIN_DEPOSIT)`. (Deposits are valid regardless of fork version, `bls_domain` will default to zeroes there). * Send a transaction on the Ethereum 1.0 chain to `DEPOSIT_CONTRACT_ADDRESS` executing `def deposit(pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96])` along with a deposit of `amount` Gwei. -*Note*: Deposits made for the same `pubkey` are treated as for the same validator. A singular `Validator` will be added to `state.validator_registry` with each additional deposit amount added to the validator's balance. A validator can only be activated when total deposits for the validator pubkey meet or exceed `MAX_EFFECTIVE_BALANCE`. +*Note*: Deposits made for the same `pubkey` are treated as for the same validator. A singular `Validator` will be added to `state.validators` with each additional deposit amount added to the validator's balance. A validator can only be activated when total deposits for the validator pubkey meet or exceed `MAX_EFFECTIVE_BALANCE`. ### Process deposit -Deposits cannot be processed into the beacon chain until the Eth 1.0 block in which they were deposited or any of its descendants is added to the beacon chain `state.eth1_data`. This takes _a minimum_ of `ETH1_FOLLOW_DISTANCE` Eth 1.0 blocks (~4 hours) plus `ETH1_DATA_VOTING_PERIOD` epochs (~1.7 hours). Once the requisite Eth 1.0 data is added, the deposit will normally be added to a beacon chain block and processed into the `state.validator_registry` within an epoch or two. The validator is then in a queue to be activated. +Deposits cannot be processed into the beacon chain until the Eth 1.0 block in which they were deposited or any of its descendants is added to the beacon chain `state.eth1_data`. This takes _a minimum_ of `ETH1_FOLLOW_DISTANCE` Eth 1.0 blocks (~4 hours) plus `ETH1_DATA_VOTING_PERIOD` epochs (~1.7 hours). Once the requisite Eth 1.0 data is added, the deposit will normally be added to a beacon chain block and processed into the `state.validators` within an epoch or two. The validator is then in a queue to be activated. ### Validator index -Once a validator has been processed and added to the beacon state's `validator_registry`, the validator's `validator_index` is defined by the index into the registry at which the [`ValidatorRecord`](../core/0_beacon-chain.md#validator) contains the `pubkey` specified in the validator's deposit. A validator's `validator_index` is guaranteed to not change from the time of initial deposit until the validator exits and fully withdraws. This `validator_index` is used throughout the specification to dictate validator roles and responsibilities at any point and should be stored locally. +Once a validator has been processed and added to the beacon state's `validators`, the validator's `validator_index` is defined by the index into the registry at which the [`ValidatorRecord`](../core/0_beacon-chain.md#validator) contains the `pubkey` specified in the validator's deposit. A validator's `validator_index` is guaranteed to not change from the time of initial deposit until the validator exits and fully withdraws. This `validator_index` is used throughout the specification to dictate validator roles and responsibilities at any point and should be stored locally. ### Activation @@ -118,7 +118,7 @@ In normal operation, the validator is quickly activated at which point the valid The function [`is_active_validator`](../core/0_beacon-chain.md#is_active_validator) can be used to check if a validator is active during a given epoch. Usage is as follows: ```python -validator = state.validator_registry[validator_index] +validator = state.validators[validator_index] is_active = is_active_validator(validator, get_current_epoch(state)) ``` @@ -221,10 +221,10 @@ epoch_signature = bls_sign( ##### Eth1 Data -`block.eth1_data` is a mechanism used by block proposers vote on a recent Ethereum 1.0 block hash and an associated deposit root found in the Ethereum 1.0 deposit contract. When consensus is formed, `state.latest_eth1_data` is updated, and validator deposits up to this root can be processed. The deposit root can be calculated by calling the `get_deposit_root()` function of the deposit contract using the post-state of the block hash. +`block.eth1_data` is a mechanism used by block proposers vote on a recent Ethereum 1.0 block hash and an associated deposit root found in the Ethereum 1.0 deposit contract. When consensus is formed, `state.eth1_data` is updated, and validator deposits up to this root can be processed. The deposit root can be calculated by calling the `get_deposit_root()` function of the deposit contract using the post-state of the block hash. * Let `D` be the list of `Eth1DataVote` objects `vote` in `state.eth1_data_votes` where: - * `vote.eth1_data.block_hash` is the hash of an Eth 1.0 block that is (i) part of the canonical chain, (ii) >= `ETH1_FOLLOW_DISTANCE` blocks behind the head, and (iii) newer than `state.latest_eth1_data.block_hash`. + * `vote.eth1_data.block_hash` is the hash of an Eth 1.0 block that is (i) part of the canonical chain, (ii) >= `ETH1_FOLLOW_DISTANCE` blocks behind the head, and (iii) newer than `state.eth1_data.block_hash`. * `vote.eth1_data.deposit_count` is the deposit count of the Eth 1.0 deposit contract at the block defined by `vote.eth1_data.block_hash`. * `vote.eth1_data.deposit_root` is the deposit root of the Eth 1.0 deposit contract at the block defined by `vote.eth1_data.block_hash`. * If `D` is empty: @@ -267,9 +267,9 @@ Up to `MAX_ATTESTATIONS` aggregate attestations can be included in the `block`. ##### Deposits -If there are any unprocessed deposits for the existing `state.latest_eth1_data` (i.e. `state.latest_eth1_data.deposit_count > state.deposit_index`), then pending deposits _must_ be added to the block. The expected number of deposits is exactly `min(MAX_DEPOSITS, latest_eth1_data.deposit_count - state.deposit_index)`. These [`deposits`](../core/0_beacon-chain.md#deposit) are constructed from the `Deposit` logs from the [Eth 1.0 deposit contract](../core/0_deposit-contract) and must be processed in sequential order. The deposits included in the `block` must satisfy the verification conditions found in [deposits processing](../core/0_beacon-chain.md#deposits). +If there are any unprocessed deposits for the existing `state.eth1_data` (i.e. `state.eth1_data.deposit_count > state.eth1_deposit_index`), then pending deposits _must_ be added to the block. The expected number of deposits is exactly `min(MAX_DEPOSITS, eth1_data.deposit_count - state.eth1_deposit_index)`. These [`deposits`](../core/0_beacon-chain.md#deposit) are constructed from the `Deposit` logs from the [Eth 1.0 deposit contract](../core/0_deposit-contract) and must be processed in sequential order. The deposits included in the `block` must satisfy the verification conditions found in [deposits processing](../core/0_beacon-chain.md#deposits). -The `proof` for each deposit must be constructed against the deposit root contained in `state.latest_eth1_data` rather than the deposit root at the time the deposit was initially logged from the 1.0 chain. This entails storing a full deposit merkle tree locally and computing updated proofs against the `latest_eth1_data.deposit_root` as needed. See [`minimal_merkle.py`](https://github.com/ethereum/research/blob/master/spec_pythonizer/utils/merkle_minimal.py) for a sample implementation. +The `proof` for each deposit must be constructed against the deposit root contained in `state.eth1_data` rather than the deposit root at the time the deposit was initially logged from the 1.0 chain. This entails storing a full deposit merkle tree locally and computing updated proofs against the `eth1_data.deposit_root` as needed. See [`minimal_merkle.py`](https://github.com/ethereum/research/blob/master/spec_pythonizer/utils/merkle_minimal.py) for a sample implementation. ##### Voluntary exits diff --git a/test_libs/pyspec/eth2spec/test/helpers/block.py b/test_libs/pyspec/eth2spec/test/helpers/block.py index 5c7cb02a0..ebd20f1b5 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/block.py +++ b/test_libs/pyspec/eth2spec/test/helpers/block.py @@ -57,8 +57,8 @@ def build_empty_block(spec, state, slot=None, signed=False): slot = state.slot empty_block = spec.BeaconBlock() empty_block.slot = slot - empty_block.body.eth1_data.deposit_count = state.deposit_index - previous_block_header = deepcopy(state.latest_block_header) + empty_block.body.eth1_data.deposit_count = state.eth1_deposit_index + previous_block_header = deepcopy(state.block_header) if previous_block_header.state_root == spec.ZERO_HASH: previous_block_header.state_root = state.hash_tree_root() empty_block.parent_root = signing_root(previous_block_header) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index c85d265eb..e3bd839aa 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -58,7 +58,7 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c """ Prepare the state for the deposit, and create a deposit for the given validator, depositing the given amount. """ - pre_validator_count = len(state.validator_registry) + pre_validator_count = len(state.validators) # fill previous deposits with zero-hash deposit_data_leaves = [spec.ZERO_HASH] * pre_validator_count @@ -80,6 +80,6 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c signed ) - state.latest_eth1_data.deposit_root = root - state.latest_eth1_data.deposit_count = len(deposit_data_leaves) + state.eth1_data.deposit_root = root + state.eth1_data.deposit_count = len(deposit_data_leaves) return deposit diff --git a/test_libs/pyspec/eth2spec/test/helpers/genesis.py b/test_libs/pyspec/eth2spec/test/helpers/genesis.py index 83af56621..241706957 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/genesis.py +++ b/test_libs/pyspec/eth2spec/test/helpers/genesis.py @@ -22,8 +22,8 @@ def create_genesis_state(spec, num_validators): state = spec.BeaconState( genesis_time=0, - deposit_index=num_validators, - latest_eth1_data=spec.Eth1Data( + eth1_deposit_index=num_validators, + eth1_data=spec.Eth1Data( deposit_root=deposit_root, deposit_count=num_validators, block_hash=spec.ZERO_HASH, @@ -32,16 +32,16 @@ def create_genesis_state(spec, num_validators): # We "hack" in the initial validators, # as it is much faster than creating and processing genesis deposits for every single test case. state.balances = [spec.MAX_EFFECTIVE_BALANCE] * num_validators - state.validator_registry = [build_mock_validator(spec, i, state.balances[i]) for i in range(num_validators)] + state.validators = [build_mock_validator(spec, i, state.balances[i]) for i in range(num_validators)] # Process genesis activations - for validator in state.validator_registry: + for validator in state.validators: if validator.effective_balance >= spec.MAX_EFFECTIVE_BALANCE: validator.activation_eligibility_epoch = spec.GENESIS_EPOCH validator.activation_epoch = spec.GENESIS_EPOCH genesis_active_index_root = hash_tree_root(spec.get_active_validator_indices(state, spec.GENESIS_EPOCH)) - for index in range(spec.LATEST_ACTIVE_INDEX_ROOTS_LENGTH): - state.latest_active_index_roots[index] = genesis_active_index_root + for index in range(spec.ACTIVE_INDEX_ROOTS_LENGTH): + state.active_index_roots[index] = genesis_active_index_root return state diff --git a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py index 86c6acf47..d5b7f7b7f 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py +++ b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py @@ -7,7 +7,7 @@ from eth2spec.test.helpers.keys import pubkey_to_privkey def get_valid_proposer_slashing(spec, state, signed_1=False, signed_2=False): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] slot = state.slot header_1 = spec.BeaconBlockHeader( diff --git a/test_libs/pyspec/eth2spec/test/helpers/state.py b/test_libs/pyspec/eth2spec/test/helpers/state.py index 63aa27d70..19a04b04f 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/state.py +++ b/test_libs/pyspec/eth2spec/test/helpers/state.py @@ -22,4 +22,4 @@ def get_state_root(spec, state, slot) -> bytes: Return the state root at a recent ``slot``. """ assert slot < state.slot <= slot + spec.SLOTS_PER_HISTORICAL_ROOT - return state.latest_state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT] + return state.state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT] diff --git a/test_libs/pyspec/eth2spec/test/helpers/transfers.py b/test_libs/pyspec/eth2spec/test/helpers/transfers.py index e619c5569..f7397ca38 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/transfers.py +++ b/test_libs/pyspec/eth2spec/test/helpers/transfers.py @@ -31,7 +31,7 @@ def get_valid_transfer(spec, state, slot=None, sender_index=None, amount=None, f sign_transfer(spec, state, transfer, transfer_privkey) # ensure withdrawal_credentials reproducible - state.validator_registry[transfer.sender].withdrawal_credentials = ( + state.validators[transfer.sender].withdrawal_credentials = ( spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(transfer.pubkey)[1:] ) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py index 2b34ab405..6cd2d1194 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py @@ -31,17 +31,17 @@ def run_attestation_processing(spec, state, attestation, valid=True): yield 'post', None return - current_epoch_count = len(state.current_epoch_attestations) - previous_epoch_count = len(state.previous_epoch_attestations) + current_epoch_count = len(state.current_attestations) + previous_epoch_count = len(state.previous_attestations) # process attestation spec.process_attestation(state, attestation) # Make sure the attestation has been processed if attestation.data.target_epoch == spec.get_current_epoch(state): - assert len(state.current_epoch_attestations) == current_epoch_count + 1 + assert len(state.current_attestations) == current_epoch_count + 1 else: - assert len(state.previous_epoch_attestations) == previous_epoch_count + 1 + assert len(state.previous_attestations) == previous_epoch_count + 1 # yield post-state yield 'post', state diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py index 6c7637d59..c51f5a8a9 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py @@ -34,7 +34,7 @@ def run_attester_slashing_processing(spec, state, attester_slashing, valid=True) # Process slashing spec.process_attester_slashing(state, attester_slashing) - slashed_validator = state.validator_registry[slashed_index] + slashed_validator = state.validators[slashed_index] # Check slashing assert slashed_validator.slashed @@ -135,7 +135,7 @@ def test_participants_already_slashed(spec, state): attestation_1 = attester_slashing.attestation_1 validator_indices = attestation_1.custody_bit_0_indices + attestation_1.custody_bit_1_indices for index in validator_indices: - state.validator_registry[index].slashed = True + state.validators[index].slashed = True yield from run_attester_slashing_processing(spec, state, attester_slashing, False) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_block_header.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_block_header.py index f3c017982..a2306ef4d 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_block_header.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_block_header.py @@ -78,7 +78,7 @@ def test_proposer_slashed(spec, state): proposer_index = spec.get_beacon_proposer_index(stub_state) # set proposer to slashed - state.validator_registry[proposer_index].slashed = True + state.validators[proposer_index].slashed = True block = build_empty_block_for_next_slot(spec, state, signed=True) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py index c50b11f2e..7efb57871 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py @@ -16,7 +16,7 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef - post-state ('post'). If ``valid == False``, run expecting ``AssertionError`` """ - pre_validator_count = len(state.validator_registry) + pre_validator_count = len(state.validators) pre_balance = 0 if validator_index < pre_validator_count: pre_balance = get_balance(state, validator_index) @@ -34,29 +34,29 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef yield 'post', state if not effective: - assert len(state.validator_registry) == pre_validator_count + assert len(state.validators) == pre_validator_count assert len(state.balances) == pre_validator_count if validator_index < pre_validator_count: assert get_balance(state, validator_index) == pre_balance else: if validator_index < pre_validator_count: # top-up - assert len(state.validator_registry) == pre_validator_count + assert len(state.validators) == pre_validator_count assert len(state.balances) == pre_validator_count else: # new validator - assert len(state.validator_registry) == pre_validator_count + 1 + assert len(state.validators) == pre_validator_count + 1 assert len(state.balances) == pre_validator_count + 1 assert get_balance(state, validator_index) == pre_balance + deposit.data.amount - assert state.deposit_index == state.latest_eth1_data.deposit_count + assert state.eth1_deposit_index == state.eth1_data.deposit_count @with_all_phases @spec_state_test def test_new_deposit(spec, state): # fresh deposit = next validator index = validator appended to registry - validator_index = len(state.validator_registry) + validator_index = len(state.validators) amount = spec.MAX_EFFECTIVE_BALANCE deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) @@ -68,7 +68,7 @@ def test_new_deposit(spec, state): @spec_state_test def test_invalid_sig_new_deposit(spec, state): # fresh deposit = next validator index = validator appended to registry - validator_index = len(state.validator_registry) + validator_index = len(state.validators) amount = spec.MAX_EFFECTIVE_BALANCE deposit = prepare_state_and_deposit(spec, state, validator_index, amount) yield from run_deposit_processing(spec, state, deposit, validator_index, valid=True, effective=False) @@ -117,12 +117,12 @@ def test_invalid_withdrawal_credentials_top_up(spec, state): @with_all_phases @spec_state_test def test_wrong_index(spec, state): - validator_index = len(state.validator_registry) + validator_index = len(state.validators) amount = spec.MAX_EFFECTIVE_BALANCE deposit = prepare_state_and_deposit(spec, state, validator_index, amount) - # mess up deposit_index - deposit.index = state.deposit_index + 1 + # mess up eth1_deposit_index + deposit.index = state.eth1_deposit_index + 1 sign_deposit_data(spec, state, deposit.data, privkeys[validator_index]) @@ -132,7 +132,7 @@ def test_wrong_index(spec, state): @with_all_phases @spec_state_test def test_wrong_deposit_for_deposit_count(spec, state): - deposit_data_leaves = [spec.ZERO_HASH] * len(state.validator_registry) + deposit_data_leaves = [spec.ZERO_HASH] * len(state.validators) # build root for deposit_1 index_1 = len(deposit_data_leaves) @@ -166,8 +166,8 @@ def test_wrong_deposit_for_deposit_count(spec, state): ) # state has root for deposit_2 but is at deposit_count for deposit_1 - state.latest_eth1_data.deposit_root = root_2 - state.latest_eth1_data.deposit_count = deposit_count_1 + state.eth1_data.deposit_root = root_2 + state.eth1_data.deposit_count = deposit_count_1 yield from run_deposit_processing(spec, state, deposit_2, index_2, valid=False) @@ -178,7 +178,7 @@ def test_wrong_deposit_for_deposit_count(spec, state): @with_all_phases @spec_state_test def test_bad_merkle_proof(spec, state): - validator_index = len(state.validator_registry) + validator_index = len(state.validators) amount = spec.MAX_EFFECTIVE_BALANCE deposit = prepare_state_and_deposit(spec, state, validator_index, amount) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py index b35241859..af34ea709 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py @@ -28,7 +28,7 @@ def run_proposer_slashing_processing(spec, state, proposer_slashing, valid=True) yield 'post', state # check if slashed - slashed_validator = state.validator_registry[proposer_slashing.proposer_index] + slashed_validator = state.validators[proposer_slashing.proposer_index] assert slashed_validator.slashed assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH @@ -77,7 +77,7 @@ def test_invalid_sig_1_and_2(spec, state): def test_invalid_proposer_index(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=True) # Index just too high (by 1) - proposer_slashing.proposer_index = len(state.validator_registry) + proposer_slashing.proposer_index = len(state.validators) yield from run_proposer_slashing_processing(spec, state, proposer_slashing, False) @@ -111,7 +111,7 @@ def test_proposer_is_not_activated(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=True) # set proposer to be not active yet - state.validator_registry[proposer_slashing.proposer_index].activation_epoch = spec.get_current_epoch(state) + 1 + state.validators[proposer_slashing.proposer_index].activation_epoch = spec.get_current_epoch(state) + 1 yield from run_proposer_slashing_processing(spec, state, proposer_slashing, False) @@ -122,7 +122,7 @@ def test_proposer_is_slashed(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=True) # set proposer to slashed - state.validator_registry[proposer_slashing.proposer_index].slashed = True + state.validators[proposer_slashing.proposer_index].slashed = True yield from run_proposer_slashing_processing(spec, state, proposer_slashing, False) @@ -137,6 +137,6 @@ def test_proposer_is_withdrawn(spec, state): # set proposer withdrawable_epoch in past current_epoch = spec.get_current_epoch(state) proposer_index = proposer_slashing.proposer_index - state.validator_registry[proposer_index].withdrawable_epoch = current_epoch - 1 + state.validators[proposer_index].withdrawable_epoch = current_epoch - 1 yield from run_proposer_slashing_processing(spec, state, proposer_slashing, False) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py index 1294ca84a..e9d282b3a 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py @@ -41,7 +41,7 @@ def run_transfer_processing(spec, state, transfer, valid=True): def test_success_non_activated(spec, state): transfer = get_valid_transfer(spec, state, signed=True) # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer) @@ -55,7 +55,7 @@ def test_success_withdrawable(spec, state): transfer = get_valid_transfer(spec, state, signed=True) # withdrawable_epoch in past so can transfer - state.validator_registry[transfer.sender].withdrawable_epoch = spec.get_current_epoch(state) - 1 + state.validators[transfer.sender].withdrawable_epoch = spec.get_current_epoch(state) - 1 yield from run_transfer_processing(spec, state, transfer) @@ -86,7 +86,7 @@ def test_success_active_above_max_effective_fee(spec, state): def test_invalid_signature(spec, state): transfer = get_valid_transfer(spec, state) # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) @@ -107,7 +107,7 @@ def test_active_but_transfer_past_effective_balance(spec, state): def test_incorrect_slot(spec, state): transfer = get_valid_transfer(spec, state, slot=state.slot + 1, signed=True) # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) @@ -120,7 +120,7 @@ def test_insufficient_balance_for_fee(spec, state): transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=0, fee=1, signed=True) # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) @@ -133,7 +133,7 @@ def test_insufficient_balance(spec, state): transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0, signed=True) # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) @@ -153,7 +153,7 @@ def test_no_dust_sender(spec, state): ) # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) @@ -167,7 +167,7 @@ def test_no_dust_recipient(spec, state): state.balances[transfer.recipient] = 0 # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) @@ -176,9 +176,9 @@ def test_no_dust_recipient(spec, state): @spec_state_test def test_invalid_pubkey(spec, state): transfer = get_valid_transfer(spec, state, signed=True) - state.validator_registry[transfer.sender].withdrawal_credentials = spec.ZERO_HASH + state.validators[transfer.sender].withdrawal_credentials = spec.ZERO_HASH # un-activate so validator can transfer - state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH yield from run_transfer_processing(spec, state, transfer, False) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py index 3359c5e78..33cacc4e2 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py @@ -21,14 +21,14 @@ def run_voluntary_exit_processing(spec, state, voluntary_exit, valid=True): yield 'post', None return - pre_exit_epoch = state.validator_registry[validator_index].exit_epoch + pre_exit_epoch = state.validators[validator_index].exit_epoch spec.process_voluntary_exit(state, voluntary_exit) yield 'post', state assert pre_exit_epoch == spec.FAR_FUTURE_EPOCH - assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH + assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH @with_all_phases @@ -39,7 +39,7 @@ def test_success(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit(spec, state, current_epoch, validator_index, privkey, signed=True) @@ -55,7 +55,7 @@ def test_invalid_signature(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit(spec, state, current_epoch, validator_index, privkey) @@ -76,7 +76,7 @@ def test_success_exit_queue(spec, state): # Prepare a bunch of exits, based on the current state exit_queue = [] for index in initial_indices: - privkey = pubkey_to_privkey[state.validator_registry[index].pubkey] + privkey = pubkey_to_privkey[state.validators[index].pubkey] exit_queue.append(build_voluntary_exit( spec, state, @@ -94,7 +94,7 @@ def test_success_exit_queue(spec, state): # exit an additional validator validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit( spec, state, @@ -109,8 +109,8 @@ def test_success_exit_queue(spec, state): yield from run_voluntary_exit_processing(spec, state, voluntary_exit) assert ( - state.validator_registry[validator_index].exit_epoch == - state.validator_registry[initial_indices[0]].exit_epoch + 1 + state.validators[validator_index].exit_epoch == + state.validators[initial_indices[0]].exit_epoch + 1 ) @@ -122,7 +122,7 @@ def test_validator_exit_in_future(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit( spec, @@ -146,7 +146,7 @@ def test_validator_invalid_validator_index(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit( spec, @@ -156,7 +156,7 @@ def test_validator_invalid_validator_index(spec, state): privkey, signed=False, ) - voluntary_exit.validator_index = len(state.validator_registry) + voluntary_exit.validator_index = len(state.validators) sign_voluntary_exit(spec, state, voluntary_exit, privkey) yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) @@ -167,9 +167,9 @@ def test_validator_invalid_validator_index(spec, state): def test_validator_not_active(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] - state.validator_registry[validator_index].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[validator_index].activation_epoch = spec.FAR_FUTURE_EPOCH # build and test voluntary exit voluntary_exit = build_voluntary_exit( @@ -192,10 +192,10 @@ def test_validator_already_exited(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] # but validator already has exited - state.validator_registry[validator_index].exit_epoch = current_epoch + 2 + state.validators[validator_index].exit_epoch = current_epoch + 2 voluntary_exit = build_voluntary_exit( spec, @@ -214,7 +214,7 @@ def test_validator_already_exited(spec, state): def test_validator_not_active_long_enough(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[0] - privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit( spec, @@ -226,7 +226,7 @@ def test_validator_not_active_long_enough(spec, state): ) assert ( - current_epoch - state.validator_registry[validator_index].activation_epoch < + current_epoch - state.validators[validator_index].activation_epoch < spec.PERSISTENT_COMMITTEE_PERIOD ) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py index 65d958678..cd4532457 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py @@ -56,7 +56,7 @@ def test_single_crosslink_update_from_current_epoch(spec, state): fill_aggregate_attestation(spec, state, attestation) add_attestation_to_state(spec, state, attestation, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) - assert len(state.current_epoch_attestations) == 1 + assert len(state.current_attestations) == 1 shard = attestation.data.crosslink.shard pre_crosslink = deepcopy(state.current_crosslinks[shard]) @@ -77,7 +77,7 @@ def test_single_crosslink_update_from_previous_epoch(spec, state): fill_aggregate_attestation(spec, state, attestation) add_attestation_to_state(spec, state, attestation, state.slot + spec.SLOTS_PER_EPOCH) - assert len(state.previous_epoch_attestations) == 1 + assert len(state.previous_attestations) == 1 shard = attestation.data.crosslink.shard pre_crosslink = deepcopy(state.current_crosslinks[shard]) @@ -130,8 +130,8 @@ def test_double_late_crosslink(spec, state): next_epoch(spec, state) add_attestation_to_state(spec, state, attestation_2, state.slot + 1) - assert len(state.previous_epoch_attestations) == 1 - assert len(state.current_epoch_attestations) == 0 + assert len(state.previous_attestations) == 1 + assert len(state.current_attestations) == 0 crosslink_deltas = spec.get_crosslink_deltas(state) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_registry_updates.py b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_registry_updates.py index e6679f844..6863af3b1 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_registry_updates.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_registry_updates.py @@ -35,23 +35,23 @@ def run_process_registry_updates(spec, state, valid=True): @spec_state_test def test_activation(spec, state): index = 0 - assert spec.is_active_validator(state.validator_registry[index], spec.get_current_epoch(state)) + assert spec.is_active_validator(state.validators[index], spec.get_current_epoch(state)) # Mock a new deposit - state.validator_registry[index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH - state.validator_registry[index].activation_epoch = spec.FAR_FUTURE_EPOCH - state.validator_registry[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE - assert not spec.is_active_validator(state.validator_registry[index], spec.get_current_epoch(state)) + state.validators[index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH + state.validators[index].activation_epoch = spec.FAR_FUTURE_EPOCH + state.validators[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE + assert not spec.is_active_validator(state.validators[index], spec.get_current_epoch(state)) for _ in range(spec.ACTIVATION_EXIT_DELAY + 1): next_epoch(spec, state) yield from run_process_registry_updates(spec, state) - assert state.validator_registry[index].activation_eligibility_epoch != spec.FAR_FUTURE_EPOCH - assert state.validator_registry[index].activation_epoch != spec.FAR_FUTURE_EPOCH + assert state.validators[index].activation_eligibility_epoch != spec.FAR_FUTURE_EPOCH + assert state.validators[index].activation_epoch != spec.FAR_FUTURE_EPOCH assert spec.is_active_validator( - state.validator_registry[index], + state.validators[index], spec.get_current_epoch(state), ) @@ -60,19 +60,19 @@ def test_activation(spec, state): @spec_state_test def test_ejection(spec, state): index = 0 - assert spec.is_active_validator(state.validator_registry[index], spec.get_current_epoch(state)) - assert state.validator_registry[index].exit_epoch == spec.FAR_FUTURE_EPOCH + assert spec.is_active_validator(state.validators[index], spec.get_current_epoch(state)) + assert state.validators[index].exit_epoch == spec.FAR_FUTURE_EPOCH # Mock an ejection - state.validator_registry[index].effective_balance = spec.EJECTION_BALANCE + state.validators[index].effective_balance = spec.EJECTION_BALANCE for _ in range(spec.ACTIVATION_EXIT_DELAY + 1): next_epoch(spec, state) yield from run_process_registry_updates(spec, state) - assert state.validator_registry[index].exit_epoch != spec.FAR_FUTURE_EPOCH + assert state.validators[index].exit_epoch != spec.FAR_FUTURE_EPOCH assert not spec.is_active_validator( - state.validator_registry[index], + state.validators[index], spec.get_current_epoch(state), ) diff --git a/test_libs/pyspec/eth2spec/test/phase_1/block_processing/test_process_early_derived_secret_reveal.py b/test_libs/pyspec/eth2spec/test/phase_1/block_processing/test_process_early_derived_secret_reveal.py index 110231d77..87297d443 100644 --- a/test_libs/pyspec/eth2spec/test/phase_1/block_processing/test_process_early_derived_secret_reveal.py +++ b/test_libs/pyspec/eth2spec/test/phase_1/block_processing/test_process_early_derived_secret_reveal.py @@ -24,7 +24,7 @@ def run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, v spec.process_early_derived_secret_reveal(state, randao_key_reveal) - slashed_validator = state.validator_registry[randao_key_reveal.revealed_index] + slashed_validator = state.validators[randao_key_reveal.revealed_index] if randao_key_reveal.epoch >= spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING: assert slashed_validator.slashed @@ -111,7 +111,7 @@ def test_double_reveal(spec, state): @spec_state_test def test_revealer_is_slashed(spec, state): randao_key_reveal = get_valid_early_derived_secret_reveal(spec, state, spec.get_current_epoch(state)) - state.validator_registry[randao_key_reveal.revealed_index].slashed = True + state.validators[randao_key_reveal.revealed_index].slashed = True yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, False) diff --git a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py index 587c37742..a5079b901 100644 --- a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py @@ -91,7 +91,7 @@ def test_empty_epoch_transition(spec, state): # assert state.slot == block.slot # assert state.finalized_epoch < spec.get_current_epoch(state) - 4 -# for index in range(len(state.validator_registry)): +# for index in range(len(state.validators)): # assert get_balance(state, index) < get_balance(pre_state, index) @@ -103,7 +103,7 @@ def test_proposer_slashing(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=True) validator_index = proposer_slashing.proposer_index - assert not state.validator_registry[validator_index].slashed + assert not state.validators[validator_index].slashed yield 'pre', state @@ -119,7 +119,7 @@ def test_proposer_slashing(spec, state): yield 'post', state # check if slashed - slashed_validator = state.validator_registry[validator_index] + slashed_validator = state.validators[validator_index] assert slashed_validator.slashed assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH @@ -137,7 +137,7 @@ def test_attester_slashing(spec, state): validator_index = (attester_slashing.attestation_1.custody_bit_0_indices + attester_slashing.attestation_1.custody_bit_1_indices)[0] - assert not state.validator_registry[validator_index].slashed + assert not state.validators[validator_index].slashed yield 'pre', state @@ -152,7 +152,7 @@ def test_attester_slashing(spec, state): spec.state_transition(state, block) yield 'post', state - slashed_validator = state.validator_registry[validator_index] + slashed_validator = state.validators[validator_index] assert slashed_validator.slashed assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH @@ -172,10 +172,10 @@ def test_attester_slashing(spec, state): @with_all_phases @spec_state_test def test_deposit_in_block(spec, state): - initial_registry_len = len(state.validator_registry) + initial_registry_len = len(state.validators) initial_balances_len = len(state.balances) - validator_index = len(state.validator_registry) + validator_index = len(state.validators) amount = spec.MAX_EFFECTIVE_BALANCE deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) @@ -190,10 +190,10 @@ def test_deposit_in_block(spec, state): spec.state_transition(state, block) yield 'post', state - assert len(state.validator_registry) == initial_registry_len + 1 + assert len(state.validators) == initial_registry_len + 1 assert len(state.balances) == initial_balances_len + 1 assert get_balance(state, validator_index) == spec.MAX_EFFECTIVE_BALANCE - assert state.validator_registry[validator_index].pubkey == pubkeys[validator_index] + assert state.validators[validator_index].pubkey == pubkeys[validator_index] @with_all_phases @@ -203,7 +203,7 @@ def test_deposit_top_up(spec, state): amount = spec.MAX_EFFECTIVE_BALANCE // 4 deposit = prepare_state_and_deposit(spec, state, validator_index, amount) - initial_registry_len = len(state.validator_registry) + initial_registry_len = len(state.validators) initial_balances_len = len(state.balances) validator_pre_balance = get_balance(state, validator_index) @@ -218,7 +218,7 @@ def test_deposit_top_up(spec, state): spec.state_transition(state, block) yield 'post', state - assert len(state.validator_registry) == initial_registry_len + assert len(state.validators) == initial_registry_len assert len(state.balances) == initial_balances_len assert get_balance(state, validator_index) == validator_pre_balance + amount @@ -233,17 +233,17 @@ def test_attestation(spec, state): attestation = get_valid_attestation(spec, state, signed=True) # Add to state via block transition - pre_current_attestations_len = len(state.current_epoch_attestations) + pre_current_attestations_len = len(state.current_attestations) attestation_block = build_empty_block_for_next_slot(spec, state) attestation_block.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY attestation_block.body.attestations.append(attestation) sign_block(spec, state, attestation_block) spec.state_transition(state, attestation_block) - assert len(state.current_epoch_attestations) == pre_current_attestations_len + 1 + assert len(state.current_attestations) == pre_current_attestations_len + 1 - # Epoch transition should move to previous_epoch_attestations - pre_current_attestations_root = spec.hash_tree_root(state.current_epoch_attestations) + # Epoch transition should move to previous_attestations + pre_current_attestations_root = spec.hash_tree_root(state.current_attestations) epoch_block = build_empty_block_for_next_slot(spec, state) epoch_block.slot += spec.SLOTS_PER_EPOCH @@ -253,8 +253,8 @@ def test_attestation(spec, state): yield 'blocks', [attestation_block, epoch_block], List[spec.BeaconBlock] yield 'post', state - assert len(state.current_epoch_attestations) == 0 - assert spec.hash_tree_root(state.previous_epoch_attestations) == pre_current_attestations_root + assert len(state.current_attestations) == 0 + assert spec.hash_tree_root(state.previous_attestations) == pre_current_attestations_root @with_all_phases @@ -289,7 +289,7 @@ def test_voluntary_exit(spec, state): sign_block(spec, state, initiate_exit_block) spec.state_transition(state, initiate_exit_block) - assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH + assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH # Process within epoch transition exit_block = build_empty_block_for_next_slot(spec, state) @@ -300,7 +300,7 @@ def test_voluntary_exit(spec, state): yield 'blocks', [initiate_exit_block, exit_block], List[spec.BeaconBlock] yield 'post', state - assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH + assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH @with_all_phases @@ -317,7 +317,7 @@ def test_transfer(spec, state): pre_transfer_recipient_balance = get_balance(state, recipient_index) # un-activate so validator can transfer - state.validator_registry[sender_index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH + state.validators[sender_index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH yield 'pre', state @@ -343,10 +343,10 @@ def test_balance_driven_status_transitions(spec, state): current_epoch = spec.get_current_epoch(state) validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] - assert state.validator_registry[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH + assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH # set validator balance to below ejection threshold - state.validator_registry[validator_index].effective_balance = spec.EJECTION_BALANCE + state.validators[validator_index].effective_balance = spec.EJECTION_BALANCE yield 'pre', state @@ -359,7 +359,7 @@ def test_balance_driven_status_transitions(spec, state): yield 'blocks', [block], List[spec.BeaconBlock] yield 'post', state - assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH + assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH @with_all_phases From 9bb0f25f18198a5e02b3290f5d5eee025d8c68a1 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 10 Jun 2019 13:41:28 +0100 Subject: [PATCH 09/39] Update specs/core/0_beacon-chain.md Co-Authored-By: NIC Lin --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 8cacde746..543f20d59 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1166,7 +1166,7 @@ Whenever the deposit contract emits a `Deposit` log call the function `is_genesi When `is_genesis_trigger(deposits, timestamp) is True` for the first time let: * `genesis_deposits = deposits` -* `genesis_time = timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY` where `SECONDS_PER_DAY = 86400` +* `genesis_time = timestamp - timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY` where `SECONDS_PER_DAY = 86400` * `genesis_eth1_data` be the object of type `Eth1Data` where: * `genesis_eth1_data.block_hash` is the block hash for the last deposit in `deposits` * `genesis_eth1_data.deposit_root` is the deposit root for the last deposit in `deposits` From 4d6a25f16101782ff41ac4dbf62c8c321e5d8dbe Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 10 Jun 2019 13:43:00 +0100 Subject: [PATCH 10/39] Update conftest.py --- deposit_contract/tests/contracts/conftest.py | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/deposit_contract/tests/contracts/conftest.py b/deposit_contract/tests/contracts/conftest.py index b8b488abe..d4c7da9aa 100644 --- a/deposit_contract/tests/contracts/conftest.py +++ b/deposit_contract/tests/contracts/conftest.py @@ -62,26 +62,6 @@ def registration_contract(w3, tester): return registration_deployed -@pytest.fixture(params=[0, 1, 2]) -def modified_registration_contract( - request, - w3, - tester): - registration_code = get_deposit_contract_code() - contract_bytecode = compiler.compile_code(registration_code)['bytecode'] - contract_abi = compiler.mk_full_signature(registration_code) - registration = w3.eth.contract( - abi=contract_abi, - bytecode=contract_bytecode) - tx_hash = registration.constructor().transact() - tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash) - registration_deployed = w3.eth.contract( - address=tx_receipt.contractAddress, - abi=contract_abi - ) - return registration_deployed - - @pytest.fixture def assert_tx_failed(tester): def assert_tx_failed(function_to_test, exception=eth_tester.exceptions.TransactionFailed): From ef91ee5698ce5a82fa47c62efa29cb8890df0216 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Mon, 10 Jun 2019 15:55:08 +0100 Subject: [PATCH 11/39] Address Danny's comments --- deposit_contract/contracts/validator_registration.v.py | 8 +++++--- specs/core/0_beacon-chain.md | 4 ++-- specs/core/0_deposit-contract.md | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index b99a30784..8510380ee 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -1,5 +1,6 @@ MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32 +MAX_DEPOSIT_COUNT: constant(uint256) = 4294967295 # 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1 PUBKEY_LENGTH: constant(uint256) = 48 # bytes WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes AMOUNT_LENGTH: constant(uint256) = 8 # bytes @@ -27,7 +28,8 @@ def __init__(): @constant def to_little_endian_64(value: uint256) -> bytes[8]: # Reversing bytes using bitwise uint256 manipulations - # (array accesses of bytes[] are not currently supported in Vyper) + # Note: array accesses of bytes[] are not currently supported in Vyper + # Note: this function is only called when `value < 2**64` y: uint256 = 0 x: uint256 = value for _ in range(8): @@ -62,8 +64,8 @@ def get_deposit_count() -> bytes[8]: def deposit(pubkey: bytes[PUBKEY_LENGTH], withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH], signature: bytes[SIGNATURE_LENGTH]): - # Avoid overflowing the Merkle tree - assert self.deposit_count < 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1 + # Avoid overflowing the Merkle tree (and prevent edge case in computing `self.branch`) + assert self.deposit_count < MAX_DEPOSIT_COUNT # Validate deposit data deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 8cacde746..fa15bc5dc 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1158,7 +1158,7 @@ def slash_validator(state: BeaconState, ### Genesis trigger -Whenever the deposit contract emits a `Deposit` log call the function `is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool` where: +Before genesis has been triggered and whenever the deposit contract emits a `Deposit`, log call the function `is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool` where: * `deposits` is the list of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log * `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log @@ -1168,7 +1168,7 @@ When `is_genesis_trigger(deposits, timestamp) is True` for the first time let: * `genesis_deposits = deposits` * `genesis_time = timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY` where `SECONDS_PER_DAY = 86400` * `genesis_eth1_data` be the object of type `Eth1Data` where: - * `genesis_eth1_data.block_hash` is the block hash for the last deposit in `deposits` + * `genesis_eth1_data.block_hash` is the Ethereum 1.0 block hash that emitted to log for the last deposit in `deposits` * `genesis_eth1_data.deposit_root` is the deposit root for the last deposit in `deposits` * `genesis_eth1_data.deposit_count = len(genesis_deposits)` diff --git a/specs/core/0_deposit-contract.md b/specs/core/0_deposit-contract.md index 50f52fd37..338ece487 100644 --- a/specs/core/0_deposit-contract.md +++ b/specs/core/0_deposit-contract.md @@ -42,7 +42,7 @@ The deposit contract has a public `deposit` function to make deposits. It takes #### Deposit amount -The ETH sent to the deposit contract, i.e. the deposit amount, is burnt on Ethereum 1.0. The minimum deposit amount is `MIN_DEPOSIT_AMOUNT` Gwei. +The amount of ETH (rounded down to the closest Gwei) sent to the deposit contract is the deposit amount, which must be of size at least `MIN_DEPOSIT_AMOUNT` Gwei. Note that ETH consumed by the deposit contract is no longer usable on Ethereum 1.0. #### Withdrawal credentials From dc56d87eef3a367783e57723cf8be060f3de7926 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Mon, 10 Jun 2019 21:16:51 +0100 Subject: [PATCH 12/39] Revert a couple of renamings --- specs/core/0_beacon-chain.md | 28 +++++++++---------- specs/test_formats/operations/README.md | 6 ++-- test_generators/operations/main.py | 4 +-- .../pyspec/eth2spec/test/helpers/block.py | 2 +- .../test/helpers/proposer_slashings.py | 2 +- .../test_process_attestation.py | 8 +++--- .../test_process_proposer_slashing.py | 2 +- .../test_process_crosslinks.py | 8 +++--- .../eth2spec/test/sanity/test_blocks.py | 12 ++++---- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 9fd38d7db..013d002c9 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -493,7 +493,7 @@ class BeaconState(Container): slot: Slot fork: Fork # History - block_header: BeaconBlockHeader + parent_block_header: BeaconBlockHeader block_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] state_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] historical_roots: List[Hash] @@ -511,8 +511,8 @@ class BeaconState(Container): # Slashings slashed_balances: Vector[Gwei, SLASHED_EXIT_LENGTH] # Attestations - previous_attestations: List[PendingAttestation] - current_attestations: List[PendingAttestation] + previous_epoch_attestations: List[PendingAttestation] + current_epoch_attestations: List[PendingAttestation] # Crosslinks previous_crosslinks: Vector[Crosslink, SHARD_COUNT] current_crosslinks: Vector[Crosslink, SHARD_COUNT] @@ -1126,7 +1126,7 @@ def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis state = BeaconState( genesis_time=genesis_time, eth1_data=genesis_eth1_data, - block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), + parent_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), ) # Process genesis deposits @@ -1186,11 +1186,11 @@ def process_slot(state: BeaconState) -> None: state.state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_state_root # Cache latest block header state root - if state.block_header.state_root == ZERO_HASH: - state.block_header.state_root = previous_state_root + if state.parent_block_header.state_root == ZERO_HASH: + state.parent_block_header.state_root = previous_state_root # Cache block root - previous_block_root = signing_root(state.block_header) + previous_block_root = signing_root(state.parent_block_header) state.block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root ``` @@ -1221,7 +1221,7 @@ def get_total_active_balance(state: BeaconState) -> Gwei: ```python def get_matching_source_attestations(state: BeaconState, epoch: Epoch) -> List[PendingAttestation]: assert epoch in (get_current_epoch(state), get_previous_epoch(state)) - return state.current_attestations if epoch == get_current_epoch(state) else state.previous_attestations + return state.current_epoch_attestations if epoch == get_current_epoch(state) else state.previous_epoch_attestations ``` ```python @@ -1509,8 +1509,8 @@ def process_final_updates(state: BeaconState) -> None: ) state.historical_roots.append(hash_tree_root(historical_batch)) # Rotate current/previous epoch attestations - state.previous_attestations = state.current_attestations - state.current_attestations = [] + state.previous_epoch_attestations = state.current_epoch_attestations + state.current_epoch_attestations = [] ``` ### Block processing @@ -1530,9 +1530,9 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None: # Verify that the slots match assert block.slot == state.slot # Verify that the parent matches - assert block.parent_root == signing_root(state.block_header) + assert block.parent_root == signing_root(state.parent_block_header) # Save current block as the new latest block - state.block_header = BeaconBlockHeader( + state.parent_block_header = BeaconBlockHeader( slot=block.slot, parent_root=block.parent_root, body_root=hash_tree_root(block.body), @@ -1661,11 +1661,11 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: if data.target_epoch == get_current_epoch(state): ffg_data = (state.current_justified_epoch, state.current_justified_root, get_current_epoch(state)) parent_crosslink = state.current_crosslinks[data.crosslink.shard] - state.current_attestations.append(pending_attestation) + state.current_epoch_attestations.append(pending_attestation) else: ffg_data = (state.previous_justified_epoch, state.previous_justified_root, get_previous_epoch(state)) parent_crosslink = state.previous_crosslinks[data.crosslink.shard] - state.previous_attestations.append(pending_attestation) + state.previous_epoch_attestations.append(pending_attestation) # Check FFG data, crosslink data, and signature assert ffg_data == (data.source_epoch, data.source_root, data.target_epoch) diff --git a/specs/test_formats/operations/README.md b/specs/test_formats/operations/README.md index 32cf880b3..dead3722b 100644 --- a/specs/test_formats/operations/README.md +++ b/specs/test_formats/operations/README.md @@ -14,7 +14,7 @@ post: BeaconState -- state after applying the operation. No ## Condition -A handler of the `operations` test-runner should process these cases, +A handler of the `operations` test-runner should process these cases, calling the corresponding processing implementation. Operations: @@ -23,13 +23,13 @@ Operations: |-------------------------|----------------------|----------------------|--------------------------------------------------------| | `attestation` | `Attestation` | `attestation` | `process_attestation(state, attestation)` | | `attester_slashing` | `AttesterSlashing` | `attester_slashing` | `process_attester_slashing(state, attester_slashing)` | -| `block_header` | `Block` | `block` | `process_block_header(state, block)` | +| `parent_block_header` | `Block` | `block` | `process_block_header(state, block)` | | `deposit` | `Deposit` | `deposit` | `process_deposit(state, deposit)` | | `proposer_slashing` | `ProposerSlashing` | `proposer_slashing` | `process_proposer_slashing(state, proposer_slashing)` | | `transfer` | `Transfer` | `transfer` | `process_transfer(state, transfer)` | | `voluntary_exit` | `VoluntaryExit` | `voluntary_exit` | `process_voluntary_exit(state, voluntary_exit)` | -Note that `block_header` is not strictly an operation (and is a full `Block`), but processed in the same manner, and hence included here. +Note that `parent_block_header` is not strictly an operation (and is a full `Block`), but processed in the same manner, and hence included here. The resulting state should match the expected `post` state, or if the `post` state is left blank, the handler should reject the input operation as invalid. diff --git a/test_generators/operations/main.py b/test_generators/operations/main.py index 82e05b307..70089ebe8 100644 --- a/test_generators/operations/main.py +++ b/test_generators/operations/main.py @@ -42,8 +42,8 @@ if __name__ == "__main__": create_suite('attestation', 'mainnet', lambda: generate_from_tests(test_process_attestation)), create_suite('attester_slashing', 'minimal', lambda: generate_from_tests(test_process_attester_slashing)), create_suite('attester_slashing', 'mainnet', lambda: generate_from_tests(test_process_attester_slashing)), - create_suite('block_header', 'minimal', lambda: generate_from_tests(test_process_block_header)), - create_suite('block_header', 'mainnet', lambda: generate_from_tests(test_process_block_header)), + create_suite('parent_block_header', 'minimal', lambda: generate_from_tests(test_process_block_header)), + create_suite('parent_block_header', 'mainnet', lambda: generate_from_tests(test_process_block_header)), create_suite('deposit', 'minimal', lambda: generate_from_tests(test_process_deposit)), create_suite('deposit', 'mainnet', lambda: generate_from_tests(test_process_deposit)), create_suite('proposer_slashing', 'minimal', lambda: generate_from_tests(test_process_proposer_slashing)), diff --git a/test_libs/pyspec/eth2spec/test/helpers/block.py b/test_libs/pyspec/eth2spec/test/helpers/block.py index ebd20f1b5..03f8d5b7e 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/block.py +++ b/test_libs/pyspec/eth2spec/test/helpers/block.py @@ -58,7 +58,7 @@ def build_empty_block(spec, state, slot=None, signed=False): empty_block = spec.BeaconBlock() empty_block.slot = slot empty_block.body.eth1_data.deposit_count = state.eth1_deposit_index - previous_block_header = deepcopy(state.block_header) + previous_block_header = deepcopy(state.parent_block_header) if previous_block_header.state_root == spec.ZERO_HASH: previous_block_header.state_root = state.hash_tree_root() empty_block.parent_root = signing_root(previous_block_header) diff --git a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py index d5b7f7b7f..d725f3316 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py +++ b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py @@ -1,6 +1,6 @@ from copy import deepcopy -from eth2spec.test.helpers.block_header import sign_block_header +from eth2spec.test.helpers.parent_block_header import sign_block_header from eth2spec.test.helpers.keys import pubkey_to_privkey diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py index 6cd2d1194..2b34ab405 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py @@ -31,17 +31,17 @@ def run_attestation_processing(spec, state, attestation, valid=True): yield 'post', None return - current_epoch_count = len(state.current_attestations) - previous_epoch_count = len(state.previous_attestations) + current_epoch_count = len(state.current_epoch_attestations) + previous_epoch_count = len(state.previous_epoch_attestations) # process attestation spec.process_attestation(state, attestation) # Make sure the attestation has been processed if attestation.data.target_epoch == spec.get_current_epoch(state): - assert len(state.current_attestations) == current_epoch_count + 1 + assert len(state.current_epoch_attestations) == current_epoch_count + 1 else: - assert len(state.previous_attestations) == previous_epoch_count + 1 + assert len(state.previous_epoch_attestations) == previous_epoch_count + 1 # yield post-state yield 'post', state diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py index af34ea709..41039ab06 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py @@ -1,5 +1,5 @@ from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls, with_all_phases -from eth2spec.test.helpers.block_header import sign_block_header +from eth2spec.test.helpers.parent_block_header import sign_block_header from eth2spec.test.helpers.keys import privkeys from eth2spec.test.helpers.proposer_slashings import get_valid_proposer_slashing from eth2spec.test.helpers.state import get_balance diff --git a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py index cd4532457..65d958678 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_crosslinks.py @@ -56,7 +56,7 @@ def test_single_crosslink_update_from_current_epoch(spec, state): fill_aggregate_attestation(spec, state, attestation) add_attestation_to_state(spec, state, attestation, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) - assert len(state.current_attestations) == 1 + assert len(state.current_epoch_attestations) == 1 shard = attestation.data.crosslink.shard pre_crosslink = deepcopy(state.current_crosslinks[shard]) @@ -77,7 +77,7 @@ def test_single_crosslink_update_from_previous_epoch(spec, state): fill_aggregate_attestation(spec, state, attestation) add_attestation_to_state(spec, state, attestation, state.slot + spec.SLOTS_PER_EPOCH) - assert len(state.previous_attestations) == 1 + assert len(state.previous_epoch_attestations) == 1 shard = attestation.data.crosslink.shard pre_crosslink = deepcopy(state.current_crosslinks[shard]) @@ -130,8 +130,8 @@ def test_double_late_crosslink(spec, state): next_epoch(spec, state) add_attestation_to_state(spec, state, attestation_2, state.slot + 1) - assert len(state.previous_attestations) == 1 - assert len(state.current_attestations) == 0 + assert len(state.previous_epoch_attestations) == 1 + assert len(state.current_epoch_attestations) == 0 crosslink_deltas = spec.get_crosslink_deltas(state) diff --git a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py index a5079b901..967846cb7 100644 --- a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py @@ -233,17 +233,17 @@ def test_attestation(spec, state): attestation = get_valid_attestation(spec, state, signed=True) # Add to state via block transition - pre_current_attestations_len = len(state.current_attestations) + pre_current_attestations_len = len(state.current_epoch_attestations) attestation_block = build_empty_block_for_next_slot(spec, state) attestation_block.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY attestation_block.body.attestations.append(attestation) sign_block(spec, state, attestation_block) spec.state_transition(state, attestation_block) - assert len(state.current_attestations) == pre_current_attestations_len + 1 + assert len(state.current_epoch_attestations) == pre_current_attestations_len + 1 - # Epoch transition should move to previous_attestations - pre_current_attestations_root = spec.hash_tree_root(state.current_attestations) + # Epoch transition should move to previous_epoch_attestations + pre_current_attestations_root = spec.hash_tree_root(state.current_epoch_attestations) epoch_block = build_empty_block_for_next_slot(spec, state) epoch_block.slot += spec.SLOTS_PER_EPOCH @@ -253,8 +253,8 @@ def test_attestation(spec, state): yield 'blocks', [attestation_block, epoch_block], List[spec.BeaconBlock] yield 'post', state - assert len(state.current_attestations) == 0 - assert spec.hash_tree_root(state.previous_attestations) == pre_current_attestations_root + assert len(state.current_epoch_attestations) == 0 + assert spec.hash_tree_root(state.previous_epoch_attestations) == pre_current_attestations_root @with_all_phases From 05f1a44a3846b207a089f6ce454060325d4ea77d Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Mon, 10 Jun 2019 21:20:45 +0100 Subject: [PATCH 13/39] Fix tests --- specs/test_formats/operations/README.md | 4 ++-- test_generators/operations/main.py | 4 ++-- test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py | 2 +- .../block_processing/test_process_proposer_slashing.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/specs/test_formats/operations/README.md b/specs/test_formats/operations/README.md index dead3722b..cc7e43f3f 100644 --- a/specs/test_formats/operations/README.md +++ b/specs/test_formats/operations/README.md @@ -23,13 +23,13 @@ Operations: |-------------------------|----------------------|----------------------|--------------------------------------------------------| | `attestation` | `Attestation` | `attestation` | `process_attestation(state, attestation)` | | `attester_slashing` | `AttesterSlashing` | `attester_slashing` | `process_attester_slashing(state, attester_slashing)` | -| `parent_block_header` | `Block` | `block` | `process_block_header(state, block)` | +| `block_header` | `Block` | `block` | `process_block_header(state, block)` | | `deposit` | `Deposit` | `deposit` | `process_deposit(state, deposit)` | | `proposer_slashing` | `ProposerSlashing` | `proposer_slashing` | `process_proposer_slashing(state, proposer_slashing)` | | `transfer` | `Transfer` | `transfer` | `process_transfer(state, transfer)` | | `voluntary_exit` | `VoluntaryExit` | `voluntary_exit` | `process_voluntary_exit(state, voluntary_exit)` | -Note that `parent_block_header` is not strictly an operation (and is a full `Block`), but processed in the same manner, and hence included here. +Note that `block_header` is not strictly an operation (and is a full `Block`), but processed in the same manner, and hence included here. The resulting state should match the expected `post` state, or if the `post` state is left blank, the handler should reject the input operation as invalid. diff --git a/test_generators/operations/main.py b/test_generators/operations/main.py index 70089ebe8..82e05b307 100644 --- a/test_generators/operations/main.py +++ b/test_generators/operations/main.py @@ -42,8 +42,8 @@ if __name__ == "__main__": create_suite('attestation', 'mainnet', lambda: generate_from_tests(test_process_attestation)), create_suite('attester_slashing', 'minimal', lambda: generate_from_tests(test_process_attester_slashing)), create_suite('attester_slashing', 'mainnet', lambda: generate_from_tests(test_process_attester_slashing)), - create_suite('parent_block_header', 'minimal', lambda: generate_from_tests(test_process_block_header)), - create_suite('parent_block_header', 'mainnet', lambda: generate_from_tests(test_process_block_header)), + create_suite('block_header', 'minimal', lambda: generate_from_tests(test_process_block_header)), + create_suite('block_header', 'mainnet', lambda: generate_from_tests(test_process_block_header)), create_suite('deposit', 'minimal', lambda: generate_from_tests(test_process_deposit)), create_suite('deposit', 'mainnet', lambda: generate_from_tests(test_process_deposit)), create_suite('proposer_slashing', 'minimal', lambda: generate_from_tests(test_process_proposer_slashing)), diff --git a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py index d725f3316..d5b7f7b7f 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py +++ b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py @@ -1,6 +1,6 @@ from copy import deepcopy -from eth2spec.test.helpers.parent_block_header import sign_block_header +from eth2spec.test.helpers.block_header import sign_block_header from eth2spec.test.helpers.keys import pubkey_to_privkey diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py index 41039ab06..af34ea709 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_proposer_slashing.py @@ -1,5 +1,5 @@ from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls, with_all_phases -from eth2spec.test.helpers.parent_block_header import sign_block_header +from eth2spec.test.helpers.block_header import sign_block_header from eth2spec.test.helpers.keys import privkeys from eth2spec.test.helpers.proposer_slashings import get_valid_proposer_slashing from eth2spec.test.helpers.state import get_balance From b56539a47d31d40a8e0f07d7915b9fa9a65edd9c Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 10 Jun 2019 15:14:42 -0600 Subject: [PATCH 14/39] add missing colon to v-guide --- specs/validator/0_beacon-chain-validator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/validator/0_beacon-chain-validator.md b/specs/validator/0_beacon-chain-validator.md index e7a3a1a9d..29724da51 100644 --- a/specs/validator/0_beacon-chain-validator.md +++ b/specs/validator/0_beacon-chain-validator.md @@ -147,7 +147,7 @@ def get_committee_assignment( committees_per_slot = get_epoch_committee_count(state, epoch) // SLOTS_PER_EPOCH epoch_start_slot = get_epoch_start_slot(epoch) - for slot in range(epoch_start_slot, epoch_start_slot + SLOTS_PER_EPOCH) + for slot in range(epoch_start_slot, epoch_start_slot + SLOTS_PER_EPOCH): offset = committees_per_slot * (slot % SLOTS_PER_EPOCH) slot_start_shard = (get_epoch_start_shard(state, epoch) + offset) % SHARD_COUNT for i in range(committees_per_slot): From c4bf772d30b4a557c484e9527c2c670cdbacfe89 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 10 Jun 2019 17:26:39 -0600 Subject: [PATCH 15/39] demo phase restricted generators for epoch processing --- test_generators/epoch_processing/main.py | 10 +++++----- test_libs/gen_helpers/gen_from_tests/gen.py | 7 ++++--- test_libs/pyspec/eth2spec/test/context.py | 13 +++++++++++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/test_generators/epoch_processing/main.py b/test_generators/epoch_processing/main.py index 2ce895fc5..679ef4c6b 100644 --- a/test_generators/epoch_processing/main.py +++ b/test_generators/epoch_processing/main.py @@ -2,7 +2,7 @@ from typing import Callable, Iterable from eth2spec.phase0 import spec as spec_phase0 from eth2spec.phase1 import spec as spec_phase1 -from eth2spec.test.epoch_processing import ( +from eth2spec.test.phase_0.epoch_processing import ( test_process_crosslinks, test_process_registry_updates ) @@ -33,8 +33,8 @@ def create_suite(transition_name: str, config_name: str, get_cases: Callable[[], if __name__ == "__main__": gen_runner.run_generator("epoch_processing", [ - create_suite('crosslinks', 'minimal', lambda: generate_from_tests(test_process_crosslinks)), - create_suite('crosslinks', 'mainnet', lambda: generate_from_tests(test_process_crosslinks)), - create_suite('registry_updates', 'minimal', lambda: generate_from_tests(test_process_registry_updates)), - create_suite('registry_updates', 'mainnet', lambda: generate_from_tests(test_process_registry_updates)), + create_suite('crosslinks', 'minimal', lambda: generate_from_tests(test_process_crosslinks, 'phase0')), + create_suite('crosslinks', 'mainnet', lambda: generate_from_tests(test_process_crosslinks, 'phase0')), + create_suite('registry_updates', 'minimal', lambda: generate_from_tests(test_process_registry_updates, 'phase0')), + create_suite('registry_updates', 'mainnet', lambda: generate_from_tests(test_process_registry_updates, 'phase0')), ]) diff --git a/test_libs/gen_helpers/gen_from_tests/gen.py b/test_libs/gen_helpers/gen_from_tests/gen.py index e7d801131..3810c385e 100644 --- a/test_libs/gen_helpers/gen_from_tests/gen.py +++ b/test_libs/gen_helpers/gen_from_tests/gen.py @@ -1,9 +1,10 @@ from inspect import getmembers, isfunction -def generate_from_tests(src, bls_active=True): +def generate_from_tests(src, phase, bls_active=True): """ Generate a list of test cases by running tests from the given src in generator-mode. - :param src: to retrieve tests from (discovered using inspect.getmembers) + :param src: to retrieve tests from (discovered using inspect.getmembers). + :param phase: to run tests against particular phase. :param bls_active: optional, to override BLS switch preference. Defaults to True. :return: the list of test cases. """ @@ -16,7 +17,7 @@ def generate_from_tests(src, bls_active=True): for name in fn_names: tfn = getattr(src, name) try: - test_case = tfn(generator_mode=True, bls_active=bls_active) + test_case = tfn(generator_mode=True, phase=phase, bls_active=bls_active) # If no test case data is returned, the test is ignored. if test_case is not None: out.append(test_case) diff --git a/test_libs/pyspec/eth2spec/test/context.py b/test_libs/pyspec/eth2spec/test/context.py index cbc594cd8..520eb9745 100644 --- a/test_libs/pyspec/eth2spec/test/context.py +++ b/test_libs/pyspec/eth2spec/test/context.py @@ -119,9 +119,18 @@ def with_phases(phases): fn(*args, **kw) def wrapper(*args, **kw): - if 'phase0' in phases: + run_phases = phases + + # limit phases if one explicitly specified + if 'phase' in kw: + phase = kw.pop('phase') + if phase not in phases: + return + run_phases = [phase] + + if 'phase0' in run_phases: run_with_spec_version(spec_phase0, *args, **kw) - if 'phase1' in phases: + if 'phase1' in run_phases: run_with_spec_version(spec_phase1, *args, **kw) return wrapper return decorator From 94d4e3a94402f65a58a23d434f40d021c4f80259 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 10 Jun 2019 20:05:43 -0600 Subject: [PATCH 16/39] operations suite generator for phase0 --- test_generators/epoch_processing/main.py | 6 +++-- test_generators/operations/main.py | 32 +++++++++++------------ test_libs/pyspec/eth2spec/test/context.py | 7 ++--- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/test_generators/epoch_processing/main.py b/test_generators/epoch_processing/main.py index 679ef4c6b..846f463a1 100644 --- a/test_generators/epoch_processing/main.py +++ b/test_generators/epoch_processing/main.py @@ -35,6 +35,8 @@ if __name__ == "__main__": gen_runner.run_generator("epoch_processing", [ create_suite('crosslinks', 'minimal', lambda: generate_from_tests(test_process_crosslinks, 'phase0')), create_suite('crosslinks', 'mainnet', lambda: generate_from_tests(test_process_crosslinks, 'phase0')), - create_suite('registry_updates', 'minimal', lambda: generate_from_tests(test_process_registry_updates, 'phase0')), - create_suite('registry_updates', 'mainnet', lambda: generate_from_tests(test_process_registry_updates, 'phase0')), + create_suite('registry_updates', 'minimal', + lambda: generate_from_tests(test_process_registry_updates, 'phase0')), + create_suite('registry_updates', 'mainnet', + lambda: generate_from_tests(test_process_registry_updates, 'phase0')), ]) diff --git a/test_generators/operations/main.py b/test_generators/operations/main.py index 82e05b307..38fa42f68 100644 --- a/test_generators/operations/main.py +++ b/test_generators/operations/main.py @@ -1,13 +1,13 @@ from typing import Callable, Iterable -from eth2spec.test.block_processing import ( +from eth2spec.test.phase_0.block_processing import ( test_process_attestation, test_process_attester_slashing, test_process_block_header, test_process_deposit, test_process_proposer_slashing, test_process_transfer, - test_process_voluntary_exit + test_process_voluntary_exit, ) from gen_base import gen_runner, gen_suite, gen_typing @@ -38,18 +38,18 @@ def create_suite(operation_name: str, config_name: str, get_cases: Callable[[], if __name__ == "__main__": gen_runner.run_generator("operations", [ - create_suite('attestation', 'minimal', lambda: generate_from_tests(test_process_attestation)), - create_suite('attestation', 'mainnet', lambda: generate_from_tests(test_process_attestation)), - create_suite('attester_slashing', 'minimal', lambda: generate_from_tests(test_process_attester_slashing)), - create_suite('attester_slashing', 'mainnet', lambda: generate_from_tests(test_process_attester_slashing)), - create_suite('block_header', 'minimal', lambda: generate_from_tests(test_process_block_header)), - create_suite('block_header', 'mainnet', lambda: generate_from_tests(test_process_block_header)), - create_suite('deposit', 'minimal', lambda: generate_from_tests(test_process_deposit)), - create_suite('deposit', 'mainnet', lambda: generate_from_tests(test_process_deposit)), - create_suite('proposer_slashing', 'minimal', lambda: generate_from_tests(test_process_proposer_slashing)), - create_suite('proposer_slashing', 'mainnet', lambda: generate_from_tests(test_process_proposer_slashing)), - create_suite('transfer', 'minimal', lambda: generate_from_tests(test_process_transfer)), - create_suite('transfer', 'mainnet', lambda: generate_from_tests(test_process_transfer)), - create_suite('voluntary_exit', 'minimal', lambda: generate_from_tests(test_process_voluntary_exit)), - create_suite('voluntary_exit', 'mainnet', lambda: generate_from_tests(test_process_voluntary_exit)), + create_suite('attestation', 'minimal', lambda: generate_from_tests(test_process_attestation, 'phase0')), + create_suite('attestation', 'mainnet', lambda: generate_from_tests(test_process_attestation, 'phase0')), + create_suite('attester_slashing', 'minimal', lambda: generate_from_tests(test_process_attester_slashing, 'phase0')), + create_suite('attester_slashing', 'mainnet', lambda: generate_from_tests(test_process_attester_slashing, 'phase0')), + create_suite('block_header', 'minimal', lambda: generate_from_tests(test_process_block_header, 'phase0')), + create_suite('block_header', 'mainnet', lambda: generate_from_tests(test_process_block_header, 'phase0')), + create_suite('deposit', 'minimal', lambda: generate_from_tests(test_process_deposit, 'phase0')), + create_suite('deposit', 'mainnet', lambda: generate_from_tests(test_process_deposit, 'phase0')), + create_suite('proposer_slashing', 'minimal', lambda: generate_from_tests(test_process_proposer_slashing, 'phase0')), + create_suite('proposer_slashing', 'mainnet', lambda: generate_from_tests(test_process_proposer_slashing, 'phase0')), + create_suite('transfer', 'minimal', lambda: generate_from_tests(test_process_transfer, 'phase0')), + create_suite('transfer', 'mainnet', lambda: generate_from_tests(test_process_transfer, 'phase0')), + create_suite('voluntary_exit', 'minimal', lambda: generate_from_tests(test_process_voluntary_exit, 'phase0')), + create_suite('voluntary_exit', 'mainnet', lambda: generate_from_tests(test_process_voluntary_exit, 'phase0')), ]) diff --git a/test_libs/pyspec/eth2spec/test/context.py b/test_libs/pyspec/eth2spec/test/context.py index 520eb9745..97266acf2 100644 --- a/test_libs/pyspec/eth2spec/test/context.py +++ b/test_libs/pyspec/eth2spec/test/context.py @@ -116,7 +116,7 @@ def with_phases(phases): def decorator(fn): def run_with_spec_version(spec, *args, **kw): kw['spec'] = spec - fn(*args, **kw) + return fn(*args, **kw) def wrapper(*args, **kw): run_phases = phases @@ -129,8 +129,9 @@ def with_phases(phases): run_phases = [phase] if 'phase0' in run_phases: - run_with_spec_version(spec_phase0, *args, **kw) + ret = run_with_spec_version(spec_phase0, *args, **kw) if 'phase1' in run_phases: - run_with_spec_version(spec_phase1, *args, **kw) + ret = run_with_spec_version(spec_phase1, *args, **kw) + return ret return wrapper return decorator From abe48cc98857c3350d45656d3d4a6f2e6af2784d Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Tue, 11 Jun 2019 15:25:25 +0100 Subject: [PATCH 17/39] Address final comments by HW and Danny --- .../contracts/validator_registration.json | 2 +- .../contracts/validator_registration.v.py | 8 +++--- .../tests/contracts/test_deposit.py | 26 +++---------------- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json index 676bf470a..1779423d2 100644 --- a/deposit_contract/contracts/validator_registration.json +++ b/deposit_contract/contracts/validator_registration.json @@ -1 +1 @@ -{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "to_little_endian_64", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [{"type": "uint256", "name": "value"}], "constant": true, "payable": false, "type": "function", "gas": 6973}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10922}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1318334}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b5050610e9956600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a0526380673289600051141561025357602060046101403734156100b457600080fd5b60006101605261014051610180526101a060006008818352015b61016051600860008112156100eb578060000360020a82046100f2565b8060020a82025b905090506101605260ff61018051166101c052610160516101c0516101605101101561011d57600080fd5b6101c051610160510161016052610180517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610166578060000360020a820461016d565b8060020a82025b90509050610180525b81516001018083528114156100ce575b505060186008602082066101e001602082840111156101a457600080fd5b60208061020082610160600060046015f15050818152809050905090508051602001806102a0828460006004600a8704601201f16101e157600080fd5b50506102a05160206001820306601f82010390506103006102a0516008818352015b826103005111156102135761022f565b6000610300516102c001535b8151600101808352811415610203575b50505060206102805260406102a0510160206001820306601f8201039050610280f3005b63c5f2892f60005114156103ab57341561026c57600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561030657600061018051602081106102a757600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af16102f857600080fd5b60c051905061014052610374565b6000610140516020826101a0010152602081019050610180516020811061032c57600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161036a57600080fd5b60c0519050610140525b610160600261038257600080fd5b60028151048152505b8151600101808352811415610285575b50506101405160005260206000f3005b63621fd13060005114156104815734156103c457600080fd5b60606101c060246380673289610140526001546101605261015c6000305af16103ec57600080fd5b6101e0805160200180610260828460006004600a8704601201f161040f57600080fd5b50506102605160206001820306601f82010390506102c0610260516008818352015b826102c05111156104415761045d565b60006102c05161028001535b8151600101808352811415610431575b5050506020610240526040610260510160206001820306601f8201039050610240f3005b63c47e300d6000511415610d0e57606060046101403760506004356004016101a03760306004356004013511156104b757600080fd5b60406024356004016102203760206024356004013511156104d757600080fd5b60806044356004016102803760606044356004013511156104f757600080fd5b63ffffffff6001541061050957600080fd5b633b9aca00610340526103405161051f57600080fd5b61034051340461032052633b9aca0061032051101561053d57600080fd5b60306101a0511461054d57600080fd5b6020610220511461055d57600080fd5b6060610280511461056d57600080fd5b6060610440602463806732896103c052610320516103e0526103dc6000305af161059657600080fd5b610460805160200180610360828460006004600a8704601201f16105b957600080fd5b505060006101a060308060208461056001018260208501600060046016f150508051820191505060006010602082066104e001602082840111156105fc57600080fd5b602080610500826104a0600060046015f150508181528090509050905060108060208461056001018260208501600060046013f150508051820191505080610560526105609050602060c0825160208401600060025af161065c57600080fd5b60c05190506104c052600060006040602082066106000161028051828401111561068557600080fd5b606080610620826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af16106c557600080fd5b60c0519050602082610800010152602081019050600060406020602082066106c0016102805182840111156106f957600080fd5b6060806106e0826020602088068803016102800160006004601bf150508181528090509050905060208060208461078001018260208501600060046015f15050805182019150506104a05160208261078001015260208101905080610780526107809050602060c0825160208401600060025af161077657600080fd5b60c051905060208261080001015260208101905080610800526108009050602060c0825160208401600060025af16107ad57600080fd5b60c05190506105e052600060006104c0516020826108a00101526020810190506102206020806020846108a001018260208501600060046015f1505080518201915050806108a0526108a09050602060c0825160208401600060025af161081357600080fd5b60c0519050602082610a2001015260208101905060006103606008806020846109a001018260208501600060046012f15050805182019150506000601860208206610920016020828401111561086857600080fd5b602080610940826104a0600060046015f15050818152809050905090506018806020846109a001018260208501600060046014f15050805182019150506105e0516020826109a0010152602081019050806109a0526109a09050602060c0825160208401600060025af16108db57600080fd5b60c0519050602082610a2001015260208101905080610a2052610a209050602060c0825160208401600060025af161091257600080fd5b60c051905061088052600180546001825401101561092f57600080fd5b6001815401815550600154610aa052610ac060006020818352015b60016001610aa05116141561097f5761088051610ac0516020811061096e57600080fd5b600060c052602060c0200155610a13565b6000610ac0516020811061099257600080fd5b600060c052602060c0200154602082610ae001015260208101905061088051602082610ae001015260208101905080610ae052610ae09050602060c0825160208401600060025af16109e357600080fd5b60c051905061088052610aa060026109fa57600080fd5b60028151048152505b815160010180835281141561094a575b50506080610b8052610b8051610bc0526101a0805160200180610b8051610bc001828460006004600a8704601201f1610a4b57600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516040818352015b83610b6051101515610a8957610aa6565b6000610b60516020850101535b8151600101808352811415610a78575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b8052610b8051610be052610220805160200180610b8051610bc001828460006004600a8704601201f1610afd57600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516020818352015b83610b6051101515610b3b57610b58565b6000610b60516020850101535b8151600101808352811415610b2a575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b8052610b8051610c0052610360805160200180610b8051610bc001828460006004600a8704601201f1610baf57600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516020818352015b83610b6051101515610bed57610c0a565b6000610b60516020850101535b8151600101808352811415610bdc575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b8052610b8051610c2052610280805160200180610b8051610bc001828460006004600a8704601201f1610c6157600080fd5b5050610b8051610bc0015160206001820306601f8201039050610b8051610bc001610b6081516060818352015b83610b6051101515610c9f57610cbc565b6000610b60516020850101535b8151600101808352811415610c8e575b505050506020610b8051610bc0015160206001820306601f8201039050610b80510101610b80527f9d0a206c338cfcaf9198c04fe61b39a988e26b623ef97cb2f72bafcfbf8bb93e610b8051610bc0a1005b60006000fd5b610185610e9903610185600039610185610e99036000f3"} \ No newline at end of file +{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}, {"type": "bytes", "name": "index", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10433}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1334984}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b505061134056600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610277575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100da578060000360020a82046100e1565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561010c57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610155578060000360020a820461015c565b8060020a82025b905090506101a0525b81516001018083528114156100bd575b50506018600860208206610200016020828401111561019357600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101d057600080fd5b50506102c05160206001820306601f82010390506103206102c0516008818352015b826103205111156102025761021e565b6000610320516102e001535b81516001018083528114156101f2575b50505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102535761026f565b602061028051036102a001516020610280510361028052610241565b610160515650005b63c5f2892f60005114156103cf57341561029057600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561032a57600061018051602081106102cb57600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af161031c57600080fd5b60c051905061014052610398565b6000610140516020826101a0010152602081019050610180516020811061035057600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161038e57600080fd5b60c0519050610140525b61016060026103a657600080fd5b60028151048152505b81516001018083528114156102a9575b50506101405160005260206000f3005b63621fd13060005114156104e15734156103e857600080fd5b63806732896101405260015461016052610160516006580161009b565b506101c0526000610220525b6101c05160206001820306601f8201039050610220511015156104335761044c565b610220516101e001526102205160200161022052610411565b6101c0805160200180610280828460006004600a8704601201f161046f57600080fd5b50506102805160206001820306601f82010390506102e0610280516008818352015b826102e05111156104a1576104bd565b60006102e0516102a001535b8151600101808352811415610491575b5050506020610260526040610280510160206001820306601f8201039050610260f3005b63c47e300d60005114156111b557606060046101403760506004356004016101a037603060043560040135111561051757600080fd5b604060243560040161022037602060243560040135111561053757600080fd5b608060443560040161028037606060443560040135111561055757600080fd5b63ffffffff6001541061056957600080fd5b633b9aca00610340526103405161057f57600080fd5b61034051340461032052633b9aca0061032051101561059d57600080fd5b60306101a051146105ad57600080fd5b602061022051146105bd57600080fd5b606061028051146105cd57600080fd5b6101a0516101c0516101e05161020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a05163806732896103c052610320516103e0526103e0516006580161009b565b506104405260006104a0525b6104405160206001820306601f82010390506104a05110151561065d57610676565b6104a05161046001526104a0516020016104a05261063b565b6103a05261038052610360526103405261032052610300526102e0526102c0526102a05261028052610260526102405261022052610200526101e0526101c0526101a052610440805160200180610360828460006004600a8704601201f16106dd57600080fd5b505060006101a060308060208461058001018260208501600060046016f15050805182019150506000601060208206610500016020828401111561072057600080fd5b602080610520826104c0600060046015f150508181528090509050905060108060208461058001018260208501600060046013f150508051820191505080610580526105809050602060c0825160208401600060025af161078057600080fd5b60c05190506104e05260006000604060208206610620016102805182840111156107a957600080fd5b606080610640826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af16107e957600080fd5b60c0519050602082610820010152602081019050600060406020602082066106e00161028051828401111561081d57600080fd5b606080610700826020602088068803016102800160006004601bf15050818152809050905090506020806020846107a001018260208501600060046015f15050805182019150506104c0516020826107a0010152602081019050806107a0526107a09050602060c0825160208401600060025af161089a57600080fd5b60c051905060208261082001015260208101905080610820526108209050602060c0825160208401600060025af16108d157600080fd5b60c051905061060052600060006104e0516020826108c00101526020810190506102206020806020846108c001018260208501600060046015f1505080518201915050806108c0526108c09050602060c0825160208401600060025af161093757600080fd5b60c0519050602082610a4001015260208101905060006103606008806020846109c001018260208501600060046012f15050805182019150506000601860208206610940016020828401111561098c57600080fd5b602080610960826104c0600060046015f15050818152809050905090506018806020846109c001018260208501600060046014f1505080518201915050610600516020826109c0010152602081019050806109c0526109c09050602060c0825160208401600060025af16109ff57600080fd5b60c0519050602082610a4001015260208101905080610a4052610a409050602060c0825160208401600060025af1610a3657600080fd5b60c05190506108a0526101a0516101c0516101e05161020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a0516103c0516103e05161040051610420516104405161046051610480516104a0516104c0516104e05161050051610520516105405161056051610580516105a0516105c0516105e05161060051610620516106405161066051610680516106a0516106c0516106e05161070051610720516107405161076051610780516107a0516107c0516107e05161080051610820516108405161086051610880516108a0516108c0516108e05161090051610920516109405161096051610980516109a0516109c0516109e051610a0051610a2051610a4051610a6051610a8051610aa0516380673289610ac052600154610ae052610ae0516006580161009b565b50610b40526000610ba0525b610b405160206001820306601f8201039050610ba051101515610bae57610bc7565b610ba051610b600152610ba051602001610ba052610b8c565b610aa052610a8052610a6052610a4052610a2052610a00526109e0526109c0526109a05261098052610960526109405261092052610900526108e0526108c0526108a05261088052610860526108405261082052610800526107e0526107c0526107a05261078052610760526107405261072052610700526106e0526106c0526106a05261068052610660526106405261062052610600526105e0526105c0526105a05261058052610560526105405261052052610500526104e0526104c0526104a05261048052610460526104405261042052610400526103e0526103c0526103a05261038052610360526103405261032052610300526102e0526102c0526102a05261028052610260526102405261022052610200526101e0526101c0526101a052610b40805160200180610bc0828460006004600a8704601201f1610d0e57600080fd5b505060a0610c4052610c4051610c80526101a0805160200180610c4051610c8001828460006004600a8704601201f1610d4657600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516040818352015b83610c2051101515610d8457610da1565b6000610c20516020850101535b8151600101808352811415610d73575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610ca052610220805160200180610c4051610c8001828460006004600a8704601201f1610df857600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516020818352015b83610c2051101515610e3657610e53565b6000610c20516020850101535b8151600101808352811415610e25575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610cc052610360805160200180610c4051610c8001828460006004600a8704601201f1610eaa57600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516020818352015b83610c2051101515610ee857610f05565b6000610c20516020850101535b8151600101808352811415610ed7575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610ce052610280805160200180610c4051610c8001828460006004600a8704601201f1610f5c57600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516060818352015b83610c2051101515610f9a57610fb7565b6000610c20516020850101535b8151600101808352811415610f89575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610d0052610bc0805160200180610c4051610c8001828460006004600a8704601201f161100e57600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516020818352015b83610c205110151561104c57611069565b6000610c20516020850101535b815160010180835281141561103b575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c40527fdc5fc95703516abd38fa03c3737ff3b52dc52347055c8028460fdf5bbe2f12ce610c4051610c80a160018054600182540110156110cd57600080fd5b6001815401815550600154610d2052610d4060006020818352015b60016001610d205116141561111d576108a051610d40516020811061110c57600080fd5b600060c052602060c02001556111b1565b6000610d40516020811061113057600080fd5b600060c052602060c0200154602082610d600101526020810190506108a051602082610d6001015260208101905080610d6052610d609050602060c0825160208401600060025af161118157600080fd5b60c05190506108a052610d20600261119857600080fd5b60028151048152505b81516001018083528114156110e8575b5050005b60006000fd5b61018561134003610185600039610185611340036000f3"} \ No newline at end of file diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index 8510380ee..10daf42b9 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -11,6 +11,7 @@ Deposit: event({ withdrawal_credentials: bytes[32], amount: bytes[8], signature: bytes[96], + index: bytes[8], }) branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] @@ -24,7 +25,7 @@ def __init__(): self.zero_hashes[i + 1] = sha256(concat(self.zero_hashes[i], self.zero_hashes[i])) -@public +@private @constant def to_little_endian_64(value: uint256) -> bytes[8]: # Reversing bytes using bitwise uint256 manipulations @@ -87,6 +88,9 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)), )) + # Emit `Deposit` log + log.Deposit(pubkey, withdrawal_credentials, amount, signature, self.to_little_endian_64(self.deposit_count)) + # Add `DepositData` root to Merkle tree (update a single `branch` node) self.deposit_count += 1 size: uint256 = self.deposit_count @@ -97,5 +101,3 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], node = sha256(concat(self.branch[height], node)) size /= 2 - # Emit `Deposit` log - log.Deposit(pubkey, withdrawal_credentials, amount, signature) diff --git a/deposit_contract/tests/contracts/test_deposit.py b/deposit_contract/tests/contracts/test_deposit.py index 12f3e7f8b..783af3356 100644 --- a/deposit_contract/tests/contracts/test_deposit.py +++ b/deposit_contract/tests/contracts/test_deposit.py @@ -49,28 +49,6 @@ def deposit_input(): ) -@pytest.mark.parametrize( - 'value,success', - [ - (0, True), - (10, True), - (55555, True), - (2**64 - 1, True), - # (2**64, True), # Note that all calls to `to_little_endian_64` have an input less than 2**64 - ] -) -def test_to_little_endian_64(registration_contract, value, success, assert_tx_failed): - call = registration_contract.functions.to_little_endian_64(value) - - if success: - little_endian_64 = call.call() - assert little_endian_64 == (value).to_bytes(8, 'little') - else: - assert_tx_failed( - lambda: call.call() - ) - - @pytest.mark.parametrize( 'success,deposit_amount', [ @@ -151,7 +129,7 @@ def test_deposit_log(registration_contract, a0, w3, deposit_input): assert log['withdrawal_credentials'] == deposit_input[1] assert log['amount'] == deposit_amount_list[i].to_bytes(8, 'little') assert log['signature'] == deposit_input[2] - + assert log['index'] == i.to_bytes(8, 'little') def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input): log_filter = registration_contract.events.Deposit.createFilter( @@ -171,6 +149,8 @@ def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input assert len(logs) == 1 log = logs[0]['args'] + assert log["index"] == i.to_bytes(8, 'little') + deposit_data = DepositData( pubkey=deposit_input[0], withdrawal_credentials=deposit_input[1], From 6a83205420cde658f9ac1a0f9e324313d3dacb2f Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Tue, 11 Jun 2019 15:29:34 +0100 Subject: [PATCH 18/39] Minor copy edit --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 61729e8d9..c25d43d96 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1168,7 +1168,7 @@ When `is_genesis_trigger(deposits, timestamp) is True` for the first time let: * `genesis_deposits = deposits` * `genesis_time = timestamp - timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY` where `SECONDS_PER_DAY = 86400` * `genesis_eth1_data` be the object of type `Eth1Data` where: - * `genesis_eth1_data.block_hash` is the Ethereum 1.0 block hash that emitted to log for the last deposit in `deposits` + * `genesis_eth1_data.block_hash` is the Ethereum 1.0 block hash that emitted the log for the last deposit in `deposits` * `genesis_eth1_data.deposit_root` is the deposit root for the last deposit in `deposits` * `genesis_eth1_data.deposit_count = len(genesis_deposits)` From 46d8422510e5235cecb9da08982dc5c8c77f8920 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 16:42:50 +0200 Subject: [PATCH 19/39] fix ssz container recognition for generators --- test_libs/pyspec/eth2spec/test/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test_libs/pyspec/eth2spec/test/utils.py b/test_libs/pyspec/eth2spec/test/utils.py index b61801c3d..817c952b7 100644 --- a/test_libs/pyspec/eth2spec/test/utils.py +++ b/test_libs/pyspec/eth2spec/test/utils.py @@ -1,5 +1,6 @@ from typing import Dict, Any, Callable, Iterable from eth2spec.debug.encode import encode +from eth2spec.utils.ssz.ssz_typing import Container def spectest(description: str = None): @@ -30,9 +31,13 @@ def spectest(description: str = None): else: # Otherwise, try to infer the type, but keep it as-is if it's not a SSZ container. (key, value) = data - if hasattr(value.__class__, 'fields'): + if isinstance(value, Container): out[key] = encode(value, value.__class__) else: + # not a ssz value. + # It could be vector or bytes still, but it is a rare case, + # and lists can't be inferred fully (generics lose element type). + # In such cases, explicitly state the type of the yielded value as a third yielded object. out[key] = value if has_contents: return out From 481c9664bee8746a9eb7cb5388ffcf314e05fcda Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 17:07:48 +0200 Subject: [PATCH 20/39] update naming of 2 constants, fix comments in minimal config (mainnet time estimates do not hold with different amount of slots per epoch) --- configs/constant_presets/mainnet.yaml | 4 ++-- configs/constant_presets/minimal.yaml | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/configs/constant_presets/mainnet.yaml b/configs/constant_presets/mainnet.yaml index 6ac3f422f..bca7febc7 100644 --- a/configs/constant_presets/mainnet.yaml +++ b/configs/constant_presets/mainnet.yaml @@ -48,7 +48,7 @@ GENESIS_FORK_VERSION: 0x00000000 GENESIS_SLOT: 0 # 2**64 - 1 FAR_FUTURE_EPOCH: 18446744073709551615 -BLS_WITHDRAWAL_PREFIX_BYTE: 0x00 +BLS_WITHDRAWAL_PREFIX: 0x00 # Time parameters @@ -90,7 +90,7 @@ LATEST_SLASHED_EXIT_LENGTH: 8192 # Reward and penalty quotients # --------------------------------------------------------------- # 2**5 (= 32) -BASE_REWARD_QUOTIENT: 32 +BASE_REWARD_FACTOR: 32 # 2**9 (= 512) WHISTLEBLOWING_REWARD_QUOTIENT: 512 # 2**3 (= 8) diff --git a/configs/constant_presets/minimal.yaml b/configs/constant_presets/minimal.yaml index 73448c3c6..23bc6e819 100644 --- a/configs/constant_presets/minimal.yaml +++ b/configs/constant_presets/minimal.yaml @@ -47,7 +47,7 @@ GENESIS_FORK_VERSION: 0x00000000 GENESIS_SLOT: 0 # 2**64 - 1 FAR_FUTURE_EPOCH: 18446744073709551615 -BLS_WITHDRAWAL_PREFIX_BYTE: 0x00 +BLS_WITHDRAWAL_PREFIX: 0x00 # Time parameters @@ -58,23 +58,23 @@ SECONDS_PER_SLOT: 6 MIN_ATTESTATION_INCLUSION_DELAY: 2 # [customized] fast epochs SLOTS_PER_EPOCH: 8 -# 2**0 (= 1) epochs 6.4 minutes +# 2**0 (= 1) epochs MIN_SEED_LOOKAHEAD: 1 -# 2**2 (= 4) epochs 25.6 minutes +# 2**2 (= 4) epochs ACTIVATION_EXIT_DELAY: 4 # [customized] higher frequency new deposits from eth1 for testing SLOTS_PER_ETH1_VOTING_PERIOD: 16 # [customized] smaller state SLOTS_PER_HISTORICAL_ROOT: 64 -# 2**8 (= 256) epochs ~27 hours +# 2**8 (= 256) epochs MIN_VALIDATOR_WITHDRAWABILITY_DELAY: 256 -# 2**11 (= 2,048) epochs 9 days +# 2**11 (= 2,048) epochs PERSISTENT_COMMITTEE_PERIOD: 2048 -# 2**6 (= 64) epochs ~7 hours +# 2**6 (= 64) epochs MAX_EPOCHS_PER_CROSSLINK: 64 -# 2**2 (= 4) epochs 25.6 minutes +# 2**2 (= 4) epochs MIN_EPOCHS_TO_INACTIVITY_PENALTY: 4 -# [customized] 2**12 (= 4,096) epochs 18 days +# [customized] 2**12 (= 4,096) epochs EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS: 4096 @@ -91,7 +91,7 @@ LATEST_SLASHED_EXIT_LENGTH: 64 # Reward and penalty quotients # --------------------------------------------------------------- # 2**5 (= 32) -BASE_REWARD_QUOTIENT: 32 +BASE_REWARD_FACTOR: 32 # 2**9 (= 512) WHISTLEBLOWING_REWARD_QUOTIENT: 512 # 2**3 (= 8) From 98a798bd54d7d1969cf84d32a2e6de073e4a68e0 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Tue, 11 Jun 2019 09:10:09 -0600 Subject: [PATCH 21/39] sanity tests gens to use explicit phase0 --- test_generators/sanity/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test_generators/sanity/main.py b/test_generators/sanity/main.py index a9c0fe160..805ee6139 100644 --- a/test_generators/sanity/main.py +++ b/test_generators/sanity/main.py @@ -30,8 +30,8 @@ def create_suite(handler_name: str, config_name: str, get_cases: Callable[[], It if __name__ == "__main__": gen_runner.run_generator("sanity", [ - create_suite('blocks', 'minimal', lambda: generate_from_tests(test_blocks)), - create_suite('blocks', 'mainnet', lambda: generate_from_tests(test_blocks)), - create_suite('slots', 'minimal', lambda: generate_from_tests(test_slots)), - create_suite('slots', 'mainnet', lambda: generate_from_tests(test_slots)), + create_suite('blocks', 'minimal', lambda: generate_from_tests(test_blocks, 'phase0')), + create_suite('blocks', 'mainnet', lambda: generate_from_tests(test_blocks, 'phase0')), + create_suite('slots', 'minimal', lambda: generate_from_tests(test_slots, 'phase0')), + create_suite('slots', 'mainnet', lambda: generate_from_tests(test_slots, 'phase0')), ]) From e86771250b4fd34d4a1aa95369b3fd2c12ee9792 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 17:16:02 +0200 Subject: [PATCH 22/39] Fix tests to use new constant name --- test_libs/pyspec/eth2spec/test/helpers/deposits.py | 2 +- test_libs/pyspec/eth2spec/test/helpers/genesis.py | 2 +- test_libs/pyspec/eth2spec/test/helpers/transfers.py | 2 +- .../test/phase_0/block_processing/test_process_deposit.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index 8f437ec89..b84970044 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -67,7 +67,7 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c # insecurely use pubkey as withdrawal key if no credentials provided if withdrawal_credentials is None: - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] deposit, root, deposit_data_leaves = build_deposit( spec, diff --git a/test_libs/pyspec/eth2spec/test/helpers/genesis.py b/test_libs/pyspec/eth2spec/test/helpers/genesis.py index 83af56621..23451b06b 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/genesis.py +++ b/test_libs/pyspec/eth2spec/test/helpers/genesis.py @@ -5,7 +5,7 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root def build_mock_validator(spec, i: int, balance: int): pubkey = pubkeys[i] # insecurely use pubkey as withdrawal key as well - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] return spec.Validator( pubkey=pubkeys[i], withdrawal_credentials=withdrawal_credentials, diff --git a/test_libs/pyspec/eth2spec/test/helpers/transfers.py b/test_libs/pyspec/eth2spec/test/helpers/transfers.py index e619c5569..eea12a36d 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/transfers.py +++ b/test_libs/pyspec/eth2spec/test/helpers/transfers.py @@ -32,7 +32,7 @@ def get_valid_transfer(spec, state, slot=None, sender_index=None, amount=None, f # ensure withdrawal_credentials reproducible state.validator_registry[transfer.sender].withdrawal_credentials = ( - spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(transfer.pubkey)[1:] + spec.BLS_WITHDRAWAL_PREFIX + spec.hash(transfer.pubkey)[1:] ) return transfer diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py index c50b11f2e..42de447ec 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py @@ -101,7 +101,7 @@ def test_invalid_sig_top_up(spec, state): def test_invalid_withdrawal_credentials_top_up(spec, state): validator_index = 0 amount = spec.MAX_EFFECTIVE_BALANCE // 4 - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(b"junk")[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(b"junk")[1:] deposit = prepare_state_and_deposit( spec, state, From ef1ef753a3ecff39b8a85cca5c407e7ff17a359d Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Tue, 11 Jun 2019 09:16:37 -0600 Subject: [PATCH 23/39] comment out transfer test from sanity tests --- .../eth2spec/test/sanity/test_blocks.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py index 587c37742..593202dab 100644 --- a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py @@ -303,38 +303,38 @@ def test_voluntary_exit(spec, state): assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH -@with_all_phases -@spec_state_test -def test_transfer(spec, state): +# @with_all_phases +# @spec_state_test +# def test_transfer(spec, state): # overwrite default 0 to test - spec.MAX_TRANSFERS = 1 + # spec.MAX_TRANSFERS = 1 - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] - amount = get_balance(state, sender_index) + # sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + # amount = get_balance(state, sender_index) - transfer = get_valid_transfer(spec, state, state.slot + 1, sender_index, amount, signed=True) - recipient_index = transfer.recipient - pre_transfer_recipient_balance = get_balance(state, recipient_index) + # transfer = get_valid_transfer(spec, state, state.slot + 1, sender_index, amount, signed=True) + # recipient_index = transfer.recipient + # pre_transfer_recipient_balance = get_balance(state, recipient_index) # un-activate so validator can transfer - state.validator_registry[sender_index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH + # state.validator_registry[sender_index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH - yield 'pre', state + # yield 'pre', state # Add to state via block transition - block = build_empty_block_for_next_slot(spec, state) - block.body.transfers.append(transfer) - sign_block(spec, state, block) + # block = build_empty_block_for_next_slot(spec, state) + # block.body.transfers.append(transfer) + # sign_block(spec, state, block) - yield 'blocks', [block], List[spec.BeaconBlock] + # yield 'blocks', [block], List[spec.BeaconBlock] - spec.state_transition(state, block) - yield 'post', state + # spec.state_transition(state, block) + # yield 'post', state - sender_balance = get_balance(state, sender_index) - recipient_balance = get_balance(state, recipient_index) - assert sender_balance == 0 - assert recipient_balance == pre_transfer_recipient_balance + amount + # sender_balance = get_balance(state, sender_index) + # recipient_balance = get_balance(state, recipient_index) + # assert sender_balance == 0 + # assert recipient_balance == pre_transfer_recipient_balance + amount @with_all_phases From cc625c7d6d0c25664b46b119e5389dc7aee3bda9 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Tue, 11 Jun 2019 09:26:23 -0600 Subject: [PATCH 24/39] lint --- test_libs/pyspec/eth2spec/test/sanity/test_blocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py index 593202dab..2275161c8 100644 --- a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py @@ -5,7 +5,7 @@ from eth2spec.utils.ssz.ssz_impl import signing_root from eth2spec.utils.bls import bls_sign from eth2spec.test.helpers.state import get_balance -from eth2spec.test.helpers.transfers import get_valid_transfer +# from eth2spec.test.helpers.transfers import get_valid_transfer from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block from eth2spec.test.helpers.keys import privkeys, pubkeys from eth2spec.test.helpers.attester_slashings import get_valid_attester_slashing From 2d636ddf5acff71808a044148095b28e8bf52522 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 17:27:34 +0200 Subject: [PATCH 25/39] BLS withdrawal byte is formatted as int now, but still one byte. Justin changed spec, now fix tests + configs --- configs/constant_presets/mainnet.yaml | 2 +- configs/constant_presets/minimal.yaml | 2 +- test_libs/pyspec/eth2spec/test/helpers/deposits.py | 2 +- test_libs/pyspec/eth2spec/test/helpers/transfers.py | 2 +- .../test/phase_0/block_processing/test_process_deposit.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/configs/constant_presets/mainnet.yaml b/configs/constant_presets/mainnet.yaml index bca7febc7..dca2a69ba 100644 --- a/configs/constant_presets/mainnet.yaml +++ b/configs/constant_presets/mainnet.yaml @@ -48,7 +48,7 @@ GENESIS_FORK_VERSION: 0x00000000 GENESIS_SLOT: 0 # 2**64 - 1 FAR_FUTURE_EPOCH: 18446744073709551615 -BLS_WITHDRAWAL_PREFIX: 0x00 +BLS_WITHDRAWAL_PREFIX: 0 # Time parameters diff --git a/configs/constant_presets/minimal.yaml b/configs/constant_presets/minimal.yaml index 23bc6e819..fa337cfbf 100644 --- a/configs/constant_presets/minimal.yaml +++ b/configs/constant_presets/minimal.yaml @@ -47,7 +47,7 @@ GENESIS_FORK_VERSION: 0x00000000 GENESIS_SLOT: 0 # 2**64 - 1 FAR_FUTURE_EPOCH: 18446744073709551615 -BLS_WITHDRAWAL_PREFIX: 0x00 +BLS_WITHDRAWAL_PREFIX: 0 # Time parameters diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index b84970044..1d4761753 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -67,7 +67,7 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c # insecurely use pubkey as withdrawal key if no credentials provided if withdrawal_credentials is None: - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.int_to_bytes(spec.BLS_WITHDRAWAL_PREFIX, length=1) + spec.hash(pubkey)[1:] deposit, root, deposit_data_leaves = build_deposit( spec, diff --git a/test_libs/pyspec/eth2spec/test/helpers/transfers.py b/test_libs/pyspec/eth2spec/test/helpers/transfers.py index eea12a36d..4d40deee7 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/transfers.py +++ b/test_libs/pyspec/eth2spec/test/helpers/transfers.py @@ -32,7 +32,7 @@ def get_valid_transfer(spec, state, slot=None, sender_index=None, amount=None, f # ensure withdrawal_credentials reproducible state.validator_registry[transfer.sender].withdrawal_credentials = ( - spec.BLS_WITHDRAWAL_PREFIX + spec.hash(transfer.pubkey)[1:] + spec.int_to_bytes(spec.BLS_WITHDRAWAL_PREFIX, length=1) + spec.hash(transfer.pubkey)[1:] ) return transfer diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py index 42de447ec..603a07c3d 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py @@ -101,7 +101,7 @@ def test_invalid_sig_top_up(spec, state): def test_invalid_withdrawal_credentials_top_up(spec, state): validator_index = 0 amount = spec.MAX_EFFECTIVE_BALANCE // 4 - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(b"junk")[1:] + withdrawal_credentials = spec.int_to_bytes(spec.BLS_WITHDRAWAL_PREFIX, length=1) + spec.hash(b"junk")[1:] deposit = prepare_state_and_deposit( spec, state, From 7fafebd6cf7f0747e162e23ca327d5e3354053e6 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 17:32:42 +0200 Subject: [PATCH 26/39] missed one case, fix withdrawal byte <> int problem --- test_libs/pyspec/eth2spec/test/helpers/genesis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_libs/pyspec/eth2spec/test/helpers/genesis.py b/test_libs/pyspec/eth2spec/test/helpers/genesis.py index 23451b06b..a3ca915ef 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/genesis.py +++ b/test_libs/pyspec/eth2spec/test/helpers/genesis.py @@ -5,7 +5,7 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root def build_mock_validator(spec, i: int, balance: int): pubkey = pubkeys[i] # insecurely use pubkey as withdrawal key as well - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.int_to_bytes(spec.BLS_WITHDRAWAL_PREFIX, length=1) + spec.hash(pubkey)[1:] return spec.Validator( pubkey=pubkeys[i], withdrawal_credentials=withdrawal_credentials, From 85b0c237ca2f58e210d0aa410e4892e4c9be6671 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 17:58:13 +0200 Subject: [PATCH 27/39] fix formatting sanity generator file names --- test_generators/sanity/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_generators/sanity/main.py b/test_generators/sanity/main.py index 805ee6139..fbef4da96 100644 --- a/test_generators/sanity/main.py +++ b/test_generators/sanity/main.py @@ -16,7 +16,7 @@ def create_suite(handler_name: str, config_name: str, get_cases: Callable[[], It spec_phase0.apply_constants_preset(presets) spec_phase1.apply_constants_preset(presets) - return ("%sanity_s_%s" % (handler_name, config_name), handler_name, gen_suite.render_suite( + return ("sanity_%s_%s" % (handler_name, config_name), handler_name, gen_suite.render_suite( title="sanity testing", summary="Sanity test suite, %s type, generated from pytests" % handler_name, forks_timeline="testing", From 22f624e12e5b7765c052b8a82b34b050e65fdada Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 18:08:27 +0200 Subject: [PATCH 28/39] re-order type check for more speed --- test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 368041f90..832fd5ae2 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -513,13 +513,11 @@ def read_vector_elem_type(vector_typ: Type[Vector[T, L]]) -> T: def read_elem_type(typ): - if typ == bytes: + if issubclass(typ, bytes): # bytes or bytesN return byte elif is_list_type(typ): return read_list_elem_type(typ) elif is_vector_type(typ): return read_vector_elem_type(typ) - elif issubclass(typ, bytes): # bytes or bytesN - return byte else: raise TypeError("Unexpected type: {}".format(typ)) From d6fd19ce44f96dc7976ca9f48c43f93048a4478a Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 18:30:50 +0200 Subject: [PATCH 29/39] fix ssz-static generator --- test_generators/ssz_static/main.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test_generators/ssz_static/main.py b/test_generators/ssz_static/main.py index 7de5237d1..9d9af8c5e 100644 --- a/test_generators/ssz_static/main.py +++ b/test_generators/ssz_static/main.py @@ -1,7 +1,10 @@ from random import Random +from inspect import getmembers, isclass + from eth2spec.debug import random_value, encode from eth2spec.phase0 import spec +from eth2spec.utils.ssz.ssz_typing import Container from eth2spec.utils.ssz.ssz_impl import ( hash_tree_root, signing_root, @@ -27,17 +30,23 @@ def create_test_case_contents(value, typ): @to_dict -def create_test_case(rng: Random, name: str, mode: random_value.RandomizationMode, chaos: bool): - typ = spec.get_ssz_type_by_name(name) +def create_test_case(rng: Random, name: str, typ, mode: random_value.RandomizationMode, chaos: bool): value = random_value.get_random_ssz_object(rng, typ, MAX_BYTES_LENGTH, MAX_LIST_LENGTH, mode, chaos) yield name, create_test_case_contents(value, typ) +def get_spec_ssz_types(): + return [ + (name, value) for (name, value) in getmembers(spec, isclass) + if issubclass(value, Container) and value != Container # only the subclasses, not the imported base class + ] + + @to_tuple def ssz_static_cases(rng: Random, mode: random_value.RandomizationMode, chaos: bool, count: int): - for type_name in spec.ssz_types: + for (name, ssz_type) in get_spec_ssz_types(): for i in range(count): - yield create_test_case(rng, type_name, mode, chaos) + yield create_test_case(rng, name, ssz_type, mode, chaos) def get_ssz_suite(seed: int, config_name: str, mode: random_value.RandomizationMode, chaos: bool, cases_if_random: int): @@ -81,8 +90,6 @@ if __name__ == "__main__": settings.append((seed, "mainnet", random_value.RandomizationMode.mode_random, False, 5)) seed += 1 - print("Settings: %d, SSZ-types: %d" % (len(settings), len(spec.ssz_types))) - gen_runner.run_generator("ssz_static", [ get_ssz_suite(seed, config_name, mode, chaos, cases_if_random) for (seed, config_name, mode, chaos, cases_if_random) in settings From 1157d9b8d8f17c475b4bdb34d60c598075e478be Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 18:34:49 +0200 Subject: [PATCH 30/39] fix shuffling generator --- test_generators/shuffling/main.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test_generators/shuffling/main.py b/test_generators/shuffling/main.py index 862c4d910..291aa2c47 100644 --- a/test_generators/shuffling/main.py +++ b/test_generators/shuffling/main.py @@ -1,5 +1,4 @@ -from eth2spec.phase0 import spec as spec_phase0 -from eth2spec.phase1 import spec as spec_phase1 +from eth2spec.phase0 import spec as spec from eth_utils import ( to_dict, to_tuple ) @@ -8,7 +7,7 @@ from preset_loader import loader @to_dict -def shuffling_case(seed: spec.Bytes32, count: int): +def shuffling_case(seed, count): yield 'seed', '0x' + seed.hex() yield 'count', count yield 'shuffled', [spec.get_shuffled_index(i, count, seed) for i in range(count)] @@ -23,8 +22,7 @@ def shuffling_test_cases(): def mini_shuffling_suite(configs_path: str) -> gen_typing.TestSuiteOutput: presets = loader.load_presets(configs_path, 'minimal') - spec_phase0.apply_constants_preset(presets) - spec_phase1.apply_constants_preset(presets) + spec.apply_constants_preset(presets) return ("shuffling_minimal", "core", gen_suite.render_suite( title="Swap-or-Not Shuffling tests with minimal config", @@ -39,8 +37,7 @@ def mini_shuffling_suite(configs_path: str) -> gen_typing.TestSuiteOutput: def full_shuffling_suite(configs_path: str) -> gen_typing.TestSuiteOutput: presets = loader.load_presets(configs_path, 'mainnet') - spec_phase0.apply_constants_preset(presets) - spec_phase1.apply_constants_preset(presets) + spec.apply_constants_preset(presets) return ("shuffling_full", "core", gen_suite.render_suite( title="Swap-or-Not Shuffling tests with mainnet config", From f4814862fe3245f43bbdb3570fe6eda88207e217 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 19:23:45 +0200 Subject: [PATCH 31/39] fix typing check, add zero-hash cache to hash function --- test_libs/pyspec/eth2spec/utils/hash_function.py | 16 +++++++++++++++- .../pyspec/eth2spec/utils/ssz/ssz_typing.py | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/test_libs/pyspec/eth2spec/utils/hash_function.py b/test_libs/pyspec/eth2spec/utils/hash_function.py index f965827d0..4aecc57f8 100644 --- a/test_libs/pyspec/eth2spec/utils/hash_function.py +++ b/test_libs/pyspec/eth2spec/utils/hash_function.py @@ -1,5 +1,19 @@ from hashlib import sha256 +ZERO_BYTES32 = b'\x00' * 32 + +def _hash(x): + return sha256(x).digest() + +zerohashes = [(None, ZERO_BYTES32)] +for layer in range(1, 32): + k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1] + zerohashes.append((k, _hash(k))) +zerohashes = zerohashes[1:] + def hash(x): - return sha256(x).digest() + for (k, h) in zerohashes: + if x == k: + return h + return _hash(x) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 832fd5ae2..55ced4ee2 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -513,7 +513,7 @@ def read_vector_elem_type(vector_typ: Type[Vector[T, L]]) -> T: def read_elem_type(typ): - if issubclass(typ, bytes): # bytes or bytesN + if typ == bytes or (isinstance(typ, type) and issubclass(typ, bytes)): # bytes or bytesN return byte elif is_list_type(typ): return read_list_elem_type(typ) From 193b628fb4fb7c3ef08cf160a9bbafade61a839e Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Tue, 11 Jun 2019 19:03:26 +0100 Subject: [PATCH 32/39] Bypass Vyper compiler bug --- deposit_contract/contracts/validator_registration.json | 2 +- deposit_contract/contracts/validator_registration.v.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json index 1779423d2..1988b28c0 100644 --- a/deposit_contract/contracts/validator_registration.json +++ b/deposit_contract/contracts/validator_registration.json @@ -1 +1 @@ -{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}, {"type": "bytes", "name": "index", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10433}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1334984}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b505061134056600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610277575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100da578060000360020a82046100e1565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561010c57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610155578060000360020a820461015c565b8060020a82025b905090506101a0525b81516001018083528114156100bd575b50506018600860208206610200016020828401111561019357600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101d057600080fd5b50506102c05160206001820306601f82010390506103206102c0516008818352015b826103205111156102025761021e565b6000610320516102e001535b81516001018083528114156101f2575b50505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102535761026f565b602061028051036102a001516020610280510361028052610241565b610160515650005b63c5f2892f60005114156103cf57341561029057600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561032a57600061018051602081106102cb57600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af161031c57600080fd5b60c051905061014052610398565b6000610140516020826101a0010152602081019050610180516020811061035057600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161038e57600080fd5b60c0519050610140525b61016060026103a657600080fd5b60028151048152505b81516001018083528114156102a9575b50506101405160005260206000f3005b63621fd13060005114156104e15734156103e857600080fd5b63806732896101405260015461016052610160516006580161009b565b506101c0526000610220525b6101c05160206001820306601f8201039050610220511015156104335761044c565b610220516101e001526102205160200161022052610411565b6101c0805160200180610280828460006004600a8704601201f161046f57600080fd5b50506102805160206001820306601f82010390506102e0610280516008818352015b826102e05111156104a1576104bd565b60006102e0516102a001535b8151600101808352811415610491575b5050506020610260526040610280510160206001820306601f8201039050610260f3005b63c47e300d60005114156111b557606060046101403760506004356004016101a037603060043560040135111561051757600080fd5b604060243560040161022037602060243560040135111561053757600080fd5b608060443560040161028037606060443560040135111561055757600080fd5b63ffffffff6001541061056957600080fd5b633b9aca00610340526103405161057f57600080fd5b61034051340461032052633b9aca0061032051101561059d57600080fd5b60306101a051146105ad57600080fd5b602061022051146105bd57600080fd5b606061028051146105cd57600080fd5b6101a0516101c0516101e05161020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a05163806732896103c052610320516103e0526103e0516006580161009b565b506104405260006104a0525b6104405160206001820306601f82010390506104a05110151561065d57610676565b6104a05161046001526104a0516020016104a05261063b565b6103a05261038052610360526103405261032052610300526102e0526102c0526102a05261028052610260526102405261022052610200526101e0526101c0526101a052610440805160200180610360828460006004600a8704601201f16106dd57600080fd5b505060006101a060308060208461058001018260208501600060046016f15050805182019150506000601060208206610500016020828401111561072057600080fd5b602080610520826104c0600060046015f150508181528090509050905060108060208461058001018260208501600060046013f150508051820191505080610580526105809050602060c0825160208401600060025af161078057600080fd5b60c05190506104e05260006000604060208206610620016102805182840111156107a957600080fd5b606080610640826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af16107e957600080fd5b60c0519050602082610820010152602081019050600060406020602082066106e00161028051828401111561081d57600080fd5b606080610700826020602088068803016102800160006004601bf15050818152809050905090506020806020846107a001018260208501600060046015f15050805182019150506104c0516020826107a0010152602081019050806107a0526107a09050602060c0825160208401600060025af161089a57600080fd5b60c051905060208261082001015260208101905080610820526108209050602060c0825160208401600060025af16108d157600080fd5b60c051905061060052600060006104e0516020826108c00101526020810190506102206020806020846108c001018260208501600060046015f1505080518201915050806108c0526108c09050602060c0825160208401600060025af161093757600080fd5b60c0519050602082610a4001015260208101905060006103606008806020846109c001018260208501600060046012f15050805182019150506000601860208206610940016020828401111561098c57600080fd5b602080610960826104c0600060046015f15050818152809050905090506018806020846109c001018260208501600060046014f1505080518201915050610600516020826109c0010152602081019050806109c0526109c09050602060c0825160208401600060025af16109ff57600080fd5b60c0519050602082610a4001015260208101905080610a4052610a409050602060c0825160208401600060025af1610a3657600080fd5b60c05190506108a0526101a0516101c0516101e05161020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a0516103c0516103e05161040051610420516104405161046051610480516104a0516104c0516104e05161050051610520516105405161056051610580516105a0516105c0516105e05161060051610620516106405161066051610680516106a0516106c0516106e05161070051610720516107405161076051610780516107a0516107c0516107e05161080051610820516108405161086051610880516108a0516108c0516108e05161090051610920516109405161096051610980516109a0516109c0516109e051610a0051610a2051610a4051610a6051610a8051610aa0516380673289610ac052600154610ae052610ae0516006580161009b565b50610b40526000610ba0525b610b405160206001820306601f8201039050610ba051101515610bae57610bc7565b610ba051610b600152610ba051602001610ba052610b8c565b610aa052610a8052610a6052610a4052610a2052610a00526109e0526109c0526109a05261098052610960526109405261092052610900526108e0526108c0526108a05261088052610860526108405261082052610800526107e0526107c0526107a05261078052610760526107405261072052610700526106e0526106c0526106a05261068052610660526106405261062052610600526105e0526105c0526105a05261058052610560526105405261052052610500526104e0526104c0526104a05261048052610460526104405261042052610400526103e0526103c0526103a05261038052610360526103405261032052610300526102e0526102c0526102a05261028052610260526102405261022052610200526101e0526101c0526101a052610b40805160200180610bc0828460006004600a8704601201f1610d0e57600080fd5b505060a0610c4052610c4051610c80526101a0805160200180610c4051610c8001828460006004600a8704601201f1610d4657600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516040818352015b83610c2051101515610d8457610da1565b6000610c20516020850101535b8151600101808352811415610d73575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610ca052610220805160200180610c4051610c8001828460006004600a8704601201f1610df857600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516020818352015b83610c2051101515610e3657610e53565b6000610c20516020850101535b8151600101808352811415610e25575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610cc052610360805160200180610c4051610c8001828460006004600a8704601201f1610eaa57600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516020818352015b83610c2051101515610ee857610f05565b6000610c20516020850101535b8151600101808352811415610ed7575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610ce052610280805160200180610c4051610c8001828460006004600a8704601201f1610f5c57600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516060818352015b83610c2051101515610f9a57610fb7565b6000610c20516020850101535b8151600101808352811415610f89575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c4052610c4051610d0052610bc0805160200180610c4051610c8001828460006004600a8704601201f161100e57600080fd5b5050610c4051610c80015160206001820306601f8201039050610c4051610c8001610c2081516020818352015b83610c205110151561104c57611069565b6000610c20516020850101535b815160010180835281141561103b575b505050506020610c4051610c80015160206001820306601f8201039050610c40510101610c40527fdc5fc95703516abd38fa03c3737ff3b52dc52347055c8028460fdf5bbe2f12ce610c4051610c80a160018054600182540110156110cd57600080fd5b6001815401815550600154610d2052610d4060006020818352015b60016001610d205116141561111d576108a051610d40516020811061110c57600080fd5b600060c052602060c02001556111b1565b6000610d40516020811061113057600080fd5b600060c052602060c0200154602082610d600101526020810190506108a051602082610d6001015260208101905080610d6052610d609050602060c0825160208401600060025af161118157600080fd5b60c05190506108a052610d20600261119857600080fd5b60028151048152505b81516001018083528114156110e8575b5050005b60006000fd5b61018561134003610185600039610185611340036000f3"} \ No newline at end of file +{"abi": [{"name": "Deposit", "inputs": [{"type": "bytes", "name": "pubkey", "indexed": false}, {"type": "bytes", "name": "withdrawal_credentials", "indexed": false}, {"type": "bytes", "name": "amount", "indexed": false}, {"type": "bytes", "name": "signature", "indexed": false}, {"type": "bytes", "name": "index", "indexed": false}], "anonymous": false, "type": "event"}, {"outputs": [], "inputs": [], "constant": false, "payable": false, "type": "constructor"}, {"name": "get_deposit_root", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 79221}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 10433}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}], "constant": false, "payable": true, "type": "function", "gas": 1334417}], "bytecode": "0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6101406000601f818352015b600061014051602081106100bd57600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e957600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012757600080fd5b60c0519050606051600161014051018060405190131561014657600080fd5b809190121561015457600080fd5b6020811061016157600080fd5b600260c052602060c02001555b81516001018083528114156100aa575b50506111c656600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610277575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100da578060000360020a82046100e1565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561010c57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610155578060000360020a820461015c565b8060020a82025b905090506101a0525b81516001018083528114156100bd575b50506018600860208206610200016020828401111561019357600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101d057600080fd5b50506102c05160206001820306601f82010390506103206102c0516008818352015b826103205111156102025761021e565b6000610320516102e001535b81516001018083528114156101f2575b50505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102535761026f565b602061028051036102a001516020610280510361028052610241565b610160515650005b63c5f2892f60005114156103cf57341561029057600080fd5b6000610140526001546101605261018060006020818352015b600160016101605116141561032a57600061018051602081106102cb57600080fd5b600060c052602060c02001546020826102200101526020810190506101405160208261022001015260208101905080610220526102209050602060c0825160208401600060025af161031c57600080fd5b60c051905061014052610398565b6000610140516020826101a0010152602081019050610180516020811061035057600080fd5b600260c052602060c02001546020826101a0010152602081019050806101a0526101a09050602060c0825160208401600060025af161038e57600080fd5b60c0519050610140525b61016060026103a657600080fd5b60028151048152505b81516001018083528114156102a9575b50506101405160005260206000f3005b63621fd13060005114156104e15734156103e857600080fd5b63806732896101405260015461016052610160516006580161009b565b506101c0526000610220525b6101c05160206001820306601f8201039050610220511015156104335761044c565b610220516101e001526102205160200161022052610411565b6101c0805160200180610280828460006004600a8704601201f161046f57600080fd5b50506102805160206001820306601f82010390506102e0610280516008818352015b826102e05111156104a1576104bd565b60006102e0516102a001535b8151600101808352811415610491575b5050506020610260526040610280510160206001820306601f8201039050610260f3005b63c47e300d600051141561103b57606060046101403760506004356004016101a037603060043560040135111561051757600080fd5b604060243560040161022037602060243560040135111561053757600080fd5b608060443560040161028037606060443560040135111561055757600080fd5b63ffffffff6001541061056957600080fd5b633b9aca00610340526103405161057f57600080fd5b61034051340461032052633b9aca0061032051101561059d57600080fd5b60306101a051146105ad57600080fd5b602061022051146105bd57600080fd5b606061028051146105cd57600080fd5b6101a0516101c0516101e05161020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a05163806732896103c052610320516103e0526103e0516006580161009b565b506104405260006104a0525b6104405160206001820306601f82010390506104a05110151561065d57610676565b6104a05161046001526104a0516020016104a05261063b565b6103a05261038052610360526103405261032052610300526102e0526102c0526102a05261028052610260526102405261022052610200526101e0526101c0526101a052610440805160200180610360828460006004600a8704601201f16106dd57600080fd5b50506101a0516101c0516101e05161020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a0516103c0516103e05161040051610420516104405161046051610480516104a05163806732896104c0526001546104e0526104e0516006580161009b565b506105405260006105a0525b6105405160206001820306601f82010390506105a05110151561078e576107a7565b6105a05161056001526105a0516020016105a05261076c565b6104a05261048052610460526104405261042052610400526103e0526103c0526103a05261038052610360526103405261032052610300526102e0526102c0526102a05261028052610260526102405261022052610200526101e0526101c0526101a0526105408051602001806105c0828460006004600a8704601201f161082e57600080fd5b505060a06106405261064051610680526101a08051602001806106405161068001828460006004600a8704601201f161086657600080fd5b505061064051610680015160206001820306601f8201039050610640516106800161062081516040818352015b83610620511015156108a4576108c1565b6000610620516020850101535b8151600101808352811415610893575b50505050602061064051610680015160206001820306601f820103905061064051010161064052610640516106a0526102208051602001806106405161068001828460006004600a8704601201f161091857600080fd5b505061064051610680015160206001820306601f8201039050610640516106800161062081516020818352015b836106205110151561095657610973565b6000610620516020850101535b8151600101808352811415610945575b50505050602061064051610680015160206001820306601f820103905061064051010161064052610640516106c0526103608051602001806106405161068001828460006004600a8704601201f16109ca57600080fd5b505061064051610680015160206001820306601f8201039050610640516106800161062081516020818352015b8361062051101515610a0857610a25565b6000610620516020850101535b81516001018083528114156109f7575b50505050602061064051610680015160206001820306601f820103905061064051010161064052610640516106e0526102808051602001806106405161068001828460006004600a8704601201f1610a7c57600080fd5b505061064051610680015160206001820306601f8201039050610640516106800161062081516060818352015b8361062051101515610aba57610ad7565b6000610620516020850101535b8151600101808352811415610aa9575b50505050602061064051610680015160206001820306601f82010390506106405101016106405261064051610700526105c08051602001806106405161068001828460006004600a8704601201f1610b2e57600080fd5b505061064051610680015160206001820306601f8201039050610640516106800161062081516020818352015b8361062051101515610b6c57610b89565b6000610620516020850101535b8151600101808352811415610b5b575b50505050602061064051610680015160206001820306601f8201039050610640510101610640527fdc5fc95703516abd38fa03c3737ff3b52dc52347055c8028460fdf5bbe2f12ce61064051610680a160006107205260006101a06030806020846107e001018260208501600060046016f150508051820191505060006010602082066107600160208284011115610c2057600080fd5b60208061078082610720600060046015f15050818152809050905090506010806020846107e001018260208501600060046013f1505080518201915050806107e0526107e09050602060c0825160208401600060025af1610c8057600080fd5b60c0519050610740526000600060406020820661088001610280518284011115610ca957600080fd5b6060806108a0826020602088068803016102800160006004601bf1505081815280905090509050602060c0825160208401600060025af1610ce957600080fd5b60c0519050602082610a800101526020810190506000604060206020820661094001610280518284011115610d1d57600080fd5b606080610960826020602088068803016102800160006004601bf1505081815280905090509050602080602084610a0001018260208501600060046015f150508051820191505061072051602082610a0001015260208101905080610a0052610a009050602060c0825160208401600060025af1610d9a57600080fd5b60c0519050602082610a8001015260208101905080610a8052610a809050602060c0825160208401600060025af1610dd157600080fd5b60c0519050610860526000600061074051602082610b20010152602081019050610220602080602084610b2001018260208501600060046015f150508051820191505080610b2052610b209050602060c0825160208401600060025af1610e3757600080fd5b60c0519050602082610ca00101526020810190506000610360600880602084610c2001018260208501600060046012f15050805182019150506000601860208206610ba00160208284011115610e8c57600080fd5b602080610bc082610720600060046015f1505081815280905090509050601880602084610c2001018260208501600060046014f150508051820191505061086051602082610c2001015260208101905080610c2052610c209050602060c0825160208401600060025af1610eff57600080fd5b60c0519050602082610ca001015260208101905080610ca052610ca09050602060c0825160208401600060025af1610f3657600080fd5b60c0519050610b00526001805460018254011015610f5357600080fd5b6001815401815550600154610d2052610d4060006020818352015b60016001610d2051161415610fa357610b0051610d405160208110610f9257600080fd5b600060c052602060c0200155611037565b6000610d405160208110610fb657600080fd5b600060c052602060c0200154602082610d60010152602081019050610b0051602082610d6001015260208101905080610d6052610d609050602060c0825160208401600060025af161100757600080fd5b60c0519050610b0052610d20600261101e57600080fd5b60028151048152505b8151600101808352811415610f6e575b5050005b60006000fd5b6101856111c6036101856000396101856111c6036000f3"} \ No newline at end of file diff --git a/deposit_contract/contracts/validator_registration.v.py b/deposit_contract/contracts/validator_registration.v.py index 10daf42b9..14f100520 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -75,9 +75,12 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH assert len(signature) == SIGNATURE_LENGTH - # Compute `DepositData` root + # Emit `Deposit` log amount: bytes[8] = self.to_little_endian_64(deposit_amount) - zero_bytes32: bytes32 + log.Deposit(pubkey, withdrawal_credentials, amount, signature, self.to_little_endian_64(self.deposit_count)) + + # Compute `DepositData` root + zero_bytes32: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH))) signature_root: bytes32 = sha256(concat( sha256(slice(signature, start=0, len=64)), @@ -88,9 +91,6 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)), )) - # Emit `Deposit` log - log.Deposit(pubkey, withdrawal_credentials, amount, signature, self.to_little_endian_64(self.deposit_count)) - # Add `DepositData` root to Merkle tree (update a single `branch` node) self.deposit_count += 1 size: uint256 = self.deposit_count From 9ec395c04fedf9572b1bfd815c6da1f9bd4f4b1c Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 21:53:38 +0200 Subject: [PATCH 33/39] fig linting + improve docs + structure of hash optimization --- .../pyspec/eth2spec/utils/hash_function.py | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test_libs/pyspec/eth2spec/utils/hash_function.py b/test_libs/pyspec/eth2spec/utils/hash_function.py index 4aecc57f8..2c9b5a579 100644 --- a/test_libs/pyspec/eth2spec/utils/hash_function.py +++ b/test_libs/pyspec/eth2spec/utils/hash_function.py @@ -2,18 +2,27 @@ from hashlib import sha256 ZERO_BYTES32 = b'\x00' * 32 + def _hash(x): return sha256(x).digest() -zerohashes = [(None, ZERO_BYTES32)] -for layer in range(1, 32): - k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1] - zerohashes.append((k, _hash(k))) -zerohashes = zerohashes[1:] + +# Minimal collection of (key, value) pairs, for fast hash-retrieval, to save on repetitive computation cost. +# Key = the hash input +# Value = the hash output +hash_cache = [] + + +def add_zero_hashes_to_cache(): + zerohashes = [(None, ZERO_BYTES32)] + for layer in range(1, 32): + k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1] + zerohashes.append((k, _hash(k))) + hash_cache.extend(zerohashes[1:]) def hash(x): - for (k, h) in zerohashes: + for (k, h) in hash_cache: if x == k: return h return _hash(x) From f095ab43bb2df7934ef03fcaca3be1b87ea058e7 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 13 Jun 2019 10:40:02 +0100 Subject: [PATCH 34/39] Update 0_beacon-chain.md (#1170) minor typo fix --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index c25d43d96..e594ab898 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1158,7 +1158,7 @@ def slash_validator(state: BeaconState, ### Genesis trigger -Before genesis has been triggered and whenever the deposit contract emits a `Deposit`, log call the function `is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool` where: +Before genesis has been triggered and whenever the deposit contract emits a `Deposit` log, call the function `is_genesis_trigger(deposits: List[Deposit], timestamp: uint64) -> bool` where: * `deposits` is the list of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log * `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log From aabd2b08adcc2771f741a50f71cb7120645487f0 Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 13 Jun 2019 15:46:22 +0200 Subject: [PATCH 35/39] attestations: check shard, and check epoch earlier --- specs/core/0_beacon-chain.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index e594ab898..86df18898 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1722,6 +1722,10 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: Process ``Attestation`` operation. """ data = attestation.data + + assert data.crosslink.shard < SHARD_COUNT + assert data.target_epoch in (get_previous_epoch(state), get_current_epoch(state)) + attestation_slot = get_attestation_data_slot(state, data) assert attestation_slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= attestation_slot + SLOTS_PER_EPOCH @@ -1732,7 +1736,6 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: proposer_index=get_beacon_proposer_index(state), ) - assert data.target_epoch in (get_previous_epoch(state), get_current_epoch(state)) if data.target_epoch == get_current_epoch(state): ffg_data = (state.current_justified_epoch, state.current_justified_root, get_current_epoch(state)) parent_crosslink = state.current_crosslinks[data.crosslink.shard] From 67d2585ec0e24a0b59d1d5db0804faf04ac3f128 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 13 Jun 2019 21:01:10 +0100 Subject: [PATCH 36/39] Fix #1173 The bug is that it's possible to include a participating validator which has custody bit one *without* specifying that validator in `attestation.aggregation_bitfield`. In other words, we want to check that every bit in `custody_bitfield` is zero whenever the corresponding bit in `aggregation_bitfield` is zero. Well spotted @protolambda --- specs/core/0_beacon-chain.md | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 86df18898..0725f5211 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -991,6 +991,7 @@ def convert_to_indexed(state: BeaconState, attestation: Attestation) -> IndexedA """ attesting_indices = get_attesting_indices(state, attestation.data, attestation.aggregation_bitfield) custody_bit_1_indices = get_attesting_indices(state, attestation.data, attestation.custody_bitfield) + assert set(custody_bit_1_indices).issubset(attesting_indices) custody_bit_0_indices = [index for index in attesting_indices if index not in custody_bit_1_indices] return IndexedAttestation( From ed748a7d769e3d81697a2df92f1638cccf9c5057 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sat, 15 Jun 2019 15:09:50 +0100 Subject: [PATCH 37/39] Address Danny's comments --- scripts/build_spec.py | 2 +- specs/core/0_beacon-chain.md | 14 +++++++------- specs/core/0_fork-choice.md | 4 ++-- test_libs/pyspec/eth2spec/test/helpers/block.py | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index e25147a7f..eae9b60d5 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -62,7 +62,6 @@ from eth2spec.utils.bls import ( from eth2spec.utils.hash_function import hash ''' -BYTE_TYPES = [4, 32, 48, 96] NEW_TYPES = { 'Slot': 'int', 'Epoch': 'int', @@ -70,6 +69,7 @@ NEW_TYPES = { 'ValidatorIndex': 'int', 'Gwei': 'int', } +BYTE_TYPES = [4, 32, 48, 96] SUNDRY_FUNCTIONS = ''' def get_ssz_type_by_name(name: str) -> Container: return globals()[name] diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index b4049bb30..367a1cceb 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -493,7 +493,7 @@ class BeaconState(Container): slot: Slot fork: Fork # History - parent_block_header: BeaconBlockHeader + latest_block_header: BeaconBlockHeader block_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] state_roots: Vector[Hash, SLOTS_PER_HISTORICAL_ROOT] historical_roots: List[Hash] @@ -1152,7 +1152,7 @@ def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis state = BeaconState( genesis_time=genesis_time, eth1_data=genesis_eth1_data, - parent_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), + latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), ) # Process genesis deposits @@ -1212,11 +1212,11 @@ def process_slot(state: BeaconState) -> None: state.state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_state_root # Cache latest block header state root - if state.parent_block_header.state_root == ZERO_HASH: - state.parent_block_header.state_root = previous_state_root + if state.latest_block_header.state_root == ZERO_HASH: + state.latest_block_header.state_root = previous_state_root # Cache block root - previous_block_root = signing_root(state.parent_block_header) + previous_block_root = signing_root(state.latest_block_header) state.block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root ``` @@ -1556,9 +1556,9 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None: # Verify that the slots match assert block.slot == state.slot # Verify that the parent matches - assert block.parent_root == signing_root(state.parent_block_header) + assert block.parent_root == signing_root(state.latest_block_header) # Save current block as the new latest block - state.parent_block_header = BeaconBlockHeader( + state.latest_block_header = BeaconBlockHeader( slot=block.slot, parent_root=block.parent_root, body_root=hash_tree_root(block.body), diff --git a/specs/core/0_fork-choice.md b/specs/core/0_fork-choice.md index 739c06d59..8103bbeb2 100644 --- a/specs/core/0_fork-choice.md +++ b/specs/core/0_fork-choice.md @@ -68,8 +68,8 @@ def get_ancestor(store: Store, block: BeaconBlock, slot: Slot) -> BeaconBlock: return get_ancestor(store, store.get_parent(block), slot) ``` -* Let `get_attestation(store: Store, index: ValidatorIndex) -> Attestation` be the attestation with the highest slot number in `store` from the validator with the given `index`. If several such attestations exist, use the one the validator `v` observed first. -* Let `get_attestation_target(store: Store, index: ValidatorIndex) -> BeaconBlock` be the target block in the attestation `get_attestation(store, index)`. +* Let `get_latest_attestation(store: Store, index: ValidatorIndex) -> Attestation` be the attestation with the highest slot number in `store` from the validator with the given `index`. If several such attestations exist, use the one the validator `v` observed first. +* Let `get_attestation_target(store: Store, index: ValidatorIndex) -> BeaconBlock` be the target block in the attestation `get_latest_attestation(store, index)`. * Let `get_children(store: Store, block: BeaconBlock) -> List[BeaconBlock]` return the child blocks of the given `block`. * Let `justified_head_state` be the resulting `BeaconState` object from processing the chain up to the `justified_head`. * The `head` is `lmd_ghost(store, justified_head_state, justified_head)` where the function `lmd_ghost` is defined below. Note that the implementation below is suboptimal; there are implementations that compute the head in time logarithmic in slot count. diff --git a/test_libs/pyspec/eth2spec/test/helpers/block.py b/test_libs/pyspec/eth2spec/test/helpers/block.py index 03f8d5b7e..16249fe93 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/block.py +++ b/test_libs/pyspec/eth2spec/test/helpers/block.py @@ -58,7 +58,7 @@ def build_empty_block(spec, state, slot=None, signed=False): empty_block = spec.BeaconBlock() empty_block.slot = slot empty_block.body.eth1_data.deposit_count = state.eth1_deposit_index - previous_block_header = deepcopy(state.parent_block_header) + previous_block_header = deepcopy(state.latest_block_header) if previous_block_header.state_root == spec.ZERO_HASH: previous_block_header.state_root = state.hash_tree_root() empty_block.parent_root = signing_root(previous_block_header) From 7cd7659a4b68ad515231ba635a800ae69e5f6798 Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sat, 15 Jun 2019 15:51:17 +0100 Subject: [PATCH 38/39] Add comments to non-obvious container fields --- specs/core/0_beacon-chain.md | 40 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 367a1cceb..cc4500d3f 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -283,7 +283,7 @@ The following types are [SimpleSerialize (SSZ)](../simple-serialize.md) containe class Fork(Container): previous_version: Bytes4 current_version: Bytes4 - epoch: Epoch + epoch: Epoch # Epoch of latest fork ``` #### `Validator` @@ -291,14 +291,14 @@ class Fork(Container): ```python class Validator(Container): pubkey: BLSPubkey - withdrawal_credentials: Hash - effective_balance: Gwei + withdrawal_credentials: Hash # Commitment to pubkey for withdrawals and transfers + effective_balance: Gwei # Balance at stake slashed: bool # Status epochs - activation_eligibility_epoch: Epoch + activation_eligibility_epoch: Epoch # When criteria for activation were met activation_epoch: Epoch exit_epoch: Epoch - withdrawable_epoch: Epoch + withdrawable_epoch: Epoch # When validator can withdraw or transfer funds ``` #### `Crosslink` @@ -333,7 +333,7 @@ class AttestationData(Container): ```python class AttestationDataAndCustodyBit(Container): data: AttestationData - custody_bit: bool + custody_bit: bool # Challengeable bit for the custody of crosslink data ``` #### `IndexedAttestation` @@ -350,7 +350,7 @@ class IndexedAttestation(Container): ```python class PendingAttestation(Container): - aggregation_bitfield: bytes + aggregation_bitfield: bytes # Bit set for every attesting participant within a committee data: AttestationData inclusion_delay: Slot proposer_index: ValidatorIndex @@ -427,7 +427,7 @@ class Attestation(Container): ```python class Deposit(Container): - proof: Vector[Hash, DEPOSIT_CONTRACT_TREE_DEPTH] + proof: Vector[Hash, DEPOSIT_CONTRACT_TREE_DEPTH] # Merkle path to deposit root data: DepositData ``` @@ -435,7 +435,7 @@ class Deposit(Container): ```python class VoluntaryExit(Container): - epoch: Epoch + epoch: Epoch # Earliest epoch when voluntary exit can be processed validator_index: ValidatorIndex signature: BLSSignature ``` @@ -448,9 +448,9 @@ class Transfer(Container): recipient: ValidatorIndex amount: Gwei fee: Gwei - slot: Slot - pubkey: BLSPubkey - signature: BLSSignature + slot: Slot # Slot at which transfer must be processed + pubkey: BLSPubkey # Withdrawal pubkey + signature: BLSSignature # Signature checked against withdrawal pubkey ``` ### Beacon blocks @@ -460,8 +460,8 @@ class Transfer(Container): ```python class BeaconBlockBody(Container): randao_reveal: BLSSignature - eth1_data: Eth1Data - graffiti: Bytes32 + eth1_data: Eth1Data # Eth1 data vote + graffiti: Bytes32 # Arbitrary data # Operations proposer_slashings: List[ProposerSlashing] attester_slashings: List[AttesterSlashing] @@ -507,21 +507,21 @@ class BeaconState(Container): # Shuffling start_shard: Shard randao_mixes: Vector[Hash, RANDAO_MIXES_LENGTH] - active_index_roots: Vector[Hash, ACTIVE_INDEX_ROOTS_LENGTH] + active_index_roots: Vector[Hash, ACTIVE_INDEX_ROOTS_LENGTH] # Digests of the active registry, for light clients # Slashings - slashed_balances: Vector[Gwei, SLASHED_EXIT_LENGTH] + slashed_balances: Vector[Gwei, SLASHED_EXIT_LENGTH] # Sums of the effective balances of slashed validators # Attestations previous_epoch_attestations: List[PendingAttestation] current_epoch_attestations: List[PendingAttestation] # Crosslinks - previous_crosslinks: Vector[Crosslink, SHARD_COUNT] + previous_crosslinks: Vector[Crosslink, SHARD_COUNT] # Previous epoch snapshot current_crosslinks: Vector[Crosslink, SHARD_COUNT] # Justification - previous_justified_epoch: Epoch - previous_justified_root: Hash + previous_justified_epoch: Epoch # Previous epoch snapshot + previous_justified_root: Hash # Previous epoch snapshot current_justified_epoch: Epoch current_justified_root: Hash - justification_bitfield: uint64 + justification_bitfield: uint64 # Bit set for every recent justified epoch # Finality finalized_epoch: Epoch finalized_root: Hash From 75b469281e870523a4145e8c7bdcf2da9a166aac Mon Sep 17 00:00:00 2001 From: protolambda Date: Sat, 15 Jun 2019 18:05:01 +0200 Subject: [PATCH 39/39] fix linting issue --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index cc4500d3f..968098895 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -298,7 +298,7 @@ class Validator(Container): activation_eligibility_epoch: Epoch # When criteria for activation were met activation_epoch: Epoch exit_epoch: Epoch - withdrawable_epoch: Epoch # When validator can withdraw or transfer funds + withdrawable_epoch: Epoch # When validator can withdraw or transfer funds ``` #### `Crosslink`