From 8ec1e1ff06c27a3a7b985f72437e7dc2f20f9ecd Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Tue, 26 Nov 2019 20:40:58 +0100 Subject: [PATCH 1/5] Vyper b12->b13 --- deposit_contract/contracts/validator_registration.json | 2 +- deposit_contract/contracts/validator_registration.v.py | 4 ++-- deposit_contract/requirements-testing.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json index fbf20e74c9..cd20042335 100644 --- a/deposit_contract/contracts/validator_registration.json +++ b/deposit_contract/contracts/validator_registration.json @@ -1 +1 @@ -{"abi": [{"name": "DepositEvent", "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": 95389}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 17683}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}, {"type": "bytes32", "name": "deposit_data_root"}], "constant": false, "payable": true, "type": "function", "gas": 1754607}], "bytecode": "0x740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009857600080fd5b6101406000601f818352015b600061014051602081106100b757600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e357600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012157600080fd5b60c0519050606051600161014051018060405190131561014057600080fd5b809190121561014e57600080fd5b6020811061015b57600080fd5b600260c052602060c02001555b81516001018083528114156100a4575b50506111d656600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610265575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100da578060000360020a82046100e1565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561010c57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610155578060000360020a820461015c565b8060020a82025b905090506101a0525b81516001018083528114156100bd575b50506018600860208206610200016020828401111561019357600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101d057600080fd5b50506103206102c0516020818352015b60206103205111156101f15761020d565b6000610320516102e001535b81516001018083528114156101e0575b505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102415761025d565b602061028051036102a00151602061028051036102805261022f565b610160515650005b63c5f2892f60005114156104f757341561027e57600080fd5b6000610140526101405161016052600154610180526101a060006020818352015b60016001610180511614156103205760006101a051602081106102c157600080fd5b600060c052602060c02001546020826102400101526020810190506101605160208261024001015260208101905080610240526102409050602060c0825160208401600060025af161031257600080fd5b60c05190506101605261038e565b6000610160516020826101c00101526020810190506101a0516020811061034657600080fd5b600260c052602060c02001546020826101c0010152602081019050806101c0526101c09050602060c0825160208401600060025af161038457600080fd5b60c0519050610160525b610180600261039c57600080fd5b60028151048152505b815160010180835281141561029f575b505060006101605160208261046001015260208101905061014051610160516101805163806732896102e05260015461030052610300516006580161009b565b506103605260006103c0525b6103605160206001820306601f82010390506103c0511015156104235761043c565b6103c05161038001526103c0516020016103c052610401565b61018052610160526101405261036060088060208461046001018260208501600060046012f150508051820191505060006018602082066103e0016020828401111561048757600080fd5b60208061040082610140600060046015f150508181528090509050905060188060208461046001018260208501600060046014f150508051820191505080610460526104609050602060c0825160208401600060025af16104e757600080fd5b60c051905060005260206000f350005b63621fd13060005114156105f857341561051057600080fd5b63806732896101405260015461016052610160516006580161009b565b506101c0526000610220525b6101c05160206001820306601f82010390506102205110151561055b57610574565b610220516101e001526102205160200161022052610539565b6101c0805160200180610280828460006004600a8704601201f161059757600080fd5b50506102e0610280516020818352015b60206102e05111156105b8576105d4565b60006102e0516102a001535b81516001018083528114156105a7575b50506020610260526040610280510160206001820306601f8201039050610260f350005b6322895118600051141561105157605060043560040161014037603060043560040135111561062657600080fd5b60406024356004016101c037602060243560040135111561064657600080fd5b608060443560040161022037606060443560040135111561066657600080fd5b63ffffffff6001541061067857600080fd5b633b9aca006102e0526102e05161068e57600080fd5b6102e05134046102c052633b9aca006102c05110156106ac57600080fd5b603061014051146106bc57600080fd5b60206101c051146106cc57600080fd5b606061022051146106dc57600080fd5b610140610360525b61036051516020610360510161036052610360610360511015610706576106e4565b6380673289610380526102c0516103a0526103a0516006580161009b565b50610400526000610460525b6104005160206001820306601f8201039050610460511015156107525761076b565b6104605161042001526104605160200161046052610730565b610340610360525b610360515260206103605103610360526101406103605110151561079657610773565b610400805160200180610300828460006004600a8704601201f16107b957600080fd5b5050610140610480525b610480515160206104805101610480526104806104805110156107e5576107c3565b63806732896104a0526001546104c0526104c0516006580161009b565b50610520526000610580525b6105205160206001820306601f82010390506105805110151561083057610849565b610580516105400152610580516020016105805261080e565b610460610480525b610480515260206104805103610480526101406104805110151561087457610851565b6105208051602001806105a0828460006004600a8704601201f161089757600080fd5b505060a06106205261062051610660526101408051602001806106205161066001828460006004600a8704601201f16108cf57600080fd5b5050610600610620516106600151610240818352015b6102406106005111156108f757610918565b600061060051610620516106800101535b81516001018083528114156108e5575b5050602061062051610660015160206001820306601f82010390506106205101016106205261062051610680526101c08051602001806106205161066001828460006004600a8704601201f161096d57600080fd5b5050610600610620516106600151610240818352015b610240610600511115610995576109b6565b600061060051610620516106800101535b8151600101808352811415610983575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106a0526103008051602001806106205161066001828460006004600a8704601201f1610a0b57600080fd5b5050610600610620516106600151610240818352015b610240610600511115610a3357610a54565b600061060051610620516106800101535b8151600101808352811415610a21575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106c0526102208051602001806106205161066001828460006004600a8704601201f1610aa957600080fd5b5050610600610620516106600151610240818352015b610240610600511115610ad157610af2565b600061060051610620516106800101535b8151600101808352811415610abf575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106e0526105a08051602001806106205161066001828460006004600a8704601201f1610b4757600080fd5b5050610600610620516106600151610240818352015b610240610600511115610b6f57610b90565b600061060051610620516106800101535b8151600101808352811415610b5d575b5050602061062051610660015160206001820306601f8201039050610620510101610620527f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c561062051610660a160006107005260006101406030806020846107c001018260208501600060046016f150508051820191505060006010602082066107400160208284011115610c2557600080fd5b60208061076082610700600060046015f15050818152809050905090506010806020846107c001018260208501600060046013f1505080518201915050806107c0526107c09050602060c0825160208401600060025af1610c8557600080fd5b60c0519050610720526000600060406020820661086001610220518284011115610cae57600080fd5b606080610880826020602088068803016102200160006004601bf1505081815280905090509050602060c0825160208401600060025af1610cee57600080fd5b60c0519050602082610a600101526020810190506000604060206020820661092001610220518284011115610d2257600080fd5b606080610940826020602088068803016102200160006004601bf15050818152809050905090506020806020846109e001018260208501600060046015f1505080518201915050610700516020826109e0010152602081019050806109e0526109e09050602060c0825160208401600060025af1610d9f57600080fd5b60c0519050602082610a6001015260208101905080610a6052610a609050602060c0825160208401600060025af1610dd657600080fd5b60c0519050610840526000600061072051602082610b000101526020810190506101c0602080602084610b0001018260208501600060046015f150508051820191505080610b0052610b009050602060c0825160208401600060025af1610e3c57600080fd5b60c0519050602082610c800101526020810190506000610300600880602084610c0001018260208501600060046012f15050805182019150506000601860208206610b800160208284011115610e9157600080fd5b602080610ba082610700600060046015f1505081815280905090509050601880602084610c0001018260208501600060046014f150508051820191505061084051602082610c0001015260208101905080610c0052610c009050602060c0825160208401600060025af1610f0457600080fd5b60c0519050602082610c8001015260208101905080610c8052610c809050602060c0825160208401600060025af1610f3b57600080fd5b60c0519050610ae052606435610ae05114610f5557600080fd5b6001805460018254011015610f6957600080fd5b6001815401815550600154610d0052610d2060006020818352015b60016001610d0051161415610fb957610ae051610d205160208110610fa857600080fd5b600060c052602060c020015561104d565b6000610d205160208110610fcc57600080fd5b600060c052602060c0200154602082610d40010152602081019050610ae051602082610d4001015260208101905080610d4052610d409050602060c0825160208401600060025af161101d57600080fd5b60c0519050610ae052610d00600261103457600080fd5b60028151048152505b8151600101808352811415610f84575b5050005b60006000fd5b61017f6111d60361017f60003961017f6111d6036000f3"} \ No newline at end of file +{"abi": [{"name": "DepositEvent", "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": 95727}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 18283}, {"name": "deposit", "outputs": [], "inputs": [{"type": "bytes", "name": "pubkey"}, {"type": "bytes", "name": "withdrawal_credentials"}, {"type": "bytes", "name": "signature"}, {"type": "bytes32", "name": "deposit_data_root"}], "constant": false, "payable": true, "type": "function", "gas": 1342680}], "bytecode": "0x740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009857600080fd5b6101406000601f818352015b600061014051602081106100b757600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e357600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012157600080fd5b60c0519050606051600161014051018060405190131561014057600080fd5b809190121561014e57600080fd5b6020811061015b57600080fd5b600260c052602060c02001555b81516001018083528114156100a4575b505061127956600436101561000d576110f4565b600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610286575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100e8578060000360020a82046100ef565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561011a57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610163578060000360020a820461016a565b8060020a82025b905090506101a0525b81516001018083528114156100cb575b5050601860086020820661020001602082840111156101a157600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101de57600080fd5b50506102c05160206001820306601f82010390506103206102c0516020818352015b82610320511015156102115761022d565b6000610320516102e001535b8151600101808352811415610200575b50505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102625761027e565b602061028051036102a001516020610280510361028052610250565b610160515650005b63c5f2892f600051141561051857341561029f57600080fd5b6000610140526101405161016052600154610180526101a060006020818352015b60016001610180511614156103415760006101a051602081106102e257600080fd5b600060c052602060c02001546020826102400101526020810190506101605160208261024001015260208101905080610240526102409050602060c0825160208401600060025af161033357600080fd5b60c0519050610160526103af565b6000610160516020826101c00101526020810190506101a0516020811061036757600080fd5b600260c052602060c02001546020826101c0010152602081019050806101c0526101c09050602060c0825160208401600060025af16103a557600080fd5b60c0519050610160525b61018060026103bd57600080fd5b60028151048152505b81516001018083528114156102c0575b505060006101605160208261046001015260208101905061014051610160516101805163806732896102e0526001546103005261030051600658016100a9565b506103605260006103c0525b6103605160206001820306601f82010390506103c0511015156104445761045d565b6103c05161038001526103c0516020016103c052610422565b61018052610160526101405261036060088060208461046001018260208501600060046012f150508051820191505060006018602082066103e001602082840111156104a857600080fd5b60208061040082610140600060046015f150508181528090509050905060188060208461046001018260208501600060046014f150508051820191505080610460526104609050602060c0825160208401600060025af161050857600080fd5b60c051905060005260206000f350005b63621fd130600051141561062c57341561053157600080fd5b6380673289610140526001546101605261016051600658016100a9565b506101c0526000610220525b6101c05160206001820306601f82010390506102205110151561057c57610595565b610220516101e00152610220516020016102205261055a565b6101c0805160200180610280828460006004600a8704601201f16105b857600080fd5b50506102805160206001820306601f82010390506102e0610280516020818352015b826102e0511015156105eb57610607565b60006102e0516102a001535b81516001018083528114156105da575b5050506020610260526040610280510160206001820306601f8201039050610260f350005b632289511860005114156110f357605060043560040161014037603060043560040135111561065a57600080fd5b60406024356004016101c037602060243560040135111561067a57600080fd5b608060443560040161022037606060443560040135111561069a57600080fd5b63ffffffff600154106106ac57600080fd5b633b9aca006102e0526102e0516106c257600080fd5b6102e05134046102c052633b9aca006102c05110156106e057600080fd5b603061014051146106f057600080fd5b60206101c0511461070057600080fd5b6060610220511461071057600080fd5b610140610360525b6103605151602061036051016103605261036061036051101561073a57610718565b6380673289610380526102c0516103a0526103a051600658016100a9565b50610400526000610460525b6104005160206001820306601f8201039050610460511015156107865761079f565b6104605161042001526104605160200161046052610764565b610340610360525b61036051526020610360510361036052610140610360511015156107ca576107a7565b610400805160200180610300828460006004600a8704601201f16107ed57600080fd5b5050610140610480525b61048051516020610480510161048052610480610480511015610819576107f7565b63806732896104a0526001546104c0526104c051600658016100a9565b50610520526000610580525b6105205160206001820306601f8201039050610580511015156108645761087d565b6105805161054001526105805160200161058052610842565b610460610480525b61048051526020610480510361048052610140610480511015156108a857610885565b6105208051602001806105a0828460006004600a8704601201f16108cb57600080fd5b505060a06106205261062051610660526101408051602001806106205161066001828460006004600a8704601201f161090357600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516040818352015b826106005110151561094057610961565b600061060051610620516106800101535b815160010180835281141561092f575b505050602061062051610660015160206001820306601f82010390506106205101016106205261062051610680526101c08051602001806106205161066001828460006004600a8704601201f16109b757600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b82610600511015156109f457610a15565b600061060051610620516106800101535b81516001018083528114156109e3575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106a0526103008051602001806106205161066001828460006004600a8704601201f1610a6b57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b8261060051101515610aa857610ac9565b600061060051610620516106800101535b8151600101808352811415610a97575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106c0526102208051602001806106205161066001828460006004600a8704601201f1610b1f57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516060818352015b8261060051101515610b5c57610b7d565b600061060051610620516106800101535b8151600101808352811415610b4b575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106e0526105a08051602001806106205161066001828460006004600a8704601201f1610bd357600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b8261060051101515610c1057610c31565b600061060051610620516106800101535b8151600101808352811415610bff575b505050602061062051610660015160206001820306601f8201039050610620510101610620527f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c561062051610660a160006107005260006101406030806020846107c001018260208501600060046016f150508051820191505060006010602082066107400160208284011115610cc757600080fd5b60208061076082610700600060046015f15050818152809050905090506010806020846107c001018260208501600060046013f1505080518201915050806107c0526107c09050602060c0825160208401600060025af1610d2757600080fd5b60c0519050610720526000600060406020820661086001610220518284011115610d5057600080fd5b606080610880826020602088068803016102200160006004601bf1505081815280905090509050602060c0825160208401600060025af1610d9057600080fd5b60c0519050602082610a600101526020810190506000604060206020820661092001610220518284011115610dc457600080fd5b606080610940826020602088068803016102200160006004601bf15050818152809050905090506020806020846109e001018260208501600060046015f1505080518201915050610700516020826109e0010152602081019050806109e0526109e09050602060c0825160208401600060025af1610e4157600080fd5b60c0519050602082610a6001015260208101905080610a6052610a609050602060c0825160208401600060025af1610e7857600080fd5b60c0519050610840526000600061072051602082610b000101526020810190506101c0602080602084610b0001018260208501600060046015f150508051820191505080610b0052610b009050602060c0825160208401600060025af1610ede57600080fd5b60c0519050602082610c800101526020810190506000610300600880602084610c0001018260208501600060046012f15050805182019150506000601860208206610b800160208284011115610f3357600080fd5b602080610ba082610700600060046015f1505081815280905090509050601880602084610c0001018260208501600060046014f150508051820191505061084051602082610c0001015260208101905080610c0052610c009050602060c0825160208401600060025af1610fa657600080fd5b60c0519050602082610c8001015260208101905080610c8052610c809050602060c0825160208401600060025af1610fdd57600080fd5b60c0519050610ae052606435610ae05114610ff757600080fd5b600180546001825401101561100b57600080fd5b6001815401815550600154610d0052610d2060006020818352015b60016001610d005116141561105b57610ae051610d20516020811061104a57600080fd5b600060c052602060c02001556110ef565b6000610d20516020811061106e57600080fd5b600060c052602060c0200154602082610d40010152602081019050610ae051602082610d4001015260208101905080610d4052610d409050602060c0825160208401600060025af16110bf57600080fd5b60c0519050610ae052610d0060026110d657600080fd5b60028151048152505b8151600101808352811415611026575b5050005b5b60006000fd5b61017f6112790361017f60003961017f611279036000f3"} \ 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 6ee27db7ac..ca18cbdfd5 100644 --- a/deposit_contract/contracts/validator_registration.v.py +++ b/deposit_contract/contracts/validator_registration.v.py @@ -1,4 +1,4 @@ -# Vyper target 0.1.0b12 +# Vyper target 0.1.0b13 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 @@ -75,7 +75,7 @@ def deposit(pubkey: bytes[PUBKEY_LENGTH], deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") assert deposit_amount >= MIN_DEPOSIT_AMOUNT - # Length checks to facilitate formal verification (see https://github.com/ethereum/eth2.0-specs/pull/1362/files#r320361859) + # Length checks for safety assert len(pubkey) == PUBKEY_LENGTH assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH assert len(signature) == SIGNATURE_LENGTH diff --git a/deposit_contract/requirements-testing.txt b/deposit_contract/requirements-testing.txt index 0b3d9d22c5..9b07eae6f0 100644 --- a/deposit_contract/requirements-testing.txt +++ b/deposit_contract/requirements-testing.txt @@ -1,5 +1,5 @@ eth-tester[py-evm]==0.1.0b39 -vyper==0.1.0b12 +vyper==0.1.0b13 web3==5.0.0b2 pytest==3.6.1 ../test_libs/pyspec From 1113953a59ecbd3c6ad7105092b74bad6f19094a Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 2 Dec 2019 10:25:13 -0800 Subject: [PATCH 2/5] Update p2p-interface.md --- specs/networking/p2p-interface.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/networking/p2p-interface.md b/specs/networking/p2p-interface.md index 5bb9fff6d3..3844f0928b 100644 --- a/specs/networking/p2p-interface.md +++ b/specs/networking/p2p-interface.md @@ -175,7 +175,7 @@ There are two primary global topics used to propagate beacon blocks and aggregat - The block being voted for (`aggregate_and_proof.aggregate.data.beacon_block_root`) passes validation. - `aggregate_and_proof.aggregate.data.slot` is within the last `ATTESTATION_PROPAGATION_SLOT_RANGE` slots (`aggregate_and_proof.aggregate.data.slot + ATTESTATION_PROPAGATION_SLOT_RANGE >= current_slot >= aggregate_and_proof.aggregate.data.slot`). - The validator index is within the aggregate's committee -- i.e. `aggregate_and_proof.aggregator_index in get_attesting_indices(state, aggregate_and_proof.aggregate.data, aggregate_and_proof.aggregate.aggregation_bits)`. - - `aggregate_and_proof.selection_proof` selects the validator as an aggregator for the slot -- i.e. `is_aggregator(state, aggregate_and_proof.aggregate.data.index, aggregate_and_proof.selection_proof)` returns `True`. + - `aggregate_and_proof.selection_proof` selects the validator as an aggregator for the slot -- i.e. `is_aggregator(state, aggregate_and_proof.aggregate.data.slot, aggregate_and_proof.aggregate.data.index, aggregate_and_proof.selection_proof)` returns `True`. - The `aggregate_and_proof.selection_proof` is a valid signature of the `aggregate_and_proof.aggregate.data.slot` by the validator with index `aggregate_and_proof.aggregator_index`. - The signature of `aggregate_and_proof.aggregate` is valid. From 0f4faf11ab348913b6e6997df981fa385f4c34c4 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 25 Nov 2019 10:37:06 -0800 Subject: [PATCH 3/5] Update 0_fork-choice.md --- specs/core/0_fork-choice.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/specs/core/0_fork-choice.md b/specs/core/0_fork-choice.md index 0998430670..1793b51ffd 100644 --- a/specs/core/0_fork-choice.md +++ b/specs/core/0_fork-choice.md @@ -251,8 +251,6 @@ def on_attestation(store: Store, attestation: Attestation) -> None: # Use GENESIS_EPOCH for previous when genesis to avoid underflow previous_epoch = current_epoch - 1 if current_epoch > GENESIS_EPOCH else GENESIS_EPOCH assert target.epoch in [current_epoch, previous_epoch] - # Cannot calculate the current shuffling if have not seen the target - assert target.root in store.blocks # Attestations target be for a known block. If target block is unknown, delay consideration until the block is found assert target.root in store.blocks From 103a66b2af9d9ec1fd1c70adc8e9029af5775c1c Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 21 Nov 2019 23:13:45 +0100 Subject: [PATCH 4/5] remove signing_root: see issue #1487 --- scripts/build_spec.py | 6 +- specs/core/0_beacon-chain.md | 104 ++++++--- specs/core/0_fork-choice.md | 15 +- specs/core/1_custody-game.md | 3 +- specs/core/1_shard-data-chains.md | 6 +- specs/networking/p2p-interface.md | 20 +- specs/simple-serialize.md | 7 +- specs/test_formats/ssz_generic/README.md | 1 - specs/test_formats/ssz_static/core.md | 4 - specs/validator/0_beacon-chain-validator.md | 69 +++--- test_generators/ssz_generic/ssz_test_case.py | 4 +- test_generators/ssz_static/main.py | 3 - .../test/fork_choice/test_get_head.py | 48 ++--- .../test/fork_choice/test_on_attestation.py | 56 ++--- .../test/fork_choice/test_on_block.py | 73 +++---- .../eth2spec/test/helpers/attestations.py | 9 +- .../pyspec/eth2spec/test/helpers/block.py | 66 ++++-- .../eth2spec/test/helpers/block_header.py | 8 +- .../pyspec/eth2spec/test/helpers/deposits.py | 8 +- .../test/helpers/phase1/shard_block.py | 8 +- .../test/helpers/proposer_slashings.py | 12 +- .../pyspec/eth2spec/test/helpers/state.py | 27 ++- .../eth2spec/test/helpers/voluntary_exits.py | 29 +-- .../test_process_block_header.py | 21 +- .../test_process_proposer_slashing.py | 8 +- .../test_process_voluntary_exit.py | 119 ++++------- .../test/phase_1/sanity/test_shard_blocks.py | 2 +- .../eth2spec/test/sanity/test_blocks.py | 199 +++++++++--------- .../pyspec/eth2spec/utils/ssz/ssz_impl.py | 7 - .../pyspec/eth2spec/utils/ssz/ssz_typing.py | 4 - 30 files changed, 483 insertions(+), 463 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 549357cd75..c38d0bd65f 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -19,10 +19,7 @@ field, ) -from eth2spec.utils.ssz.ssz_impl import ( - hash_tree_root, - signing_root, -) +from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_typing import ( boolean, Container, List, Vector, uint64, Bytes1, Bytes4, Bytes8, Bytes32, Bytes48, Bytes96, Bitlist, Bitvector, @@ -50,7 +47,6 @@ from eth2spec.utils.ssz.ssz_impl import ( hash_tree_root, - signing_root, is_zero, ) from eth2spec.utils.ssz.ssz_typing import ( diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 88a666f6b7..cde426c03f 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -30,17 +30,21 @@ - [`PendingAttestation`](#pendingattestation) - [`Eth1Data`](#eth1data) - [`HistoricalBatch`](#historicalbatch) + - [`DepositMessage`](#depositmessage) - [`DepositData`](#depositdata) - [`BeaconBlockHeader`](#beaconblockheader) + - [`SignedBeaconBlockHeader`](#signedbeaconblockheader) - [Beacon operations](#beacon-operations) - [`ProposerSlashing`](#proposerslashing) - [`AttesterSlashing`](#attesterslashing) - [`Attestation`](#attestation) - [`Deposit`](#deposit) - [`VoluntaryExit`](#voluntaryexit) + - [`SignedVoluntaryExit`](#signedvoluntaryexit) - [Beacon blocks](#beacon-blocks) - [`BeaconBlockBody`](#beaconblockbody) - [`BeaconBlock`](#beaconblock) + - [`SignedBeaconBlock`](#signedbeaconblock) - [Beacon state](#beacon-state) - [`BeaconState`](#beaconstate) - [Helper functions](#helper-functions) @@ -52,7 +56,6 @@ - [Crypto](#crypto) - [`hash`](#hash) - [`hash_tree_root`](#hash_tree_root) - - [`signing_root`](#signing_root) - [`bls_verify`](#bls_verify) - [`bls_aggregate_pubkeys`](#bls_aggregate_pubkeys) - [Predicates](#predicates) @@ -342,6 +345,15 @@ class HistoricalBatch(Container): state_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT] ``` +#### `DepositMessage` + +```python +class DepositMessage(Container): + pubkey: BLSPubkey + withdrawal_credentials: Bytes32 + amount: Gwei +``` + #### `DepositData` ```python @@ -349,7 +361,7 @@ class DepositData(Container): pubkey: BLSPubkey withdrawal_credentials: Bytes32 amount: Gwei - signature: BLSSignature + signature: BLSSignature # signing over DepositMessage ``` #### `BeaconBlockHeader` @@ -360,6 +372,13 @@ class BeaconBlockHeader(Container): parent_root: Root state_root: Root body_root: Root +``` + +#### `SignedBeaconBlockHeader` + +```python +class SignedBeaconBlockHeader(Container): + message: BeaconBlockHeader signature: BLSSignature ``` @@ -370,8 +389,8 @@ class BeaconBlockHeader(Container): ```python class ProposerSlashing(Container): proposer_index: ValidatorIndex - header_1: BeaconBlockHeader - header_2: BeaconBlockHeader + signed_header_1: SignedBeaconBlockHeader + signed_header_2: SignedBeaconBlockHeader ``` #### `AttesterSlashing` @@ -405,6 +424,13 @@ class Deposit(Container): class VoluntaryExit(Container): epoch: Epoch # Earliest epoch when voluntary exit can be processed validator_index: ValidatorIndex +``` + +#### `SignedVoluntaryExit` + +```python +class SignedVoluntaryExit(Container): + message: VoluntaryExit signature: BLSSignature ``` @@ -422,7 +448,7 @@ class BeaconBlockBody(Container): attester_slashings: List[AttesterSlashing, MAX_ATTESTER_SLASHINGS] attestations: List[Attestation, MAX_ATTESTATIONS] deposits: List[Deposit, MAX_DEPOSITS] - voluntary_exits: List[VoluntaryExit, MAX_VOLUNTARY_EXITS] + voluntary_exits: List[SignedVoluntaryExit, MAX_VOLUNTARY_EXITS] ``` #### `BeaconBlock` @@ -433,6 +459,13 @@ class BeaconBlock(Container): parent_root: Root state_root: Root body: BeaconBlockBody +``` + +#### `SignedBeaconBlock` + +```python +class SignedBeaconBlock(Container): + message: BeaconBlock signature: BLSSignature ``` @@ -533,10 +566,6 @@ def bytes_to_int(data: bytes) -> uint64: `def hash_tree_root(object: SSZSerializable) -> Root` is a function for hashing objects into a single root by utilizing a hash tree structure, as defined in the [SSZ spec](../simple-serialize.md#merkleization). -#### `signing_root` - -`def signing_root(object: Container) -> Root` is a function for computing signing messages, as defined in the [SSZ spec](../simple-serialize.md#self-signed-containers). - #### `bls_verify` `bls_verify` is a function for verifying a BLS signature, as defined in the [BLS Signature spec](../bls_signature.md#bls_verify). @@ -1048,18 +1077,27 @@ Let `genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))`. The post-state corresponding to a pre-state `state` and a block `block` is defined as `state_transition(state, block)`. State transitions that trigger an unhandled exception (e.g. a failed `assert` or an out-of-range list access) are considered invalid. ```python -def state_transition(state: BeaconState, block: BeaconBlock, validate_state_root: bool=False) -> BeaconState: +def state_transition(state: BeaconState, signed_block: SignedBeaconBlock, validate_result: bool=True) -> BeaconState: # Process slots (including those with no blocks) since block - process_slots(state, block.slot) + process_slots(state, signed_block.message.slot) + # Verify signature + if validate_result: + assert verify_block_signature(state, signed_block) # Process block - process_block(state, block) - # Validate state root (`validate_state_root == True` in production) - if validate_state_root: - assert block.state_root == hash_tree_root(state) + process_block(state, signed_block.message) + if validate_result: + assert signed_block.message.state_root == hash_tree_root(state) # Validate state root # Return post-state return state ``` +```python +def verify_block_signature(state: BeaconState, signed_block: SignedBeaconBlock) -> bool: + proposer = state.validators[get_beacon_proposer_index(state)] + domain = get_domain(state, DOMAIN_BEACON_PROPOSER) + return bls_verify(proposer.pubkey, hash_tree_root(signed_block.message), signed_block.signature, domain) +``` + ```python def process_slots(state: BeaconState, slot: Slot) -> None: assert state.slot <= slot @@ -1080,7 +1118,7 @@ def process_slot(state: BeaconState) -> None: if state.latest_block_header.state_root == Bytes32(): state.latest_block_header.state_root = previous_state_root # Cache block root - previous_block_root = signing_root(state.latest_block_header) + previous_block_root = hash_tree_root(state.latest_block_header) state.block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root ``` @@ -1340,20 +1378,17 @@ 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 == hash_tree_root(state.latest_block_header) # Save current block as the new latest block state.latest_block_header = BeaconBlockHeader( slot=block.slot, parent_root=block.parent_root, # `state_root` is zeroed and overwritten in the next `process_slot` call body_root=hash_tree_root(block.body), - # `signature` is zeroed ) # Verify proposer is not slashed 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)) ``` #### RANDAO @@ -1403,15 +1438,15 @@ def process_operations(state: BeaconState, body: BeaconBlockBody) -> None: def process_proposer_slashing(state: BeaconState, proposer_slashing: ProposerSlashing) -> None: proposer = state.validators[proposer_slashing.proposer_index] # Verify slots match - assert proposer_slashing.header_1.slot == proposer_slashing.header_2.slot + assert proposer_slashing.signed_header_1.message.slot == proposer_slashing.signed_header_2.message.slot # But the headers are different - assert proposer_slashing.header_1 != proposer_slashing.header_2 + assert proposer_slashing.signed_header_1.message != proposer_slashing.signed_header_2.message # Check proposer is slashable assert is_slashable_validator(proposer, get_current_epoch(state)) # Signatures are valid - for header in (proposer_slashing.header_1, proposer_slashing.header_2): - domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(header.slot)) - assert bls_verify(proposer.pubkey, signing_root(header), header.signature, domain) + for signed_header in (proposer_slashing.signed_header_1, proposer_slashing.signed_header_2): + domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(signed_header.message.slot)) + assert bls_verify(proposer.pubkey, hash_tree_root(signed_header.message), signed_header.signature, domain) slash_validator(state, proposer_slashing.proposer_index) ``` @@ -1489,7 +1524,11 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None: # Note: The deposit contract does not check signatures. # Note: Deposits are valid across forks, thus the deposit domain is retrieved directly from `compute_domain`. domain = compute_domain(DOMAIN_DEPOSIT) - if not bls_verify(pubkey, signing_root(deposit.data), deposit.data.signature, domain): + deposit_message = DepositMessage( + pubkey=deposit.data.pubkey, + withdrawal_credentials=deposit.data.withdrawal_credentials, + amount=deposit.data.amount) + if not bls_verify(pubkey, hash_tree_root(deposit_message), deposit.data.signature, domain): return # Add validator and balance entries @@ -1512,19 +1551,20 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None: ##### Voluntary exits ```python -def process_voluntary_exit(state: BeaconState, exit: VoluntaryExit) -> None: - validator = state.validators[exit.validator_index] +def process_voluntary_exit(state: BeaconState, signed_voluntary_exit: SignedVoluntaryExit) -> None: + voluntary_exit = signed_voluntary_exit.message + validator = state.validators[voluntary_exit.validator_index] # Verify the validator is active assert is_active_validator(validator, get_current_epoch(state)) # Verify the validator has not yet exited assert validator.exit_epoch == FAR_FUTURE_EPOCH # Exits must specify an epoch when they become valid; they are not valid before then - assert get_current_epoch(state) >= exit.epoch + assert get_current_epoch(state) >= voluntary_exit.epoch # Verify the validator has been active long enough assert get_current_epoch(state) >= validator.activation_epoch + PERSISTENT_COMMITTEE_PERIOD # Verify signature - domain = get_domain(state, DOMAIN_VOLUNTARY_EXIT, exit.epoch) - assert bls_verify(validator.pubkey, signing_root(exit), exit.signature, domain) + domain = get_domain(state, DOMAIN_VOLUNTARY_EXIT, voluntary_exit.epoch) + assert bls_verify(validator.pubkey, hash_tree_root(voluntary_exit), signed_voluntary_exit.signature, domain) # Initiate exit - initiate_validator_exit(state, exit.validator_index) + initiate_validator_exit(state, voluntary_exit.validator_index) ``` diff --git a/specs/core/0_fork-choice.md b/specs/core/0_fork-choice.md index 0998430670..0b5858a9a9 100644 --- a/specs/core/0_fork-choice.md +++ b/specs/core/0_fork-choice.md @@ -32,7 +32,7 @@ This document is the beacon chain fork choice spec, part of Ethereum 2.0 Phase 0 The head block root associated with a `store` is defined as `get_head(store)`. At genesis, let `store = get_genesis_store(genesis_state)` and update `store` by running: - `on_tick(time)` whenever `time > store.time` where `time` is the current Unix time -- `on_block(block)` whenever a block `block` is received +- `on_block(block)` whenever a block `block: SignedBeaconBlock` is received - `on_attestation(attestation)` whenever an attestation `attestation` is received *Notes*: @@ -81,7 +81,7 @@ class Store(object): ```python def get_genesis_store(genesis_state: BeaconState) -> Store: genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state)) - root = signing_root(genesis_block) + root = hash_tree_root(genesis_block) justified_checkpoint = Checkpoint(epoch=GENESIS_EPOCH, root=root) finalized_checkpoint = Checkpoint(epoch=GENESIS_EPOCH, root=root) return Store( @@ -203,25 +203,26 @@ def on_tick(store: Store, time: uint64) -> None: #### `on_block` ```python -def on_block(store: Store, block: BeaconBlock) -> None: +def on_block(store: Store, signed_block: SignedBeaconBlock) -> None: + block = signed_block.message # Make a copy of the state to avoid mutability issues assert block.parent_root in store.block_states pre_state = store.block_states[block.parent_root].copy() # Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past. assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT # Add new block to the store - store.blocks[signing_root(block)] = block + store.blocks[hash_tree_root(block)] = block # Check block is a descendant of the finalized block assert ( - get_ancestor(store, signing_root(block), store.blocks[store.finalized_checkpoint.root].slot) == + get_ancestor(store, hash_tree_root(block), store.blocks[store.finalized_checkpoint.root].slot) == store.finalized_checkpoint.root ) # Check that block is later than the finalized epoch slot assert block.slot > compute_start_slot_at_epoch(store.finalized_checkpoint.epoch) # Check the block is valid and compute the post-state - state = state_transition(pre_state, block, True) + state = state_transition(pre_state, signed_block, True) # Add new state for this block to the store - store.block_states[signing_root(block)] = state + store.block_states[hash_tree_root(block)] = state # Update justified checkpoint if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch: diff --git a/specs/core/1_custody-game.md b/specs/core/1_custody-game.md index b487d3c34a..946165bb70 100644 --- a/specs/core/1_custody-game.md +++ b/specs/core/1_custody-game.md @@ -595,7 +595,8 @@ def process_bit_challenge(state: BeaconState, challenge: CustodyBitChallenge) -> # Verify challenge signature challenger = state.validators[challenge.challenger_index] domain = get_domain(state, DOMAIN_CUSTODY_BIT_CHALLENGE, get_current_epoch(state)) - assert bls_verify(challenger.pubkey, signing_root(challenge), challenge.signature, domain) + # TODO incorrect hash-tree-root, but this changes with phase 1 PR #1483 + assert bls_verify(challenger.pubkey, hash_tree_root(challenge), challenge.signature, domain) # Verify challenger is slashable assert is_slashable_validator(challenger, get_current_epoch(state)) # Verify attestation diff --git a/specs/core/1_shard-data-chains.md b/specs/core/1_shard-data-chains.md index 7b951dd111..6fce90626b 100644 --- a/specs/core/1_shard-data-chains.md +++ b/specs/core/1_shard-data-chains.md @@ -359,9 +359,9 @@ def process_shard_block_header(beacon_state: BeaconState, shard_state: ShardStat ) if beacon_block_header.state_root == Bytes32(): beacon_block_header.state_root = hash_tree_root(beacon_state) - assert block.beacon_block_root == signing_root(beacon_block_header) + assert block.beacon_block_root == hash_tree_root(beacon_block_header) # Verify the parent root - assert block.parent_root == signing_root(shard_state.latest_block_header) + assert block.parent_root == hash_tree_root(shard_state.latest_block_header) # Save current block as the new latest block shard_state.latest_block_header = ShardBlockHeader( shard=block.shard, @@ -384,7 +384,7 @@ def process_shard_block_header(beacon_state: BeaconState, shard_state: ShardStat assert not proposer.slashed # Verify proposer signature domain = get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, compute_epoch_of_shard_slot(block.slot)) - assert bls_verify(proposer.pubkey, signing_root(block), block.signature, domain) + assert bls_verify(proposer.pubkey, hash_tree_root(block), block.signature, domain) ``` #### Attestations diff --git a/specs/networking/p2p-interface.md b/specs/networking/p2p-interface.md index 5bb9fff6d3..0d8f1d335a 100644 --- a/specs/networking/p2p-interface.md +++ b/specs/networking/p2p-interface.md @@ -151,7 +151,7 @@ The payload is carried in the `data` field of a gossipsub message, and varies de | Topic | Message Type | |----------------------------------------|-------------------| -| beacon_block | BeaconBlock | +| beacon_block | SignedBeaconBlock | | beacon_aggregate_and_proof | AggregateAndProof | | beacon_attestation\* | Attestation | | committee_index{subnet_id}\_beacon_attestation | Attestation | @@ -214,7 +214,7 @@ Topics are post-fixed with an encoding. Encodings define how the payload of a go #### Interop -- `ssz` - All objects are [SSZ-encoded](#ssz-encoding). Example: The beacon block topic string is `/eth2/beacon_block/ssz`, and the data field of a gossipsub message is an ssz-encoded `BeaconBlock`. +- `ssz` - All objects are [SSZ-encoded](#ssz-encoding). Example: The beacon block topic string is `/eth2/beacon_block/ssz`, and the data field of a gossipsub message is an ssz-encoded `SignedBeaconBlock`. #### Mainnet @@ -331,9 +331,9 @@ The [SimpleSerialize (SSZ) specification](../simple-serialize.md) outlines how o All messages that contain only a single field MUST be encoded directly as the type of that field and MUST NOT be encoded as an SSZ container. -Responses that are SSZ-lists (for example `[]BeaconBlocks`) send their +Responses that are SSZ-lists (for example `[]SignedBeaconBlock`) send their constituents individually as `response_chunk`s. For example, the -`[]BeaconBlocks` response type sends one or more `response_chunk`s. Each _successful_ `response_chunk` contains a single `BeaconBlock` payload. +`[]SignedBeaconBlock` response type sends one or more `response_chunk`s. Each _successful_ `response_chunk` contains a single `SignedBeaconBlock` payload. ### Messages @@ -413,15 +413,15 @@ Request Content: Response Content: ``` ( - []BeaconBlock + []SignedBeaconBlock ) ``` -Requests count beacon blocks from the peer starting from `start_slot` on the chain defined by `head_block_root` (= `signing_root(BeaconBlock)`). The response MUST contain no more than count blocks. `step` defines the slot increment between blocks. For example, requesting blocks starting at `start_slot` 2 with a step value of 2 would return the blocks at [2, 4, 6, …]. In cases where a slot is empty for a given slot number, no block is returned. For example, if slot 4 were empty in the previous example, the returned array would contain [2, 6, …]. A step value of 1 returns all blocks on the range `[start_slot, start_slot + count)`. +Requests count beacon blocks from the peer starting from `start_slot` on the chain defined by `head_block_root` (= `hash_tree_root(SignedBeaconBlock.message)`). The response MUST contain no more than count blocks. `step` defines the slot increment between blocks. For example, requesting blocks starting at `start_slot` 2 with a step value of 2 would return the blocks at [2, 4, 6, …]. In cases where a slot is empty for a given slot number, no block is returned. For example, if slot 4 were empty in the previous example, the returned array would contain [2, 6, …]. A step value of 1 returns all blocks on the range `[start_slot, start_slot + count)`. The request MUST be encoded as an SSZ-container. -The response MUST consist of at least one `response_chunk` and MAY consist of many. Each _successful_ `response_chunk` MUST contain a single `BeaconBlock` payload. +The response MUST consist of at least one `response_chunk` and MAY consist of many. Each _successful_ `response_chunk` MUST contain a single `SignedBeaconBlock` payload. `BeaconBlocksByRange` is primarily used to sync historical blocks. @@ -449,17 +449,17 @@ Response Content: ``` ( - []BeaconBlock + []SignedBeaconBlock ) ``` -Requests blocks by block root (= `signing_root(BeaconBlock)`). The response is a list of `BeaconBlock` whose length is less than or equal to the number of requested blocks. It may be less in the case that the responding peer is missing blocks. +Requests blocks by block root (= `hash_tree_root(SignedBeaconBlock.message)`). The response is a list of `SignedBeaconBlock` whose length is less than or equal to the number of requested blocks. It may be less in the case that the responding peer is missing blocks. `BeaconBlocksByRoot` is primarily used to recover recent blocks (e.g. when receiving a block or attestation whose parent is unknown). The request MUST be encoded as an SSZ-field. -The response MUST consist of at least one `response_chunk` and MAY consist of many. Each _successful_ `response_chunk` MUST contain a single `BeaconBlock` payload. +The response MUST consist of at least one `response_chunk` and MAY consist of many. Each _successful_ `response_chunk` MUST contain a single `SignedBeaconBlock` payload. Clients MUST support requesting blocks since the latest finalized epoch. diff --git a/specs/simple-serialize.md b/specs/simple-serialize.md index 7c4667ec80..556c31b274 100644 --- a/specs/simple-serialize.md +++ b/specs/simple-serialize.md @@ -25,7 +25,6 @@ - [Vectors, containers, lists, unions](#vectors-containers-lists-unions) - [Deserialization](#deserialization) - [Merkleization](#merkleization) - - [Self-signed containers](#self-signed-containers) - [Summaries and expansions](#summaries-and-expansions) - [Implementations](#implementations) @@ -106,7 +105,7 @@ An SSZ object is called zeroed (and thus, `is_zero(object)` returns true) if it We recursively define the `serialize` function which consumes an object `value` (of the type specified) and returns a bytestring of type `bytes`. -*Note*: In the function definitions below (`serialize`, `hash_tree_root`, `signing_root`, `is_variable_size`, etc.) objects implicitly carry their type. +*Note*: In the function definitions below (`serialize`, `hash_tree_root`, `is_variable_size`, etc.) objects implicitly carry their type. ### `uintN` @@ -234,10 +233,6 @@ We now define Merkleization `hash_tree_root(value)` of an object `value` recursi * `mix_in_length(merkleize([hash_tree_root(element) for element in value], limit=chunk_count(type)), len(value))` if `value` is a list of composite objects. * `mix_in_type(merkleize(value.value), value.type_index)` if `value` is of union type. -## Self-signed containers - -Let `value` be a self-signed container object. The convention is that the signature (e.g. a `"bytes96"` BLS12-381 signature) be the last field of `value`. Further, the signed message for `value` is `signing_root(value) = hash_tree_root(truncate_last(value))` where `truncate_last` truncates the last element of `value`. - ## Summaries and expansions Let `A` be an object derived from another object `B` by replacing some of the (possibly nested) values of `B` by their `hash_tree_root`. We say `A` is a "summary" of `B`, and that `B` is an "expansion" of `A`. Notice `hash_tree_root(A) == hash_tree_root(B)`. diff --git a/specs/test_formats/ssz_generic/README.md b/specs/test_formats/ssz_generic/README.md index 2096dae7d2..ce0f709dde 100644 --- a/specs/test_formats/ssz_generic/README.md +++ b/specs/test_formats/ssz_generic/README.md @@ -42,7 +42,6 @@ The expected roots are encoded into the metadata yaml: ```yaml root: Bytes32 -- Hash-tree-root of the object -signing_root: Bytes32 -- Signing-root of the object ``` The `Bytes32` is encoded as a string, hexadecimal encoding, prefixed with `0x`. diff --git a/specs/test_formats/ssz_static/core.md b/specs/test_formats/ssz_static/core.md index d0cfd25f63..ddb3709837 100644 --- a/specs/test_formats/ssz_static/core.md +++ b/specs/test_formats/ssz_static/core.md @@ -24,8 +24,6 @@ The output parts are: `roots.yaml`, `serialized.ssz`, `value.yaml` ```yaml root: bytes32 -- string, hash-tree-root of the value, hex encoded, with prefix 0x -signing_root: bytes32 -- string, signing-root of the value, hex encoded, with prefix 0x. - *Optional*, present if type is a container and ends with a ``signature`` field. ``` ### `serialized.ssz` @@ -47,11 +45,9 @@ A test-runner can implement the following assertions: - Serialization in 2 steps: deserialize `serialized`, then serialize the result, and verify if the bytes match the original `serialized`. - Hash-tree-root: After parsing the `value` (or deserializing `serialized`), Hash-tree-root it: the output should match `root` - - Optionally also check `signing_root`, if present. ## References **`serialized`**—[SSZ serialization](../../simple-serialize.md#serialization) **`root`**—[hash_tree_root](../../simple-serialize.md#merkleization) function -**`signing_root`**—[signing_root](../../simple-serialize.md#self-signed-containers) function diff --git a/specs/validator/0_beacon-chain-validator.md b/specs/validator/0_beacon-chain-validator.md index 43ea7ab07a..3b90350e39 100644 --- a/specs/validator/0_beacon-chain-validator.md +++ b/specs/validator/0_beacon-chain-validator.md @@ -110,8 +110,11 @@ To submit a deposit: - Pack the validator's [initialization parameters](#initialization) into `deposit_data`, a [`DepositData`](../core/0_beacon-chain.md#depositdata) SSZ object. - Let `amount` be the amount in Gwei to be deposited by the validator where `amount >= MIN_DEPOSIT_AMOUNT`. -- Set `deposit_data.amount = amount`. -- Let `signature` be the result of `bls_sign` of the `signing_root(deposit_data)` with `domain=compute_domain(DOMAIN_DEPOSIT)`. (Deposits are valid regardless of fork version, `compute_domain` will default to zeroes there). +- Set `deposit_data.pubkey` to validator's `pubkey`. +- Set `deposit_data.withdrawal_credentials` to `withdrawal_credentials`. +- Set `deposit_data.amount` to `amount`. +- Let `deposit_message` be a `DepositMessage` with all the `DepositData` contents except the `signature`. +- Let `signature` be the result of `bls_sign` of the `hash_tree_root(deposit_message)` with `domain=compute_domain(DOMAIN_DEPOSIT)`. (Deposits are valid regardless of fork version, `compute_domain` will default to zeroes there). - Let `deposit_data_root` be `hash_tree_root(deposit_data)`. - 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], deposit_data_root: bytes32)` along with a deposit of `amount` Gwei. @@ -200,11 +203,13 @@ A validator has two primary responsibilities to the beacon chain: [proposing blo ### Block proposal -A validator is expected to propose a [`BeaconBlock`](../core/0_beacon-chain.md#beaconblock) at the beginning of any slot during which `is_proposer(state, validator_index)` returns `True`. To propose, the validator selects the `BeaconBlock`, `parent`, that in their view of the fork choice is the head of the chain during `slot - 1`. The validator creates, signs, and broadcasts a `block` that is a child of `parent` that satisfies a valid [beacon chain state transition](../core/0_beacon-chain.md#beacon-chain-state-transition-function). +A validator is expected to propose a [`SignedBeaconBlock`](../core/0_beacon-chain.md#signedbeaconblock) at the beginning of any slot during which `is_proposer(state, validator_index)` returns `True`. To propose, the validator selects the `BeaconBlock`, `parent`, that in their view of the fork choice is the head of the chain during `slot - 1`. The validator creates, signs, and broadcasts a `block` that is a child of `parent` that satisfies a valid [beacon chain state transition](../core/0_beacon-chain.md#beacon-chain-state-transition-function). There is one proposer per slot, so if there are N active validators any individual validator will on average be assigned to propose once per N slots (e.g. at 312,500 validators = 10 million ETH, that's once per ~6 weeks). -#### Block header +#### Preparing for a `BeaconBlock` + +To construct a `BeaconBlockBody`, a `block` (`BeaconBlock`) is defined with the necessary context for a block proposal: ##### Slot @@ -214,17 +219,14 @@ Set `block.slot = slot` where `slot` is the current slot at which the validator ##### Parent root -Set `block.parent_root = signing_root(parent)`. - -##### State root +Set `block.parent_root = hash_tree_root(parent)`. -Set `block.state_root = hash_tree_root(state)` of the resulting `state` of the `parent -> block` state transition. -*Note*: To calculate `state_root`, the validator should first run the state transition function on an unsigned `block` containing a stub for the `state_root`. It is useful to be able to run a state transition function that does _not_ validate signatures or state root for this purpose. +#### Constructing the `BeaconBlockBody` ##### Randao reveal -Set `block.randao_reveal = epoch_signature` where `epoch_signature` is obtained from: +Set `block.body.randao_reveal = epoch_signature` where `epoch_signature` is obtained from: ```python def get_epoch_signature(state: BeaconState, block: BeaconBlock, privkey: int) -> BLSSignature: @@ -234,11 +236,11 @@ def get_epoch_signature(state: BeaconState, block: BeaconBlock, privkey: int) -> ##### Eth1 Data -The `block.eth1_data` field is for block proposers to vote on recent Eth1 data. This recent data contains an Eth1 block hash as well as the associated deposit root (as calculated by the `get_deposit_root()` method of the deposit contract) and deposit count after execution of the corresponding Eth1 block. If over half of the block proposers in the current Eth1 voting period vote for the same `eth1_data` then `state.eth1_data` updates at the end of the voting period. Each deposit in `block.body.deposits` must verify against `state.eth1_data.eth1_deposit_root`. +The `block.body.eth1_data` field is for block proposers to vote on recent Eth1 data. This recent data contains an Eth1 block hash as well as the associated deposit root (as calculated by the `get_deposit_root()` method of the deposit contract) and deposit count after execution of the corresponding Eth1 block. If over half of the block proposers in the current Eth1 voting period vote for the same `eth1_data` then `state.eth1_data` updates at the end of the voting period. Each deposit in `block.body.deposits` must verify against `state.eth1_data.eth1_deposit_root`. Let `get_eth1_data(distance: uint64) -> Eth1Data` be the (subjective) function that returns the Eth1 data at distance `distance` relative to the Eth1 head at the start of the current Eth1 voting period. Let `previous_eth1_distance` be the distance relative to the Eth1 block corresponding to `eth1_data.block_hash` found in the state at the _start_ of the current Eth1 voting period. Note that `eth1_data` can be updated in the middle of a voting period and thus the starting `eth1_data.block_hash` must be stored separately. -An honest block proposer sets `block.eth1_data = get_eth1_vote(state, previous_eth1_distance)` where: +An honest block proposer sets `block.body.eth1_data = get_eth1_vote(state, previous_eth1_distance)` where: ```python def get_eth1_vote(state: BeaconState, previous_eth1_distance: uint64) -> Eth1Data: @@ -260,18 +262,6 @@ def get_eth1_vote(state: BeaconState, previous_eth1_distance: uint64) -> Eth1Dat ) ``` -##### Signature - -Set `header.signature = block_signature` where `block_signature` is obtained from: - -```python -def get_block_signature(state: BeaconState, header: BeaconBlockHeader, privkey: int) -> BLSSignature: - domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(header.slot)) - return bls_sign(privkey, signing_root(header), domain) -``` - -#### Block body - ##### Proposer slashings Up to `MAX_PROPOSER_SLASHINGS`, [`ProposerSlashing`](../core/0_beacon-chain.md#proposerslashing) objects can be included in the `block`. The proposer slashings must satisfy the verification conditions found in [proposer slashings processing](../core/0_beacon-chain.md#proposer-slashings). The validator receives a small "whistleblower" reward for each proposer slashing found and included. @@ -294,6 +284,33 @@ The `proof` for each deposit must be constructed against the deposit root contai Up to `MAX_VOLUNTARY_EXITS`, [`VoluntaryExit`](../core/0_beacon-chain.md#voluntaryexit) objects can be included in the `block`. The exits must satisfy the verification conditions found in [exits processing](../core/0_beacon-chain.md#voluntary-exits). + +#### Packaging into a `SignedBeaconBlock` + +##### State root + +Set `block.state_root = hash_tree_root(state)` of the resulting `state` of the `parent -> block` state transition. + +*Note*: To calculate `state_root`, the validator should first run the state transition function on an unsigned `block` containing a stub for the `state_root`. +It is useful to be able to run a state transition function (working on a copy of the state) that does _not_ validate signatures or state root for this purpose: + +```python +def compute_new_state_root(state: BeaconState, block: BeaconBlock) -> Root: + process_slots(state, block.slot) + process_block(state, block) + return hash_tree_root(state) +``` + +##### Signature + +`signed_block = SignedBeaconBlock(message=block, signature=block_signature)`, where `block_signature` is obtained from: + +```python +def get_block_signature(state: BeaconState, header: BeaconBlockHeader, privkey: int) -> BLSSignature: + domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(header.slot)) + return bls_sign(privkey, hash_tree_root(header), domain) +``` + ### Attesting A validator is expected to create, sign, and broadcast an attestation during each epoch. The `committee`, assigned `index`, and assigned `slot` for which the validator performs this role during an epoch are defined by `get_committee_assignment(state, epoch, validator_index)`. @@ -316,7 +333,7 @@ First, the validator should construct `attestation_data`, an [`AttestationData`] ##### LMD GHOST vote -Set `attestation_data.beacon_block_root = signing_root(head_block)`. +Set `attestation_data.beacon_block_root = hash_tree_root(head_block)`. ##### FFG vote @@ -326,7 +343,7 @@ Set `attestation_data.beacon_block_root = signing_root(head_block)`. *Note*: `epoch_boundary_block_root` can be looked up in the state using: - Let `start_slot = compute_start_slot_at_epoch(get_current_epoch(head_state))`. -- Let `epoch_boundary_block_root = signing_root(head_block) if start_slot == head_state.slot else get_block_root(state, start_slot)`. +- Let `epoch_boundary_block_root = hash_tree_root(head_block) if start_slot == head_state.slot else get_block_root(state, start_slot)`. #### Construct attestation diff --git a/test_generators/ssz_generic/ssz_test_case.py b/test_generators/ssz_generic/ssz_test_case.py index e6993888c6..42955bd3ed 100644 --- a/test_generators/ssz_generic/ssz_test_case.py +++ b/test_generators/ssz_generic/ssz_test_case.py @@ -1,4 +1,4 @@ -from eth2spec.utils.ssz.ssz_impl import serialize, hash_tree_root, signing_root +from eth2spec.utils.ssz.ssz_impl import serialize, hash_tree_root from eth2spec.debug.encode import encode from eth2spec.utils.ssz.ssz_typing import SSZValue, Container from typing import Callable @@ -10,8 +10,6 @@ def case_fn(): yield "value", "data", encode(value) yield "serialized", "ssz", serialize(value) yield "root", "meta", '0x' + hash_tree_root(value).hex() - if isinstance(value, Container): - yield "signing_root", "meta", '0x' + signing_root(value).hex() return case_fn diff --git a/test_generators/ssz_static/main.py b/test_generators/ssz_static/main.py index 32178cfe00..334f45fa7e 100644 --- a/test_generators/ssz_static/main.py +++ b/test_generators/ssz_static/main.py @@ -7,7 +7,6 @@ from eth2spec.utils.ssz.ssz_typing import Container from eth2spec.utils.ssz.ssz_impl import ( hash_tree_root, - signing_root, serialize, ) from gen_base import gen_runner, gen_typing @@ -24,8 +23,6 @@ def create_test_case(rng: Random, typ, mode: random_value.RandomizationMode, cha roots_data = { "root": '0x' + hash_tree_root(value).hex() } - if isinstance(value, Container) and hasattr(value, "signature"): - roots_data["signing_root"] = '0x' + signing_root(value).hex() yield "roots", "data", roots_data diff --git a/test_libs/pyspec/eth2spec/test/fork_choice/test_get_head.py b/test_libs/pyspec/eth2spec/test/fork_choice/test_get_head.py index ff5a822fba..7a125c3be2 100644 --- a/test_libs/pyspec/eth2spec/test/fork_choice/test_get_head.py +++ b/test_libs/pyspec/eth2spec/test/fork_choice/test_get_head.py @@ -4,19 +4,19 @@ from eth2spec.test.helpers.state import state_transition_and_sign_block -def add_block_to_store(spec, store, block): - pre_state = store.block_states[block.parent_root] - block_time = pre_state.genesis_time + block.slot * spec.SECONDS_PER_SLOT +def add_block_to_store(spec, store, signed_block): + pre_state = store.block_states[signed_block.message.parent_root] + block_time = pre_state.genesis_time + signed_block.message.slot * spec.SECONDS_PER_SLOT if store.time < block_time: spec.on_tick(store, block_time) - spec.on_block(store, block) + spec.on_block(store, signed_block) def add_attestation_to_store(spec, store, attestation): parent_block = store.blocks[attestation.data.beacon_block_root] - pre_state = store.block_states[spec.signing_root(parent_block)] + pre_state = store.block_states[spec.hash_tree_root(parent_block)] block_time = pre_state.genesis_time + parent_block.slot * spec.SECONDS_PER_SLOT next_epoch_time = block_time + spec.SLOTS_PER_EPOCH * spec.SECONDS_PER_SLOT @@ -32,7 +32,7 @@ def test_genesis(spec, state): # Initialization store = spec.get_genesis_store(state) genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) - assert spec.get_head(store) == spec.signing_root(genesis_block) + assert spec.get_head(store) == spec.hash_tree_root(genesis_block) @with_all_phases @@ -41,19 +41,19 @@ def test_chain_no_attestations(spec, state): # Initialization store = spec.get_genesis_store(state) genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) - assert spec.get_head(store) == spec.signing_root(genesis_block) + assert spec.get_head(store) == spec.hash_tree_root(genesis_block) # On receiving a block of `GENESIS_SLOT + 1` slot block_1 = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block_1) - add_block_to_store(spec, store, block_1) + signed_block_1 = state_transition_and_sign_block(spec, state, block_1) + add_block_to_store(spec, store, signed_block_1) # On receiving a block of next epoch block_2 = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block_2) - add_block_to_store(spec, store, block_2) + signed_block_2 = state_transition_and_sign_block(spec, state, block_2) + add_block_to_store(spec, store, signed_block_2) - assert spec.get_head(store) == spec.signing_root(block_2) + assert spec.get_head(store) == spec.hash_tree_root(block_2) @with_all_phases @@ -64,22 +64,22 @@ def test_split_tie_breaker_no_attestations(spec, state): # Initialization store = spec.get_genesis_store(state) genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) - assert spec.get_head(store) == spec.signing_root(genesis_block) + assert spec.get_head(store) == spec.hash_tree_root(genesis_block) # block at slot 1 block_1_state = genesis_state.copy() block_1 = build_empty_block_for_next_slot(spec, block_1_state) - state_transition_and_sign_block(spec, block_1_state, block_1) - add_block_to_store(spec, store, block_1) + signed_block_1 = state_transition_and_sign_block(spec, block_1_state, block_1) + add_block_to_store(spec, store, signed_block_1) # additional block at slot 1 block_2_state = genesis_state.copy() block_2 = build_empty_block_for_next_slot(spec, block_2_state) block_2.body.graffiti = b'\x42' * 32 - state_transition_and_sign_block(spec, block_2_state, block_2) - add_block_to_store(spec, store, block_2) + signed_block_2 = state_transition_and_sign_block(spec, block_2_state, block_2) + add_block_to_store(spec, store, signed_block_2) - highest_root = max(spec.signing_root(block_1), spec.signing_root(block_2)) + highest_root = max(spec.hash_tree_root(block_1), spec.hash_tree_root(block_2)) assert spec.get_head(store) == highest_root @@ -92,23 +92,23 @@ def test_shorter_chain_but_heavier_weight(spec, state): # Initialization store = spec.get_genesis_store(state) genesis_block = spec.BeaconBlock(state_root=state.hash_tree_root()) - assert spec.get_head(store) == spec.signing_root(genesis_block) + assert spec.get_head(store) == spec.hash_tree_root(genesis_block) # build longer tree long_state = genesis_state.copy() for i in range(3): long_block = build_empty_block_for_next_slot(spec, long_state) - state_transition_and_sign_block(spec, long_state, long_block) - add_block_to_store(spec, store, long_block) + signed_long_block = state_transition_and_sign_block(spec, long_state, long_block) + add_block_to_store(spec, store, signed_long_block) # build short tree short_state = genesis_state.copy() short_block = build_empty_block_for_next_slot(spec, short_state) short_block.body.graffiti = b'\x42' * 32 - state_transition_and_sign_block(spec, short_state, short_block) - add_block_to_store(spec, store, short_block) + signed_short_block = state_transition_and_sign_block(spec, short_state, short_block) + add_block_to_store(spec, store, signed_short_block) short_attestation = get_valid_attestation(spec, short_state, short_block.slot, signed=True) add_attestation_to_store(spec, store, short_attestation) - assert spec.get_head(store) == spec.signing_root(short_block) + assert spec.get_head(store) == spec.hash_tree_root(short_block) diff --git a/test_libs/pyspec/eth2spec/test/fork_choice/test_on_attestation.py b/test_libs/pyspec/eth2spec/test/fork_choice/test_on_attestation.py index 50e3fc0704..8f1d6f74f7 100644 --- a/test_libs/pyspec/eth2spec/test/fork_choice/test_on_attestation.py +++ b/test_libs/pyspec/eth2spec/test/fork_choice/test_on_attestation.py @@ -31,12 +31,12 @@ def test_on_attestation_current_epoch(spec, state): spec.on_tick(store, store.time + spec.SECONDS_PER_SLOT * 2) block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) # store block in store - spec.on_block(store, block) + spec.on_block(store, signed_block) - attestation = get_valid_attestation(spec, state, slot=block.slot) + attestation = get_valid_attestation(spec, state, slot=block.slot, signed=True) assert attestation.data.target.epoch == spec.GENESIS_EPOCH assert spec.compute_epoch_at_slot(spec.get_current_slot(store)) == spec.GENESIS_EPOCH @@ -50,12 +50,12 @@ def test_on_attestation_previous_epoch(spec, state): spec.on_tick(store, store.time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH) block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) # store block in store - spec.on_block(store, block) + spec.on_block(store, signed_block) - attestation = get_valid_attestation(spec, state, slot=block.slot) + attestation = get_valid_attestation(spec, state, slot=block.slot, signed=True) assert attestation.data.target.epoch == spec.GENESIS_EPOCH assert spec.compute_epoch_at_slot(spec.get_current_slot(store)) == spec.GENESIS_EPOCH + 1 @@ -73,11 +73,11 @@ def test_on_attestation_past_epoch(spec, state): # create and store block from 3 epochs ago block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) - spec.on_block(store, block) + signed_block = state_transition_and_sign_block(spec, state, block) + spec.on_block(store, signed_block) # create attestation for past block - attestation = get_valid_attestation(spec, state, slot=state.slot) + attestation = get_valid_attestation(spec, state, slot=state.slot, signed=True) assert attestation.data.target.epoch == spec.GENESIS_EPOCH assert spec.compute_epoch_at_slot(spec.get_current_slot(store)) == spec.GENESIS_EPOCH + 2 @@ -99,8 +99,8 @@ def test_on_attestation_target_not_in_store(spec, state): # do not add target block to store - attestation = get_valid_attestation(spec, state, slot=target_block.slot) - assert attestation.data.target.root == target_block.signing_root() + attestation = get_valid_attestation(spec, state, slot=target_block.slot, signed=True) + assert attestation.data.target.root == target_block.hash_tree_root() run_on_attestation(spec, state, store, attestation, False) @@ -116,19 +116,19 @@ def test_on_attestation_beacon_block_not_in_store(spec, state): transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH - 1) target_block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, target_block) + signed_target_block = state_transition_and_sign_block(spec, state, target_block) # store target in store - spec.on_block(store, target_block) + spec.on_block(store, signed_target_block) head_block = build_empty_block_for_next_slot(spec, state) state_transition_and_sign_block(spec, state, head_block) # do not add head block to store - attestation = get_valid_attestation(spec, state, slot=head_block.slot) - assert attestation.data.target.root == target_block.signing_root() - assert attestation.data.beacon_block_root == head_block.signing_root() + attestation = get_valid_attestation(spec, state, slot=head_block.slot, signed=True) + assert attestation.data.target.root == target_block.hash_tree_root() + assert attestation.data.beacon_block_root == head_block.hash_tree_root() run_on_attestation(spec, state, store, attestation, False) @@ -141,15 +141,15 @@ def test_on_attestation_future_epoch(spec, state): spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) # store block in store - spec.on_block(store, block) + spec.on_block(store, signed_block) # move state forward but not store state.slot = block.slot + spec.SLOTS_PER_EPOCH - attestation = get_valid_attestation(spec, state, slot=state.slot) + attestation = get_valid_attestation(spec, state, slot=state.slot, signed=True) run_on_attestation(spec, state, store, attestation, False) @@ -161,13 +161,13 @@ def test_on_attestation_future_block(spec, state): spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - spec.on_block(store, block) + spec.on_block(store, signed_block) # attestation for slot immediately prior to the block being attested to attestation = get_valid_attestation(spec, state, slot=block.slot - 1, signed=False) - attestation.data.beacon_block_root = block.signing_root() + attestation.data.beacon_block_root = block.hash_tree_root() sign_attestation(spec, state, attestation) run_on_attestation(spec, state, store, attestation, False) @@ -181,11 +181,11 @@ def test_on_attestation_same_slot(spec, state): spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - spec.on_block(store, block) + spec.on_block(store, signed_block) - attestation = get_valid_attestation(spec, state, slot=block.slot) + attestation = get_valid_attestation(spec, state, slot=block.slot, signed=True) run_on_attestation(spec, state, store, attestation, False) @@ -197,11 +197,11 @@ def test_on_attestation_invalid_attestation(spec, state): spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - spec.on_block(store, block) + spec.on_block(store, signed_block) - attestation = get_valid_attestation(spec, state, slot=block.slot) + attestation = get_valid_attestation(spec, state, slot=block.slot, signed=True) # make invalid by using an invalid committee index attestation.data.index = spec.MAX_COMMITTEES_PER_SLOT * spec.SLOTS_PER_EPOCH diff --git a/test_libs/pyspec/eth2spec/test/fork_choice/test_on_block.py b/test_libs/pyspec/eth2spec/test/fork_choice/test_on_block.py index e42cb1ac4c..4fda49664b 100644 --- a/test_libs/pyspec/eth2spec/test/fork_choice/test_on_block.py +++ b/test_libs/pyspec/eth2spec/test/fork_choice/test_on_block.py @@ -1,33 +1,35 @@ from copy import deepcopy -from eth2spec.utils.ssz.ssz_impl import signing_root +from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.test.context import with_all_phases, spec_state_test -from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block +from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block, transition_unsigned_block, \ + build_empty_block from eth2spec.test.helpers.state import next_epoch, next_epoch_with_attestations, state_transition_and_sign_block -def run_on_block(spec, store, block, valid=True): +def run_on_block(spec, store, signed_block, valid=True): if not valid: try: - spec.on_block(store, block) + spec.on_block(store, signed_block) except AssertionError: return else: assert False - spec.on_block(store, block) - assert store.blocks[signing_root(block)] == block + spec.on_block(store, signed_block) + assert store.blocks[hash_tree_root(signed_block.message)] == signed_block.message def apply_next_epoch_with_attestations(spec, state, store): - _, new_blocks, post_state = next_epoch_with_attestations(spec, state, True, False) - for block in new_blocks: - block_root = signing_root(block) + _, new_signed_blocks, post_state = next_epoch_with_attestations(spec, state, True, False) + for signed_block in new_signed_blocks: + block = signed_block.message + block_root = hash_tree_root(block) store.blocks[block_root] = block store.block_states[block_root] = post_state - last_block = block + last_signed_block = signed_block spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - return post_state, store, last_block + return post_state, store, last_signed_block @with_all_phases @@ -41,16 +43,15 @@ def test_basic(spec, state): # On receiving a block of `GENESIS_SLOT + 1` slot block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) - run_on_block(spec, store, block) + signed_block = state_transition_and_sign_block(spec, state, block) + run_on_block(spec, store, signed_block) # On receiving a block of next epoch store.time = time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH - block = build_empty_block_for_next_slot(spec, state) - block.slot += spec.SLOTS_PER_EPOCH - state_transition_and_sign_block(spec, state, block) + block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH) + signed_block = state_transition_and_sign_block(spec, state, block) - run_on_block(spec, store, block) + run_on_block(spec, store, signed_block) # TODO: add tests for justified_root and finalized_root @@ -65,10 +66,10 @@ def test_on_block_checkpoints(spec, state): next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - state, store, last_block = apply_next_epoch_with_attestations(spec, state, store) + state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - last_block_root = signing_root(last_block) + last_block_root = hash_tree_root(last_signed_block.message) # Mock the finalized_checkpoint fin_state = store.block_states[last_block_root] @@ -77,8 +78,8 @@ def test_on_block_checkpoints(spec, state): ) block = build_empty_block_for_next_slot(spec, fin_state) - state_transition_and_sign_block(spec, deepcopy(fin_state), block) - run_on_block(spec, store, block) + signed_block = state_transition_and_sign_block(spec, deepcopy(fin_state), block) + run_on_block(spec, store, signed_block) @with_all_phases @@ -91,8 +92,8 @@ def test_on_block_future_block(spec, state): # Fail receiving block of `GENESIS_SLOT + 1` slot block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) - run_on_block(spec, store, block, False) + signed_block = state_transition_and_sign_block(spec, state, block) + run_on_block(spec, store, signed_block, False) @with_all_phases @@ -105,14 +106,14 @@ def test_on_block_bad_parent_root(spec, state): # Fail receiving block of `GENESIS_SLOT + 1` slot block = build_empty_block_for_next_slot(spec, state) - spec.state_transition(state, block) + transition_unsigned_block(spec, state, block) block.state_root = state.hash_tree_root() block.parent_root = b'\x45' * 32 - sign_block(spec, state, block) + signed_block = sign_block(spec, state, block) - run_on_block(spec, store, block, False) + run_on_block(spec, store, signed_block, False) @with_all_phases @@ -130,8 +131,8 @@ def test_on_block_before_finalized(spec, state): # Fail receiving block of `GENESIS_SLOT + 1` slot block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) - run_on_block(spec, store, block, False) + signed_block = state_transition_and_sign_block(spec, state, block) + run_on_block(spec, store, signed_block, False) @with_all_phases @@ -144,10 +145,10 @@ def test_on_block_update_justified_checkpoint_within_safe_slots(spec, state): next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - state, store, last_block = apply_next_epoch_with_attestations(spec, state, store) + state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - last_block_root = signing_root(last_block) + last_block_root = hash_tree_root(last_signed_block.message) # Mock the justified checkpoint just_state = store.block_states[last_block_root] @@ -158,9 +159,9 @@ def test_on_block_update_justified_checkpoint_within_safe_slots(spec, state): just_state.current_justified_checkpoint = new_justified block = build_empty_block_for_next_slot(spec, just_state) - state_transition_and_sign_block(spec, deepcopy(just_state), block) + signed_block = state_transition_and_sign_block(spec, deepcopy(just_state), block) assert spec.get_current_slot(store) % spec.SLOTS_PER_EPOCH < spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED - run_on_block(spec, store, block) + run_on_block(spec, store, signed_block) assert store.justified_checkpoint == new_justified @@ -175,10 +176,10 @@ def test_on_block_outside_safe_slots_and_old_block(spec, state): next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - state, store, last_block = apply_next_epoch_with_attestations(spec, state, store) + state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - last_block_root = signing_root(last_block) + last_block_root = hash_tree_root(last_signed_block.message) # Mock justified block in store just_block = build_empty_block_for_next_slot(spec, state) @@ -195,11 +196,11 @@ def test_on_block_outside_safe_slots_and_old_block(spec, state): just_state.current_justified_checkpoint = new_justified block = build_empty_block_for_next_slot(spec, just_state) - state_transition_and_sign_block(spec, deepcopy(just_state), block) + signed_block = state_transition_and_sign_block(spec, deepcopy(just_state), block) spec.on_tick(store, store.time + spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED * spec.SECONDS_PER_SLOT) assert spec.get_current_slot(store) % spec.SLOTS_PER_EPOCH >= spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED - run_on_block(spec, store, block) + run_on_block(spec, store, signed_block) assert store.justified_checkpoint != new_justified assert store.best_justified_checkpoint == new_justified diff --git a/test_libs/pyspec/eth2spec/test/helpers/attestations.py b/test_libs/pyspec/eth2spec/test/helpers/attestations.py index 299ca32ffb..3ed54888f4 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/attestations.py +++ b/test_libs/pyspec/eth2spec/test/helpers/attestations.py @@ -1,6 +1,7 @@ from typing import List -from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block +from eth2spec.test.helpers.block import build_empty_block_for_next_slot, transition_unsigned_block, \ + build_empty_block from eth2spec.test.helpers.keys import privkeys from eth2spec.utils.bls import bls_sign, bls_aggregate_signatures from eth2spec.utils.ssz.ssz_typing import Bitlist @@ -122,10 +123,8 @@ def fill_aggregate_attestation(spec, state, attestation, signed=False): def add_attestations_to_state(spec, state, attestations, slot): - block = build_empty_block_for_next_slot(spec, state) - block.slot = slot + block = build_empty_block(spec, state, slot) for attestation in attestations: block.body.attestations.append(attestation) spec.process_slots(state, block.slot) - sign_block(spec, state, block) - spec.state_transition(state, block) + transition_unsigned_block(spec, state, block) diff --git a/test_libs/pyspec/eth2spec/test/helpers/block.py b/test_libs/pyspec/eth2spec/test/helpers/block.py index 930311665d..b8c514eb4f 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/block.py +++ b/test_libs/pyspec/eth2spec/test/helpers/block.py @@ -2,26 +2,30 @@ from eth2spec.test.helpers.keys import privkeys from eth2spec.utils.bls import bls_sign, only_with_bls -from eth2spec.utils.ssz.ssz_impl import signing_root, hash_tree_root +from eth2spec.utils.ssz.ssz_impl import hash_tree_root -# Fully ignore the function if BLS is off, beacon-proposer index calculation is slow. -@only_with_bls() -def sign_block(spec, state, block, proposer_index=None): - assert state.slot <= block.slot - +def get_proposer_index_maybe(spec, state, slot, proposer_index=None): if proposer_index is None: - if block.slot == state.slot: + assert state.slot <= slot + if slot == state.slot: proposer_index = spec.get_beacon_proposer_index(state) else: - if spec.compute_epoch_at_slot(state.slot) + 1 > spec.compute_epoch_at_slot(block.slot): + if spec.compute_epoch_at_slot(state.slot) + 1 > spec.compute_epoch_at_slot(slot): print("warning: block slot far away, and no proposer index manually given." " Signing block is slow due to transition for proposer index calculation.") # use stub state to get proposer index of future slot stub_state = deepcopy(state) - spec.process_slots(stub_state, block.slot) + spec.process_slots(stub_state, slot) proposer_index = spec.get_beacon_proposer_index(stub_state) + return proposer_index + +@only_with_bls() +def apply_randao_reveal(spec, state, block, proposer_index=None): + assert state.slot <= block.slot + + proposer_index = get_proposer_index_maybe(spec, state, block.slot, proposer_index) privkey = privkeys[proposer_index] block.body.randao_reveal = bls_sign( @@ -33,8 +37,18 @@ def sign_block(spec, state, block, proposer_index=None): domain_type=spec.DOMAIN_RANDAO, ) ) - block.signature = bls_sign( - message_hash=signing_root(block), + + +# Fully ignore the function if BLS is off, beacon-proposer index calculation is slow. +@only_with_bls() +def apply_sig(spec, state, signed_block, proposer_index=None): + block = signed_block.message + + proposer_index = get_proposer_index_maybe(spec, state, block.slot, proposer_index) + privkey = privkeys[proposer_index] + + signed_block.signature = bls_sign( + message_hash=hash_tree_root(block), privkey=privkey, domain=spec.get_domain( state, @@ -42,17 +56,26 @@ def sign_block(spec, state, block, proposer_index=None): spec.compute_epoch_at_slot(block.slot))) +def sign_block(spec, state, block, proposer_index=None): + signed_block = spec.SignedBeaconBlock(message=block) + apply_sig(spec, state, signed_block, proposer_index) + return signed_block + + +def transition_unsigned_block(spec, state, block): + spec.process_slots(state, block.slot) + spec.process_block(state, block) + + def apply_empty_block(spec, state): """ Transition via an empty block (on current slot, assuming no block has been applied yet). - :return: the empty block that triggered the transition. """ - block = build_empty_block(spec, state, signed=True) - spec.state_transition(state, block) - return block + block = build_empty_block(spec, state) + transition_unsigned_block(spec, state, block) -def build_empty_block(spec, state, slot=None, signed=False): +def build_empty_block(spec, state, slot=None): if slot is None: slot = state.slot empty_block = spec.BeaconBlock() @@ -61,13 +84,10 @@ def build_empty_block(spec, state, slot=None, signed=False): previous_block_header = deepcopy(state.latest_block_header) if previous_block_header.state_root == spec.Root(): previous_block_header.state_root = state.hash_tree_root() - empty_block.parent_root = signing_root(previous_block_header) - - if signed: - sign_block(spec, state, empty_block) - + empty_block.parent_root = hash_tree_root(previous_block_header) + apply_randao_reveal(spec, state, empty_block) return empty_block -def build_empty_block_for_next_slot(spec, state, signed=False): - return build_empty_block(spec, state, state.slot + 1, signed=signed) +def build_empty_block_for_next_slot(spec, state): + return build_empty_block(spec, state, state.slot + 1) diff --git a/test_libs/pyspec/eth2spec/test/helpers/block_header.py b/test_libs/pyspec/eth2spec/test/helpers/block_header.py index 4564141121..a9c8145ae0 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/block_header.py +++ b/test_libs/pyspec/eth2spec/test/helpers/block_header.py @@ -1,5 +1,5 @@ from eth2spec.utils.bls import bls_sign -from eth2spec.utils.ssz.ssz_impl import signing_root +from eth2spec.utils.ssz.ssz_impl import hash_tree_root def sign_block_header(spec, state, header, privkey): @@ -7,8 +7,8 @@ def sign_block_header(spec, state, header, privkey): state=state, domain_type=spec.DOMAIN_BEACON_PROPOSER, ) - header.signature = bls_sign( - message_hash=signing_root(header), + return spec.SignedBeaconBlockHeader(message=header, signature=bls_sign( + message_hash=hash_tree_root(header), privkey=privkey, domain=domain, - ) + )) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index 9f91338e7c..fdab01ca9f 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -1,7 +1,7 @@ from eth2spec.test.helpers.keys import pubkeys, privkeys from eth2spec.utils.bls import bls_sign from eth2spec.utils.merkle_minimal import calc_merkle_tree_from_leaves, get_merkle_proof -from eth2spec.utils.ssz.ssz_impl import signing_root, hash_tree_root +from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_typing import List @@ -26,8 +26,12 @@ def sign_deposit_data(spec, deposit_data, privkey, state=None): spec.DOMAIN_DEPOSIT, ) + deposit_message = spec.DepositMessage( + pubkey=deposit_data.pubkey, + withdrawal_credentials=deposit_data.withdrawal_credentials, + amount=deposit_data.amount) signature = bls_sign( - message_hash=signing_root(deposit_data), + message_hash=hash_tree_root(deposit_message), privkey=privkey, domain=domain, ) diff --git a/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py b/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py index 7955c613ec..8d12110e7b 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py +++ b/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py @@ -6,7 +6,7 @@ only_with_bls, ) from eth2spec.utils.ssz.ssz_impl import ( - signing_root, + hash_tree_root, ) from .attestations import ( @@ -22,7 +22,7 @@ def sign_shard_block(spec, beacon_state, shard_state, block, proposer_index=None privkey = privkeys[proposer_index] block.signature = bls_sign( - message_hash=signing_root(block), + message_hash=hash_tree_root(block), privkey=privkey, domain=spec.get_domain( beacon_state, @@ -44,12 +44,12 @@ def build_empty_shard_block(spec, previous_beacon_header = deepcopy(beacon_state.latest_block_header) if previous_beacon_header.state_root == spec.Bytes32(): previous_beacon_header.state_root = beacon_state.hash_tree_root() - beacon_block_root = spec.signing_root(previous_beacon_header) + beacon_block_root = hash_tree_root(previous_beacon_header) previous_block_header = deepcopy(shard_state.latest_block_header) if previous_block_header.state_root == spec.Bytes32(): previous_block_header.state_root = shard_state.hash_tree_root() - parent_root = signing_root(previous_block_header) + parent_root = hash_tree_root(previous_block_header) block = spec.ShardBlock( shard=shard_state.shard, diff --git a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py index ce53c59318..59eaa56cf7 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py +++ b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py @@ -20,12 +20,16 @@ def get_valid_proposer_slashing(spec, state, signed_1=False, signed_2=False): header_2.parent_root = b'\x99' * 32 if signed_1: - sign_block_header(spec, state, header_1, privkey) + signed_header_1 = sign_block_header(spec, state, header_1, privkey) + else: + signed_header_1 = spec.SignedBeaconBlockHeader(message=header_1) if signed_2: - sign_block_header(spec, state, header_2, privkey) + signed_header_2 = sign_block_header(spec, state, header_2, privkey) + else: + signed_header_2 = spec.SignedBeaconBlockHeader(message=header_2) return spec.ProposerSlashing( proposer_index=validator_index, - header_1=header_1, - header_2=header_2, + signed_header_1=signed_header_1, + signed_header_2=signed_header_2, ) diff --git a/test_libs/pyspec/eth2spec/test/helpers/state.py b/test_libs/pyspec/eth2spec/test/helpers/state.py index a26621c841..059469f6c0 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/state.py +++ b/test_libs/pyspec/eth2spec/test/helpers/state.py @@ -1,6 +1,8 @@ from copy import deepcopy + +from eth2spec.test.context import expect_assertion_error from eth2spec.test.helpers.attestations import get_valid_attestation -from eth2spec.test.helpers.block import sign_block, build_empty_block_for_next_slot +from eth2spec.test.helpers.block import sign_block, build_empty_block_for_next_slot, transition_unsigned_block def get_balance(state, index): @@ -40,14 +42,17 @@ def get_state_root(spec, state, slot) -> bytes: return state.state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT] -def state_transition_and_sign_block(spec, state, block): +def state_transition_and_sign_block(spec, state, block, expect_fail=False): """ State transition via the provided ``block`` - then package the block with the state root and signature. + then package the block with the correct state root and signature. """ - spec.state_transition(state, block) + if expect_fail: + expect_assertion_error(lambda: transition_unsigned_block(spec, state, block)) + else: + transition_unsigned_block(spec, state, block) block.state_root = state.hash_tree_root() - sign_block(spec, state, block) + return sign_block(spec, state, block) def next_epoch_with_attestations(spec, @@ -57,7 +62,7 @@ def next_epoch_with_attestations(spec, assert state.slot % spec.SLOTS_PER_EPOCH == 0 post_state = deepcopy(state) - blocks = [] + signed_blocks = [] for _ in range(spec.SLOTS_PER_EPOCH): block = build_empty_block_for_next_slot(spec, post_state) if fill_cur_epoch and post_state.slot >= spec.MIN_ATTESTATION_INCLUSION_DELAY: @@ -65,17 +70,17 @@ def next_epoch_with_attestations(spec, committees_per_slot = spec.get_committee_count_at_slot(state, slot_to_attest) if slot_to_attest >= spec.compute_start_slot_at_epoch(spec.get_current_epoch(post_state)): for index in range(committees_per_slot): - cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index) + cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index, signed=True) block.body.attestations.append(cur_attestation) if fill_prev_epoch: slot_to_attest = post_state.slot - spec.SLOTS_PER_EPOCH + 1 committees_per_slot = spec.get_committee_count_at_slot(state, slot_to_attest) for index in range(committees_per_slot): - prev_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index) + prev_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index, signed=True) block.body.attestations.append(prev_attestation) - state_transition_and_sign_block(spec, post_state, block) - blocks.append(block) + signed_block = state_transition_and_sign_block(spec, post_state, block) + signed_blocks.append(signed_block) - return state, blocks, post_state + return state, signed_blocks, post_state diff --git a/test_libs/pyspec/eth2spec/test/helpers/voluntary_exits.py b/test_libs/pyspec/eth2spec/test/helpers/voluntary_exits.py index 120a9f6007..62d8f13baa 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/voluntary_exits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/voluntary_exits.py @@ -1,24 +1,17 @@ from eth2spec.utils.bls import bls_sign -from eth2spec.utils.ssz.ssz_impl import signing_root - - -def build_voluntary_exit(spec, state, epoch, validator_index, privkey, signed=False): - voluntary_exit = spec.VoluntaryExit( - epoch=epoch, - validator_index=validator_index, - ) - if signed: - sign_voluntary_exit(spec, state, voluntary_exit, privkey) - return voluntary_exit +from eth2spec.utils.ssz.ssz_impl import hash_tree_root def sign_voluntary_exit(spec, state, voluntary_exit, privkey): - voluntary_exit.signature = bls_sign( - message_hash=signing_root(voluntary_exit), - privkey=privkey, - domain=spec.get_domain( - state=state, - domain_type=spec.DOMAIN_VOLUNTARY_EXIT, - message_epoch=voluntary_exit.epoch, + return spec.SignedVoluntaryExit( + message=voluntary_exit, + signature=bls_sign( + message_hash=hash_tree_root(voluntary_exit), + privkey=privkey, + domain=spec.get_domain( + state=state, + domain_type=spec.DOMAIN_VOLUNTARY_EXIT, + message_epoch=voluntary_exit.epoch, + ) ) ) 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 c790c612c0..b51584ce52 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 @@ -1,10 +1,7 @@ from copy import deepcopy -from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls, with_all_phases -from eth2spec.test.helpers.block import ( - build_empty_block_for_next_slot, - sign_block -) +from eth2spec.test.context import spec_state_test, expect_assertion_error, with_all_phases +from eth2spec.test.helpers.block import build_empty_block_for_next_slot from eth2spec.test.helpers.state import next_slot @@ -37,16 +34,8 @@ def run_block_header_processing(spec, state, block, valid=True): @with_all_phases @spec_state_test def test_success_block_header(spec, state): - block = build_empty_block_for_next_slot(spec, state, signed=True) - yield from run_block_header_processing(spec, state, block) - - -@with_all_phases -@spec_state_test -@always_bls -def test_invalid_sig_block_header(spec, state): block = build_empty_block_for_next_slot(spec, state) - yield from run_block_header_processing(spec, state, block, valid=False) + yield from run_block_header_processing(spec, state, block) @with_all_phases @@ -54,7 +43,6 @@ def test_invalid_sig_block_header(spec, state): def test_invalid_slot_block_header(spec, state): block = build_empty_block_for_next_slot(spec, state) block.slot = state.slot + 2 # invalid slot - sign_block(spec, state, block) yield from run_block_header_processing(spec, state, block, valid=False) @@ -64,7 +52,6 @@ def test_invalid_slot_block_header(spec, state): def test_invalid_parent_root(spec, state): block = build_empty_block_for_next_slot(spec, state) block.parent_root = b'\12' * 32 # invalid prev root - sign_block(spec, state, block) yield from run_block_header_processing(spec, state, block, valid=False) @@ -80,6 +67,6 @@ def test_proposer_slashed(spec, state): # set proposer to slashed state.validators[proposer_index].slashed = True - block = build_empty_block_for_next_slot(spec, state, signed=True) + block = build_empty_block_for_next_slot(spec, state) yield from run_block_header_processing(spec, state, block, valid=False) 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 5eaec9f03f..30b3c1fdde 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 @@ -88,8 +88,10 @@ def test_epochs_are_different(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=False) # set slots to be in different epochs - proposer_slashing.header_2.slot += spec.SLOTS_PER_EPOCH - sign_block_header(spec, state, proposer_slashing.header_2, privkeys[proposer_slashing.proposer_index]) + header_2 = proposer_slashing.signed_header_2.message + header_2.slot += spec.SLOTS_PER_EPOCH + proposer_slashing.signed_header_2 = sign_block_header( + spec, state, header_2, privkeys[proposer_slashing.proposer_index]) yield from run_proposer_slashing_processing(spec, state, proposer_slashing, False) @@ -100,7 +102,7 @@ def test_headers_are_same(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=False) # set headers to be the same - proposer_slashing.header_2 = proposer_slashing.header_1 + proposer_slashing.signed_header_2 = proposer_slashing.signed_header_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_voluntary_exit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py index 155f706217..461c38b737 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 @@ -1,9 +1,9 @@ from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls, with_all_phases from eth2spec.test.helpers.keys import pubkey_to_privkey -from eth2spec.test.helpers.voluntary_exits import build_voluntary_exit, sign_voluntary_exit +from eth2spec.test.helpers.voluntary_exits import sign_voluntary_exit -def run_voluntary_exit_processing(spec, state, voluntary_exit, valid=True): +def run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=True): """ Run ``process_voluntary_exit``, yielding: - pre-state ('pre') @@ -11,19 +11,19 @@ def run_voluntary_exit_processing(spec, state, voluntary_exit, valid=True): - post-state ('post'). If ``valid == False``, run expecting ``AssertionError`` """ - validator_index = voluntary_exit.validator_index + validator_index = signed_voluntary_exit.message.validator_index yield 'pre', state - yield 'voluntary_exit', voluntary_exit + yield 'voluntary_exit', signed_voluntary_exit if not valid: - expect_assertion_error(lambda: spec.process_voluntary_exit(state, voluntary_exit)) + expect_assertion_error(lambda: spec.process_voluntary_exit(state, signed_voluntary_exit)) yield 'post', None return pre_exit_epoch = state.validators[validator_index].exit_epoch - spec.process_voluntary_exit(state, voluntary_exit) + spec.process_voluntary_exit(state, signed_voluntary_exit) yield 'post', state @@ -41,9 +41,10 @@ def test_success(spec, state): validator_index = spec.get_active_validator_indices(state, current_epoch)[0] privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] - voluntary_exit = build_voluntary_exit(spec, state, current_epoch, validator_index, privkey, signed=True) + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit) @with_all_phases @@ -55,11 +56,14 @@ 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.validators[validator_index].pubkey] - voluntary_exit = build_voluntary_exit(spec, state, current_epoch, validator_index, privkey) + voluntary_exit = spec.VoluntaryExit( + epoch=current_epoch, + validator_index=validator_index, + ) + signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, 12345) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, False) @with_all_phases @@ -77,14 +81,11 @@ def test_success_exit_queue(spec, state): exit_queue = [] for index in initial_indices: privkey = pubkey_to_privkey[state.validators[index].pubkey] - exit_queue.append(build_voluntary_exit( - spec, - state, - current_epoch, - index, - privkey, - signed=True, - )) + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=index), privkey) + + exit_queue.append(signed_voluntary_exit) # Now run all the exits for voluntary_exit in exit_queue: @@ -95,18 +96,13 @@ 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.validators[validator_index].pubkey] - voluntary_exit = build_voluntary_exit( - spec, - state, - current_epoch, - validator_index, - privkey, - signed=True, - ) + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) # This is the interesting part of the test: on a pre-state with a full exit queue, # when processing an additional exit, it results in an exit in a later epoch - yield from run_voluntary_exit_processing(spec, state, voluntary_exit) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit) assert ( state.validators[validator_index].exit_epoch == @@ -124,18 +120,13 @@ def test_validator_exit_in_future(spec, state): validator_index = spec.get_active_validator_indices(state, current_epoch)[0] privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] - voluntary_exit = build_voluntary_exit( - spec, - state, - current_epoch, - validator_index, - privkey, - signed=False, + voluntary_exit = spec.VoluntaryExit( + epoch=current_epoch + 1, + validator_index=validator_index, ) - voluntary_exit.epoch += 1 - sign_voluntary_exit(spec, state, voluntary_exit, privkey) + signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, privkey) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, False) @with_all_phases @@ -148,18 +139,13 @@ def test_validator_invalid_validator_index(spec, state): validator_index = spec.get_active_validator_indices(state, current_epoch)[0] privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] - voluntary_exit = build_voluntary_exit( - spec, - state, - current_epoch, - validator_index, - privkey, - signed=False, + voluntary_exit = spec.VoluntaryExit( + epoch=current_epoch, + validator_index=len(state.validators), ) - voluntary_exit.validator_index = len(state.validators) - sign_voluntary_exit(spec, state, voluntary_exit, privkey) + signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, privkey) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, False) @with_all_phases @@ -171,17 +157,10 @@ def test_validator_not_active(spec, state): state.validators[validator_index].activation_epoch = spec.FAR_FUTURE_EPOCH - # build and test voluntary exit - voluntary_exit = build_voluntary_exit( - spec, - state, - current_epoch, - validator_index, - privkey, - signed=True, - ) + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, False) @with_all_phases @@ -197,16 +176,10 @@ def test_validator_already_exited(spec, state): # but validator already has exited state.validators[validator_index].exit_epoch = current_epoch + 2 - voluntary_exit = build_voluntary_exit( - spec, - state, - current_epoch, - validator_index, - privkey, - signed=True, - ) + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, False) @with_all_phases @@ -216,18 +189,12 @@ def test_validator_not_active_long_enough(spec, state): validator_index = spec.get_active_validator_indices(state, current_epoch)[0] privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] - voluntary_exit = build_voluntary_exit( - spec, - state, - current_epoch, - validator_index, - privkey, - signed=True, - ) + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) assert ( current_epoch - state.validators[validator_index].activation_epoch < spec.PERSISTENT_COMMITTEE_PERIOD ) - yield from run_voluntary_exit_processing(spec, state, voluntary_exit, False) + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, False) diff --git a/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py b/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py index 51575f2d56..43b0c56c24 100644 --- a/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py +++ b/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py @@ -140,7 +140,7 @@ def test_skipped_slots(spec, state): assert shard_state.slot == block.slot latest_block_header = deepcopy(shard_state.latest_block_header) latest_block_header.state_root = shard_state.hash_tree_root() - assert latest_block_header.signing_root() == block.signing_root() + assert latest_block_header.hash_tree_root() == block.hash_tree_root() @with_all_phases_except(['phase0']) diff --git a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py index ba577b89eb..41316e92d2 100644 --- a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py @@ -1,17 +1,18 @@ from copy import deepcopy -from eth2spec.utils.ssz.ssz_impl import signing_root +from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.bls import bls_sign from eth2spec.test.helpers.state import get_balance, state_transition_and_sign_block -from eth2spec.test.helpers.block import build_empty_block_for_next_slot, build_empty_block, sign_block +from eth2spec.test.helpers.block import build_empty_block_for_next_slot, build_empty_block, sign_block, \ + transition_unsigned_block from eth2spec.test.helpers.keys import privkeys, pubkeys from eth2spec.test.helpers.attester_slashings import get_valid_attester_slashing from eth2spec.test.helpers.proposer_slashings import get_valid_proposer_slashing from eth2spec.test.helpers.attestations import get_valid_attestation from eth2spec.test.helpers.deposits import prepare_state_and_deposit -from eth2spec.test.context import spec_state_test, with_all_phases, expect_assertion_error +from eth2spec.test.context import spec_state_test, with_all_phases, expect_assertion_error, always_bls @with_all_phases @@ -20,13 +21,18 @@ def test_prev_slot_block_transition(spec, state): # Go to clean slot spec.process_slots(state, state.slot + 1) # Make a block for it - block = build_empty_block(spec, state, slot=state.slot, signed=True) + block = build_empty_block(spec, state, slot=state.slot) + proposer_index = spec.get_beacon_proposer_index(state) # Transition to next slot, above block will not be invalid on top of new state. spec.process_slots(state, state.slot + 1) yield 'pre', state - expect_assertion_error(lambda: state_transition_and_sign_block(spec, state, block)) - yield 'blocks', [block] + # State is beyond block slot, but the block can still be realistic when invalid. + # Try the transition, and update the state root to where it is halted. Then sign with the supposed proposer. + expect_assertion_error(lambda: transition_unsigned_block(spec, state, block)) + block.state_root = state.hash_tree_root() + signed_block = sign_block(spec, state, block, proposer_index=proposer_index) + yield 'blocks', [signed_block] yield 'post', None @@ -36,13 +42,13 @@ def test_same_slot_block_transition(spec, state): # Same slot on top of pre-state, but move out of slot 0 first. spec.process_slots(state, state.slot + 1) - block = build_empty_block(spec, state, slot=state.slot, signed=True) + block = build_empty_block(spec, state, slot=state.slot) yield 'pre', state - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state @@ -54,15 +60,15 @@ def test_empty_block_transition(spec, state): yield 'pre', state - block = build_empty_block_for_next_slot(spec, state, signed=True) + block = build_empty_block_for_next_slot(spec, state) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert len(state.eth1_data_votes) == pre_eth1_votes + 1 - assert spec.get_block_root_at_slot(state, pre_slot) == block.parent_root + assert spec.get_block_root_at_slot(state, pre_slot) == signed_block.message.parent_root assert spec.get_randao_mix(state, spec.get_current_epoch(state)) != spec.Bytes32() @@ -73,28 +79,58 @@ def test_invalid_state_root(spec, state): block = build_empty_block_for_next_slot(spec, state) block.state_root = b"\xaa" * 32 - sign_block(spec, state, block) + signed_block = sign_block(spec, state, block) - expect_assertion_error( - lambda: spec.state_transition(state, block, validate_state_root=True)) + expect_assertion_error(lambda: spec.state_transition(state, signed_block)) yield 'blocks', [block] yield 'post', None +@with_all_phases +@spec_state_test +@always_bls +def test_zero_block_sig(spec, state): + block = build_empty_block_for_next_slot(spec, state) + invalid_signed_block = spec.SignedBeaconBlock(message=block) + expect_assertion_error(lambda: spec.state_transition(state, invalid_signed_block)) + + yield 'blocks', [invalid_signed_block] + yield 'post', None + + +@with_all_phases +@spec_state_test +@always_bls +def test_invalid_block_sig(spec, state): + block = build_empty_block_for_next_slot(spec, state) + invalid_signed_block = spec.SignedBeaconBlock( + message=block, + signature=bls_sign( + message_hash=hash_tree_root(block), + privkey=123456, + domain=spec.get_domain( + state, + spec.DOMAIN_BEACON_PROPOSER, + spec.compute_epoch_at_slot(block.slot))) + ) + expect_assertion_error(lambda: spec.state_transition(state, invalid_signed_block)) + + yield 'blocks', [invalid_signed_block] + yield 'post', None + + @with_all_phases @spec_state_test def test_skipped_slots(spec, state): pre_slot = state.slot yield 'pre', state - block = build_empty_block_for_next_slot(spec, state) - block.slot += 3 - sign_block(spec, state, block) + block = build_empty_block(spec, state, state.slot + 4) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert state.slot == block.slot @@ -109,13 +145,11 @@ def test_empty_epoch_transition(spec, state): pre_slot = state.slot yield 'pre', state - block = build_empty_block_for_next_slot(spec, state) - block.slot += spec.SLOTS_PER_EPOCH - sign_block(spec, state, block) + block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert state.slot == block.slot @@ -136,10 +170,10 @@ def test_empty_epoch_transition_not_finalizing(spec, state): yield 'pre', state spec.process_slots(state, state.slot + (spec.SLOTS_PER_EPOCH * 5)) - block = build_empty_block_for_next_slot(spec, state, signed=True) - state_transition_and_sign_block(spec, state, block) + block = build_empty_block_for_next_slot(spec, state) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert state.slot == block.slot @@ -165,11 +199,10 @@ def test_proposer_slashing(spec, state): # block = build_empty_block_for_next_slot(spec, state) block.body.proposer_slashings.append(proposer_slashing) - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state # check if slashed @@ -199,11 +232,10 @@ def test_attester_slashing(spec, state): # block = build_empty_block_for_next_slot(spec, state) block.body.attester_slashings.append(attester_slashing) - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state slashed_validator = state.validators[validator_index] @@ -229,17 +261,9 @@ def test_expected_deposit_in_block(spec, state): yield 'pre', state block = build_empty_block_for_next_slot(spec, state) - sign_block(spec, state, block) - bad = False - try: - state_transition_and_sign_block(spec, state, block) - bad = True - except AssertionError: - pass - if bad: - raise AssertionError("expected deposit was not enforced") + signed_block = state_transition_and_sign_block(spec, state, block, expect_fail=True) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', None @@ -257,11 +281,9 @@ def test_deposit_in_block(spec, state): block = build_empty_block_for_next_slot(spec, state) block.body.deposits.append(deposit) - sign_block(spec, state, block) - - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert len(state.validators) == initial_registry_len + 1 @@ -285,11 +307,10 @@ def test_deposit_top_up(spec, state): block = build_empty_block_for_next_slot(spec, state) block.body.deposits.append(deposit) - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert len(state.validators) == initial_registry_len @@ -308,23 +329,19 @@ def test_attestation(spec, state): # Add to state via block transition 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 = build_empty_block(spec, state, state.slot + 1 + spec.MIN_ATTESTATION_INCLUSION_DELAY) attestation_block.body.attestations.append(attestation) - sign_block(spec, state, attestation_block) - state_transition_and_sign_block(spec, state, attestation_block) + signed_attestation_block = state_transition_and_sign_block(spec, state, attestation_block) assert len(state.current_epoch_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_block = build_empty_block_for_next_slot(spec, state) - epoch_block.slot += spec.SLOTS_PER_EPOCH - sign_block(spec, state, epoch_block) - state_transition_and_sign_block(spec, state, epoch_block) + epoch_block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH) + signed_epoch_block = state_transition_and_sign_block(spec, state, epoch_block) - yield 'blocks', [attestation_block, epoch_block] + yield 'blocks', [signed_attestation_block, signed_epoch_block] yield 'post', state assert len(state.current_epoch_attestations) == 0 @@ -348,30 +365,30 @@ def test_voluntary_exit(spec, state): epoch=spec.get_current_epoch(state), validator_index=validator_index, ) - voluntary_exit.signature = bls_sign( - message_hash=signing_root(voluntary_exit), - privkey=privkeys[validator_index], - domain=spec.get_domain( - state=state, - domain_type=spec.DOMAIN_VOLUNTARY_EXIT, + signed_voluntary_exit = spec.SignedVoluntaryExit( + message=voluntary_exit, + signature=bls_sign( + message_hash=hash_tree_root(voluntary_exit), + privkey=privkeys[validator_index], + domain=spec.get_domain( + state=state, + domain_type=spec.DOMAIN_VOLUNTARY_EXIT, + ) ) ) # Add to state via block transition initiate_exit_block = build_empty_block_for_next_slot(spec, state) - initiate_exit_block.body.voluntary_exits.append(voluntary_exit) - sign_block(spec, state, initiate_exit_block) - state_transition_and_sign_block(spec, state, initiate_exit_block) + initiate_exit_block.body.voluntary_exits.append(signed_voluntary_exit) + signed_initiate_exit_block = state_transition_and_sign_block(spec, state, initiate_exit_block) 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) - exit_block.slot += spec.SLOTS_PER_EPOCH - sign_block(spec, state, exit_block) - state_transition_and_sign_block(spec, state, exit_block) + exit_block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH) + signed_exit_block = state_transition_and_sign_block(spec, state, exit_block) - yield 'blocks', [initiate_exit_block, exit_block] + yield 'blocks', [signed_initiate_exit_block, signed_exit_block] yield 'post', state assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH @@ -391,12 +408,10 @@ def test_balance_driven_status_transitions(spec, state): yield 'pre', state # trigger epoch transition - block = build_empty_block_for_next_slot(spec, state) - block.slot += spec.SLOTS_PER_EPOCH - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) + block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH @@ -410,11 +425,10 @@ def test_historical_batch(spec, state): yield 'pre', state - block = build_empty_block_for_next_slot(spec, state, signed=True) - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) + block = build_empty_block_for_next_slot(spec, state) + signed_block = state_transition_and_sign_block(spec, state, block) - yield 'blocks', [block] + yield 'blocks', [signed_block] yield 'post', state assert state.slot == block.slot @@ -430,7 +444,6 @@ def test_eth1_data_votes_consensus(spec, state): return offset_block = build_empty_block(spec, state, slot=spec.SLOTS_PER_ETH1_VOTING_PERIOD - 1) - sign_block(spec, state, offset_block) state_transition_and_sign_block(spec, state, offset_block) yield 'pre', state @@ -444,9 +457,8 @@ def test_eth1_data_votes_consensus(spec, state): block = build_empty_block_for_next_slot(spec, state) # wait for over 50% for A, then start voting B block.body.eth1_data.block_hash = b if i * 2 > spec.SLOTS_PER_ETH1_VOTING_PERIOD else a - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) - blocks.append(block) + signed_block = state_transition_and_sign_block(spec, state, block) + blocks.append(signed_block) assert len(state.eth1_data_votes) == spec.SLOTS_PER_ETH1_VOTING_PERIOD assert state.eth1_data.block_hash == a @@ -454,9 +466,8 @@ def test_eth1_data_votes_consensus(spec, state): # transition to next eth1 voting period block = build_empty_block_for_next_slot(spec, state) block.body.eth1_data.block_hash = c - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) - blocks.append(block) + signed_block = state_transition_and_sign_block(spec, state, block) + blocks.append(signed_block) yield 'blocks', blocks yield 'post', state @@ -477,7 +488,6 @@ def test_eth1_data_votes_no_consensus(spec, state): pre_eth1_hash = state.eth1_data.block_hash offset_block = build_empty_block(spec, state, slot=spec.SLOTS_PER_ETH1_VOTING_PERIOD - 1) - sign_block(spec, state, offset_block) state_transition_and_sign_block(spec, state, offset_block) yield 'pre', state @@ -490,9 +500,8 @@ def test_eth1_data_votes_no_consensus(spec, state): block = build_empty_block_for_next_slot(spec, state) # wait for precisely 50% for A, then start voting B for other 50% block.body.eth1_data.block_hash = b if i * 2 >= spec.SLOTS_PER_ETH1_VOTING_PERIOD else a - sign_block(spec, state, block) - state_transition_and_sign_block(spec, state, block) - blocks.append(block) + signed_block = state_transition_and_sign_block(spec, state, block) + blocks.append(signed_block) assert len(state.eth1_data_votes) == spec.SLOTS_PER_ETH1_VOTING_PERIOD assert state.eth1_data.block_hash == pre_eth1_hash diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py index 7483867332..ee103698b4 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py @@ -155,10 +155,3 @@ def hash_tree_root(obj: SSZValue): return mix_in_length(merkleize_chunks(leaves, limit=chunk_count(obj.type())), len(obj)) else: return merkleize_chunks(leaves) - - -def signing_root(obj: Container): - # ignore last field - fields = [field for field in obj][:-1] - leaves = [hash_tree_root(f) for f in fields] - return merkleize_chunks(chunkify(b''.join(leaves))) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 6bc8f83261..2b0e7e0bed 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -156,10 +156,6 @@ def hash_tree_root(self): from .ssz_impl import hash_tree_root return hash_tree_root(self) - def signing_root(self): - from .ssz_impl import signing_root - return signing_root(self) - def __setattr__(self, name, value): if name not in self.__class__.__annotations__: raise AttributeError("Cannot change non-existing SSZ-container attribute") From afb9a1d7c06d80bfeef3fec6c73433b4078bb773 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 3 Dec 2019 21:34:48 +0100 Subject: [PATCH 5/5] move signed envelopes --- specs/core/0_beacon-chain.md | 59 +++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index cde426c03f..fca21994f7 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -33,20 +33,21 @@ - [`DepositMessage`](#depositmessage) - [`DepositData`](#depositdata) - [`BeaconBlockHeader`](#beaconblockheader) - - [`SignedBeaconBlockHeader`](#signedbeaconblockheader) - [Beacon operations](#beacon-operations) - [`ProposerSlashing`](#proposerslashing) - [`AttesterSlashing`](#attesterslashing) - [`Attestation`](#attestation) - [`Deposit`](#deposit) - [`VoluntaryExit`](#voluntaryexit) - - [`SignedVoluntaryExit`](#signedvoluntaryexit) - [Beacon blocks](#beacon-blocks) - [`BeaconBlockBody`](#beaconblockbody) - [`BeaconBlock`](#beaconblock) - - [`SignedBeaconBlock`](#signedbeaconblock) - [Beacon state](#beacon-state) - [`BeaconState`](#beaconstate) + - [Signed envelopes](#signed-envelopes) + - [`SignedVoluntaryExit`](#signedvoluntaryexit) + - [`SignedBeaconBlock`](#signedbeaconblock) + - [`SignedBeaconBlockHeader`](#signedbeaconblockheader) - [Helper functions](#helper-functions) - [Math](#math) - [`integer_squareroot`](#integer_squareroot) @@ -374,14 +375,6 @@ class BeaconBlockHeader(Container): body_root: Root ``` -#### `SignedBeaconBlockHeader` - -```python -class SignedBeaconBlockHeader(Container): - message: BeaconBlockHeader - signature: BLSSignature -``` - ### Beacon operations #### `ProposerSlashing` @@ -426,14 +419,6 @@ class VoluntaryExit(Container): validator_index: ValidatorIndex ``` -#### `SignedVoluntaryExit` - -```python -class SignedVoluntaryExit(Container): - message: VoluntaryExit - signature: BLSSignature -``` - ### Beacon blocks #### `BeaconBlockBody` @@ -461,14 +446,6 @@ class BeaconBlock(Container): body: BeaconBlockBody ``` -#### `SignedBeaconBlock` - -```python -class SignedBeaconBlock(Container): - message: BeaconBlock - signature: BLSSignature -``` - ### Beacon state #### `BeaconState` @@ -505,6 +482,34 @@ class BeaconState(Container): finalized_checkpoint: Checkpoint ``` +### Signed envelopes + +Some messages in the protocol are wrapped in an envelop to better facilitate adding/pruning the signature and to `hash_tree_root` the `message` separate from the signature. + +#### `SignedVoluntaryExit` + +```python +class SignedVoluntaryExit(Container): + message: VoluntaryExit + signature: BLSSignature +``` + +#### `SignedBeaconBlock` + +```python +class SignedBeaconBlock(Container): + message: BeaconBlock + signature: BLSSignature +``` + +#### `SignedBeaconBlockHeader` + +```python +class SignedBeaconBlockHeader(Container): + message: BeaconBlockHeader + signature: BLSSignature +``` + ## Helper functions *Note*: The definitions below are for specification purposes and are not necessarily optimal implementations.