diff --git a/beacon-chain/core/BUILD.bazel b/beacon-chain/core/BUILD.bazel index a8e7a36ce..ddb00f5a1 100644 --- a/beacon-chain/core/BUILD.bazel +++ b/beacon-chain/core/BUILD.bazel @@ -6,12 +6,12 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core", visibility = [ "//beacon-chain:__subpackages__", + "//contracts/deposit:__pkg__", "//fuzz:__pkg__", "//network/forks:__pkg__", "//proto/prysm/v1alpha1/attestation:__pkg__", "//runtime/interop:__pkg__", "//shared/attestationutil:__pkg__", - "//shared/depositutil:__pkg__", "//shared/keystore:__pkg__", "//shared/testutil:__pkg__", "//shared/testutil/altair:__pkg__", diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index 429098848..32439f996 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//config/params:go_default_library", "//container/slice:go_default_library", "//container/trie:go_default_library", + "//contracts/deposit:go_default_library", "//crypto/bls:go_default_library", "//crypto/hash:go_default_library", "//math:go_default_library", @@ -41,7 +42,6 @@ go_library( "//proto/prysm/v1alpha1/slashings:go_default_library", "//runtime/version:go_default_library", "//shared/bytesutil:go_default_library", - "//shared/depositutil:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", diff --git a/beacon-chain/core/blocks/deposit.go b/beacon-chain/core/blocks/deposit.go index 1c79204b9..a1c2aac87 100644 --- a/beacon-chain/core/blocks/deposit.go +++ b/beacon-chain/core/blocks/deposit.go @@ -9,11 +9,11 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/container/trie" + "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/crypto/bls" "github.com/prysmaticlabs/prysm/math" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" - "github.com/prysmaticlabs/prysm/shared/depositutil" ) // ProcessPreGenesisDeposits processes a deposit for the beacon state before chainstart. @@ -238,7 +238,7 @@ func verifyDeposit(beaconState state.ReadOnlyBeaconState, deposit *ethpb.Deposit } func verifyDepositDataSigningRoot(obj *ethpb.Deposit_Data, domain []byte) error { - return depositutil.VerifyDepositSignature(obj, domain) + return deposit.VerifyDepositSignature(obj, domain) } func verifyDepositDataWithDomain(ctx context.Context, deps []*ethpb.Deposit, domain []byte) error { diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index 628ceb077..c65c8c5b7 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -18,12 +18,12 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers", visibility = [ "//beacon-chain:__subpackages__", + "//contracts/deposit:__pkg__", "//fuzz:__pkg__", "//network/forks:__pkg__", "//proto/prysm/v1alpha1/attestation:__pkg__", "//runtime/interop:__pkg__", "//shared/attestationutil:__pkg__", - "//shared/depositutil:__pkg__", "//shared/keystore:__pkg__", "//shared/testutil:__pkg__", "//shared/testutil/altair:__pkg__", diff --git a/beacon-chain/node/config_test.go b/beacon-chain/node/config_test.go index 89518dc25..7e6abb224 100644 --- a/beacon-chain/node/config_test.go +++ b/beacon-chain/node/config_test.go @@ -60,14 +60,14 @@ func TestConfigureProofOfWork(t *testing.T) { set.String(flags.DepositContractFlag.Name, "", "") require.NoError(t, set.Set(flags.ChainID.Name, strconv.Itoa(100))) require.NoError(t, set.Set(flags.NetworkID.Name, strconv.Itoa(200))) - require.NoError(t, set.Set(flags.DepositContractFlag.Name, "deposit")) + require.NoError(t, set.Set(flags.DepositContractFlag.Name, "deposit-contract")) cliCtx := cli.NewContext(&app, set, nil) configureEth1Config(cliCtx) assert.Equal(t, uint64(100), params.BeaconConfig().DepositChainID) assert.Equal(t, uint64(200), params.BeaconConfig().DepositNetworkID) - assert.Equal(t, "deposit", params.BeaconConfig().DepositContractAddress) + assert.Equal(t, "deposit-contract", params.BeaconConfig().DepositContractAddress) } func TestConfigureNetwork(t *testing.T) { diff --git a/beacon-chain/powchain/BUILD.bazel b/beacon-chain/powchain/BUILD.bazel index 9d87d0039..3ae956f56 100644 --- a/beacon-chain/powchain/BUILD.bazel +++ b/beacon-chain/powchain/BUILD.bazel @@ -32,7 +32,7 @@ go_library( "//beacon-chain/state/v1:go_default_library", "//config/params:go_default_library", "//container/trie:go_default_library", - "//contracts/deposit-contract:go_default_library", + "//contracts/deposit:go_default_library", "//crypto/hash:go_default_library", "//io/logs:go_default_library", "//monitoring/clientstats:go_default_library", @@ -85,7 +85,7 @@ go_test( "//beacon-chain/powchain/types:go_default_library", "//config/params:go_default_library", "//container/trie:go_default_library", - "//contracts/deposit-contract:go_default_library", + "//contracts/deposit:go_default_library", "//crypto/bls:go_default_library", "//monitoring/clientstats:go_default_library", "//network:go_default_library", diff --git a/beacon-chain/powchain/block_reader_test.go b/beacon-chain/powchain/block_reader_test.go index 32c695ab9..fac0a64b2 100644 --- a/beacon-chain/powchain/block_reader_test.go +++ b/beacon-chain/powchain/block_reader_test.go @@ -12,7 +12,7 @@ import ( "github.com/ethereum/go-ethereum/trie" dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" mockPOW "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index f8802fea1..04fd1bacc 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -18,7 +18,7 @@ import ( coreState "github.com/prysmaticlabs/prysm/beacon-chain/core/transition" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" "github.com/prysmaticlabs/prysm/config/params" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/crypto/hash" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" protodb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" diff --git a/beacon-chain/powchain/log_processing_test.go b/beacon-chain/powchain/log_processing_test.go index e06e25c40..c8662e764 100644 --- a/beacon-chain/powchain/log_processing_test.go +++ b/beacon-chain/powchain/log_processing_test.go @@ -16,7 +16,7 @@ import ( testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" mockPOW "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing" "github.com/prysmaticlabs/prysm/config/params" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 46287e17e..517391aba 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -34,7 +34,7 @@ import ( v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/container/trie" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/io/logs" "github.com/prysmaticlabs/prysm/monitoring/clientstats" "github.com/prysmaticlabs/prysm/network" diff --git a/beacon-chain/powchain/service_test.go b/beacon-chain/powchain/service_test.go index 6eb280ae2..df9838079 100644 --- a/beacon-chain/powchain/service_test.go +++ b/beacon-chain/powchain/service_test.go @@ -20,7 +20,7 @@ import ( dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" mockPOW "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing" "github.com/prysmaticlabs/prysm/config/params" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/monitoring/clientstats" "github.com/prysmaticlabs/prysm/network" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/BUILD.bazel b/beacon-chain/rpc/prysm/v1alpha1/validator/BUILD.bazel index 2c767ad7e..9abae1a4e 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/BUILD.bazel +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/BUILD.bazel @@ -45,6 +45,7 @@ go_library( "//config/features:go_default_library", "//config/params:go_default_library", "//container/trie:go_default_library", + "//contracts/deposit:go_default_library", "//crypto/bls:go_default_library", "//crypto/hash:go_default_library", "//crypto/rand:go_default_library", @@ -59,7 +60,6 @@ go_library( "//proto/prysm/v1alpha1/wrapper:go_default_library", "//runtime/version:go_default_library", "//shared/bytesutil:go_default_library", - "//shared/depositutil:go_default_library", "//time:go_default_library", "//time/slots:go_default_library", "@com_github_ferranbt_fastssz//:go_default_library", diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/status.go b/beacon-chain/rpc/prysm/v1alpha1/validator/status.go index 4954b9a19..033f4e0d1 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/status.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/status.go @@ -9,10 +9,10 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/config/params" + "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/monitoring/tracing" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" - "github.com/prysmaticlabs/prysm/shared/depositutil" "go.opencensus.io/trace" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -259,7 +259,7 @@ func (vs *Server) validatorStatus( log.Warn("Not connected to ETH1. Cannot determine validator ETH1 deposit block number") return resp, nonExistentIndex } - deposit, eth1BlockNumBigInt := vs.DepositFetcher.DepositByPubkey(ctx, pubKey) + dep, eth1BlockNumBigInt := vs.DepositFetcher.DepositByPubkey(ctx, pubKey) if eth1BlockNumBigInt == nil { // No deposit found in ETH1. return resp, nonExistentIndex } @@ -272,13 +272,13 @@ func (vs *Server) validatorStatus( log.Warn("Could not compute domain") return resp, nonExistentIndex } - if err := depositutil.VerifyDepositSignature(deposit.Data, domain); err != nil { + if err := deposit.VerifyDepositSignature(dep.Data, domain); err != nil { resp.Status = ethpb.ValidatorStatus_INVALID log.Warn("Invalid Eth1 deposit") return resp, nonExistentIndex } // Set validator deposit status if their deposit is visible. - resp.Status = depositStatus(deposit.Data.Amount) + resp.Status = depositStatus(dep.Data.Amount) resp.Eth1DepositBlockNumber = eth1BlockNumBigInt.Uint64() return resp, nonExistentIndex diff --git a/beacon-chain/state/BUILD.bazel b/beacon-chain/state/BUILD.bazel index 6c13a3fdf..d67c04133 100644 --- a/beacon-chain/state/BUILD.bazel +++ b/beacon-chain/state/BUILD.bazel @@ -9,9 +9,9 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/state", visibility = [ "//beacon-chain:__subpackages__", + "//contracts/deposit:__subpackages__", "//proto/testing:__subpackages__", "//shared/aggregation:__subpackages__", - "//shared/depositutil:__subpackages__", "//shared/testutil:__pkg__", "//slasher/rpc:__subpackages__", "//testing/benchmark:__pkg__", diff --git a/beacon-chain/state/v1/BUILD.bazel b/beacon-chain/state/v1/BUILD.bazel index e5609f75f..a15e5864f 100644 --- a/beacon-chain/state/v1/BUILD.bazel +++ b/beacon-chain/state/v1/BUILD.bazel @@ -30,10 +30,10 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/state/v1", visibility = [ "//beacon-chain:__subpackages__", + "//contracts/deposit:__subpackages__", "//proto/testing:__subpackages__", "//runtime/interop:__subpackages__", "//shared/aggregation:__subpackages__", - "//shared/depositutil:__subpackages__", "//shared/testutil:__pkg__", "//slasher/rpc:__subpackages__", "//testing/benchmark:__pkg__", diff --git a/container/trie/BUILD.bazel b/container/trie/BUILD.bazel index cd6d1b8c0..6703930af 100644 --- a/container/trie/BUILD.bazel +++ b/container/trie/BUILD.bazel @@ -23,7 +23,7 @@ go_test( deps = [ ":go_default_library", "//config/params:go_default_library", - "//contracts/deposit-contract:go_default_library", + "//contracts/deposit:go_default_library", "//crypto/hash:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//shared/bytesutil:go_default_library", diff --git a/container/trie/sparse_merkle.go b/container/trie/sparse_merkle.go index 17155f469..6c3d4eb9d 100644 --- a/container/trie/sparse_merkle.go +++ b/container/trie/sparse_merkle.go @@ -28,6 +28,13 @@ func NewTrie(depth uint64) (*SparseMerkleTrie, error) { return GenerateTrieFromItems(items, depth) } +// NewTrieWithBranches returns a new merkle trie filled with branches to use. +func NewTrieWithBranches(branches [][][]byte, depth uint64) (*SparseMerkleTrie, error) { + var zeroBytes [32]byte + items := [][]byte{zeroBytes[:]} + return GenerateTrieFromItems(items, depth) +} + // CreateTrieFromProto creates a Sparse Merkle Trie from its corresponding merkle trie. func CreateTrieFromProto(trieObj *protodb.SparseMerkleTrie) *SparseMerkleTrie { trie := &SparseMerkleTrie{ diff --git a/container/trie/sparse_merkle_test.go b/container/trie/sparse_merkle_test.go index 358565b11..ab7141f4d 100644 --- a/container/trie/sparse_merkle_test.go +++ b/container/trie/sparse_merkle_test.go @@ -7,7 +7,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/container/trie" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/crypto/hash" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" diff --git a/contracts/README.md b/contracts/README.md deleted file mode 100644 index f005273bc..000000000 --- a/contracts/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Contracts - -This page serves as a main reference for the smart contract tooling used internally in Prysm. - -**THIS DOES NOT CONTAIN CONTRACTS TO BE USED IN PRODUCTION**. - diff --git a/contracts/deposit-contract/README.md b/contracts/deposit-contract/README.md deleted file mode 100644 index 1c2c00463..000000000 --- a/contracts/deposit-contract/README.md +++ /dev/null @@ -1,28 +0,0 @@ -## Prysm Internal Validator Deposit Contract - -**NOTE: THIS IS NOT THE OFFICIAL ETHEREUM VALIDATOR DEPOSIT CONTRACT. THE OFFICIAL CONTRACT CAN ONLY BE FOUND [HERE](https://github.com/ethereum/consensus-specs/blob/e4a9c5fa29def20c4264cd860868f131d6f40e72/solidity_deposit_contract/deposit_contract.sol). THE ONLY DEPOSIT CONTRACT ON MAINNET HAS ADDRESS 0x00000000219ab540356cbb839cbe05303d7705fa. DO NOT USE THE CONTRACT IN THIS FOLDER OUTSIDE OF DEVELOPMENT** - -## How to execute tests - -``` -bazel test //contracts/deposit-contract:go_default_test - -``` - -Run with `-v` option for detailed log output - -``` -bazel test //contracts/deposit-contract:go_default_test --test_arg=-test.v --test_output=streamed -=== RUN TestSetupRegistrationContract_OK ---- PASS: TestSetupRegistrationContract_OK (0.07s) -=== RUN TestRegister_Below1ETH ---- PASS: TestRegister_Below1ETH (0.02s) -=== RUN TestRegister_Above32Eth ---- PASS: TestRegister_Above32Eth (0.02s) -=== RUN TestValidatorRegister_OK ---- PASS: TestValidatorRegister_OK (0.08s) -=== RUN TestDrain ---- PASS: TestDrain (0.04s) -PASS -ok contracts/deposit-contract 0.633s -``` diff --git a/contracts/deposit-contract/abi.json b/contracts/deposit-contract/abi.json deleted file mode 100644 index 529d6d07a..000000000 --- a/contracts/deposit-contract/abi.json +++ /dev/null @@ -1 +0,0 @@ -[{"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": [{"type": "address", "name": "_drain_address"}], "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}, {"name": "drain", "outputs": [], "inputs": [], "constant": false, "payable": false, "type": "function", "gas": 35793}, {"name": "drain_address", "outputs": [{"type": "address", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 663}] diff --git a/contracts/deposit-contract/bytecode.bin b/contracts/deposit-contract/bytecode.bin deleted file mode 100644 index 195c6986c..000000000 --- a/contracts/deposit-contract/bytecode.bin +++ /dev/null @@ -1 +0,0 @@ -0x740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a05260206112736101403934156100a157600080fd5b602061127360c03960c05160205181106100ba57600080fd5b50610140516002556101606000601f818352015b600061016051602081106100e157600080fd5b600360c052602060c0200154602082610180010152602081019050610160516020811061010d57600080fd5b600360c052602060c020015460208261018001015260208101905080610180526101809050602060c0825160208401600060025af161014b57600080fd5b60c0519050606051600161016051018060405190131561016a57600080fd5b809190121561017857600080fd5b6020811061018557600080fd5b600360c052602060c02001555b81516001018083528114156100ce575b505061125b56600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610265575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100da578060000360020a82046100e1565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561010c57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610155578060000360020a820461015c565b8060020a82025b905090506101a0525b81516001018083528114156100bd575b50506018600860208206610200016020828401111561019357600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101d057600080fd5b50506103206102c0516020818352015b60206103205111156101f15761020d565b6000610320516102e001535b81516001018083528114156101e0575b505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102415761025d565b602061028051036102a00151602061028051036102805261022f565b610160515650005b63c5f2892f60005114156104f757341561027e57600080fd5b6000610140526101405161016052600154610180526101a060006020818352015b60016001610180511614156103205760006101a051602081106102c157600080fd5b600060c052602060c02001546020826102400101526020810190506101605160208261024001015260208101905080610240526102409050602060c0825160208401600060025af161031257600080fd5b60c05190506101605261038e565b6000610160516020826101c00101526020810190506101a0516020811061034657600080fd5b600360c052602060c02001546020826101c0010152602081019050806101c0526101c09050602060c0825160208401600060025af161038457600080fd5b60c0519050610160525b610180600261039c57600080fd5b60028151048152505b815160010180835281141561029f575b505060006101605160208261046001015260208101905061014051610160516101805163806732896102e05260015461030052610300516006580161009b565b506103605260006103c0525b6103605160206001820306601f82010390506103c0511015156104235761043c565b6103c05161038001526103c0516020016103c052610401565b61018052610160526101405261036060088060208461046001018260208501600060046012f150508051820191505060006018602082066103e0016020828401111561048757600080fd5b60208061040082610140600060046015f150508181528090509050905060188060208461046001018260208501600060046014f150508051820191505080610460526104609050602060c0825160208401600060025af16104e757600080fd5b60c051905060005260206000f350005b63621fd13060005114156105f857341561051057600080fd5b63806732896101405260015461016052610160516006580161009b565b506101c0526000610220525b6101c05160206001820306601f82010390506102205110151561055b57610574565b610220516101e001526102205160200161022052610539565b6101c0805160200180610280828460006004600a8704601201f161059757600080fd5b50506102e0610280516020818352015b60206102e05111156105b8576105d4565b60006102e0516102a001535b81516001018083528114156105a7575b50506020610260526040610280510160206001820306601f8201039050610260f350005b6322895118600051141561105157605060043560040161014037603060043560040135111561062657600080fd5b60406024356004016101c037602060243560040135111561064657600080fd5b608060443560040161022037606060443560040135111561066657600080fd5b63ffffffff6001541061067857600080fd5b633b9aca006102e0526102e05161068e57600080fd5b6102e05134046102c052633b9aca006102c05110156106ac57600080fd5b603061014051146106bc57600080fd5b60206101c051146106cc57600080fd5b606061022051146106dc57600080fd5b610140610360525b61036051516020610360510161036052610360610360511015610706576106e4565b6380673289610380526102c0516103a0526103a0516006580161009b565b50610400526000610460525b6104005160206001820306601f8201039050610460511015156107525761076b565b6104605161042001526104605160200161046052610730565b610340610360525b610360515260206103605103610360526101406103605110151561079657610773565b610400805160200180610300828460006004600a8704601201f16107b957600080fd5b5050610140610480525b610480515160206104805101610480526104806104805110156107e5576107c3565b63806732896104a0526001546104c0526104c0516006580161009b565b50610520526000610580525b6105205160206001820306601f82010390506105805110151561083057610849565b610580516105400152610580516020016105805261080e565b610460610480525b610480515260206104805103610480526101406104805110151561087457610851565b6105208051602001806105a0828460006004600a8704601201f161089757600080fd5b505060a06106205261062051610660526101408051602001806106205161066001828460006004600a8704601201f16108cf57600080fd5b5050610600610620516106600151610240818352015b6102406106005111156108f757610918565b600061060051610620516106800101535b81516001018083528114156108e5575b5050602061062051610660015160206001820306601f82010390506106205101016106205261062051610680526101c08051602001806106205161066001828460006004600a8704601201f161096d57600080fd5b5050610600610620516106600151610240818352015b610240610600511115610995576109b6565b600061060051610620516106800101535b8151600101808352811415610983575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106a0526103008051602001806106205161066001828460006004600a8704601201f1610a0b57600080fd5b5050610600610620516106600151610240818352015b610240610600511115610a3357610a54565b600061060051610620516106800101535b8151600101808352811415610a21575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106c0526102208051602001806106205161066001828460006004600a8704601201f1610aa957600080fd5b5050610600610620516106600151610240818352015b610240610600511115610ad157610af2565b600061060051610620516106800101535b8151600101808352811415610abf575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106e0526105a08051602001806106205161066001828460006004600a8704601201f1610b4757600080fd5b5050610600610620516106600151610240818352015b610240610600511115610b6f57610b90565b600061060051610620516106800101535b8151600101808352811415610b5d575b5050602061062051610660015160206001820306601f8201039050610620510101610620527f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c561062051610660a160006107005260006101406030806020846107c001018260208501600060046016f150508051820191505060006010602082066107400160208284011115610c2557600080fd5b60208061076082610700600060046015f15050818152809050905090506010806020846107c001018260208501600060046013f1505080518201915050806107c0526107c09050602060c0825160208401600060025af1610c8557600080fd5b60c0519050610720526000600060406020820661086001610220518284011115610cae57600080fd5b606080610880826020602088068803016102200160006004601bf1505081815280905090509050602060c0825160208401600060025af1610cee57600080fd5b60c0519050602082610a600101526020810190506000604060206020820661092001610220518284011115610d2257600080fd5b606080610940826020602088068803016102200160006004601bf15050818152809050905090506020806020846109e001018260208501600060046015f1505080518201915050610700516020826109e0010152602081019050806109e0526109e09050602060c0825160208401600060025af1610d9f57600080fd5b60c0519050602082610a6001015260208101905080610a6052610a609050602060c0825160208401600060025af1610dd657600080fd5b60c0519050610840526000600061072051602082610b000101526020810190506101c0602080602084610b0001018260208501600060046015f150508051820191505080610b0052610b009050602060c0825160208401600060025af1610e3c57600080fd5b60c0519050602082610c800101526020810190506000610300600880602084610c0001018260208501600060046012f15050805182019150506000601860208206610b800160208284011115610e9157600080fd5b602080610ba082610700600060046015f1505081815280905090509050601880602084610c0001018260208501600060046014f150508051820191505061084051602082610c0001015260208101905080610c0052610c009050602060c0825160208401600060025af1610f0457600080fd5b60c0519050602082610c8001015260208101905080610c8052610c809050602060c0825160208401600060025af1610f3b57600080fd5b60c0519050610ae052606435610ae05114610f5557600080fd5b6001805460018254011015610f6957600080fd5b6001815401815550600154610d0052610d2060006020818352015b60016001610d0051161415610fb957610ae051610d205160208110610fa857600080fd5b600060c052602060c020015561104d565b6000610d205160208110610fcc57600080fd5b600060c052602060c0200154602082610d40010152602081019050610ae051602082610d4001015260208101905080610d4052610d409050602060c0825160208401600060025af161101d57600080fd5b60c0519050610ae052610d00600261103457600080fd5b60028151048152505b8151600101808352811415610f84575b5050005b639890220b600051141561108557341561106a57600080fd5b600060006000600030316002546000f161108357600080fd5b005b638ba35cdf60005114156110ac57341561109e57600080fd5b60025460005260206000f350005b60006000fd5b6101a961125b036101a96000396101a961125b036000f3 \ No newline at end of file diff --git a/contracts/deposit-contract/depositContract.v.py b/contracts/deposit-contract/depositContract.v.py deleted file mode 100644 index 7fb8a217f..000000000 --- a/contracts/deposit-contract/depositContract.v.py +++ /dev/null @@ -1,117 +0,0 @@ -# Vyper target 0.1.0b12 -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 -drain_address: public(address) - -# Compute hashes in empty sparse Merkle tree -zero_hashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] -@public -def __init__(_drain_address: address): - self.drain_address = _drain_address - 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 to facilitate formal verification (see https://github.com/ethereum/consensus-specs/pull/1362/files#r320361859) - 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 - -# !!! DEBUG ONLY !!! -# This method is NOT part of the final ETH2.0 deposit contract, but we use it -# to recover test funds. -@public -def drain(): - send(self.drain_address, self.balance) diff --git a/contracts/deposit-contract/testutils.go b/contracts/deposit-contract/testutils.go deleted file mode 100644 index 73fa72718..000000000 --- a/contracts/deposit-contract/testutils.go +++ /dev/null @@ -1,75 +0,0 @@ -package depositcontract - -import ( - "crypto/ecdsa" - "fmt" - "math/big" - - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/crypto" -) - -var ( - amount32Eth = "32000000000000000000" - amountLessThan1Eth = "500000000000000000" -) - -// TestAccount represents a test account in the simulated backend, -// through which we can perform actions on the eth1.0 chain. -type TestAccount struct { - Addr common.Address - ContractAddr common.Address - Contract *DepositContract - Backend *backends.SimulatedBackend - TxOpts *bind.TransactOpts -} - -// Setup creates the simulated backend with the deposit contract deployed -func Setup() (*TestAccount, error) { - genesis := make(core.GenesisAlloc) - privKey, err := crypto.GenerateKey() - if err != nil { - return nil, err - } - pubKeyECDSA, ok := privKey.Public().(*ecdsa.PublicKey) - if !ok { - return nil, fmt.Errorf("error casting public key to ECDSA") - } - - // strip off the 0x and the first 2 characters 04 which is always the EC prefix and is not required. - publicKeyBytes := crypto.FromECDSAPub(pubKeyECDSA)[4:] - var pubKey = make([]byte, 48) - copy(pubKey, publicKeyBytes) - - addr := crypto.PubkeyToAddress(privKey.PublicKey) - txOpts, err := bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337)) - if err != nil { - return nil, err - } - startingBalance, _ := new(big.Int).SetString("100000000000000000000000000000000000000", 10) - genesis[addr] = core.GenesisAccount{Balance: startingBalance} - backend := backends.NewSimulatedBackend(genesis, 210000000000) - - contractAddr, _, contract, err := DeployDepositContract(txOpts, backend, addr) - if err != nil { - return nil, err - } - backend.Commit() - - return &TestAccount{addr, contractAddr, contract, backend, txOpts}, nil -} - -// Amount32Eth returns 32Eth(in wei) in terms of the big.Int type. -func Amount32Eth() *big.Int { - amount, _ := new(big.Int).SetString(amount32Eth, 10) - return amount -} - -// LessThan1Eth returns less than 1 Eth(in wei) in terms of the big.Int type. -func LessThan1Eth() *big.Int { - amount, _ := new(big.Int).SetString(amountLessThan1Eth, 10) - return amount -} diff --git a/contracts/deposit-contract/BUILD.bazel b/contracts/deposit/BUILD.bazel similarity index 71% rename from contracts/deposit-contract/BUILD.bazel rename to contracts/deposit/BUILD.bazel index ae03b943f..a62a7707f 100644 --- a/contracts/deposit-contract/BUILD.bazel +++ b/contracts/deposit/BUILD.bazel @@ -3,13 +3,20 @@ load("@prysm//tools/go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", srcs = [ - "ETH1logs.go", - "depositContract.go", - "testutils.go", + "contract.go", + "deposit.go", + "logs.go", + "mock.go", ], - importpath = "github.com/prysmaticlabs/prysm/contracts/deposit-contract", + importpath = "github.com/prysmaticlabs/prysm/contracts/deposit", visibility = ["//visibility:public"], deps = [ + "//beacon-chain/core/helpers:go_default_library", + "//config/features:go_default_library", + "//config/params:go_default_library", + "//crypto/bls:go_default_library", + "//crypto/hash:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", "@com_github_ethereum_go_ethereum//:go_default_library", "@com_github_ethereum_go_ethereum//accounts/abi:go_default_library", "@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library", @@ -27,14 +34,19 @@ go_test( name = "go_default_test", size = "medium", srcs = [ - "depositContract_test.go", + "contract_test.go", + "deposit_test.go", "deposit_tree_test.go", ], deps = [ ":go_default_library", + "//beacon-chain/core/helpers:go_default_library", "//config/params:go_default_library", "//container/trie:go_default_library", + "//crypto/bls:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", "//runtime/interop:go_default_library", + "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", "@com_github_ethereum_go_ethereum//:go_default_library", diff --git a/contracts/deposit/README.md b/contracts/deposit/README.md new file mode 100644 index 000000000..e43440ccb --- /dev/null +++ b/contracts/deposit/README.md @@ -0,0 +1,3 @@ +# Validator Deposit Contract Local Copy + +This package contains a copy of the official Ethereum [Validator Deposit Contract](https://github.com/ethereum/consensus-specs/tree/e4a9c5fa29def20c4264cd860868f131d6f40e72/solidity_deposit_contract) along with its ABI, bytecode, and Go bindings generated by go-ethereum's [abigen](https://github.com/ethereum/go-ethereum/tree/master/cmd/abigen) `version 1.10.4-stable`. It contains useful test harnesses for setting up and deploying a validator deposit contract using Go bindings, which are used across tests in Prysm. diff --git a/contracts/deposit/abi.json b/contracts/deposit/abi.json new file mode 100644 index 000000000..ecde3bd85 --- /dev/null +++ b/contracts/deposit/abi.json @@ -0,0 +1 @@ +[{"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"}] \ No newline at end of file diff --git a/contracts/deposit/bytecode.bin b/contracts/deposit/bytecode.bin new file mode 100644 index 000000000..66acb7b6e --- /dev/null +++ b/contracts/deposit/bytecode.bin @@ -0,0 +1 @@ +0x608060405234801561001057600080fd5b5060005b601f8110156101025760026021826020811061002c57fe5b01546021836020811061003b57fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b602083106100925780518252601f199092019160209182019101610073565b51815160209384036101000a60001901801990921691161790526040519190930194509192505080830381855afa1580156100d1573d6000803e3d6000fd5b5050506040513d60208110156100e657600080fd5b5051602160018301602081106100f857fe5b0155600101610014565b506118d680620001136000396000f3fe60806040526004361061003f5760003560e01c806301ffc9a71461004457806322895118146100a4578063621fd130146101ba578063c5f2892f14610244575b600080fd5b34801561005057600080fd5b506100906004803603602081101561006757600080fd5b50357fffffffff000000000000000000000000000000000000000000000000000000001661026b565b604080519115158252519081900360200190f35b6101b8600480360360808110156100ba57600080fd5b8101906020810181356401000000008111156100d557600080fd5b8201836020820111156100e757600080fd5b8035906020019184600183028401116401000000008311171561010957600080fd5b91939092909160208101903564010000000081111561012757600080fd5b82018360208201111561013957600080fd5b8035906020019184600183028401116401000000008311171561015b57600080fd5b91939092909160208101903564010000000081111561017957600080fd5b82018360208201111561018b57600080fd5b803590602001918460018302840111640100000000831117156101ad57600080fd5b919350915035610304565b005b3480156101c657600080fd5b506101cf6110b5565b6040805160208082528351818301528351919283929083019185019080838360005b838110156102095781810151838201526020016101f1565b50505050905090810190601f1680156102365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561025057600080fd5b506102596110c7565b60408051918252519081900360200190f35b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a70000000000000000000000000000000000000000000000000000000014806102fe57507fffffffff0000000000000000000000000000000000000000000000000000000082167f8564090700000000000000000000000000000000000000000000000000000000145b92915050565b6030861461035d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118056026913960400191505060405180910390fd5b602084146103b6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603681526020018061179c6036913960400191505060405180910390fd5b6060821461040f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260298152602001806118786029913960400191505060405180910390fd5b670de0b6b3a7640000341015610470576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118526026913960400191505060405180910390fd5b633b9aca003406156104cd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260338152602001806117d26033913960400191505060405180910390fd5b633b9aca00340467ffffffffffffffff811115610535576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602781526020018061182b6027913960400191505060405180910390fd5b6060610540826114ba565b90507f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c589898989858a8a6105756020546114ba565b6040805160a0808252810189905290819060208201908201606083016080840160c085018e8e80828437600083820152601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690910187810386528c815260200190508c8c808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690920188810386528c5181528c51602091820193918e019250908190849084905b83811015610648578181015183820152602001610630565b50505050905090810190601f1680156106755780820380516001836020036101000a031916815260200191505b5086810383528881526020018989808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169092018881038452895181528951602091820193918b019250908190849084905b838110156106ef5781810151838201526020016106d7565b50505050905090810190601f16801561071c5780820380516001836020036101000a031916815260200191505b509d505050505050505050505050505060405180910390a1600060028a8a600060801b604051602001808484808284377fffffffffffffffffffffffffffffffff0000000000000000000000000000000090941691909301908152604080517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0818403018152601090920190819052815191955093508392506020850191508083835b602083106107fc57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016107bf565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610859573d6000803e3d6000fd5b5050506040513d602081101561086e57600080fd5b5051905060006002806108846040848a8c6116fe565b6040516020018083838082843780830192505050925050506040516020818303038152906040526040518082805190602001908083835b602083106108f857805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016108bb565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610955573d6000803e3d6000fd5b5050506040513d602081101561096a57600080fd5b5051600261097b896040818d6116fe565b60405160009060200180848480828437919091019283525050604080518083038152602092830191829052805190945090925082918401908083835b602083106109f457805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016109b7565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610a51573d6000803e3d6000fd5b5050506040513d6020811015610a6657600080fd5b5051604080516020818101949094528082019290925280518083038201815260609092019081905281519192909182918401908083835b60208310610ada57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610a9d565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610b37573d6000803e3d6000fd5b5050506040513d6020811015610b4c57600080fd5b50516040805160208101858152929350600092600292839287928f928f92018383808284378083019250505093505050506040516020818303038152906040526040518082805190602001908083835b60208310610bd957805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610b9c565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610c36573d6000803e3d6000fd5b5050506040513d6020811015610c4b57600080fd5b50516040518651600291889160009188916020918201918291908601908083835b60208310610ca957805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610c6c565b6001836020036101000a0380198251168184511680821785525050505050509050018367ffffffffffffffff191667ffffffffffffffff1916815260180182815260200193505050506040516020818303038152906040526040518082805190602001908083835b60208310610d4e57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610d11565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610dab573d6000803e3d6000fd5b5050506040513d6020811015610dc057600080fd5b5051604080516020818101949094528082019290925280518083038201815260609092019081905281519192909182918401908083835b60208310610e3457805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610df7565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610e91573d6000803e3d6000fd5b5050506040513d6020811015610ea657600080fd5b50519050858114610f02576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260548152602001806117486054913960600191505060405180910390fd5b60205463ffffffff11610f60576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260218152602001806117276021913960400191505060405180910390fd5b602080546001019081905560005b60208110156110a9578160011660011415610fa0578260008260208110610f9157fe5b0155506110ac95505050505050565b600260008260208110610faf57fe5b01548460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061102557805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610fe8565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015611082573d6000803e3d6000fd5b5050506040513d602081101561109757600080fd5b50519250600282049150600101610f6e565b50fe5b50505050505050565b60606110c26020546114ba565b905090565b6020546000908190815b60208110156112f05781600116600114156111e6576002600082602081106110f557fe5b01548460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061116b57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161112e565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa1580156111c8573d6000803e3d6000fd5b5050506040513d60208110156111dd57600080fd5b505192506112e2565b600283602183602081106111f657fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061126b57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161122e565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa1580156112c8573d6000803e3d6000fd5b5050506040513d60208110156112dd57600080fd5b505192505b6002820491506001016110d1565b506002826112ff6020546114ba565b600060401b6040516020018084815260200183805190602001908083835b6020831061135a57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161131d565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790527fffffffffffffffffffffffffffffffffffffffffffffffff000000000000000095909516920191825250604080518083037ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8018152601890920190819052815191955093508392850191508083835b6020831061143f57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101611402565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa15801561149c573d6000803e3d6000fd5b5050506040513d60208110156114b157600080fd5b50519250505090565b60408051600880825281830190925260609160208201818036833701905050905060c082901b8060071a60f81b826000815181106114f457fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060061a60f81b8260018151811061153757fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060051a60f81b8260028151811061157a57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060041a60f81b826003815181106115bd57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060031a60f81b8260048151811061160057fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060021a60f81b8260058151811061164357fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060011a60f81b8260068151811061168657fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060001a60f81b826007815181106116c957fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535050919050565b6000808585111561170d578182fd5b83861115611719578182fd5b505082019391909203915056fe4465706f736974436f6e74726163743a206d65726b6c6520747265652066756c6c4465706f736974436f6e74726163743a207265636f6e7374727563746564204465706f7369744461746120646f6573206e6f74206d6174636820737570706c696564206465706f7369745f646174615f726f6f744465706f736974436f6e74726163743a20696e76616c6964207769746864726177616c5f63726564656e7469616c73206c656e6774684465706f736974436f6e74726163743a206465706f7369742076616c7565206e6f74206d756c7469706c65206f6620677765694465706f736974436f6e74726163743a20696e76616c6964207075626b6579206c656e6774684465706f736974436f6e74726163743a206465706f7369742076616c756520746f6f20686967684465706f736974436f6e74726163743a206465706f7369742076616c756520746f6f206c6f774465706f736974436f6e74726163743a20696e76616c6964207369676e6174757265206c656e677468a2646970667358221220dceca8706b29e917dacf25fceef95acac8d90d765ac926663ce4096195952b6164736f6c634300060b0033 \ No newline at end of file diff --git a/contracts/deposit-contract/depositContract.go b/contracts/deposit/contract.go similarity index 53% rename from contracts/deposit-contract/depositContract.go rename to contracts/deposit/contract.go index 9aaa9cfa0..13b83096f 100644 --- a/contracts/deposit-contract/depositContract.go +++ b/contracts/deposit/contract.go @@ -1,7 +1,6 @@ // Code generated - DO NOT EDIT. // This file is a generated binding and any manual changes will be lost. - -package depositcontract +package deposit import ( "math/big" @@ -27,24 +26,7 @@ var ( ) // DepositContractABI is the input ABI used to generate the binding from. -const DepositContractABI = "[{\"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\":[{\"type\":\"address\",\"name\":\"_drain_address\"}],\"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},{\"name\":\"drain\",\"outputs\":[],\"inputs\":[],\"constant\":false,\"payable\":false,\"type\":\"function\",\"gas\":35793},{\"name\":\"drain_address\",\"outputs\":[{\"type\":\"address\",\"name\":\"out\"}],\"inputs\":[],\"constant\":true,\"payable\":false,\"type\":\"function\",\"gas\":663}]" - -// DepositContractBin is the compiled bytecode used for deploying new contracts. -var DepositContractBin = "0x740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a05260206112736101403934156100a157600080fd5b602061127360c03960c05160205181106100ba57600080fd5b50610140516002556101606000601f818352015b600061016051602081106100e157600080fd5b600360c052602060c0200154602082610180010152602081019050610160516020811061010d57600080fd5b600360c052602060c020015460208261018001015260208101905080610180526101809050602060c0825160208401600060025af161014b57600080fd5b60c0519050606051600161016051018060405190131561016a57600080fd5b809190121561017857600080fd5b6020811061018557600080fd5b600360c052602060c02001555b81516001018083528114156100ce575b505061125b56600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052600015610265575b6101605261014052600061018052610140516101a0526101c060006008818352015b61018051600860008112156100da578060000360020a82046100e1565b8060020a82025b905090506101805260ff6101a051166101e052610180516101e0516101805101101561010c57600080fd5b6101e0516101805101610180526101a0517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff86000811215610155578060000360020a820461015c565b8060020a82025b905090506101a0525b81516001018083528114156100bd575b50506018600860208206610200016020828401111561019357600080fd5b60208061022082610180600060046015f15050818152809050905090508051602001806102c0828460006004600a8704601201f16101d057600080fd5b50506103206102c0516020818352015b60206103205111156101f15761020d565b6000610320516102e001535b81516001018083528114156101e0575b505060206102a05260406102c0510160206001820306601f8201039050610280525b6000610280511115156102415761025d565b602061028051036102a00151602061028051036102805261022f565b610160515650005b63c5f2892f60005114156104f757341561027e57600080fd5b6000610140526101405161016052600154610180526101a060006020818352015b60016001610180511614156103205760006101a051602081106102c157600080fd5b600060c052602060c02001546020826102400101526020810190506101605160208261024001015260208101905080610240526102409050602060c0825160208401600060025af161031257600080fd5b60c05190506101605261038e565b6000610160516020826101c00101526020810190506101a0516020811061034657600080fd5b600360c052602060c02001546020826101c0010152602081019050806101c0526101c09050602060c0825160208401600060025af161038457600080fd5b60c0519050610160525b610180600261039c57600080fd5b60028151048152505b815160010180835281141561029f575b505060006101605160208261046001015260208101905061014051610160516101805163806732896102e05260015461030052610300516006580161009b565b506103605260006103c0525b6103605160206001820306601f82010390506103c0511015156104235761043c565b6103c05161038001526103c0516020016103c052610401565b61018052610160526101405261036060088060208461046001018260208501600060046012f150508051820191505060006018602082066103e0016020828401111561048757600080fd5b60208061040082610140600060046015f150508181528090509050905060188060208461046001018260208501600060046014f150508051820191505080610460526104609050602060c0825160208401600060025af16104e757600080fd5b60c051905060005260206000f350005b63621fd13060005114156105f857341561051057600080fd5b63806732896101405260015461016052610160516006580161009b565b506101c0526000610220525b6101c05160206001820306601f82010390506102205110151561055b57610574565b610220516101e001526102205160200161022052610539565b6101c0805160200180610280828460006004600a8704601201f161059757600080fd5b50506102e0610280516020818352015b60206102e05111156105b8576105d4565b60006102e0516102a001535b81516001018083528114156105a7575b50506020610260526040610280510160206001820306601f8201039050610260f350005b6322895118600051141561105157605060043560040161014037603060043560040135111561062657600080fd5b60406024356004016101c037602060243560040135111561064657600080fd5b608060443560040161022037606060443560040135111561066657600080fd5b63ffffffff6001541061067857600080fd5b633b9aca006102e0526102e05161068e57600080fd5b6102e05134046102c052633b9aca006102c05110156106ac57600080fd5b603061014051146106bc57600080fd5b60206101c051146106cc57600080fd5b606061022051146106dc57600080fd5b610140610360525b61036051516020610360510161036052610360610360511015610706576106e4565b6380673289610380526102c0516103a0526103a0516006580161009b565b50610400526000610460525b6104005160206001820306601f8201039050610460511015156107525761076b565b6104605161042001526104605160200161046052610730565b610340610360525b610360515260206103605103610360526101406103605110151561079657610773565b610400805160200180610300828460006004600a8704601201f16107b957600080fd5b5050610140610480525b610480515160206104805101610480526104806104805110156107e5576107c3565b63806732896104a0526001546104c0526104c0516006580161009b565b50610520526000610580525b6105205160206001820306601f82010390506105805110151561083057610849565b610580516105400152610580516020016105805261080e565b610460610480525b610480515260206104805103610480526101406104805110151561087457610851565b6105208051602001806105a0828460006004600a8704601201f161089757600080fd5b505060a06106205261062051610660526101408051602001806106205161066001828460006004600a8704601201f16108cf57600080fd5b5050610600610620516106600151610240818352015b6102406106005111156108f757610918565b600061060051610620516106800101535b81516001018083528114156108e5575b5050602061062051610660015160206001820306601f82010390506106205101016106205261062051610680526101c08051602001806106205161066001828460006004600a8704601201f161096d57600080fd5b5050610600610620516106600151610240818352015b610240610600511115610995576109b6565b600061060051610620516106800101535b8151600101808352811415610983575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106a0526103008051602001806106205161066001828460006004600a8704601201f1610a0b57600080fd5b5050610600610620516106600151610240818352015b610240610600511115610a3357610a54565b600061060051610620516106800101535b8151600101808352811415610a21575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106c0526102208051602001806106205161066001828460006004600a8704601201f1610aa957600080fd5b5050610600610620516106600151610240818352015b610240610600511115610ad157610af2565b600061060051610620516106800101535b8151600101808352811415610abf575b5050602061062051610660015160206001820306601f820103905061062051010161062052610620516106e0526105a08051602001806106205161066001828460006004600a8704601201f1610b4757600080fd5b5050610600610620516106600151610240818352015b610240610600511115610b6f57610b90565b600061060051610620516106800101535b8151600101808352811415610b5d575b5050602061062051610660015160206001820306601f8201039050610620510101610620527f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c561062051610660a160006107005260006101406030806020846107c001018260208501600060046016f150508051820191505060006010602082066107400160208284011115610c2557600080fd5b60208061076082610700600060046015f15050818152809050905090506010806020846107c001018260208501600060046013f1505080518201915050806107c0526107c09050602060c0825160208401600060025af1610c8557600080fd5b60c0519050610720526000600060406020820661086001610220518284011115610cae57600080fd5b606080610880826020602088068803016102200160006004601bf1505081815280905090509050602060c0825160208401600060025af1610cee57600080fd5b60c0519050602082610a600101526020810190506000604060206020820661092001610220518284011115610d2257600080fd5b606080610940826020602088068803016102200160006004601bf15050818152809050905090506020806020846109e001018260208501600060046015f1505080518201915050610700516020826109e0010152602081019050806109e0526109e09050602060c0825160208401600060025af1610d9f57600080fd5b60c0519050602082610a6001015260208101905080610a6052610a609050602060c0825160208401600060025af1610dd657600080fd5b60c0519050610840526000600061072051602082610b000101526020810190506101c0602080602084610b0001018260208501600060046015f150508051820191505080610b0052610b009050602060c0825160208401600060025af1610e3c57600080fd5b60c0519050602082610c800101526020810190506000610300600880602084610c0001018260208501600060046012f15050805182019150506000601860208206610b800160208284011115610e9157600080fd5b602080610ba082610700600060046015f1505081815280905090509050601880602084610c0001018260208501600060046014f150508051820191505061084051602082610c0001015260208101905080610c0052610c009050602060c0825160208401600060025af1610f0457600080fd5b60c0519050602082610c8001015260208101905080610c8052610c809050602060c0825160208401600060025af1610f3b57600080fd5b60c0519050610ae052606435610ae05114610f5557600080fd5b6001805460018254011015610f6957600080fd5b6001815401815550600154610d0052610d2060006020818352015b60016001610d0051161415610fb957610ae051610d205160208110610fa857600080fd5b600060c052602060c020015561104d565b6000610d205160208110610fcc57600080fd5b600060c052602060c0200154602082610d40010152602081019050610ae051602082610d4001015260208101905080610d4052610d409050602060c0825160208401600060025af161101d57600080fd5b60c0519050610ae052610d00600261103457600080fd5b60028151048152505b8151600101808352811415610f84575b5050005b639890220b600051141561108557341561106a57600080fd5b600060006000600030316002546000f161108357600080fd5b005b638ba35cdf60005114156110ac57341561109e57600080fd5b60025460005260206000f350005b60006000fd5b6101a961125b036101a96000396101a961125b036000f3" - -// DeployDepositContract deploys a new Ethereum contract, binding an instance of DepositContract to it. -func DeployDepositContract(auth *bind.TransactOpts, backend bind.ContractBackend, _drain_address common.Address) (common.Address, *types.Transaction, *DepositContract, error) { - parsed, err := abi.JSON(strings.NewReader(DepositContractABI)) - if err != nil { - return common.Address{}, nil, nil, err - } - - address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(DepositContractBin), backend, _drain_address) - if err != nil { - return common.Address{}, nil, nil, err - } - return address, tx, &DepositContract{DepositContractCaller: DepositContractCaller{contract: contract}, DepositContractTransactor: DepositContractTransactor{contract: contract}, DepositContractFilterer: DepositContractFilterer{contract: contract}}, nil -} +const DepositContractABI = "[{\"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\"}]" // DepositContract is an auto generated Go binding around an Ethereum contract. type DepositContract struct { @@ -188,40 +170,9 @@ func (_DepositContract *DepositContractTransactorRaw) Transact(opts *bind.Transa return _DepositContract.Contract.contract.Transact(opts, method, params...) } -// DrainAddress is a free data retrieval call binding the contract method 0x8ba35cdf. -// -// Solidity: function drain_address() returns(address out) -func (_DepositContract *DepositContractCaller) DrainAddress(opts *bind.CallOpts) (common.Address, error) { - var out []interface{} - err := _DepositContract.contract.Call(opts, &out, "drain_address") - - if err != nil { - return *new(common.Address), err - } - - out0 := *abi.ConvertType(out[0], new(common.Address)).(*common.Address) - - return out0, err - -} - -// DrainAddress is a free data retrieval call binding the contract method 0x8ba35cdf. -// -// Solidity: function drain_address() returns(address out) -func (_DepositContract *DepositContractSession) DrainAddress() (common.Address, error) { - return _DepositContract.Contract.DrainAddress(&_DepositContract.CallOpts) -} - -// DrainAddress is a free data retrieval call binding the contract method 0x8ba35cdf. -// -// Solidity: function drain_address() returns(address out) -func (_DepositContract *DepositContractCallerSession) DrainAddress() (common.Address, error) { - return _DepositContract.Contract.DrainAddress(&_DepositContract.CallOpts) -} - // GetDepositCount is a free data retrieval call binding the contract method 0x621fd130. // -// Solidity: function get_deposit_count() returns(bytes out) +// Solidity: function get_deposit_count() view returns(bytes) func (_DepositContract *DepositContractCaller) GetDepositCount(opts *bind.CallOpts) ([]byte, error) { var out []interface{} err := _DepositContract.contract.Call(opts, &out, "get_deposit_count") @@ -238,21 +189,21 @@ func (_DepositContract *DepositContractCaller) GetDepositCount(opts *bind.CallOp // GetDepositCount is a free data retrieval call binding the contract method 0x621fd130. // -// Solidity: function get_deposit_count() returns(bytes out) +// Solidity: function get_deposit_count() view returns(bytes) func (_DepositContract *DepositContractSession) GetDepositCount() ([]byte, error) { return _DepositContract.Contract.GetDepositCount(&_DepositContract.CallOpts) } // GetDepositCount is a free data retrieval call binding the contract method 0x621fd130. // -// Solidity: function get_deposit_count() returns(bytes out) +// Solidity: function get_deposit_count() view returns(bytes) func (_DepositContract *DepositContractCallerSession) GetDepositCount() ([]byte, error) { return _DepositContract.Contract.GetDepositCount(&_DepositContract.CallOpts) } // GetDepositRoot is a free data retrieval call binding the contract method 0xc5f2892f. // -// Solidity: function get_deposit_root() returns(bytes32 out) +// Solidity: function get_deposit_root() view returns(bytes32) func (_DepositContract *DepositContractCaller) GetDepositRoot(opts *bind.CallOpts) ([32]byte, error) { var out []interface{} err := _DepositContract.contract.Call(opts, &out, "get_deposit_root") @@ -269,60 +220,70 @@ func (_DepositContract *DepositContractCaller) GetDepositRoot(opts *bind.CallOpt // GetDepositRoot is a free data retrieval call binding the contract method 0xc5f2892f. // -// Solidity: function get_deposit_root() returns(bytes32 out) +// Solidity: function get_deposit_root() view returns(bytes32) func (_DepositContract *DepositContractSession) GetDepositRoot() ([32]byte, error) { return _DepositContract.Contract.GetDepositRoot(&_DepositContract.CallOpts) } // GetDepositRoot is a free data retrieval call binding the contract method 0xc5f2892f. // -// Solidity: function get_deposit_root() returns(bytes32 out) +// Solidity: function get_deposit_root() view returns(bytes32) func (_DepositContract *DepositContractCallerSession) GetDepositRoot() ([32]byte, error) { return _DepositContract.Contract.GetDepositRoot(&_DepositContract.CallOpts) } +// SupportsInterface is a free data retrieval call binding the contract method 0x01ffc9a7. +// +// Solidity: function supportsInterface(bytes4 interfaceId) pure returns(bool) +func (_DepositContract *DepositContractCaller) SupportsInterface(opts *bind.CallOpts, interfaceId [4]byte) (bool, error) { + var out []interface{} + err := _DepositContract.contract.Call(opts, &out, "supportsInterface", interfaceId) + + if err != nil { + return *new(bool), err + } + + out0 := *abi.ConvertType(out[0], new(bool)).(*bool) + + return out0, err + +} + +// SupportsInterface is a free data retrieval call binding the contract method 0x01ffc9a7. +// +// Solidity: function supportsInterface(bytes4 interfaceId) pure returns(bool) +func (_DepositContract *DepositContractSession) SupportsInterface(interfaceId [4]byte) (bool, error) { + return _DepositContract.Contract.SupportsInterface(&_DepositContract.CallOpts, interfaceId) +} + +// SupportsInterface is a free data retrieval call binding the contract method 0x01ffc9a7. +// +// Solidity: function supportsInterface(bytes4 interfaceId) pure returns(bool) +func (_DepositContract *DepositContractCallerSession) SupportsInterface(interfaceId [4]byte) (bool, error) { + return _DepositContract.Contract.SupportsInterface(&_DepositContract.CallOpts, interfaceId) +} + // Deposit is a paid mutator transaction binding the contract method 0x22895118. // -// Solidity: function deposit(bytes pubkey, bytes withdrawal_credentials, bytes signature, bytes32 deposit_data_root) returns() +// Solidity: function deposit(bytes pubkey, bytes withdrawal_credentials, bytes signature, bytes32 deposit_data_root) payable returns() func (_DepositContract *DepositContractTransactor) Deposit(opts *bind.TransactOpts, pubkey []byte, withdrawal_credentials []byte, signature []byte, deposit_data_root [32]byte) (*types.Transaction, error) { return _DepositContract.contract.Transact(opts, "deposit", pubkey, withdrawal_credentials, signature, deposit_data_root) } // Deposit is a paid mutator transaction binding the contract method 0x22895118. // -// Solidity: function deposit(bytes pubkey, bytes withdrawal_credentials, bytes signature, bytes32 deposit_data_root) returns() +// Solidity: function deposit(bytes pubkey, bytes withdrawal_credentials, bytes signature, bytes32 deposit_data_root) payable returns() func (_DepositContract *DepositContractSession) Deposit(pubkey []byte, withdrawal_credentials []byte, signature []byte, deposit_data_root [32]byte) (*types.Transaction, error) { return _DepositContract.Contract.Deposit(&_DepositContract.TransactOpts, pubkey, withdrawal_credentials, signature, deposit_data_root) } // Deposit is a paid mutator transaction binding the contract method 0x22895118. // -// Solidity: function deposit(bytes pubkey, bytes withdrawal_credentials, bytes signature, bytes32 deposit_data_root) returns() +// Solidity: function deposit(bytes pubkey, bytes withdrawal_credentials, bytes signature, bytes32 deposit_data_root) payable returns() func (_DepositContract *DepositContractTransactorSession) Deposit(pubkey []byte, withdrawal_credentials []byte, signature []byte, deposit_data_root [32]byte) (*types.Transaction, error) { return _DepositContract.Contract.Deposit(&_DepositContract.TransactOpts, pubkey, withdrawal_credentials, signature, deposit_data_root) } -// Drain is a paid mutator transaction binding the contract method 0x9890220b. -// -// Solidity: function drain() returns() -func (_DepositContract *DepositContractTransactor) Drain(opts *bind.TransactOpts) (*types.Transaction, error) { - return _DepositContract.contract.Transact(opts, "drain") -} - -// Drain is a paid mutator transaction binding the contract method 0x9890220b. -// -// Solidity: function drain() returns() -func (_DepositContract *DepositContractSession) Drain() (*types.Transaction, error) { - return _DepositContract.Contract.Drain(&_DepositContract.TransactOpts) -} - -// Drain is a paid mutator transaction binding the contract method 0x9890220b. -// -// Solidity: function drain() returns() -func (_DepositContract *DepositContractTransactorSession) Drain() (*types.Transaction, error) { - return _DepositContract.Contract.Drain(&_DepositContract.TransactOpts) -} - // DepositContractDepositEventIterator is returned from FilterDepositEvent and is used to iterate over the raw logs and unpacked data for DepositEvent events raised by the DepositContract contract. type DepositContractDepositEventIterator struct { Event *DepositContractDepositEvent // Event containing the contract specifics and raw log @@ -457,5 +418,6 @@ func (_DepositContract *DepositContractFilterer) ParseDepositEvent(log types.Log if err := _DepositContract.contract.UnpackLog(event, "DepositEvent", log); err != nil { return nil, err } + event.Raw = log return event, nil } diff --git a/contracts/deposit-contract/depositContract_test.go b/contracts/deposit/contract_test.go similarity index 72% rename from contracts/deposit-contract/depositContract_test.go rename to contracts/deposit/contract_test.go index 44dc3034d..2117e8bb6 100644 --- a/contracts/deposit-contract/depositContract_test.go +++ b/contracts/deposit/contract_test.go @@ -1,14 +1,13 @@ -package depositcontract_test +package deposit_test import ( "context" "encoding/binary" - "math/big" "testing" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/runtime/interop" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -82,38 +81,3 @@ func TestValidatorRegister_OK(t *testing.T) { assert.Equal(t, uint64(1), merkleTreeIndex[1], "Deposit event total desposit count miss matched") assert.Equal(t, uint64(2), merkleTreeIndex[2], "Deposit event total desposit count miss matched") } - -func TestDrain(t *testing.T) { - testAccount, err := depositcontract.Setup() - require.NoError(t, err) - testAccount.TxOpts.Value = depositcontract.Amount32Eth() - - // Generate deposit data - privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 1) - require.NoError(t, err) - depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys) - require.NoError(t, err) - - var depositDataRoot [32]byte - copy(depositDataRoot[:], depositDataRoots[0]) - _, err = testAccount.Contract.Deposit(testAccount.TxOpts, pubKeys[0].Marshal(), depositDataItems[0].WithdrawalCredentials, depositDataItems[0].Signature, depositDataRoot) - testAccount.Backend.Commit() - require.NoError(t, err, "Validator registration failed") - - testAccount.Backend.Commit() - - ctx := context.Background() - bal, err := testAccount.Backend.BalanceAt(ctx, testAccount.ContractAddr, nil) - require.NoError(t, err) - require.Equal(t, 0, bal.Cmp(depositcontract.Amount32Eth()), "Deposit did not work") - - testAccount.TxOpts.Value = big.NewInt(0) - _, err = testAccount.Contract.Drain(testAccount.TxOpts) - require.NoError(t, err) - - testAccount.Backend.Commit() - - bal, err = testAccount.Backend.BalanceAt(ctx, testAccount.ContractAddr, nil) - require.NoError(t, err) - assert.Equal(t, 0, big.NewInt(0).Cmp(bal), "Drain did not drain balance") -} diff --git a/shared/depositutil/deposit.go b/contracts/deposit/deposit.go similarity index 99% rename from shared/depositutil/deposit.go rename to contracts/deposit/deposit.go index 3875279b1..d808dcf18 100644 --- a/shared/depositutil/deposit.go +++ b/contracts/deposit/deposit.go @@ -1,6 +1,6 @@ // Package depositutil contains useful functions for dealing // with Ethereum deposit inputs. -package depositutil +package deposit import ( "github.com/pkg/errors" diff --git a/contracts/deposit/deposit_contract.sol b/contracts/deposit/deposit_contract.sol new file mode 100644 index 000000000..18f3e9eb1 --- /dev/null +++ b/contracts/deposit/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]; + } +} \ No newline at end of file diff --git a/shared/depositutil/deposit_test.go b/contracts/deposit/deposit_test.go similarity index 85% rename from shared/depositutil/deposit_test.go rename to contracts/deposit/deposit_test.go index 49f6f6f8c..8e131d88c 100644 --- a/shared/depositutil/deposit_test.go +++ b/contracts/deposit/deposit_test.go @@ -1,13 +1,13 @@ -package depositutil_test +package deposit_test import ( "testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/config/params" + "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/crypto/bls" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/shared/depositutil" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -19,7 +19,7 @@ func TestDepositInput_GeneratesPb(t *testing.T) { k2, err := bls.RandKey() require.NoError(t, err) - result, _, err := depositutil.DepositInput(k1, k2, 0) + result, _, err := deposit.DepositInput(k1, k2, 0) require.NoError(t, err) assert.DeepEqual(t, k1.PublicKey().Marshal(), result.PublicKey) @@ -46,29 +46,29 @@ func TestDepositInput_GeneratesPb(t *testing.T) { func TestVerifyDepositSignature_ValidSig(t *testing.T) { deposits, _, err := testutil.DeterministicDepositsAndKeys(1) require.NoError(t, err) - deposit := deposits[0] + dep := deposits[0] domain, err := helpers.ComputeDomain( params.BeaconConfig().DomainDeposit, params.BeaconConfig().GenesisForkVersion, params.BeaconConfig().ZeroHash[:], ) require.NoError(t, err) - err = depositutil.VerifyDepositSignature(deposit.Data, domain) + err = deposit.VerifyDepositSignature(dep.Data, domain) require.NoError(t, err) } func TestVerifyDepositSignature_InvalidSig(t *testing.T) { deposits, _, err := testutil.DeterministicDepositsAndKeys(1) require.NoError(t, err) - deposit := deposits[0] + dep := deposits[0] domain, err := helpers.ComputeDomain( params.BeaconConfig().DomainDeposit, params.BeaconConfig().GenesisForkVersion, params.BeaconConfig().ZeroHash[:], ) require.NoError(t, err) - deposit.Data.Signature = deposit.Data.Signature[1:] - err = depositutil.VerifyDepositSignature(deposit.Data, domain) + dep.Data.Signature = dep.Data.Signature[1:] + err = deposit.VerifyDepositSignature(dep.Data, domain) if err == nil { t.Fatal("Deposit Verification succeeds with a invalid signature") } diff --git a/contracts/deposit-contract/deposit_tree_test.go b/contracts/deposit/deposit_tree_test.go similarity index 98% rename from contracts/deposit-contract/deposit_tree_test.go rename to contracts/deposit/deposit_tree_test.go index e7af69005..363b64acb 100644 --- a/contracts/deposit-contract/deposit_tree_test.go +++ b/contracts/deposit/deposit_tree_test.go @@ -1,4 +1,4 @@ -package depositcontract_test +package deposit_test import ( "strconv" @@ -7,7 +7,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/container/trie" - depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/runtime/interop" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" diff --git a/contracts/deposit-contract/ETH1logs.go b/contracts/deposit/logs.go similarity index 97% rename from contracts/deposit-contract/ETH1logs.go rename to contracts/deposit/logs.go index ca672928c..5b1684779 100644 --- a/contracts/deposit-contract/ETH1logs.go +++ b/contracts/deposit/logs.go @@ -1,4 +1,4 @@ -package depositcontract +package deposit import ( "bytes" diff --git a/contracts/deposit/mock.go b/contracts/deposit/mock.go new file mode 100644 index 000000000..74102b624 --- /dev/null +++ b/contracts/deposit/mock.go @@ -0,0 +1,93 @@ +package deposit + +import ( + "crypto/ecdsa" + "fmt" + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" +) + +var ( + amount32Eth = "32000000000000000000" + amountLessThan1Eth = "500000000000000000" + depositContractBin = "0x608060405234801561001057600080fd5b5060005b601f8110156101025760026021826020811061002c57fe5b01546021836020811061003b57fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b602083106100925780518252601f199092019160209182019101610073565b51815160209384036101000a60001901801990921691161790526040519190930194509192505080830381855afa1580156100d1573d6000803e3d6000fd5b5050506040513d60208110156100e657600080fd5b5051602160018301602081106100f857fe5b0155600101610014565b506118d680620001136000396000f3fe60806040526004361061003f5760003560e01c806301ffc9a71461004457806322895118146100a4578063621fd130146101ba578063c5f2892f14610244575b600080fd5b34801561005057600080fd5b506100906004803603602081101561006757600080fd5b50357fffffffff000000000000000000000000000000000000000000000000000000001661026b565b604080519115158252519081900360200190f35b6101b8600480360360808110156100ba57600080fd5b8101906020810181356401000000008111156100d557600080fd5b8201836020820111156100e757600080fd5b8035906020019184600183028401116401000000008311171561010957600080fd5b91939092909160208101903564010000000081111561012757600080fd5b82018360208201111561013957600080fd5b8035906020019184600183028401116401000000008311171561015b57600080fd5b91939092909160208101903564010000000081111561017957600080fd5b82018360208201111561018b57600080fd5b803590602001918460018302840111640100000000831117156101ad57600080fd5b919350915035610304565b005b3480156101c657600080fd5b506101cf6110b5565b6040805160208082528351818301528351919283929083019185019080838360005b838110156102095781810151838201526020016101f1565b50505050905090810190601f1680156102365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561025057600080fd5b506102596110c7565b60408051918252519081900360200190f35b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a70000000000000000000000000000000000000000000000000000000014806102fe57507fffffffff0000000000000000000000000000000000000000000000000000000082167f8564090700000000000000000000000000000000000000000000000000000000145b92915050565b6030861461035d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118056026913960400191505060405180910390fd5b602084146103b6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603681526020018061179c6036913960400191505060405180910390fd5b6060821461040f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260298152602001806118786029913960400191505060405180910390fd5b670de0b6b3a7640000341015610470576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118526026913960400191505060405180910390fd5b633b9aca003406156104cd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260338152602001806117d26033913960400191505060405180910390fd5b633b9aca00340467ffffffffffffffff811115610535576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602781526020018061182b6027913960400191505060405180910390fd5b6060610540826114ba565b90507f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c589898989858a8a6105756020546114ba565b6040805160a0808252810189905290819060208201908201606083016080840160c085018e8e80828437600083820152601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690910187810386528c815260200190508c8c808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690920188810386528c5181528c51602091820193918e019250908190849084905b83811015610648578181015183820152602001610630565b50505050905090810190601f1680156106755780820380516001836020036101000a031916815260200191505b5086810383528881526020018989808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169092018881038452895181528951602091820193918b019250908190849084905b838110156106ef5781810151838201526020016106d7565b50505050905090810190601f16801561071c5780820380516001836020036101000a031916815260200191505b509d505050505050505050505050505060405180910390a1600060028a8a600060801b604051602001808484808284377fffffffffffffffffffffffffffffffff0000000000000000000000000000000090941691909301908152604080517ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0818403018152601090920190819052815191955093508392506020850191508083835b602083106107fc57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016107bf565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610859573d6000803e3d6000fd5b5050506040513d602081101561086e57600080fd5b5051905060006002806108846040848a8c6116fe565b6040516020018083838082843780830192505050925050506040516020818303038152906040526040518082805190602001908083835b602083106108f857805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016108bb565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610955573d6000803e3d6000fd5b5050506040513d602081101561096a57600080fd5b5051600261097b896040818d6116fe565b60405160009060200180848480828437919091019283525050604080518083038152602092830191829052805190945090925082918401908083835b602083106109f457805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016109b7565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610a51573d6000803e3d6000fd5b5050506040513d6020811015610a6657600080fd5b5051604080516020818101949094528082019290925280518083038201815260609092019081905281519192909182918401908083835b60208310610ada57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610a9d565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610b37573d6000803e3d6000fd5b5050506040513d6020811015610b4c57600080fd5b50516040805160208101858152929350600092600292839287928f928f92018383808284378083019250505093505050506040516020818303038152906040526040518082805190602001908083835b60208310610bd957805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610b9c565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610c36573d6000803e3d6000fd5b5050506040513d6020811015610c4b57600080fd5b50516040518651600291889160009188916020918201918291908601908083835b60208310610ca957805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610c6c565b6001836020036101000a0380198251168184511680821785525050505050509050018367ffffffffffffffff191667ffffffffffffffff1916815260180182815260200193505050506040516020818303038152906040526040518082805190602001908083835b60208310610d4e57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610d11565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610dab573d6000803e3d6000fd5b5050506040513d6020811015610dc057600080fd5b5051604080516020818101949094528082019290925280518083038201815260609092019081905281519192909182918401908083835b60208310610e3457805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610df7565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015610e91573d6000803e3d6000fd5b5050506040513d6020811015610ea657600080fd5b50519050858114610f02576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260548152602001806117486054913960600191505060405180910390fd5b60205463ffffffff11610f60576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260218152602001806117276021913960400191505060405180910390fd5b602080546001019081905560005b60208110156110a9578160011660011415610fa0578260008260208110610f9157fe5b0155506110ac95505050505050565b600260008260208110610faf57fe5b01548460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061102557805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610fe8565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa158015611082573d6000803e3d6000fd5b5050506040513d602081101561109757600080fd5b50519250600282049150600101610f6e565b50fe5b50505050505050565b60606110c26020546114ba565b905090565b6020546000908190815b60208110156112f05781600116600114156111e6576002600082602081106110f557fe5b01548460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061116b57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161112e565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa1580156111c8573d6000803e3d6000fd5b5050506040513d60208110156111dd57600080fd5b505192506112e2565b600283602183602081106111f657fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b6020831061126b57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161122e565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa1580156112c8573d6000803e3d6000fd5b5050506040513d60208110156112dd57600080fd5b505192505b6002820491506001016110d1565b506002826112ff6020546114ba565b600060401b6040516020018084815260200183805190602001908083835b6020831061135a57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161131d565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790527fffffffffffffffffffffffffffffffffffffffffffffffff000000000000000095909516920191825250604080518083037ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8018152601890920190819052815191955093508392850191508083835b6020831061143f57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101611402565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01801990921691161790526040519190930194509192505080830381855afa15801561149c573d6000803e3d6000fd5b5050506040513d60208110156114b157600080fd5b50519250505090565b60408051600880825281830190925260609160208201818036833701905050905060c082901b8060071a60f81b826000815181106114f457fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060061a60f81b8260018151811061153757fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060051a60f81b8260028151811061157a57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060041a60f81b826003815181106115bd57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060031a60f81b8260048151811061160057fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060021a60f81b8260058151811061164357fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060011a60f81b8260068151811061168657fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053508060001a60f81b826007815181106116c957fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535050919050565b6000808585111561170d578182fd5b83861115611719578182fd5b505082019391909203915056fe4465706f736974436f6e74726163743a206d65726b6c6520747265652066756c6c4465706f736974436f6e74726163743a207265636f6e7374727563746564204465706f7369744461746120646f6573206e6f74206d6174636820737570706c696564206465706f7369745f646174615f726f6f744465706f736974436f6e74726163743a20696e76616c6964207769746864726177616c5f63726564656e7469616c73206c656e6774684465706f736974436f6e74726163743a206465706f7369742076616c7565206e6f74206d756c7469706c65206f6620677765694465706f736974436f6e74726163743a20696e76616c6964207075626b6579206c656e6774684465706f736974436f6e74726163743a206465706f7369742076616c756520746f6f20686967684465706f736974436f6e74726163743a206465706f7369742076616c756520746f6f206c6f774465706f736974436f6e74726163743a20696e76616c6964207369676e6174757265206c656e677468a2646970667358221220dceca8706b29e917dacf25fceef95acac8d90d765ac926663ce4096195952b6164736f6c634300060b0033" +) + +// TestAccount represents a test account in the simulated backend, +// through which we can perform actions on the eth1.0 chain. +type TestAccount struct { + Addr common.Address + ContractAddr common.Address + Contract *DepositContract + Backend *backends.SimulatedBackend + TxOpts *bind.TransactOpts +} + +// Setup creates the simulated backend with the deposit contract deployed +func Setup() (*TestAccount, error) { + genesis := make(core.GenesisAlloc) + privKey, err := crypto.GenerateKey() + if err != nil { + return nil, err + } + pubKeyECDSA, ok := privKey.Public().(*ecdsa.PublicKey) + if !ok { + return nil, fmt.Errorf("error casting public key to ECDSA") + } + + // strip off the 0x and the first 2 characters 04 which is always the EC prefix and is not required. + publicKeyBytes := crypto.FromECDSAPub(pubKeyECDSA)[4:] + var pubKey = make([]byte, 48) + copy(pubKey, publicKeyBytes) + + addr := crypto.PubkeyToAddress(privKey.PublicKey) + txOpts, err := bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337)) + if err != nil { + return nil, err + } + startingBalance, _ := new(big.Int).SetString("100000000000000000000000000000000000000", 10) + genesis[addr] = core.GenesisAccount{Balance: startingBalance} + backend := backends.NewSimulatedBackend(genesis, 210000000000) + + contractAddr, _, contract, err := DeployDepositContract(txOpts, backend) + if err != nil { + return nil, err + } + backend.Commit() + + return &TestAccount{addr, contractAddr, contract, backend, txOpts}, nil +} + +// Amount32Eth returns 32Eth(in wei) in terms of the big.Int type. +func Amount32Eth() *big.Int { + amount, _ := new(big.Int).SetString(amount32Eth, 10) + return amount +} + +// LessThan1Eth returns less than 1 Eth(in wei) in terms of the big.Int type. +func LessThan1Eth() *big.Int { + amount, _ := new(big.Int).SetString(amountLessThan1Eth, 10) + return amount +} + +// DeployDepositContract deploys a new Ethereum contract, binding an instance of DepositContract to it. +func DeployDepositContract(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *DepositContract, error) { + parsed, err := abi.JSON(strings.NewReader(DepositContractABI)) + if err != nil { + return common.Address{}, nil, nil, err + } + + address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(depositContractBin), backend) + if err != nil { + return common.Address{}, nil, nil, err + } + return address, tx, &DepositContract{DepositContractCaller: DepositContractCaller{contract: contract}, DepositContractTransactor: DepositContractTransactor{contract: contract}, DepositContractFilterer: DepositContractFilterer{contract: contract}}, nil +} diff --git a/shared/depositutil/BUILD.bazel b/shared/depositutil/BUILD.bazel deleted file mode 100644 index 61eb79fda..000000000 --- a/shared/depositutil/BUILD.bazel +++ /dev/null @@ -1,32 +0,0 @@ -load("@prysm//tools/go:def.bzl", "go_library", "go_test") - -go_library( - name = "go_default_library", - srcs = ["deposit.go"], - importpath = "github.com/prysmaticlabs/prysm/shared/depositutil", - visibility = ["//visibility:public"], - deps = [ - "//beacon-chain/core/helpers:go_default_library", - "//config/features:go_default_library", - "//config/params:go_default_library", - "//crypto/bls:go_default_library", - "//crypto/hash:go_default_library", - "//proto/prysm/v1alpha1:go_default_library", - "@com_github_pkg_errors//:go_default_library", - ], -) - -go_test( - name = "go_default_test", - srcs = ["deposit_test.go"], - deps = [ - ":go_default_library", - "//beacon-chain/core/helpers:go_default_library", - "//config/params:go_default_library", - "//crypto/bls:go_default_library", - "//proto/prysm/v1alpha1:go_default_library", - "//shared/testutil:go_default_library", - "//shared/testutil/assert:go_default_library", - "//shared/testutil/require:go_default_library", - ], -) diff --git a/testing/endtoend/components/BUILD.bazel b/testing/endtoend/components/BUILD.bazel index a67e62a8e..71e599ec4 100644 --- a/testing/endtoend/components/BUILD.bazel +++ b/testing/endtoend/components/BUILD.bazel @@ -19,7 +19,7 @@ go_library( "//cmd/validator/flags:go_default_library", "//config/features:go_default_library", "//config/params:go_default_library", - "//contracts/deposit-contract:go_default_library", + "//contracts/deposit:go_default_library", "//shared/bytesutil:go_default_library", "//shared/testutil:go_default_library", "//testing/endtoend/helpers:go_default_library", diff --git a/testing/endtoend/components/eth1.go b/testing/endtoend/components/eth1.go index 991c86968..889dda3a1 100644 --- a/testing/endtoend/components/eth1.go +++ b/testing/endtoend/components/eth1.go @@ -20,7 +20,7 @@ import ( "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" "github.com/prysmaticlabs/prysm/config/params" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/testing/endtoend/helpers" e2e "github.com/prysmaticlabs/prysm/testing/endtoend/params" e2etypes "github.com/prysmaticlabs/prysm/testing/endtoend/types" @@ -133,7 +133,7 @@ func (node *Eth1Node) Start(ctx context.Context) error { } txOpts.Nonce = big.NewInt(int64(nonce)) txOpts.Context = context.Background() - contractAddr, tx, _, err := contracts.DeployDepositContract(txOpts, web3, txOpts.From) + contractAddr, tx, _, err := contracts.DeployDepositContract(txOpts, web3) if err != nil { return fmt.Errorf("failed to deploy deposit contract: %w", err) } diff --git a/testing/endtoend/components/validator.go b/testing/endtoend/components/validator.go index b84ccd802..a6d299ae3 100644 --- a/testing/endtoend/components/validator.go +++ b/testing/endtoend/components/validator.go @@ -21,7 +21,7 @@ import ( "github.com/prysmaticlabs/prysm/cmd/validator/flags" "github.com/prysmaticlabs/prysm/config/features" "github.com/prysmaticlabs/prysm/config/params" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/testing/endtoend/helpers" diff --git a/tools/deployContract/BUILD.bazel b/tools/deployContract/BUILD.bazel index 08d44c0e4..d3ee43443 100644 --- a/tools/deployContract/BUILD.bazel +++ b/tools/deployContract/BUILD.bazel @@ -7,11 +7,10 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/deployContract", visibility = ["//visibility:private"], deps = [ - "//contracts/deposit-contract:go_default_library", + "//contracts/deposit:go_default_library", "//runtime/version:go_default_library", "@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library", "@com_github_ethereum_go_ethereum//accounts/keystore:go_default_library", - "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_ethereum_go_ethereum//crypto:go_default_library", "@com_github_ethereum_go_ethereum//ethclient:go_default_library", "@com_github_ethereum_go_ethereum//rpc:go_default_library", diff --git a/tools/deployContract/deployContract.go b/tools/deployContract/deployContract.go index 57caea972..f86733a2d 100644 --- a/tools/deployContract/deployContract.go +++ b/tools/deployContract/deployContract.go @@ -11,11 +11,10 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + contracts "github.com/prysmaticlabs/prysm/contracts/deposit" "github.com/prysmaticlabs/prysm/runtime/version" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" @@ -148,18 +147,12 @@ func main() { txOps.Context = context.Background() } - drain := txOps.From - if drainAddress != "" { - drain = common.HexToAddress(drainAddress) - } - txOps.GasPrice = big.NewInt(10 * 1e9 /* 10 gwei */) // Deploy validator registration contract addr, tx, _, err := contracts.DeployDepositContract( txOps, client, - drain, ) if err != nil { diff --git a/tools/drainContracts/BUILD.bazel b/tools/drainContracts/BUILD.bazel deleted file mode 100644 index de9dc5a90..000000000 --- a/tools/drainContracts/BUILD.bazel +++ /dev/null @@ -1,30 +0,0 @@ -load("@prysm//tools/go:def.bzl", "go_library") -load("@io_bazel_rules_go//go:def.bzl", "go_binary") - -go_library( - name = "go_default_library", - srcs = ["drainContracts.go"], - importpath = "github.com/prysmaticlabs/prysm/tools/drainContracts", - visibility = ["//visibility:private"], - deps = [ - "//contracts/deposit-contract:go_default_library", - "//runtime/version:go_default_library", - "@com_github_ethereum_go_ethereum//:go_default_library", - "@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library", - "@com_github_ethereum_go_ethereum//accounts/keystore:go_default_library", - "@com_github_ethereum_go_ethereum//common:go_default_library", - "@com_github_ethereum_go_ethereum//crypto:go_default_library", - "@com_github_ethereum_go_ethereum//ethclient:go_default_library", - "@com_github_ethereum_go_ethereum//rpc:go_default_library", - "@com_github_pkg_errors//:go_default_library", - "@com_github_sirupsen_logrus//:go_default_library", - "@com_github_urfave_cli_v2//:go_default_library", - "@com_github_x_cray_logrus_prefixed_formatter//:go_default_library", - ], -) - -go_binary( - name = "drainContracts", - embed = [":go_default_library"], - visibility = ["//visibility:public"], -) diff --git a/tools/drainContracts/README.md b/tools/drainContracts/README.md deleted file mode 100644 index e5cb5384c..000000000 --- a/tools/drainContracts/README.md +++ /dev/null @@ -1,39 +0,0 @@ -## Utility to Drain All Deposit Contracts - -This is a utility to help users drain the contract addresses they have deployed in order to get their testnet ether back. To run the utility, it defaults to an infura link but you can use your own provider through the flags. The utility will print out each address it sends a transaction to. - -### Usage - -_Name:_ -**drainContracts** - this is a util to drain all deposit contracts - -_Usage:_ -drainContracts [global options] - -_Flags:_ - -- --keystoreUTCPath value keystore JSON for account -- --httpPath value HTTP-RPC server listening interface (default: "http://localhost:8545/") -- --passwordFile value Password file for unlock account (default: "./password.txt") -- --privKey value Private key to unlock account -- --help, -h show help -- --version, -v print the version - -### Example - -To use private key with default RPC: - -``` -bazel run //contracts/deposit-contract/drainContracts -- --httpPath=https://goerli.prylabs.net --privKey=$(echo /path/to/private/key/file) -``` - -### Output - -``` -current address is 0xdbA543721462680431eC4eeB26163079B3645660 -nonce is 7060 -0xd1faa3f9bca1d698df559716fe6d1c9999155b38d3158fffbc98d76d568091fc -1190 chain start logs found -1190 contracts ready to drain found -Contract address 0x4cb8976E4Bf0b6A462AF8704F0f724775B67b4Ce drained in TX hash: 0x3f963c30c4fd4ff875c641be1e7b873bfe02ae2cd2d73554cc6087c2d3acaa9e -``` diff --git a/tools/drainContracts/drainContracts.go b/tools/drainContracts/drainContracts.go deleted file mode 100644 index e74a7e13e..000000000 --- a/tools/drainContracts/drainContracts.go +++ /dev/null @@ -1,227 +0,0 @@ -package main - -import ( - "bufio" - "context" - "fmt" - "io/ioutil" - "log" - "math/big" - "os" - "time" - - "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/rpc" - "github.com/pkg/errors" - contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" - "github.com/prysmaticlabs/prysm/runtime/version" - "github.com/sirupsen/logrus" - "github.com/urfave/cli/v2" - prefixed "github.com/x-cray/logrus-prefixed-formatter" -) - -func main() { - var keystoreUTCPath string - var passwordFile string - var httpPath string - var privKeyString string - - customFormatter := new(prefixed.TextFormatter) - customFormatter.TimestampFormat = "2006-01-02 15:04:05" - customFormatter.FullTimestamp = true - logrus.SetFormatter(customFormatter) - - app := cli.App{} - app.Name = "drainContracts" - app.Usage = "this is a util to drain all (testing) deposit contracts of their ETH." - app.Version = version.Version() - app.Flags = []cli.Flag{ - &cli.StringFlag{ - Name: "keystoreUTCPath", - Usage: "Location of keystore", - Destination: &keystoreUTCPath, - }, - &cli.StringFlag{ - Name: "httpPath", - Value: "https://goerli.infura.io/v3/be3fb7ed377c418087602876a40affa1", - Usage: "HTTP-RPC server listening interface", - Destination: &httpPath, - }, - &cli.StringFlag{ - Name: "passwordFile", - Value: "./password.txt", - Usage: "Password file for unlock account", - Destination: &passwordFile, - }, - &cli.StringFlag{ - Name: "privKey", - Usage: "Private key to send ETH transaction", - Destination: &privKeyString, - }, - } - - app.Action = func(c *cli.Context) error { - // Set up RPC client - var rpcClient *rpc.Client - var err error - var txOps *bind.TransactOpts - - // Uses HTTP-RPC if IPC is not set - rpcClient, err = rpc.Dial(httpPath) - if err != nil { - return err - } - - client := ethclient.NewClient(rpcClient) - - // User inputs private key, sign tx with private key - if privKeyString != "" { - privKey, err := crypto.HexToECDSA(privKeyString) - if err != nil { - return err - } - txOps, err = bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337)) - if err != nil { - return err - } - txOps.Value = big.NewInt(0) - txOps.GasLimit = 4000000 - txOps.Context = context.Background() - nonce, err := client.NonceAt(context.Background(), crypto.PubkeyToAddress(privKey.PublicKey), nil) - if err != nil { - return errors.Wrap(err, "could not get account nonce") - } - txOps.Nonce = big.NewInt(int64(nonce)) - fmt.Printf("current address is %s\n", crypto.PubkeyToAddress(privKey.PublicKey).String()) - fmt.Printf("nonce is %d\n", nonce) - // User inputs keystore json file, sign tx with keystore json - } else { - password := loadTextFromFile(passwordFile) - - // #nosec - Inclusion of file via variable is OK for this tool. - keyJSON, err := ioutil.ReadFile(keystoreUTCPath) - if err != nil { - return err - } - privKey, err := keystore.DecryptKey(keyJSON, password) - if err != nil { - return err - } - - txOps, err = bind.NewKeyedTransactorWithChainID(privKey.PrivateKey, big.NewInt(1337)) - if err != nil { - return err - } - txOps.Value = big.NewInt(0) - txOps.GasLimit = 4000000 - txOps.Context = context.Background() - nonce, err := client.NonceAt(context.Background(), privKey.Address, nil) - if err != nil { - return err - } - txOps.Nonce = big.NewInt(int64(nonce)) - fmt.Printf("current address is %s\n", privKey.Address.String()) - fmt.Printf("nonce is %d\n", nonce) - } - - addresses, err := allDepositContractAddresses(client) - if err != nil { - return errors.Wrap(err, "Could not get all deposit contract address") - } - - fmt.Printf("%d contracts ready to drain found\n", len(addresses)) - - for _, address := range addresses { - bal, err := client.BalanceAt(context.Background(), address, nil /*blockNum*/) - if err != nil { - return err - } - if bal.Cmp(big.NewInt(0)) < 1 { - continue - } - depositContract, err := contracts.NewDepositContract(address, client) - if err != nil { - log.Fatal(err) - } - tx, err := depositContract.Drain(txOps) - if err != nil { - log.Fatalf("unable to send transaction to contract: %v", err) - } - - txOps.Nonce = txOps.Nonce.Add(txOps.Nonce, big.NewInt(1)) - - fmt.Printf("Contract address %s drained in TX hash: %s\n", address.String(), tx.Hash().String()) - time.Sleep(time.Duration(1) * time.Second) - } - return nil - } - - err := app.Run(os.Args) - if err != nil { - log.Fatal(err) - } -} - -func loadTextFromFile(filepath string) string { - // #nosec - Inclusion of file via variable is OK for this tool. - file, err := os.Open(filepath) - if err != nil { - log.Fatal(err) - } - - scanner := bufio.NewScanner(file) - scanner.Split(bufio.ScanWords) - scanner.Scan() - return scanner.Text() -} - -func allDepositContractAddresses(client *ethclient.Client) ([]common.Address, error) { - log.Print("Looking up contracts") - addresses := make(map[common.Address]bool) - - // Hash of deposit log signature - // DepositEvent: event({ - // pubkey: bytes[48], - // withdrawal_credentials: bytes[32], - // amount: bytes[8], - // signature: bytes[96], - // index: bytes[8], - // }) - depositTopicHash := common.HexToHash("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5") - fmt.Println(depositTopicHash.Hex()) - - query := ethereum.FilterQuery{ - Addresses: []common.Address{}, - Topics: [][]common.Hash{ - {depositTopicHash}, - }, - FromBlock: big.NewInt(800000), // Contracts before this may not have drain(). - } - - logs, err := client.FilterLogs(context.Background(), query) - if err != nil { - return nil, errors.Wrap(err, "could not get all deposit logs") - } - - fmt.Printf("%d deposit logs found\n", len(logs)) - for i := len(logs)/2 - 1; i >= 0; i-- { - opp := len(logs) - 1 - i - logs[i], logs[opp] = logs[opp], logs[i] - } - - for _, ll := range logs { - addresses[ll.Address] = true - } - - keys := make([]common.Address, 0, len(addresses)) - for key := range addresses { - keys = append(keys, key) - } - - return keys, nil -}