diff --git a/.circleci/config.yml b/.circleci/config.yml index 565b1c92f..df415f2eb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -44,32 +44,19 @@ commands: venv_name: v22-pyspec reqs_checksum: cache-{{ checksum "setup.py" }} venv_path: ./venv - restore_deposit_contract_compiler_cached_venv: - description: "Restore the venv from cache for the deposit contract compiler" - steps: - - restore_cached_venv: - venv_name: v23-deposit-contract-compiler - reqs_checksum: cache-{{ checksum "deposit_contract/compiler/requirements.txt" }} - save_deposit_contract_compiler_cached_venv: - description: "Save the venv to cache for later use of the deposit contract compiler" - steps: - - save_cached_venv: - venv_name: v23-deposit-contract-compiler - reqs_checksum: cache-{{ checksum "deposit_contract/compiler/requirements.txt" }} - venv_path: ./deposit_contract/compiler/venv restore_deposit_contract_tester_cached_venv: description: "Restore the venv from cache for the deposit contract tester" steps: - restore_cached_venv: - venv_name: v22-deposit-contract-tester - reqs_checksum: cache-{{ checksum "setup.py" }}-{{ checksum "deposit_contract/tester/requirements.txt" }} + venv_name: v23-deposit-contract-tester + reqs_checksum: cache-{{ checksum "setup.py" }}-{{ checksum "solidity_deposit_contract/web3_tester/requirements.txt" }} save_deposit_contract_tester_cached_venv: description: "Save the venv to cache for later use of the deposit contract tester" steps: - save_cached_venv: - venv_name: v22-deposit-contract-tester - reqs_checksum: cache-{{ checksum "setup.py" }}-{{ checksum "deposit_contract/tester/requirements.txt" }} - venv_path: ./deposit_contract/tester/venv + venv_name: v23-deposit-contract-tester + reqs_checksum: cache-{{ checksum "setup.py" }}-{{ checksum "solidity_deposit_contract/web3_tester/requirements.txt" }} + venv_path: ./solidity_deposit_contract/web3_tester/venv jobs: checkout_specs: docker: @@ -145,20 +132,49 @@ jobs: - run: name: Run linter command: make lint - install_deposit_contract_compiler: + build_deposit_contract: docker: - # The deposit contract compiler is pinned to python 3.7 because of the vyper version pin. - - image: circleci/python:3.7 - working_directory: ~/specs-repo + - image: ethereum/solc:0.6.11-alpine steps: - - restore_cache: - key: v3-specs-repo-{{ .Branch }}-{{ .Revision }} - - restore_deposit_contract_compiler_cached_venv + - checkout - run: - name: Install deposit contract compiler requirements - command: make install_deposit_contract_compiler - - save_deposit_contract_compiler_cached_venv - install_deposit_contract_tester: + name: Install build essentials + command: | + apk update + apk add git make + - run: + name: Compile the contract + command: | + make compile_deposit_contract + git diff --color --exit-code + - persist_to_workspace: + root: . + paths: + - ./solidity_deposit_contract/deposit_contract.json + - ./build/combined.json + - ./solidity_deposit_contract/lib + test_deposit_contract: + docker: + - image: nixorg/nix:circleci + steps: + - checkout + - restore_cache: + key: nix-store-test-v2 + - attach_workspace: + at: /tmp/ + - run: + name: Test the contract + command: | + mkdir build + cp -r /tmp/build/* build + cp -r /tmp/solidity_deposit_contract/lib/* ./solidity_deposit_contract/lib + cp -r /tmp/solidity_deposit_contract/deposit_contract.json ./solidity_deposit_contract/deposit_contract.json + nix-shell --command 'make test_deposit_contract' ./solidity_deposit_contract/shell.nix + - save_cache: + key: nix-store-test-v2 + paths: + - /nix + install_deposit_contract_web3_tester: docker: - image: circleci/python:3.8 working_directory: ~/specs-repo @@ -168,20 +184,9 @@ jobs: - restore_deposit_contract_tester_cached_venv - run: name: Install deposit contract tester requirements - command: make install_deposit_contract_tester + command: make install_deposit_contract_web3_tester - save_deposit_contract_tester_cached_venv - test_compile_deposit_contract: - docker: - - image: circleci/python:3.7 - working_directory: ~/specs-repo - steps: - - restore_cache: - key: v3-specs-repo-{{ .Branch }}-{{ .Revision }} - - restore_deposit_contract_compiler_cached_venv - - run: - name: Run deposit contract compile test - command: make test_compile_deposit_contract - test_deposit_contract: + test_deposit_contract_web3_tests: docker: - image: circleci/python:3.8 working_directory: ~/specs-repo @@ -190,9 +195,8 @@ jobs: key: v3-specs-repo-{{ .Branch }}-{{ .Revision }} - restore_deposit_contract_tester_cached_venv - run: - name: Run deposit contract test - command: make test_deposit_contract - + name: Run deposit contract test with web3.py + command: make test_deposit_contract_web3_tests workflows: version: 2.1 test_spec: @@ -209,15 +213,15 @@ workflows: - lint: requires: - test - - install_deposit_contract_compiler: + - install_deposit_contract_web3_tester: requires: - checkout_specs - - test_compile_deposit_contract: + - test_deposit_contract_web3_tests: requires: - - install_deposit_contract_compiler - - install_deposit_contract_tester: - requires: - - checkout_specs + - install_deposit_contract_web3_tester + build_and_test_deposit_contract: + jobs: + - build_deposit_contract - test_deposit_contract: requires: - - install_deposit_contract_tester + - build_deposit_contract diff --git a/.gitattributes b/.gitattributes index c2b17bf1a..52031de51 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -*.vy linguist-language=Python +*.sol linguist-language=Solidity diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..6fcad6b97 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/ds-test"] + path = solidity_deposit_contract/lib/ds-test + url = https://github.com/dapphub/ds-test diff --git a/Makefile b/Makefile index f5cb1c702..3914b7b27 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,10 @@ TEST_LIBS_DIR = ./tests/core PY_SPEC_DIR = $(TEST_LIBS_DIR)/pyspec TEST_VECTOR_DIR = ../eth2.0-spec-tests/tests GENERATOR_DIR = ./tests/generators -DEPOSIT_CONTRACT_COMPILER_DIR = ./deposit_contract/compiler -DEPOSIT_CONTRACT_TESTER_DIR = ./deposit_contract/tester +SOLIDITY_DEPOSIT_CONTRACT_DIR = ./solidity_deposit_contract +SOLIDITY_DEPOSIT_CONTRACT_SOURCE = ${SOLIDITY_DEPOSIT_CONTRACT_DIR}/deposit_contract.sol +SOLIDITY_FILE_NAME = deposit_contract.json +DEPOSIT_CONTRACT_TESTER_DIR = ${SOLIDITY_DEPOSIT_CONTRACT_DIR}/web3_tester CONFIGS_DIR = ./configs # Collect a list of generator names @@ -25,6 +27,11 @@ COV_INDEX_FILE=$(PY_SPEC_DIR)/$(COV_HTML_OUT)/index.html CURRENT_DIR = ${CURDIR} LINTER_CONFIG_FILE = $(CURRENT_DIR)/linter.ini +export DAPP_SKIP_BUILD:=1 +export DAPP_SRC:=$(SOLIDITY_DEPOSIT_CONTRACT_DIR) +export DAPP_LIB:=$(SOLIDITY_DEPOSIT_CONTRACT_DIR)/lib +export DAPP_JSON:=build/combined.json + .PHONY: clean partial_clean all test citest lint generate_tests pyspec install_test open_cov \ install_deposit_contract_tester test_deposit_contract install_deposit_contract_compiler \ compile_deposit_contract test_compile_deposit_contract check_toc @@ -38,7 +45,6 @@ partial_clean: rm -rf .pytest_cache rm -f .coverage rm -rf $(PY_SPEC_DIR)/.pytest_cache - rm -rf $(DEPOSIT_CONTRACT_COMPILER_DIR)/.pytest_cache rm -rf $(DEPOSIT_CONTRACT_TESTER_DIR)/.pytest_cache rm -rf $(PY_SPEC_DIR)/phase0 rm -rf $(PY_SPEC_DIR)/phase1 @@ -46,7 +52,7 @@ partial_clean: rm -rf $(PY_SPEC_DIR)/.coverage rm -rf $(PY_SPEC_DIR)/test-reports rm -rf eth2spec.egg-info dist build - + rm -rf build clean: partial_clean rm -rf venv @@ -107,24 +113,26 @@ lint: pyspec flake8 --config $(LINTER_CONFIG_FILE) ./eth2spec \ && mypy --config-file $(LINTER_CONFIG_FILE) -p eth2spec.phase0 -p eth2spec.phase1 -install_deposit_contract_tester: - cd $(DEPOSIT_CONTRACT_TESTER_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install -r requirements.txt +compile_deposit_contract: + @cd $(SOLIDITY_DEPOSIT_CONTRACT_DIR) + @git submodule update --recursive --init + @solc --metadata-literal --optimize --optimize-runs 5000000 --bin --abi --combined-json=abi,bin,bin-runtime,srcmap,srcmap-runtime,ast,metadata,storage-layout --overwrite -o build $(SOLIDITY_DEPOSIT_CONTRACT_SOURCE) $(SOLIDITY_DEPOSIT_CONTRACT_DIR)/tests/deposit_contract.t.sol + @/bin/echo -n '{"abi": ' > $(SOLIDITY_FILE_NAME) + @cat build/DepositContract.abi >> $(SOLIDITY_FILE_NAME) + @/bin/echo -n ', "bytecode": "0x' >> $(SOLIDITY_FILE_NAME) + @cat build/DepositContract.bin >> $(SOLIDITY_FILE_NAME) + @/bin/echo -n '"}' >> $(SOLIDITY_FILE_NAME) test_deposit_contract: + dapp test -v --fuzz-runs 5 + +install_deposit_contract_web3_tester: + cd $(DEPOSIT_CONTRACT_TESTER_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install -r requirements.txt + +test_deposit_contract_web3_tests: cd $(DEPOSIT_CONTRACT_TESTER_DIR); . venv/bin/activate; \ python -m pytest . -install_deposit_contract_compiler: - cd $(DEPOSIT_CONTRACT_COMPILER_DIR); python3.7 -m venv venv; . venv/bin/activate; pip3.7 install -r requirements.txt - -compile_deposit_contract: - cd $(DEPOSIT_CONTRACT_COMPILER_DIR); . venv/bin/activate; \ - python3.7 deposit_contract/compile.py ../contracts/validator_registration.vy - -test_compile_deposit_contract: - cd $(DEPOSIT_CONTRACT_COMPILER_DIR); . venv/bin/activate; \ - python3.7 -m pytest . - # Runs a generator, identified by param 1 define run_generator # Started! diff --git a/deposit_contract/README.md b/deposit_contract/README.md deleted file mode 100644 index e7ec591e5..000000000 --- a/deposit_contract/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# Deposit contract - -## How to set up the testing environment? - -Under the `eth2.0-specs` directory, execute: - -```sh -make install_deposit_contract_tester -``` - -## How to compile the contract? - -```sh -make compile_deposit_contract -``` - -The compiler dependencies can be installed with: - -```sh -make install_deposit_contract_compiler -``` - -Note that this requires python 3.7 to be installed. The pinned vyper version will not work on 3.8. - -The ABI and bytecode will be updated at [`contracts/validator_registration.json`](./contracts/validator_registration.json). - - -## How to run tests? - -For running the contract tests: -```sh -make test_deposit_contract -``` - -For testing the compiler output against the expected formally-verified bytecode: -```sh -make test_compile_deposit_contract -``` diff --git a/deposit_contract/compiler/deposit_contract/compile.py b/deposit_contract/compiler/deposit_contract/compile.py deleted file mode 100644 index 6d6781878..000000000 --- a/deposit_contract/compiler/deposit_contract/compile.py +++ /dev/null @@ -1,31 +0,0 @@ -import argparse -import json -import os - -from vyper import compiler - -DIR = os.path.dirname(__file__) - - -def generate_compiled_json(file_path: str): - deposit_contract_code = open(file_path).read() - abi = compiler.mk_full_signature(deposit_contract_code) - bytecode = compiler.compile_code(deposit_contract_code)['bytecode'] - contract_json = { - 'abi': abi, - 'bytecode': bytecode, - } - # write json - basename = os.path.basename(file_path) - dirname = os.path.dirname(file_path) - contract_name = basename.split('.')[0] - with open(dirname + "/{}.json".format(contract_name), 'w') as f_write: - json.dump(contract_json, f_write) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument("path", type=str, help="the path of the contract") - args = parser.parse_args() - path = args.path - generate_compiled_json(path) diff --git a/deposit_contract/compiler/deposit_contract/test_compile.py b/deposit_contract/compiler/deposit_contract/test_compile.py deleted file mode 100644 index 6922cf80c..000000000 --- a/deposit_contract/compiler/deposit_contract/test_compile.py +++ /dev/null @@ -1,29 +0,0 @@ -from vyper import compiler - -import json -import os - -DIR = os.path.dirname(__file__) - - -def get_deposit_contract_code(): - file_path = os.path.join(DIR, '../../contracts/validator_registration.vy') - deposit_contract_code = open(file_path).read() - return deposit_contract_code - - -def get_deposit_contract_json(): - file_path = os.path.join(DIR, '../../contracts/validator_registration.json') - deposit_contract_json = open(file_path).read() - return json.loads(deposit_contract_json) - - -def test_compile_deposit_contract(): - compiled_deposit_contract_json = get_deposit_contract_json() - - deposit_contract_code = get_deposit_contract_code() - abi = compiler.mk_full_signature(deposit_contract_code) - bytecode = compiler.compile_code(deposit_contract_code)['bytecode'] - - assert abi == compiled_deposit_contract_json["abi"] - assert bytecode == compiled_deposit_contract_json["bytecode"] diff --git a/deposit_contract/compiler/requirements.txt b/deposit_contract/compiler/requirements.txt deleted file mode 100644 index 209d43012..000000000 --- a/deposit_contract/compiler/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -# Vyper beta version used to generate the bytecode that was then formally verified. -# On top of this beta version, a later change was backported, and included in the formal verification: -# https://github.com/vyperlang/vyper/issues/1761 -# The resulting vyper version is pinned and maintained as protected branch. -git+https://github.com/vyperlang/vyper@1761-HOTFIX-v0.1.0-beta.13 - -pytest==3.6.1 diff --git a/deposit_contract/compiler/setup.py b/deposit_contract/compiler/setup.py deleted file mode 100644 index add6d8043..000000000 --- a/deposit_contract/compiler/setup.py +++ /dev/null @@ -1,10 +0,0 @@ -from distutils.core import setup - -setup( - name='deposit_contract_compiler', - packages=['deposit_contract'], - package_dir={"": "."}, - python_requires="3.7", # pinned vyper compiler stops working after 3.7. See vyper issue 1835. - tests_requires=["pytest==3.6.1"], - install_requires=[], # see requirements.txt file -) diff --git a/deposit_contract/contracts/validator_registration.json b/deposit_contract/contracts/validator_registration.json deleted file mode 100644 index 12cfd74ac..000000000 --- a/deposit_contract/contracts/validator_registration.json +++ /dev/null @@ -1 +0,0 @@ -{"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": 95628}, {"name": "get_deposit_count", "outputs": [{"type": "bytes", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 18231}, {"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": 1342274}], "bytecode": "0x740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009857600080fd5b6101406000601f818352015b600061014051602081106100b757600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e357600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012157600080fd5b60c0519050606051600161014051018060405190131561014057600080fd5b809190121561014e57600080fd5b6020811061015b57600080fd5b600260c052602060c02001555b81516001018083528114156100a4575b505061123556600436101561000d576110b0565b600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a05260001561027f575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100e8578060000360020a82046100ef565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561011a57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610163578060000360020a820461016a565b8060020a82025b905090506101a0525b81516001018083528114156100cb575b5050601860086020820661020001602082840111156101a157600080fd5b60208061022082610180600060045af15050818152809050905090508051602001806102c08284600060045af16101d757600080fd5b50506102c05160206001820306601f82010390506103206102c0516020818352015b826103205110151561020a57610226565b6000610320516102e001535b81516001018083528114156101f9575b50505060206102a05260406102c0510160206001820306601f8201039050610280525b60006102805111151561025b57610277565b602061028051036102a001516020610280510361028052610249565b610160515650005b63c5f2892f600051141561050e57341561029857600080fd5b6000610140526101405161016052600154610180526101a060006020818352015b600160016101805116141561033a5760006101a051602081106102db57600080fd5b600060c052602060c02001546020826102400101526020810190506101605160208261024001015260208101905080610240526102409050602060c0825160208401600060025af161032c57600080fd5b60c0519050610160526103a8565b6000610160516020826101c00101526020810190506101a0516020811061036057600080fd5b600260c052602060c02001546020826101c0010152602081019050806101c0526101c09050602060c0825160208401600060025af161039e57600080fd5b60c0519050610160525b61018060026103b657600080fd5b60028151048152505b81516001018083528114156102b9575b505060006101605160208261046001015260208101905061014051610160516101805163806732896102e0526001546103005261030051600658016100a9565b506103605260006103c0525b6103605160206001820306601f82010390506103c05110151561043d57610456565b6103c05161038001526103c0516020016103c05261041b565b61018052610160526101405261036060088060208461046001018260208501600060045af150508051820191505060006018602082066103e001602082840111156104a057600080fd5b60208061040082610140600060045af150508181528090509050905060188060208461046001018260208501600060045af150508051820191505080610460526104609050602060c0825160208401600060025af16104fe57600080fd5b60c051905060005260206000f350005b63621fd130600051141561061c57341561052757600080fd5b6380673289610140526001546101605261016051600658016100a9565b506101c0526000610220525b6101c05160206001820306601f8201039050610220511015156105725761058b565b610220516101e001526102205160200161022052610550565b6101c08051602001806102808284600060045af16105a857600080fd5b50506102805160206001820306601f82010390506102e0610280516020818352015b826102e0511015156105db576105f7565b60006102e0516102a001535b81516001018083528114156105ca575b5050506020610260526040610280510160206001820306601f8201039050610260f350005b632289511860005114156110af57605060043560040161014037603060043560040135111561064a57600080fd5b60406024356004016101c037602060243560040135111561066a57600080fd5b608060443560040161022037606060443560040135111561068a57600080fd5b63ffffffff6001541061069c57600080fd5b633b9aca006102e0526102e0516106b257600080fd5b6102e05134046102c052633b9aca006102c05110156106d057600080fd5b603061014051146106e057600080fd5b60206101c051146106f057600080fd5b6060610220511461070057600080fd5b610140610360525b6103605151602061036051016103605261036061036051101561072a57610708565b6380673289610380526102c0516103a0526103a051600658016100a9565b50610400526000610460525b6104005160206001820306601f8201039050610460511015156107765761078f565b6104605161042001526104605160200161046052610754565b610340610360525b61036051526020610360510361036052610140610360511015156107ba57610797565b6104008051602001806103008284600060045af16107d757600080fd5b5050610140610480525b61048051516020610480510161048052610480610480511015610803576107e1565b63806732896104a0526001546104c0526104c051600658016100a9565b50610520526000610580525b6105205160206001820306601f82010390506105805110151561084e57610867565b610580516105400152610580516020016105805261082c565b610460610480525b61048051526020610480510361048052610140610480511015156108925761086f565b6105208051602001806105a08284600060045af16108af57600080fd5b505060a061062052610620516106605261014080516020018061062051610660018284600060045af16108e157600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516040818352015b826106005110151561091e5761093f565b600061060051610620516106800101535b815160010180835281141561090d575b505050602061062051610660015160206001820306601f82010390506106205101016106205261062051610680526101c080516020018061062051610660018284600060045af161098f57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b82610600511015156109cc576109ed565b600061060051610620516106800101535b81516001018083528114156109bb575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106a05261030080516020018061062051610660018284600060045af1610a3d57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b8261060051101515610a7a57610a9b565b600061060051610620516106800101535b8151600101808352811415610a69575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106c05261022080516020018061062051610660018284600060045af1610aeb57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516060818352015b8261060051101515610b2857610b49565b600061060051610620516106800101535b8151600101808352811415610b17575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106e0526105a080516020018061062051610660018284600060045af1610b9957600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b8261060051101515610bd657610bf7565b600061060051610620516106800101535b8151600101808352811415610bc5575b505050602061062051610660015160206001820306601f8201039050610620510101610620527f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c561062051610660a160006107005260006101406030806020846107c001018260208501600060045af150508051820191505060006010602082066107400160208284011115610c8c57600080fd5b60208061076082610700600060045af15050818152809050905090506010806020846107c001018260208501600060045af1505080518201915050806107c0526107c09050602060c0825160208401600060025af1610cea57600080fd5b60c0519050610720526000600060406020820661086001610220518284011115610d1357600080fd5b6060806108808260206020880688030161022001600060045af1505081815280905090509050602060c0825160208401600060025af1610d5257600080fd5b60c0519050602082610a600101526020810190506000604060206020820661092001610220518284011115610d8657600080fd5b6060806109408260206020880688030161022001600060045af15050818152809050905090506020806020846109e001018260208501600060045af1505080518201915050610700516020826109e0010152602081019050806109e0526109e09050602060c0825160208401600060025af1610e0157600080fd5b60c0519050602082610a6001015260208101905080610a6052610a609050602060c0825160208401600060025af1610e3857600080fd5b60c0519050610840526000600061072051602082610b000101526020810190506101c0602080602084610b0001018260208501600060045af150508051820191505080610b0052610b009050602060c0825160208401600060025af1610e9d57600080fd5b60c0519050602082610c800101526020810190506000610300600880602084610c0001018260208501600060045af15050805182019150506000601860208206610b800160208284011115610ef157600080fd5b602080610ba082610700600060045af1505081815280905090509050601880602084610c0001018260208501600060045af150508051820191505061084051602082610c0001015260208101905080610c0052610c009050602060c0825160208401600060025af1610f6257600080fd5b60c0519050602082610c8001015260208101905080610c8052610c809050602060c0825160208401600060025af1610f9957600080fd5b60c0519050610ae052606435610ae05114610fb357600080fd5b6001805460018254011015610fc757600080fd5b6001815401815550600154610d0052610d2060006020818352015b60016001610d005116141561101757610ae051610d20516020811061100657600080fd5b600060c052602060c02001556110ab565b6000610d20516020811061102a57600080fd5b600060c052602060c0200154602082610d40010152602081019050610ae051602082610d4001015260208101905080610d4052610d409050602060c0825160208401600060025af161107b57600080fd5b60c0519050610ae052610d00600261109257600080fd5b60028151048152505b8151600101808352811415610fe2575b5050005b5b60006000fd5b61017f6112350361017f60003961017f611235036000f3"} \ No newline at end of file diff --git a/deposit_contract/contracts/validator_registration.vy b/deposit_contract/contracts/validator_registration.vy deleted file mode 100644 index 671252e2e..000000000 --- a/deposit_contract/contracts/validator_registration.vy +++ /dev/null @@ -1,110 +0,0 @@ -# Vyper target 0.1.0b13.hotfix1761 -MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei -DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32 -MAX_DEPOSIT_COUNT: constant(uint256) = 4294967295 # 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1 -PUBKEY_LENGTH: constant(uint256) = 48 # bytes -WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes -SIGNATURE_LENGTH: constant(uint256) = 96 # bytes -AMOUNT_LENGTH: constant(uint256) = 8 # bytes - -DepositEvent: event({ - pubkey: bytes[48], - withdrawal_credentials: bytes[32], - amount: bytes[8], - signature: bytes[96], - index: bytes[8], -}) - -branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] -deposit_count: uint256 - -# Compute hashes in empty sparse Merkle tree -zero_hashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] -@public -def __init__(): - for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1): - self.zero_hashes[i + 1] = sha256(concat(self.zero_hashes[i], self.zero_hashes[i])) - - -@private -@constant -def to_little_endian_64(value: uint256) -> bytes[8]: - # Reversing bytes using bitwise uint256 manipulations - # Note: array accesses of bytes[] are not currently supported in Vyper - # Note: this function is only called when `value < 2**64` - y: uint256 = 0 - x: uint256 = value - for _ in range(8): - y = shift(y, 8) - y = y + bitwise_and(x, 255) - x = shift(x, -8) - return slice(convert(y, bytes32), start=24, len=8) - - -@public -@constant -def get_deposit_root() -> bytes32: - zero_bytes32: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 - node: bytes32 = zero_bytes32 - size: uint256 = self.deposit_count - for height in range(DEPOSIT_CONTRACT_TREE_DEPTH): - if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1` - node = sha256(concat(self.branch[height], node)) - else: - node = sha256(concat(node, self.zero_hashes[height])) - size /= 2 - return sha256(concat(node, self.to_little_endian_64(self.deposit_count), slice(zero_bytes32, start=0, len=24))) - - -@public -@constant -def get_deposit_count() -> bytes[8]: - return self.to_little_endian_64(self.deposit_count) - - -@payable -@public -def deposit(pubkey: bytes[PUBKEY_LENGTH], - withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH], - signature: bytes[SIGNATURE_LENGTH], - deposit_data_root: bytes32): - # Avoid overflowing the Merkle tree (and prevent edge case in computing `self.branch`) - assert self.deposit_count < MAX_DEPOSIT_COUNT - - # Check deposit amount - deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") - assert deposit_amount >= MIN_DEPOSIT_AMOUNT - - # Length checks for safety - assert len(pubkey) == PUBKEY_LENGTH - assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH - assert len(signature) == SIGNATURE_LENGTH - - # Emit `DepositEvent` log - amount: bytes[8] = self.to_little_endian_64(deposit_amount) - log.DepositEvent(pubkey, withdrawal_credentials, amount, signature, self.to_little_endian_64(self.deposit_count)) - - # Compute deposit data root (`DepositData` hash tree root) - zero_bytes32: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 - pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH))) - signature_root: bytes32 = sha256(concat( - sha256(slice(signature, start=0, len=64)), - sha256(concat(slice(signature, start=64, len=SIGNATURE_LENGTH - 64), zero_bytes32)), - )) - node: bytes32 = sha256(concat( - sha256(concat(pubkey_root, withdrawal_credentials)), - sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)), - )) - # Verify computed and expected deposit data roots match - assert node == deposit_data_root - - # Add deposit data root to Merkle tree (update a single `branch` node) - self.deposit_count += 1 - size: uint256 = self.deposit_count - for height in range(DEPOSIT_CONTRACT_TREE_DEPTH): - if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1` - self.branch[height] = node - break - node = sha256(concat(self.branch[height], node)) - size /= 2 - diff --git a/deposit_contract/tester/deposit_contract/__init__.py b/deposit_contract/tester/deposit_contract/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/solidity_deposit_contract/AUTHORS b/solidity_deposit_contract/AUTHORS new file mode 100644 index 000000000..4d06c4c57 --- /dev/null +++ b/solidity_deposit_contract/AUTHORS @@ -0,0 +1,28 @@ +The contract logic here is based on the deposit contract written in Vyper. + +The original repository (https://github.com/ethereum/deposit_contract) had the following authors: + +@NIC619 +@carver +@hwwhww +@davesque +@ralexstokes +@ChihChengLiang +@nisdas +@djrtwo +@JustinDrake +@vbuterin +@njgheorghita +@CarlBeek + +In eth2.0-specs repository (https://github.com/ethereum/eth2.0-specs), its current location, it had the following additional authors: + +@daejunpark + +And this repository has the following authors: + +@axic +@MrChico +@chriseth + +(All of the above are GitHub usernames in order of they appeared first in the commit history.) diff --git a/solidity_deposit_contract/README.md b/solidity_deposit_contract/README.md new file mode 100644 index 000000000..52519eccd --- /dev/null +++ b/solidity_deposit_contract/README.md @@ -0,0 +1,25 @@ +# Deposit Contract + +## History + +This is a rewrite of the [Vyper Eth 2.0 deposit contract](https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/deposit_contract/contracts/validator_registration.vy) to Solidity. + +The original motivation was to run the SMTChecker and the new Yul IR generator option (`--ir`) in the compiler. + +As of June 2020, version `r1` of the Solidity deposit contract has been verified and is considered for adoption. +See this [blog post](https://blog.ethereum.org/2020/06/23/eth2-quick-update-no-12/) for more information. + +In August 2020, version `r2` was released with metadata modifications and relicensed to CC0-1.0. Afterward, this contract has been ported back to from [`axic/eth2-deposit-contract`](https://github.com/axic/eth2-deposit-contract) to this repository and replaced the Vyper deposit contract. + +## Running web3 tests + +1. In the `eth2.0-specs` directory run `make install_deposit_contract_web3_tester` to install the tools needed (make sure to have Python 3.7 and pip installed). +2. In the `eth2.0-specs` directory run `make test_deposit_contract_web3_tests` to execute the tests. + +## Running randomized `dapp` tests: + +Install the latest version of `dapp` by following the instructions at [dapp.tools](https://dapp.tools/). Then in the `eth2.0-specs` directory run: + +```sh +make test_deposit_contract +``` diff --git a/solidity_deposit_contract/deposit_contract.json b/solidity_deposit_contract/deposit_contract.json new file mode 100644 index 000000000..060fe729c --- /dev/null +++ b/solidity_deposit_contract/deposit_contract.json @@ -0,0 +1 @@ +{"abi": [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes","name":"pubkey","type":"bytes"},{"indexed":false,"internalType":"bytes","name":"withdrawal_credentials","type":"bytes"},{"indexed":false,"internalType":"bytes","name":"amount","type":"bytes"},{"indexed":false,"internalType":"bytes","name":"signature","type":"bytes"},{"indexed":false,"internalType":"bytes","name":"index","type":"bytes"}],"name":"DepositEvent","type":"event"},{"inputs":[{"internalType":"bytes","name":"pubkey","type":"bytes"},{"internalType":"bytes","name":"withdrawal_credentials","type":"bytes"},{"internalType":"bytes","name":"signature","type":"bytes"},{"internalType":"bytes32","name":"deposit_data_root","type":"bytes32"}],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"get_deposit_count","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"get_deposit_root","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"}], "bytecode": "0x608060405234801561001057600080fd5b5060005b601f8110156101025760026021826020811061002c57fe5b01546021836020811061003b57fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b602083106100925780518252601f199092019160209182019101610073565b51815160209384036101000a60001901801990921691161790526040519190930194509192505080830381855afa1580156100d1573d6000803e3d6000fd5b5050506040513d60208110156100e657600080fd5b5051602160018301602081106100f857fe5b0155600101610014565b506118d680620001136000396000f3fe60806040526004361061003f5760003560e01c806301ffc9a71461004457806322895118146100a4578063621fd130146101ba578063c5f2892f14610244575b600080fd5b34801561005057600080fd5b506100906004803603602081101561006757600080fd5b50357fffffffff000000000000000000000000000000000000000000000000000000001661026b565b604080519115158252519081900360200190f35b6101b8600480360360808110156100ba57600080fd5b8101906020810181356401000000008111156100d557600080fd5b8201836020820111156100e757600080fd5b8035906020019184600183028401116401000000008311171561010957600080fd5b91939092909160208101903564010000000081111561012757600080fd5b82018360208201111561013957600080fd5b8035906020019184600183028401116401000000008311171561015b57600080fd5b91939092909160208101903564010000000081111561017957600080fd5b82018360208201111561018b57600080fd5b803590602001918460018302840111640100000000831117156101ad57600080fd5b919350915035610304565b005b3480156101c657600080fd5b506101cf6110b5565b6040805160208082528351818301528351919283929083019185019080838360005b838110156102095781810151838201526020016101f1565b50505050905090810190601f1680156102365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561025057600080fd5b506102596110c7565b60408051918252519081900360200190f35b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a70000000000000000000000000000000000000000000000000000000014806102fe57507fffffffff0000000000000000000000000000000000000000000000000000000082167f8564090700000000000000000000000000000000000000000000000000000000145b92915050565b6030861461035d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118056026913960400191505060405180910390fd5b602084146103b6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603681526020018061179c6036913960400191505060405180910390fd5b6060821461040f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260298152602001806118786029913960400191505060405180910390fd5b670de0b6b3a7640000341015610470576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118526026913960400191505060405180910390fd5b633b9aca003406156104cd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260338152602001806117d26033913960400191505060405180910390fd5b633b9aca00340467ffffffffffffffff811115610535576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602781526020018061182b6027913960400191505060405180910390fd5b6060610540826114ba565b90507f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c589898989858a8a6105756020546114ba565b6040805160a0808252810189905290819060208201908201606083016080840160c085018e8e80828437600083820152601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690910187810386528c815260200190508c8c808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690920188810386528c5181528c51602091820193918e019250908190849084905b83811015610648578181015183820152602001610630565b50505050905090810190601f1680156106755780820380516001836020036101000a031916815260200191505b5086810383528881526020018989808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169092018881038452895181528951602091820193918b019250908190849084905b838110156106ef5781810151838201526020016106d7565b50505050905090810190601f16801561071c5780820380516001836020036101000a031916815260200191505b509d505050505050505050505050505060405180910390a1600060028a8a600060801b604051602001808484808284377fffffffffffffffffffffffffffffffff0000000000000000000000000000000090941691909301908152604080517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0818403018152601090920190819052815191955093508392506020850191508083835b602083106107fc57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016107bf565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610859573d6000803e3d6000fd5b5050506040513d602081101561086e57600080fd5b5051905060006002806108846040848a8c6116fe565b6040516020018083838082843780830192505050925050506040516020818303038152906040526040518082805190602001908083835b602083106108f857805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016108bb565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610955573d6000803e3d6000fd5b5050506040513d602081101561096a57600080fd5b5051600261097b896040818d6116fe565b60405160009060200180848480828437919091019283525050604080518083038152602092830191829052805190945090925082918401908083835b602083106109f457805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016109b7565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610a51573d6000803e3d6000fd5b5050506040513d6020811015610a6657600080fd5b5051604080516020818101949094528082019290925280518083038201815260609092019081905281519192909182918401908083835b60208310610ada57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610a9d565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610b37573d6000803e3d6000fd5b5050506040513d6020811015610b4c57600080fd5b50516040805160208101858152929350600092600292839287928f928f92018383808284378083019250505093505050506040516020818303038152906040526040518082805190602001908083835b60208310610bd957805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610b9c565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610c36573d6000803e3d6000fd5b5050506040513d6020811015610c4b57600080fd5b50516040518651600291889160009188916020918201918291908601908083835b60208310610ca957805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610c6c565b6001836020036101000a0380198251168184511680821785525050505050509050018367ffffffffffffffff191667ffffffffffffffff1916815260180182815260200193505050506040516020818303038152906040526040518082805190602001908083835b60208310610d4e57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610d11565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610dab573d6000803e3d6000fd5b5050506040513d6020811015610dc057600080fd5b5051604080516020818101949094528082019290925280518083038201815260609092019081905281519192909182918401908083835b60208310610e3457805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610df7565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610e91573d6000803e3d6000fd5b5050506040513d6020811015610ea657600080fd5b50519050858114610f02576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260548152602001806117486054913960600191505060405180910390fd5b60205463ffffffff11610f60576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260218152602001806117276021913960400191505060405180910390fd5b602080546001019081905560005b60208110156110a9578160011660011415610fa0578260008260208110610f9157fe5b0155506110ac95505050505050565b600260008260208110610faf57fe5b01548460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061102557805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610fe8565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015611082573d6000803e3d6000fd5b5050506040513d602081101561109757600080fd5b50519250600282049150600101610f6e565b50fe5b50505050505050565b60606110c26020546114ba565b905090565b6020546000908190815b60208110156112f05781600116600114156111e6576002600082602081106110f557fe5b01548460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061116b57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161112e565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa1580156111c8573d6000803e3d6000fd5b5050506040513d60208110156111dd57600080fd5b505192506112e2565b600283602183602081106111f657fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061126b57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161122e565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa1580156112c8573d6000803e3d6000fd5b5050506040513d60208110156112dd57600080fd5b505192505b6002820491506001016110d1565b506002826112ff6020546114ba565b600060401b6040516020018084815260200183805190602001908083835b6020831061135a57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161131d565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790527fffffffffffffffffffffffffffffffffffffffffffffffff000000000000000095909516920191825250604080518083037ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8018152601890920190819052815191955093508392850191508083835b6020831061143f57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101611402565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa15801561149c573d6000803e3d6000fd5b5050506040513d60208110156114b157600080fd5b50519250505090565b60408051600880825281830190925260609160208201818036833701905050905060c082901b8060071a60f81b826000815181106114f457fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060061a60f81b8260018151811061153757fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060051a60f81b8260028151811061157a57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060041a60f81b826003815181106115bd57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060031a60f81b8260048151811061160057fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060021a60f81b8260058151811061164357fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060011a60f81b8260068151811061168657fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060001a60f81b826007815181106116c957fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535050919050565b6000808585111561170d578182fd5b83861115611719578182fd5b505082019391909203915056fe4465706f736974436f6e74726163743a206d65726b6c6520747265652066756c6c4465706f736974436f6e74726163743a207265636f6e7374727563746564204465706f7369744461746120646f6573206e6f74206d6174636820737570706c696564206465706f7369745f646174615f726f6f744465706f736974436f6e74726163743a20696e76616c6964207769746864726177616c5f63726564656e7469616c73206c656e6774684465706f736974436f6e74726163743a206465706f7369742076616c7565206e6f74206d756c7469706c65206f6620677765694465706f736974436f6e74726163743a20696e76616c6964207075626b6579206c656e6774684465706f736974436f6e74726163743a206465706f7369742076616c756520746f6f20686967684465706f736974436f6e74726163743a206465706f7369742076616c756520746f6f206c6f774465706f736974436f6e74726163743a20696e76616c6964207369676e6174757265206c656e677468a2646970667358221220dceca8706b29e917dacf25fceef95acac8d90d765ac926663ce4096195952b6164736f6c634300060b0033"} \ No newline at end of file diff --git a/solidity_deposit_contract/deposit_contract.sol b/solidity_deposit_contract/deposit_contract.sol new file mode 100644 index 000000000..21d0b895a --- /dev/null +++ b/solidity_deposit_contract/deposit_contract.sol @@ -0,0 +1,178 @@ +// ┏━━━┓━┏┓━┏┓━━┏━━━┓━━┏━━━┓━━━━┏━━━┓━━━━━━━━━━━━━━━━━━━┏┓━━━━━┏━━━┓━━━━━━━━━┏┓━━━━━━━━━━━━━━┏┓━ +// ┃┏━━┛┏┛┗┓┃┃━━┃┏━┓┃━━┃┏━┓┃━━━━┗┓┏┓┃━━━━━━━━━━━━━━━━━━┏┛┗┓━━━━┃┏━┓┃━━━━━━━━┏┛┗┓━━━━━━━━━━━━┏┛┗┓ +// ┃┗━━┓┗┓┏┛┃┗━┓┗┛┏┛┃━━┃┃━┃┃━━━━━┃┃┃┃┏━━┓┏━━┓┏━━┓┏━━┓┏┓┗┓┏┛━━━━┃┃━┗┛┏━━┓┏━┓━┗┓┏┛┏━┓┏━━┓━┏━━┓┗┓┏┛ +// ┃┏━━┛━┃┃━┃┏┓┃┏━┛┏┛━━┃┃━┃┃━━━━━┃┃┃┃┃┏┓┃┃┏┓┃┃┏┓┃┃━━┫┣┫━┃┃━━━━━┃┃━┏┓┃┏┓┃┃┏┓┓━┃┃━┃┏┛┗━┓┃━┃┏━┛━┃┃━ +// ┃┗━━┓━┃┗┓┃┃┃┃┃┃┗━┓┏┓┃┗━┛┃━━━━┏┛┗┛┃┃┃━┫┃┗┛┃┃┗┛┃┣━━┃┃┃━┃┗┓━━━━┃┗━┛┃┃┗┛┃┃┃┃┃━┃┗┓┃┃━┃┗┛┗┓┃┗━┓━┃┗┓ +// ┗━━━┛━┗━┛┗┛┗┛┗━━━┛┗┛┗━━━┛━━━━┗━━━┛┗━━┛┃┏━┛┗━━┛┗━━┛┗┛━┗━┛━━━━┗━━━┛┗━━┛┗┛┗┛━┗━┛┗┛━┗━━━┛┗━━┛━┗━┛ +// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┃┃━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┗┛━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + +// SPDX-License-Identifier: CC0-1.0 + +pragma solidity 0.6.11; + +// This interface is designed to be compatible with the Vyper version. +/// @notice This is the Ethereum 2.0 deposit contract interface. +/// For more information see the Phase 0 specification under https://github.com/ethereum/eth2.0-specs +interface IDepositContract { + /// @notice A processed deposit event. + event DepositEvent( + bytes pubkey, + bytes withdrawal_credentials, + bytes amount, + bytes signature, + bytes index + ); + + /// @notice Submit a Phase 0 DepositData object. + /// @param pubkey A BLS12-381 public key. + /// @param withdrawal_credentials Commitment to a public key for withdrawals. + /// @param signature A BLS12-381 signature. + /// @param deposit_data_root The SHA-256 hash of the SSZ-encoded DepositData object. + /// Used as a protection against malformed input. + function deposit( + bytes calldata pubkey, + bytes calldata withdrawal_credentials, + bytes calldata signature, + bytes32 deposit_data_root + ) external payable; + + /// @notice Query the current deposit root hash. + /// @return The deposit root hash. + function get_deposit_root() external view returns (bytes32); + + /// @notice Query the current deposit count. + /// @return The deposit count encoded as a little endian 64-bit number. + function get_deposit_count() external view returns (bytes memory); +} + +// Based on official specification in https://eips.ethereum.org/EIPS/eip-165 +interface ERC165 { + /// @notice Query if a contract implements an interface + /// @param interfaceId The interface identifier, as specified in ERC-165 + /// @dev Interface identification is specified in ERC-165. This function + /// uses less than 30,000 gas. + /// @return `true` if the contract implements `interfaceId` and + /// `interfaceId` is not 0xffffffff, `false` otherwise + function supportsInterface(bytes4 interfaceId) external pure returns (bool); +} + +// This is a rewrite of the Vyper Eth2.0 deposit contract in Solidity. +// It tries to stay as close as possible to the original source code. +/// @notice This is the Ethereum 2.0 deposit contract interface. +/// For more information see the Phase 0 specification under https://github.com/ethereum/eth2.0-specs +contract DepositContract is IDepositContract, ERC165 { + uint constant DEPOSIT_CONTRACT_TREE_DEPTH = 32; + // NOTE: this also ensures `deposit_count` will fit into 64-bits + uint constant MAX_DEPOSIT_COUNT = 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1; + + bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] branch; + uint256 deposit_count; + + bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] zero_hashes; + + constructor() public { + // Compute hashes in empty sparse Merkle tree + for (uint height = 0; height < DEPOSIT_CONTRACT_TREE_DEPTH - 1; height++) + zero_hashes[height + 1] = sha256(abi.encodePacked(zero_hashes[height], zero_hashes[height])); + } + + function get_deposit_root() override external view returns (bytes32) { + bytes32 node; + uint size = deposit_count; + for (uint height = 0; height < DEPOSIT_CONTRACT_TREE_DEPTH; height++) { + if ((size & 1) == 1) + node = sha256(abi.encodePacked(branch[height], node)); + else + node = sha256(abi.encodePacked(node, zero_hashes[height])); + size /= 2; + } + return sha256(abi.encodePacked( + node, + to_little_endian_64(uint64(deposit_count)), + bytes24(0) + )); + } + + function get_deposit_count() override external view returns (bytes memory) { + return to_little_endian_64(uint64(deposit_count)); + } + + function deposit( + bytes calldata pubkey, + bytes calldata withdrawal_credentials, + bytes calldata signature, + bytes32 deposit_data_root + ) override external payable { + // Extended ABI length checks since dynamic types are used. + require(pubkey.length == 48, "DepositContract: invalid pubkey length"); + require(withdrawal_credentials.length == 32, "DepositContract: invalid withdrawal_credentials length"); + require(signature.length == 96, "DepositContract: invalid signature length"); + + // Check deposit amount + require(msg.value >= 1 ether, "DepositContract: deposit value too low"); + require(msg.value % 1 gwei == 0, "DepositContract: deposit value not multiple of gwei"); + uint deposit_amount = msg.value / 1 gwei; + require(deposit_amount <= type(uint64).max, "DepositContract: deposit value too high"); + + // Emit `DepositEvent` log + bytes memory amount = to_little_endian_64(uint64(deposit_amount)); + emit DepositEvent( + pubkey, + withdrawal_credentials, + amount, + signature, + to_little_endian_64(uint64(deposit_count)) + ); + + // Compute deposit data root (`DepositData` hash tree root) + bytes32 pubkey_root = sha256(abi.encodePacked(pubkey, bytes16(0))); + bytes32 signature_root = sha256(abi.encodePacked( + sha256(abi.encodePacked(signature[:64])), + sha256(abi.encodePacked(signature[64:], bytes32(0))) + )); + bytes32 node = sha256(abi.encodePacked( + sha256(abi.encodePacked(pubkey_root, withdrawal_credentials)), + sha256(abi.encodePacked(amount, bytes24(0), signature_root)) + )); + + // Verify computed and expected deposit data roots match + require(node == deposit_data_root, "DepositContract: reconstructed DepositData does not match supplied deposit_data_root"); + + // Avoid overflowing the Merkle tree (and prevent edge case in computing `branch`) + require(deposit_count < MAX_DEPOSIT_COUNT, "DepositContract: merkle tree full"); + + // Add deposit data root to Merkle tree (update a single `branch` node) + deposit_count += 1; + uint size = deposit_count; + for (uint height = 0; height < DEPOSIT_CONTRACT_TREE_DEPTH; height++) { + if ((size & 1) == 1) { + branch[height] = node; + return; + } + node = sha256(abi.encodePacked(branch[height], node)); + size /= 2; + } + // As the loop should always end prematurely with the `return` statement, + // this code should be unreachable. We assert `false` just to be safe. + assert(false); + } + + function supportsInterface(bytes4 interfaceId) override external pure returns (bool) { + return interfaceId == type(ERC165).interfaceId || interfaceId == type(IDepositContract).interfaceId; + } + + function to_little_endian_64(uint64 value) internal pure returns (bytes memory ret) { + ret = new bytes(8); + bytes8 bytesValue = bytes8(value); + // Byteswapping during copying to bytes. + ret[0] = bytesValue[7]; + ret[1] = bytesValue[6]; + ret[2] = bytesValue[5]; + ret[3] = bytesValue[4]; + ret[4] = bytesValue[3]; + ret[5] = bytesValue[2]; + ret[6] = bytesValue[1]; + ret[7] = bytesValue[0]; + } +} diff --git a/solidity_deposit_contract/lib/ds-test b/solidity_deposit_contract/lib/ds-test new file mode 160000 index 000000000..eb7148d43 --- /dev/null +++ b/solidity_deposit_contract/lib/ds-test @@ -0,0 +1 @@ +Subproject commit eb7148d43c1ca6f9890361e2e2378364af2430ba diff --git a/solidity_deposit_contract/shell.nix b/solidity_deposit_contract/shell.nix new file mode 100644 index 000000000..d0727d49a --- /dev/null +++ b/solidity_deposit_contract/shell.nix @@ -0,0 +1,18 @@ +let dapptools = builtins.fetchGit { + url = "https://github.com/dapphub/dapptools.git"; + rev = "11dcefe1f03b0acafe76b4d7d54821ef6bd63131"; + }; + nixpkgs = builtins.fetchGit { + url = "https://github.com/nixos/nixpkgs"; + ref = "release-19.03"; + rev = "f1707d8875276cfa110139435a7e8998b4c2a4fd"; + }; + pkgs-for-dapp = import nixpkgs { + overlays = [ + (import (dapptools + /overlay.nix)) + ]; + }; +in +pkgs-for-dapp.mkShell { + buildInputs = [ pkgs-for-dapp.dapp pkgs-for-dapp.solc pkgs-for-dapp.hevm ]; +} diff --git a/solidity_deposit_contract/tests/deposit_contract.t.sol b/solidity_deposit_contract/tests/deposit_contract.t.sol new file mode 100644 index 000000000..c4a6718ba --- /dev/null +++ b/solidity_deposit_contract/tests/deposit_contract.t.sol @@ -0,0 +1,142 @@ +pragma solidity ^0.6.2; + +import "../lib/ds-test/src/test.sol"; + +import "./vyper_setup.sol"; +import "../deposit_contract.sol"; + +contract DepositContractTest is DSTest { + DepositContract depositContract_sol; + DepositContract depositContract_vyp; + uint64 constant GWEI = 1000000000; + + function setUp() public { + VyperSetup vyperSetup = new VyperSetup(); + depositContract_vyp = DepositContract(vyperSetup.deployDeposit()); + depositContract_sol = new DepositContract(); + } + + // --- SUCCESS TESTS --- + + // Tests initialized storage values, comparing vyper and solidity + function test_empty_root() public { + bytes32 zHash = 0x0000000000000000000000000000000000000000000000000000000000000000; + bytes32 zHashN = zHash; + for (uint i = 0; i <= 31; i++) { + zHashN = sha256(abi.encodePacked(zHashN, zHashN)); + } + assertEq(sha256(abi.encodePacked(zHashN, zHash)), depositContract_vyp.get_deposit_root()); + assertEq(depositContract_sol.get_deposit_root(), depositContract_vyp.get_deposit_root()); + } + + // Generates 16 random deposits, insert them in both vyper and solidity version and compare get_deposit_root after each insertion + function test_16_deposits(bytes32[16] memory pubkey_one, bytes16[16] memory pubkey_two, bytes32[16] memory _withdrawal_credentials, + bytes32[16] memory sig_one, bytes32[16] memory sig_two, bytes32[16] memory sig_three, uint32[16] memory amount) public { + uint j; + for (uint i = 0; i < 16; i++) { + // as of dcaa774, the solidity version is more restrictive than vyper and requires deposits to be divisible by GWEI + uint64 gweiamount = amount[i] * GWEI; + if (1 ether <= gweiamount) { + j++; + deposit_in(depositContract_sol, pubkey_one[i], pubkey_two[i], _withdrawal_credentials[i], sig_one[i], sig_two[i], sig_three[i], gweiamount); + deposit_in(depositContract_vyp, pubkey_one[i], pubkey_two[i], _withdrawal_credentials[i], sig_one[i], sig_two[i], sig_three[i], gweiamount); + + assertEq(depositContract_sol.get_deposit_root(), depositContract_vyp.get_deposit_root()); + assertEq(keccak256(abi.encodePacked(depositContract_sol.get_deposit_count())), keccak256(abi.encodePacked(depositContract_vyp.get_deposit_count()))); + assertEq(keccak256(abi.encodePacked(depositContract_sol.get_deposit_count())), keccak256(to_little_endian_64(uint64(j)))); + } + } + } + + // The solidity contract fails when given a deposit which is not divisible by GWEI + + function testFail_deposit_not_divisible_by_gwei(bytes32 pubkey_one, bytes16 pubkey_two, bytes32 _withdrawal_credentials, + bytes32 sig_one, bytes32 sig_two, bytes32 sig_three) public { + deposit_in(depositContract_sol, pubkey_one, pubkey_two, _withdrawal_credentials, sig_one, sig_two, sig_three, 1 ether + 1); + } + + // --- FAILURE TESTS --- + + // if the node is given randomly instead of as the ssz root, the chances of success are so unlikely that we can assert it to be false + function testFail_malformed_node_vyp(bytes32 pubkey_one, bytes16 pubkey_two, bytes32 _withdrawal_credentials, bytes32 sig_one, + bytes32 sig_two, bytes32 sig_three, uint64 amount, bytes32 node) public { + bytes memory pubkey = abi.encodePacked(pubkey_one, pubkey_two); + bytes memory withdrawal_credentials = abi.encodePacked(_withdrawal_credentials); //I wish just recasting to `bytes` would work.. + bytes memory signature = abi.encodePacked(sig_one, sig_two, sig_three); + depositContract_vyp.deposit{value: amount}(pubkey, withdrawal_credentials, signature, node); + } + + // If the node is taken randomly instead of as the ssz root, the chances of success are so unlikely that we can assert it to be false + function testFail_malformed_node_sol(bytes32 pubkey_one, bytes16 pubkey_two, bytes32 _withdrawal_credentials, bytes32 sig_one, + bytes32 sig_two, bytes32 sig_three, uint64 amount, bytes32 node) public { + bytes memory pubkey = abi.encodePacked(pubkey_one, pubkey_two); + bytes memory withdrawal_credentials = abi.encodePacked(_withdrawal_credentials); + bytes memory signature = abi.encodePacked(sig_one, sig_two, sig_three); + depositContract_sol.deposit{value: amount}(pubkey, withdrawal_credentials, signature, node); + } + + // if bytes lengths are wrong, the call will fail + function testFail_malformed_calldata_vyp(bytes memory pubkey, bytes memory withdrawal_credentials, bytes memory signature, uint64 amount) public { + if (amount >= 1000000000000000000) { + if (!(pubkey.length == 48 && withdrawal_credentials.length == 32 && signature.length == 96)) { + depositContract_vyp.deposit{value: amount}(pubkey, withdrawal_credentials, signature, + encode_node(pubkey, withdrawal_credentials, signature, to_little_endian_64(amount / GWEI)) + ); + } else { revert(); } + } else { revert(); } + } + + function testFail_malformed_calldata_sol(bytes memory pubkey, bytes memory withdrawal_credentials, bytes memory signature, uint64 amount) public { + if (amount >= 1000000000000000000) { + if (!(pubkey.length == 48 && withdrawal_credentials.length == 32 && signature.length == 96)) { + depositContract_sol.deposit{value: amount}(pubkey, withdrawal_credentials, signature, + encode_node(pubkey, withdrawal_credentials, signature, to_little_endian_64(amount / GWEI)) + ); + } else { revert(); } + } else { revert(); } + } + + // --- HELPER FUNCTIONS --- + + function deposit_in(DepositContract depositContract, bytes32 pubkey_one, bytes16 pubkey_two, bytes32 _withdrawal_credentials, bytes32 sig_one, bytes32 sig_two, bytes32 sig_three, uint64 amount) public { + bytes memory pubkey = abi.encodePacked(pubkey_one, pubkey_two); + bytes memory withdrawal_credentials = abi.encodePacked(_withdrawal_credentials); + bytes memory signature = abi.encodePacked(sig_one, sig_two, sig_three); + bytes32 node = encode_node(pubkey, withdrawal_credentials, signature, to_little_endian_64(amount / GWEI)); + depositContract.deposit{value: amount}(pubkey, withdrawal_credentials, signature, node); + } + + function slice(bytes memory a, uint32 offset, uint32 size) pure internal returns (bytes memory result) { + result = new bytes(size); + for (uint i = 0; i < size; i++) { + result[i] = a[offset + i]; + } + } + + function encode_node(bytes memory pubkey, bytes memory withdrawal_credentials, bytes memory signature, bytes memory amount) public pure returns (bytes32) { + bytes16 zero_bytes16; + bytes24 zero_bytes24; + bytes32 zero_bytes32; + bytes32 pubkey_root = sha256(abi.encodePacked(pubkey, zero_bytes16)); + bytes32 signature_root = sha256(abi.encodePacked( + sha256(abi.encodePacked(slice(signature, 0, 64))), + sha256(abi.encodePacked(slice(signature, 64, 32), zero_bytes32)) + )); + return sha256(abi.encodePacked( + sha256(abi.encodePacked(pubkey_root, withdrawal_credentials)), + sha256(abi.encodePacked(amount, zero_bytes24, signature_root)) + )); + } + + function to_little_endian_64(uint64 value) internal pure returns (bytes memory ret) { + ret = new bytes(8); + ret[0] = bytes1(uint8(value & 0xff)); + ret[1] = bytes1(uint8((value >> 8) & 0xff)); + ret[2] = bytes1(uint8((value >> 16) & 0xff)); + ret[3] = bytes1(uint8((value >> 24) & 0xff)); + ret[4] = bytes1(uint8((value >> 32) & 0xff)); + ret[5] = bytes1(uint8((value >> 40) & 0xff)); + ret[6] = bytes1(uint8((value >> 48) & 0xff)); + ret[7] = bytes1(uint8((value >> 56) & 0xff)); + } +} diff --git a/solidity_deposit_contract/tests/vyper_setup.sol b/solidity_deposit_contract/tests/vyper_setup.sol new file mode 100644 index 000000000..15a5b6e59 --- /dev/null +++ b/solidity_deposit_contract/tests/vyper_setup.sol @@ -0,0 +1,16 @@ +pragma solidity ^0.6.0; + +contract VyperSetup { + // Bytecode from https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/deposit_contract/contracts/validator_registration.vy + bytes constant public depositCode = hex"740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009857600080fd5b6101406000601f818352015b600061014051602081106100b757600080fd5b600260c052602060c020015460208261016001015260208101905061014051602081106100e357600080fd5b600260c052602060c020015460208261016001015260208101905080610160526101609050602060c0825160208401600060025af161012157600080fd5b60c0519050606051600161014051018060405190131561014057600080fd5b809190121561014e57600080fd5b6020811061015b57600080fd5b600260c052602060c02001555b81516001018083528114156100a4575b505061123556600436101561000d576110b0565b600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a05260001561027f575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100e8578060000360020a82046100ef565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561011a57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610163578060000360020a820461016a565b8060020a82025b905090506101a0525b81516001018083528114156100cb575b5050601860086020820661020001602082840111156101a157600080fd5b60208061022082610180600060045af15050818152809050905090508051602001806102c08284600060045af16101d757600080fd5b50506102c05160206001820306601f82010390506103206102c0516020818352015b826103205110151561020a57610226565b6000610320516102e001535b81516001018083528114156101f9575b50505060206102a05260406102c0510160206001820306601f8201039050610280525b60006102805111151561025b57610277565b602061028051036102a001516020610280510361028052610249565b610160515650005b63c5f2892f600051141561050e57341561029857600080fd5b6000610140526101405161016052600154610180526101a060006020818352015b600160016101805116141561033a5760006101a051602081106102db57600080fd5b600060c052602060c02001546020826102400101526020810190506101605160208261024001015260208101905080610240526102409050602060c0825160208401600060025af161032c57600080fd5b60c0519050610160526103a8565b6000610160516020826101c00101526020810190506101a0516020811061036057600080fd5b600260c052602060c02001546020826101c0010152602081019050806101c0526101c09050602060c0825160208401600060025af161039e57600080fd5b60c0519050610160525b61018060026103b657600080fd5b60028151048152505b81516001018083528114156102b9575b505060006101605160208261046001015260208101905061014051610160516101805163806732896102e0526001546103005261030051600658016100a9565b506103605260006103c0525b6103605160206001820306601f82010390506103c05110151561043d57610456565b6103c05161038001526103c0516020016103c05261041b565b61018052610160526101405261036060088060208461046001018260208501600060045af150508051820191505060006018602082066103e001602082840111156104a057600080fd5b60208061040082610140600060045af150508181528090509050905060188060208461046001018260208501600060045af150508051820191505080610460526104609050602060c0825160208401600060025af16104fe57600080fd5b60c051905060005260206000f350005b63621fd130600051141561061c57341561052757600080fd5b6380673289610140526001546101605261016051600658016100a9565b506101c0526000610220525b6101c05160206001820306601f8201039050610220511015156105725761058b565b610220516101e001526102205160200161022052610550565b6101c08051602001806102808284600060045af16105a857600080fd5b50506102805160206001820306601f82010390506102e0610280516020818352015b826102e0511015156105db576105f7565b60006102e0516102a001535b81516001018083528114156105ca575b5050506020610260526040610280510160206001820306601f8201039050610260f350005b632289511860005114156110af57605060043560040161014037603060043560040135111561064a57600080fd5b60406024356004016101c037602060243560040135111561066a57600080fd5b608060443560040161022037606060443560040135111561068a57600080fd5b63ffffffff6001541061069c57600080fd5b633b9aca006102e0526102e0516106b257600080fd5b6102e05134046102c052633b9aca006102c05110156106d057600080fd5b603061014051146106e057600080fd5b60206101c051146106f057600080fd5b6060610220511461070057600080fd5b610140610360525b6103605151602061036051016103605261036061036051101561072a57610708565b6380673289610380526102c0516103a0526103a051600658016100a9565b50610400526000610460525b6104005160206001820306601f8201039050610460511015156107765761078f565b6104605161042001526104605160200161046052610754565b610340610360525b61036051526020610360510361036052610140610360511015156107ba57610797565b6104008051602001806103008284600060045af16107d757600080fd5b5050610140610480525b61048051516020610480510161048052610480610480511015610803576107e1565b63806732896104a0526001546104c0526104c051600658016100a9565b50610520526000610580525b6105205160206001820306601f82010390506105805110151561084e57610867565b610580516105400152610580516020016105805261082c565b610460610480525b61048051526020610480510361048052610140610480511015156108925761086f565b6105208051602001806105a08284600060045af16108af57600080fd5b505060a061062052610620516106605261014080516020018061062051610660018284600060045af16108e157600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516040818352015b826106005110151561091e5761093f565b600061060051610620516106800101535b815160010180835281141561090d575b505050602061062051610660015160206001820306601f82010390506106205101016106205261062051610680526101c080516020018061062051610660018284600060045af161098f57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b82610600511015156109cc576109ed565b600061060051610620516106800101535b81516001018083528114156109bb575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106a05261030080516020018061062051610660018284600060045af1610a3d57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b8261060051101515610a7a57610a9b565b600061060051610620516106800101535b8151600101808352811415610a69575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106c05261022080516020018061062051610660018284600060045af1610aeb57600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516060818352015b8261060051101515610b2857610b49565b600061060051610620516106800101535b8151600101808352811415610b17575b505050602061062051610660015160206001820306601f820103905061062051010161062052610620516106e0526105a080516020018061062051610660018284600060045af1610b9957600080fd5b505061062051610660015160206001820306601f82010390506106006106205161066001516020818352015b8261060051101515610bd657610bf7565b600061060051610620516106800101535b8151600101808352811415610bc5575b505050602061062051610660015160206001820306601f8201039050610620510101610620527f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c561062051610660a160006107005260006101406030806020846107c001018260208501600060045af150508051820191505060006010602082066107400160208284011115610c8c57600080fd5b60208061076082610700600060045af15050818152809050905090506010806020846107c001018260208501600060045af1505080518201915050806107c0526107c09050602060c0825160208401600060025af1610cea57600080fd5b60c0519050610720526000600060406020820661086001610220518284011115610d1357600080fd5b6060806108808260206020880688030161022001600060045af1505081815280905090509050602060c0825160208401600060025af1610d5257600080fd5b60c0519050602082610a600101526020810190506000604060206020820661092001610220518284011115610d8657600080fd5b6060806109408260206020880688030161022001600060045af15050818152809050905090506020806020846109e001018260208501600060045af1505080518201915050610700516020826109e0010152602081019050806109e0526109e09050602060c0825160208401600060025af1610e0157600080fd5b60c0519050602082610a6001015260208101905080610a6052610a609050602060c0825160208401600060025af1610e3857600080fd5b60c0519050610840526000600061072051602082610b000101526020810190506101c0602080602084610b0001018260208501600060045af150508051820191505080610b0052610b009050602060c0825160208401600060025af1610e9d57600080fd5b60c0519050602082610c800101526020810190506000610300600880602084610c0001018260208501600060045af15050805182019150506000601860208206610b800160208284011115610ef157600080fd5b602080610ba082610700600060045af1505081815280905090509050601880602084610c0001018260208501600060045af150508051820191505061084051602082610c0001015260208101905080610c0052610c009050602060c0825160208401600060025af1610f6257600080fd5b60c0519050602082610c8001015260208101905080610c8052610c809050602060c0825160208401600060025af1610f9957600080fd5b60c0519050610ae052606435610ae05114610fb357600080fd5b6001805460018254011015610fc757600080fd5b6001815401815550600154610d0052610d2060006020818352015b60016001610d005116141561101757610ae051610d20516020811061100657600080fd5b600060c052602060c02001556110ab565b6000610d20516020811061102a57600080fd5b600060c052602060c0200154602082610d40010152602081019050610ae051602082610d4001015260208101905080610d4052610d409050602060c0825160208401600060025af161107b57600080fd5b60c0519050610ae052610d00600261109257600080fd5b60028151048152505b8151600101808352811415610fe2575b5050005b5b60006000fd5b61017f6112350361017f60003961017f611235036000f3"; + function write(bytes memory _code) public returns (address target) { + assembly { + target := create(0, add(_code, 0x20), mload(_code)) + } + } + + function deployDeposit() public returns (address) { + return write(depositCode); + } +} + diff --git a/deposit_contract/tester/requirements.txt b/solidity_deposit_contract/web3_tester/requirements.txt similarity index 100% rename from deposit_contract/tester/requirements.txt rename to solidity_deposit_contract/web3_tester/requirements.txt diff --git a/deposit_contract/tester/setup.py b/solidity_deposit_contract/web3_tester/setup.py similarity index 100% rename from deposit_contract/tester/setup.py rename to solidity_deposit_contract/web3_tester/setup.py diff --git a/deposit_contract/compiler/deposit_contract/__init__.py b/solidity_deposit_contract/web3_tester/tests/__init__.py similarity index 100% rename from deposit_contract/compiler/deposit_contract/__init__.py rename to solidity_deposit_contract/web3_tester/tests/__init__.py diff --git a/deposit_contract/tester/deposit_contract/conftest.py b/solidity_deposit_contract/web3_tester/tests/conftest.py similarity index 95% rename from deposit_contract/tester/deposit_contract/conftest.py rename to solidity_deposit_contract/web3_tester/tests/conftest.py index c20501b11..57b5f6b7a 100644 --- a/deposit_contract/tester/deposit_contract/conftest.py +++ b/solidity_deposit_contract/web3_tester/tests/conftest.py @@ -15,7 +15,7 @@ DIR = os.path.dirname(__file__) def get_deposit_contract_json(): - file_path = os.path.join(DIR, '../../contracts/validator_registration.json') + file_path = os.path.join(DIR, '../../deposit_contract.json') deposit_contract_json = open(file_path).read() return json.loads(deposit_contract_json) diff --git a/deposit_contract/tester/deposit_contract/test_deposit.py b/solidity_deposit_contract/web3_tester/tests/test_deposit.py similarity index 99% rename from deposit_contract/tester/deposit_contract/test_deposit.py rename to solidity_deposit_contract/web3_tester/tests/test_deposit.py index 5fa98e232..4b16446a1 100644 --- a/deposit_contract/tester/deposit_contract/test_deposit.py +++ b/solidity_deposit_contract/web3_tester/tests/test_deposit.py @@ -6,7 +6,7 @@ from eth2spec.phase0.spec import DepositData from eth2spec.utils.ssz.ssz_typing import List from eth2spec.utils.ssz.ssz_impl import hash_tree_root -from deposit_contract.conftest import ( +from tests.conftest import ( FULL_DEPOSIT_AMOUNT, MIN_DEPOSIT_AMOUNT, ) diff --git a/specs/phase0/deposit-contract.md b/specs/phase0/deposit-contract.md index b4f8d3036..89c5bb22b 100644 --- a/specs/phase0/deposit-contract.md +++ b/specs/phase0/deposit-contract.md @@ -16,7 +16,7 @@ - [Deposit amount](#deposit-amount) - [Withdrawal credentials](#withdrawal-credentials) - [`DepositEvent` log](#depositevent-log) -- [Vyper code](#vyper-code) +- [Solidity code](#solidity-code) @@ -53,7 +53,7 @@ _Note_: See [here](https://chainid.network/) for a comprehensive list of public ### `deposit` function -The deposit contract has a public `deposit` function to make deposits. It takes as arguments `pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96], deposit_data_root: bytes32`. The first three arguments populate a [`DepositData`](./beacon-chain.md#depositdata) object, and `deposit_data_root` is the expected `DepositData` root as a protection against malformatted calldata. +The deposit contract has a public `deposit` function to make deposits. It takes as arguments `bytes calldata pubkey, bytes calldata withdrawal_credentials, bytes calldata signature, bytes32 deposit_data_root`. The first three arguments populate a [`DepositData`](./beacon-chain.md#depositdata) object, and `deposit_data_root` is the expected `DepositData` root as a protection against malformatted calldata. #### Deposit amount @@ -72,8 +72,8 @@ The private key corresponding to `withdrawal_pubkey` will be required to initiat Every Ethereum 1.0 deposit emits a `DepositEvent` log for consumption by the beacon chain. The deposit contract does little validation, pushing most of the validator onboarding logic to the beacon chain. In particular, the proof of possession (a BLS12-381 signature) is not verified by the deposit contract. -## Vyper code +## Solidity code -The deposit contract source code, written in Vyper, is available [here](../../deposit_contract/contracts/validator_registration.vy). +The deposit contract source code, written in Solidity, is available [here](../../solidity_deposit_contract/deposit_contract.sol). *Note*: To save on gas, the deposit contract uses a progressive Merkle root calculation algorithm that requires only O(log(n)) storage. See [here](https://github.com/ethereum/research/blob/master/beacon_chain_impl/progressive_merkle_tree.py) for a Python implementation, and [here](https://github.com/runtimeverification/verified-smart-contracts/blob/master/deposit/formal-incremental-merkle-tree-algorithm.pdf) for a formal correctness proof.