From 87e3dbc9b86b0ddf5422be421d61e1aed87fdd63 Mon Sep 17 00:00:00 2001 From: rkapka Date: Thu, 21 Jan 2021 13:45:49 +0100 Subject: [PATCH 01/12] Add funnction to retrieve deposit contract's address from powchain --- beacon-chain/powchain/service.go | 6 ++++++ beacon-chain/powchain/service_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 6fc4b99038f5..5e6d79979ac6 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -80,6 +80,7 @@ type ChainStartFetcher interface { type ChainInfoFetcher interface { Eth2GenesisPowchainInfo() (uint64, *big.Int) IsConnectedToETH1() bool + DepositContractAddress() common.Address } // POWBlockFetcher defines a struct that can retrieve mainchain blocks. @@ -327,6 +328,11 @@ func (s *Service) IsConnectedToETH1() bool { return s.connectedETH1 } +// DepositContractAddress returns the address of the ETH1 deposit contract. +func (s *Service) DepositContractAddress() common.Address { + return s.depositContractAddress +} + // DepositRoot returns the Merkle root of the latest deposit trie // from the ETH1.0 deposit contract. func (s *Service) DepositRoot() [32]byte { diff --git a/beacon-chain/powchain/service_test.go b/beacon-chain/powchain/service_test.go index 424b1e16bdec..c0ab6a6221e0 100644 --- a/beacon-chain/powchain/service_test.go +++ b/beacon-chain/powchain/service_test.go @@ -584,3 +584,11 @@ func Test_batchRequestHeaders_UnderflowChecks(t *testing.T) { _, err = srv.batchRequestHeaders(start, end) require.ErrorContains(t, "cannot be >", err) } + +func TestDepositContractAddress(t *testing.T) { + s := &Service{depositContractAddress: [20]byte{1, 2, 3}} + address := s.DepositContractAddress() + if !bytes.Equal(s.depositContractAddress[:], address[:]) { + t.Errorf("Expected address %v vs received %v", s.depositContractAddress, address) + } +} From 81e63245bd7f8412a7e936a387e3ae258a53f799 Mon Sep 17 00:00:00 2001 From: rkapka Date: Thu, 21 Jan 2021 14:10:38 +0100 Subject: [PATCH 02/12] change bytes.Equal to assert.DeepEqual --- beacon-chain/powchain/service_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/beacon-chain/powchain/service_test.go b/beacon-chain/powchain/service_test.go index c0ab6a6221e0..2a925b7a3d0e 100644 --- a/beacon-chain/powchain/service_test.go +++ b/beacon-chain/powchain/service_test.go @@ -588,7 +588,5 @@ func Test_batchRequestHeaders_UnderflowChecks(t *testing.T) { func TestDepositContractAddress(t *testing.T) { s := &Service{depositContractAddress: [20]byte{1, 2, 3}} address := s.DepositContractAddress() - if !bytes.Equal(s.depositContractAddress[:], address[:]) { - t.Errorf("Expected address %v vs received %v", s.depositContractAddress, address) - } + assert.DeepEqual(t, s.depositContractAddress[:], address[:]) } From 45baf6e2b706ff55acbbbd5878332fe1e4362115 Mon Sep 17 00:00:00 2001 From: rkapka Date: Thu, 21 Jan 2021 14:14:19 +0100 Subject: [PATCH 03/12] add DepositContractAddress to mocks --- beacon-chain/powchain/testing/faulty_mock.go | 5 +++++ beacon-chain/powchain/testing/mock.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/beacon-chain/powchain/testing/faulty_mock.go b/beacon-chain/powchain/testing/faulty_mock.go index 623ca5040be0..5a993a2bcb31 100644 --- a/beacon-chain/powchain/testing/faulty_mock.go +++ b/beacon-chain/powchain/testing/faulty_mock.go @@ -88,6 +88,11 @@ func (f *FaultyMockPOWChain) IsConnectedToETH1() bool { return true } +// DepositContractAddress -- +func (f *FaultyMockPOWChain) DepositContractAddress() common.Address { + return common.Address{} +} + // BlockExistsWithCache -- func (f *FaultyMockPOWChain) BlockExistsWithCache(ctx context.Context, hash common.Hash) (bool, *big.Int, error) { return f.BlockExists(ctx, hash) diff --git a/beacon-chain/powchain/testing/mock.go b/beacon-chain/powchain/testing/mock.go index f6d4b0779da2..b22ef5d0a54d 100644 --- a/beacon-chain/powchain/testing/mock.go +++ b/beacon-chain/powchain/testing/mock.go @@ -133,6 +133,11 @@ func (m *POWChain) IsConnectedToETH1() bool { return true } +// DepositContractAddress -- +func (m *POWChain) DepositContractAddress() common.Address { + return common.Address{} +} + // RPCClient defines the mock rpc client. type RPCClient struct { Backend *backends.SimulatedBackend From e01dd5222b2ddc92607391f4b927b099100e03dd Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 22 Jan 2021 08:09:18 +0100 Subject: [PATCH 04/12] Extract powchain info to a separate struct --- beacon-chain/powchain/log_processing.go | 16 ++++++++++++---- beacon-chain/powchain/service.go | 2 +- beacon-chain/rpc/validator/proposer.go | 8 ++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index b71a8af12dd2..9b63f9c4c0bc 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -37,15 +37,23 @@ const depositlogRequestLimit = 10000 const additiveFactorMultiplier = 0.10 const multiplicativeDecreaseDivisor = 2 +// GenesisPowchainInfo contains information about the genesis event. +type GenesisPowchainInfo struct { + Time uint64 // Genesis time. + BlockNumber *big.Int // Eth1 block number. +} + func tooMuchDataRequestedError(err error) bool { // this error is only infura specific (other providers might have different error messages) return err.Error() == "query returned more than 10000 results" } -// Eth2GenesisPowchainInfo retrieves the genesis time and eth1 block number of the beacon chain -// from the deposit contract. -func (s *Service) Eth2GenesisPowchainInfo() (uint64, *big.Int) { - return s.chainStartData.GenesisTime, big.NewInt(int64(s.chainStartData.GenesisBlock)) +// Eth2GenesisPowchainInfo retrieves information about genesis. +func (s *Service) Eth2GenesisPowchainInfo() GenesisPowchainInfo { + return GenesisPowchainInfo{ + Time: s.chainStartData.GenesisTime, + BlockNumber: big.NewInt(int64(s.chainStartData.GenesisBlock)), + } } // ProcessETH1Block processes the logs from the provided eth1Block. diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 5e6d79979ac6..50483cb22a9a 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -78,7 +78,7 @@ type ChainStartFetcher interface { // ChainInfoFetcher retrieves information about eth1 metadata at the eth2 genesis time. type ChainInfoFetcher interface { - Eth2GenesisPowchainInfo() (uint64, *big.Int) + Eth2GenesisPowchainInfo() GenesisPowchainInfo IsConnectedToETH1() bool DepositContractAddress() common.Address } diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index a0d23c6102ec..e59ce3593b68 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -285,8 +285,8 @@ func (vs *Server) eth1DataMajorityVote(ctx context.Context, beaconState *stateTr } func (vs *Server) slotStartTime(slot uint64) uint64 { - startTime, _ := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() - return helpers.VotingPeriodStartTime(startTime, slot) + genesisInfo := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() + return helpers.VotingPeriodStartTime(genesisInfo.Time, slot) } func (vs *Server) inRangeVotes(ctx context.Context, @@ -444,8 +444,8 @@ func (vs *Server) deposits( return nil, err } - _, genesisEth1Block := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() - if genesisEth1Block.Cmp(canonicalEth1DataHeight) == 0 { + genesisInfo := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() + if genesisInfo.BlockNumber.Cmp(canonicalEth1DataHeight) == 0 { return []*ethpb.Deposit{}, nil } From b22cf5b512b02d272d7a36de218e657387813cbe Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 22 Jan 2021 08:16:15 +0100 Subject: [PATCH 05/12] span --- beacon-chain/rpc/beaconv1/BUILD.bazel | 1 + beacon-chain/rpc/beaconv1/config.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/beacon-chain/rpc/beaconv1/BUILD.bazel b/beacon-chain/rpc/beaconv1/BUILD.bazel index a78be45450e3..e284cb9b074e 100644 --- a/beacon-chain/rpc/beaconv1/BUILD.bazel +++ b/beacon-chain/rpc/beaconv1/BUILD.bazel @@ -37,6 +37,7 @@ go_library( "@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", + "@io_opencensus_go//trace:go_default_library", "@org_golang_google_grpc//codes:go_default_library", "@org_golang_google_grpc//status:go_default_library", ], diff --git a/beacon-chain/rpc/beaconv1/config.go b/beacon-chain/rpc/beaconv1/config.go index 28db5ebd2c02..ef19fcd2cb1b 100644 --- a/beacon-chain/rpc/beaconv1/config.go +++ b/beacon-chain/rpc/beaconv1/config.go @@ -4,6 +4,8 @@ import ( "context" "errors" + "go.opencensus.io/trace" + ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" ) @@ -23,5 +25,7 @@ func (bs *Server) GetSpec(ctx context.Context, req *ptypes.Empty) (*ethpb.SpecRe // GetDepositContract retrieves deposit contract address and genesis fork version. func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*ethpb.DepositContractResponse, error) { + ctx, span := trace.StartSpan(ctx, "beaconv1.GetDepositContract") + defer span.End() return nil, errors.New("unimplemented") } From 2034ac26913b7ebb0a22df31873a9570014f29e3 Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 22 Jan 2021 08:16:20 +0100 Subject: [PATCH 06/12] Revert "Extract powchain info to a separate struct" This reverts commit e01dd5222b2ddc92607391f4b927b099100e03dd. --- beacon-chain/powchain/log_processing.go | 16 ++++------------ beacon-chain/powchain/service.go | 2 +- beacon-chain/rpc/validator/proposer.go | 8 ++++---- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index 9b63f9c4c0bc..b71a8af12dd2 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -37,23 +37,15 @@ const depositlogRequestLimit = 10000 const additiveFactorMultiplier = 0.10 const multiplicativeDecreaseDivisor = 2 -// GenesisPowchainInfo contains information about the genesis event. -type GenesisPowchainInfo struct { - Time uint64 // Genesis time. - BlockNumber *big.Int // Eth1 block number. -} - func tooMuchDataRequestedError(err error) bool { // this error is only infura specific (other providers might have different error messages) return err.Error() == "query returned more than 10000 results" } -// Eth2GenesisPowchainInfo retrieves information about genesis. -func (s *Service) Eth2GenesisPowchainInfo() GenesisPowchainInfo { - return GenesisPowchainInfo{ - Time: s.chainStartData.GenesisTime, - BlockNumber: big.NewInt(int64(s.chainStartData.GenesisBlock)), - } +// Eth2GenesisPowchainInfo retrieves the genesis time and eth1 block number of the beacon chain +// from the deposit contract. +func (s *Service) Eth2GenesisPowchainInfo() (uint64, *big.Int) { + return s.chainStartData.GenesisTime, big.NewInt(int64(s.chainStartData.GenesisBlock)) } // ProcessETH1Block processes the logs from the provided eth1Block. diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 50483cb22a9a..5e6d79979ac6 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -78,7 +78,7 @@ type ChainStartFetcher interface { // ChainInfoFetcher retrieves information about eth1 metadata at the eth2 genesis time. type ChainInfoFetcher interface { - Eth2GenesisPowchainInfo() GenesisPowchainInfo + Eth2GenesisPowchainInfo() (uint64, *big.Int) IsConnectedToETH1() bool DepositContractAddress() common.Address } diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index e59ce3593b68..a0d23c6102ec 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -285,8 +285,8 @@ func (vs *Server) eth1DataMajorityVote(ctx context.Context, beaconState *stateTr } func (vs *Server) slotStartTime(slot uint64) uint64 { - genesisInfo := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() - return helpers.VotingPeriodStartTime(genesisInfo.Time, slot) + startTime, _ := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() + return helpers.VotingPeriodStartTime(startTime, slot) } func (vs *Server) inRangeVotes(ctx context.Context, @@ -444,8 +444,8 @@ func (vs *Server) deposits( return nil, err } - genesisInfo := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() - if genesisInfo.BlockNumber.Cmp(canonicalEth1DataHeight) == 0 { + _, genesisEth1Block := vs.Eth1InfoFetcher.Eth2GenesisPowchainInfo() + if genesisEth1Block.Cmp(canonicalEth1DataHeight) == 0 { return []*ethpb.Deposit{}, nil } From a86907172d56b7a31fdd22dfb3b7bd0eeed1538c Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 22 Jan 2021 13:34:29 +0100 Subject: [PATCH 07/12] implementation + test --- beacon-chain/powchain/testing/mock.go | 2 +- beacon-chain/rpc/beaconv1/BUILD.bazel | 4 ++++ beacon-chain/rpc/beaconv1/config.go | 25 +++++++++++++++++--- beacon-chain/rpc/beaconv1/config_test.go | 30 ++++++++++++++++++++++++ beacon-chain/rpc/beaconv1/server.go | 1 + beacon-chain/rpc/service.go | 1 + 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 beacon-chain/rpc/beaconv1/config_test.go diff --git a/beacon-chain/powchain/testing/mock.go b/beacon-chain/powchain/testing/mock.go index b22ef5d0a54d..3d114281eadc 100644 --- a/beacon-chain/powchain/testing/mock.go +++ b/beacon-chain/powchain/testing/mock.go @@ -135,7 +135,7 @@ func (m *POWChain) IsConnectedToETH1() bool { // DepositContractAddress -- func (m *POWChain) DepositContractAddress() common.Address { - return common.Address{} + return common.Address{1, 2, 3, 4} } // RPCClient defines the mock rpc client. diff --git a/beacon-chain/rpc/beaconv1/BUILD.bazel b/beacon-chain/rpc/beaconv1/BUILD.bazel index e284cb9b074e..d43ec34c4f4a 100644 --- a/beacon-chain/rpc/beaconv1/BUILD.bazel +++ b/beacon-chain/rpc/beaconv1/BUILD.bazel @@ -32,11 +32,13 @@ go_library( "//proto/beacon/p2p/v1:go_default_library", "//proto/migration:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/params:go_default_library", "@com_github_gogo_protobuf//types:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", + "@com_github_status_im_keycard_go//hexutils:go_default_library", "@io_opencensus_go//trace:go_default_library", "@org_golang_google_grpc//codes:go_default_library", "@org_golang_google_grpc//status:go_default_library", @@ -47,6 +49,7 @@ go_test( name = "go_default_test", srcs = [ "blocks_test.go", + "config_test.go", "server_test.go", ], embed = [":go_default_library"], @@ -63,6 +66,7 @@ go_test( "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", + "@com_github_gogo_protobuf//types:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", ], diff --git a/beacon-chain/rpc/beaconv1/config.go b/beacon-chain/rpc/beaconv1/config.go index ef19fcd2cb1b..faf4e2f32dd6 100644 --- a/beacon-chain/rpc/beaconv1/config.go +++ b/beacon-chain/rpc/beaconv1/config.go @@ -1,13 +1,18 @@ package beaconv1 import ( + "bytes" "context" + "encoding/binary" "errors" - "go.opencensus.io/trace" - ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" + "github.com/prysmaticlabs/prysm/shared/params" + "github.com/status-im/keycard-go/hexutils" + "go.opencensus.io/trace" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // GetForkSchedule retrieve all scheduled upcoming forks this node is aware of. @@ -27,5 +32,19 @@ func (bs *Server) GetSpec(ctx context.Context, req *ptypes.Empty) (*ethpb.SpecRe func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*ethpb.DepositContractResponse, error) { ctx, span := trace.StartSpan(ctx, "beaconv1.GetDepositContract") defer span.End() - return nil, errors.New("unimplemented") + + buf := bytes.NewBuffer(params.BeaconConfig().GenesisForkVersion) + chainId, err := binary.ReadUvarint(buf) + if err != nil { + return nil, status.Errorf(codes.Internal, "could not obtain genesis fork version: %v", err) + } + byteAddress := [20]byte(bs.PowchainInfoFetcher.DepositContractAddress()) + hexAddress := hexutils.BytesToHex(byteAddress[:]) + + return ðpb.DepositContractResponse{ + Data: ðpb.DepositContract{ + ChainId: uint64(chainId), + Address: hexAddress, + }, + }, nil } diff --git a/beacon-chain/rpc/beaconv1/config_test.go b/beacon-chain/rpc/beaconv1/config_test.go new file mode 100644 index 000000000000..a65146d2c93b --- /dev/null +++ b/beacon-chain/rpc/beaconv1/config_test.go @@ -0,0 +1,30 @@ +package beaconv1 + +import ( + "context" + "testing" + + ptypes "github.com/gogo/protobuf/types" + powchaintest "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing" + "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil/assert" + "github.com/prysmaticlabs/prysm/shared/testutil/require" +) + +func TestGetDepositContract(t *testing.T) { + params.SetupTestConfigCleanup(t) + config := params.BeaconConfig() + config.GenesisForkVersion = []byte{9, 0, 0, 0} + params.OverrideBeaconConfig(config) + + infoFetcher := powchaintest.POWChain{} + infoFetcher.DepositContractAddress() + + s := Server{ + PowchainInfoFetcher: &infoFetcher, + } + resp, err := s.GetDepositContract(context.Background(), &ptypes.Empty{}) + require.NoError(t, err) + assert.Equal(t, uint64(9), resp.Data.ChainId) + assert.Equal(t, "0102030400000000000000000000000000000000", resp.Data.Address) +} diff --git a/beacon-chain/rpc/beaconv1/server.go b/beacon-chain/rpc/beaconv1/server.go index 2c71fa737c63..de6586b26eef 100644 --- a/beacon-chain/rpc/beaconv1/server.go +++ b/beacon-chain/rpc/beaconv1/server.go @@ -32,6 +32,7 @@ type Server struct { ChainInfoFetcher blockchain.ChainInfoFetcher DepositFetcher depositcache.DepositFetcher BlockFetcher powchain.POWBlockFetcher + PowchainInfoFetcher powchain.ChainInfoFetcher GenesisTimeFetcher blockchain.TimeFetcher BlockReceiver blockchain.BlockReceiver StateNotifier statefeed.Notifier diff --git a/beacon-chain/rpc/service.go b/beacon-chain/rpc/service.go index b222d09ac51d..5556214f20ab 100644 --- a/beacon-chain/rpc/service.go +++ b/beacon-chain/rpc/service.go @@ -313,6 +313,7 @@ func (s *Service) Start() { ChainStartFetcher: s.chainStartFetcher, DepositFetcher: s.depositFetcher, BlockFetcher: s.powChainService, + PowchainInfoFetcher: s.powChainService, CanonicalStateChan: s.canonicalStateChan, GenesisTimeFetcher: s.timeFetcher, StateNotifier: s.stateNotifier, From 8934b332604cbf8fd66823fdd7f52448a636dfcb Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 22 Jan 2021 13:41:43 +0100 Subject: [PATCH 08/12] use the correct hexutil library --- beacon-chain/rpc/beaconv1/BUILD.bazel | 2 +- beacon-chain/rpc/beaconv1/config.go | 4 ++-- beacon-chain/rpc/beaconv1/config_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/beacon-chain/rpc/beaconv1/BUILD.bazel b/beacon-chain/rpc/beaconv1/BUILD.bazel index d43ec34c4f4a..28aff268e643 100644 --- a/beacon-chain/rpc/beaconv1/BUILD.bazel +++ b/beacon-chain/rpc/beaconv1/BUILD.bazel @@ -33,12 +33,12 @@ go_library( "//proto/migration:go_default_library", "//shared/bytesutil:go_default_library", "//shared/params:go_default_library", + "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_gogo_protobuf//types:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", - "@com_github_status_im_keycard_go//hexutils:go_default_library", "@io_opencensus_go//trace:go_default_library", "@org_golang_google_grpc//codes:go_default_library", "@org_golang_google_grpc//status:go_default_library", diff --git a/beacon-chain/rpc/beaconv1/config.go b/beacon-chain/rpc/beaconv1/config.go index faf4e2f32dd6..bb798fc474cf 100644 --- a/beacon-chain/rpc/beaconv1/config.go +++ b/beacon-chain/rpc/beaconv1/config.go @@ -6,10 +6,10 @@ import ( "encoding/binary" "errors" + "github.com/ethereum/go-ethereum/common/hexutil" ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" "github.com/prysmaticlabs/prysm/shared/params" - "github.com/status-im/keycard-go/hexutils" "go.opencensus.io/trace" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -39,7 +39,7 @@ func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*e return nil, status.Errorf(codes.Internal, "could not obtain genesis fork version: %v", err) } byteAddress := [20]byte(bs.PowchainInfoFetcher.DepositContractAddress()) - hexAddress := hexutils.BytesToHex(byteAddress[:]) + hexAddress := hexutil.Encode(byteAddress[:]) return ðpb.DepositContractResponse{ Data: ðpb.DepositContract{ diff --git a/beacon-chain/rpc/beaconv1/config_test.go b/beacon-chain/rpc/beaconv1/config_test.go index a65146d2c93b..5de16ddf0ee2 100644 --- a/beacon-chain/rpc/beaconv1/config_test.go +++ b/beacon-chain/rpc/beaconv1/config_test.go @@ -26,5 +26,5 @@ func TestGetDepositContract(t *testing.T) { resp, err := s.GetDepositContract(context.Background(), &ptypes.Empty{}) require.NoError(t, err) assert.Equal(t, uint64(9), resp.Data.ChainId) - assert.Equal(t, "0102030400000000000000000000000000000000", resp.Data.Address) + assert.Equal(t, "0x0102030400000000000000000000000000000000", resp.Data.Address) } From d35dfb53adbaceca9d93c14bea6e2a7e92a66034 Mon Sep 17 00:00:00 2001 From: rkapka Date: Mon, 25 Jan 2021 09:21:31 +0100 Subject: [PATCH 09/12] read contract address from configuration --- beacon-chain/powchain/service.go | 6 ------ beacon-chain/powchain/service_test.go | 6 ------ beacon-chain/powchain/testing/faulty_mock.go | 5 ----- beacon-chain/powchain/testing/mock.go | 5 ----- beacon-chain/rpc/beaconv1/config.go | 5 +---- beacon-chain/rpc/beaconv1/config_test.go | 12 ++++-------- 6 files changed, 5 insertions(+), 34 deletions(-) diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 4815f6904c19..4d1ab8aaf976 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -80,7 +80,6 @@ type ChainStartFetcher interface { type ChainInfoFetcher interface { Eth2GenesisPowchainInfo() (uint64, *big.Int) IsConnectedToETH1() bool - DepositContractAddress() common.Address } // POWBlockFetcher defines a struct that can retrieve mainchain blocks. @@ -328,11 +327,6 @@ func (s *Service) IsConnectedToETH1() bool { return s.connectedETH1 } -// DepositContractAddress returns the address of the ETH1 deposit contract. -func (s *Service) DepositContractAddress() common.Address { - return s.depositContractAddress -} - // DepositRoot returns the Merkle root of the latest deposit trie // from the ETH1.0 deposit contract. func (s *Service) DepositRoot() [32]byte { diff --git a/beacon-chain/powchain/service_test.go b/beacon-chain/powchain/service_test.go index 2a925b7a3d0e..424b1e16bdec 100644 --- a/beacon-chain/powchain/service_test.go +++ b/beacon-chain/powchain/service_test.go @@ -584,9 +584,3 @@ func Test_batchRequestHeaders_UnderflowChecks(t *testing.T) { _, err = srv.batchRequestHeaders(start, end) require.ErrorContains(t, "cannot be >", err) } - -func TestDepositContractAddress(t *testing.T) { - s := &Service{depositContractAddress: [20]byte{1, 2, 3}} - address := s.DepositContractAddress() - assert.DeepEqual(t, s.depositContractAddress[:], address[:]) -} diff --git a/beacon-chain/powchain/testing/faulty_mock.go b/beacon-chain/powchain/testing/faulty_mock.go index 5a993a2bcb31..623ca5040be0 100644 --- a/beacon-chain/powchain/testing/faulty_mock.go +++ b/beacon-chain/powchain/testing/faulty_mock.go @@ -88,11 +88,6 @@ func (f *FaultyMockPOWChain) IsConnectedToETH1() bool { return true } -// DepositContractAddress -- -func (f *FaultyMockPOWChain) DepositContractAddress() common.Address { - return common.Address{} -} - // BlockExistsWithCache -- func (f *FaultyMockPOWChain) BlockExistsWithCache(ctx context.Context, hash common.Hash) (bool, *big.Int, error) { return f.BlockExists(ctx, hash) diff --git a/beacon-chain/powchain/testing/mock.go b/beacon-chain/powchain/testing/mock.go index 3d114281eadc..f6d4b0779da2 100644 --- a/beacon-chain/powchain/testing/mock.go +++ b/beacon-chain/powchain/testing/mock.go @@ -133,11 +133,6 @@ func (m *POWChain) IsConnectedToETH1() bool { return true } -// DepositContractAddress -- -func (m *POWChain) DepositContractAddress() common.Address { - return common.Address{1, 2, 3, 4} -} - // RPCClient defines the mock rpc client. type RPCClient struct { Backend *backends.SimulatedBackend diff --git a/beacon-chain/rpc/beaconv1/config.go b/beacon-chain/rpc/beaconv1/config.go index bb798fc474cf..06cbf85c7ef5 100644 --- a/beacon-chain/rpc/beaconv1/config.go +++ b/beacon-chain/rpc/beaconv1/config.go @@ -6,7 +6,6 @@ import ( "encoding/binary" "errors" - "github.com/ethereum/go-ethereum/common/hexutil" ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" "github.com/prysmaticlabs/prysm/shared/params" @@ -38,13 +37,11 @@ func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*e if err != nil { return nil, status.Errorf(codes.Internal, "could not obtain genesis fork version: %v", err) } - byteAddress := [20]byte(bs.PowchainInfoFetcher.DepositContractAddress()) - hexAddress := hexutil.Encode(byteAddress[:]) return ðpb.DepositContractResponse{ Data: ðpb.DepositContract{ ChainId: uint64(chainId), - Address: hexAddress, + Address: params.BeaconConfig().DepositContractAddress, }, }, nil } diff --git a/beacon-chain/rpc/beaconv1/config_test.go b/beacon-chain/rpc/beaconv1/config_test.go index 5de16ddf0ee2..398f6400db34 100644 --- a/beacon-chain/rpc/beaconv1/config_test.go +++ b/beacon-chain/rpc/beaconv1/config_test.go @@ -5,7 +5,6 @@ import ( "testing" ptypes "github.com/gogo/protobuf/types" - powchaintest "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -15,16 +14,13 @@ func TestGetDepositContract(t *testing.T) { params.SetupTestConfigCleanup(t) config := params.BeaconConfig() config.GenesisForkVersion = []byte{9, 0, 0, 0} + const address = "0x0102030400000000000000000000000000000000" + config.DepositContractAddress = address params.OverrideBeaconConfig(config) - infoFetcher := powchaintest.POWChain{} - infoFetcher.DepositContractAddress() - - s := Server{ - PowchainInfoFetcher: &infoFetcher, - } + s := Server{} resp, err := s.GetDepositContract(context.Background(), &ptypes.Empty{}) require.NoError(t, err) assert.Equal(t, uint64(9), resp.Data.ChainId) - assert.Equal(t, "0x0102030400000000000000000000000000000000", resp.Data.Address) + assert.Equal(t, address, resp.Data.Address) } From 6805cef294c281abf28a0ca42d8cc2d4a3e21ae8 Mon Sep 17 00:00:00 2001 From: rkapka Date: Mon, 25 Jan 2021 09:54:33 +0100 Subject: [PATCH 10/12] return ETH1 chain ID instead of fork version --- beacon-chain/rpc/beaconv1/config.go | 12 +----------- beacon-chain/rpc/beaconv1/config_test.go | 8 +++++--- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/beacon-chain/rpc/beaconv1/config.go b/beacon-chain/rpc/beaconv1/config.go index 06cbf85c7ef5..6b649ae4accd 100644 --- a/beacon-chain/rpc/beaconv1/config.go +++ b/beacon-chain/rpc/beaconv1/config.go @@ -1,17 +1,13 @@ package beaconv1 import ( - "bytes" "context" - "encoding/binary" "errors" ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" "github.com/prysmaticlabs/prysm/shared/params" "go.opencensus.io/trace" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) // GetForkSchedule retrieve all scheduled upcoming forks this node is aware of. @@ -32,15 +28,9 @@ func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*e ctx, span := trace.StartSpan(ctx, "beaconv1.GetDepositContract") defer span.End() - buf := bytes.NewBuffer(params.BeaconConfig().GenesisForkVersion) - chainId, err := binary.ReadUvarint(buf) - if err != nil { - return nil, status.Errorf(codes.Internal, "could not obtain genesis fork version: %v", err) - } - return ðpb.DepositContractResponse{ Data: ðpb.DepositContract{ - ChainId: uint64(chainId), + ChainId: params.BeaconConfig().DepositChainID, Address: params.BeaconConfig().DepositContractAddress, }, }, nil diff --git a/beacon-chain/rpc/beaconv1/config_test.go b/beacon-chain/rpc/beaconv1/config_test.go index 398f6400db34..eabe726ced8a 100644 --- a/beacon-chain/rpc/beaconv1/config_test.go +++ b/beacon-chain/rpc/beaconv1/config_test.go @@ -11,16 +11,18 @@ import ( ) func TestGetDepositContract(t *testing.T) { + const chainId = 99 + const address = "0x0000000000000000000000000000000000000009" params.SetupTestConfigCleanup(t) config := params.BeaconConfig() - config.GenesisForkVersion = []byte{9, 0, 0, 0} - const address = "0x0102030400000000000000000000000000000000" + config.DepositChainID = chainId + config.DepositContractAddress = address params.OverrideBeaconConfig(config) s := Server{} resp, err := s.GetDepositContract(context.Background(), &ptypes.Empty{}) require.NoError(t, err) - assert.Equal(t, uint64(9), resp.Data.ChainId) + assert.Equal(t, uint64(chainId), resp.Data.ChainId) assert.Equal(t, address, resp.Data.Address) } From 7acc27105fdee48c791450f8fa9ae2286753aa9f Mon Sep 17 00:00:00 2001 From: rkapka Date: Mon, 25 Jan 2021 10:14:10 +0100 Subject: [PATCH 11/12] gzl (I hate you) --- beacon-chain/rpc/beaconv1/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/beacon-chain/rpc/beaconv1/BUILD.bazel b/beacon-chain/rpc/beaconv1/BUILD.bazel index 28aff268e643..9f5f50638b3f 100644 --- a/beacon-chain/rpc/beaconv1/BUILD.bazel +++ b/beacon-chain/rpc/beaconv1/BUILD.bazel @@ -33,7 +33,6 @@ go_library( "//proto/migration:go_default_library", "//shared/bytesutil:go_default_library", "//shared/params:go_default_library", - "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_gogo_protobuf//types:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library", From 8b9c76e7bac9f57a0e71db654e9d2339b6e1a1b8 Mon Sep 17 00:00:00 2001 From: rkapka Date: Mon, 25 Jan 2021 17:00:06 +0100 Subject: [PATCH 12/12] remove unused ChainInfoFetcher --- beacon-chain/rpc/beaconv1/server.go | 1 - beacon-chain/rpc/service.go | 1 - 2 files changed, 2 deletions(-) diff --git a/beacon-chain/rpc/beaconv1/server.go b/beacon-chain/rpc/beaconv1/server.go index de6586b26eef..2c71fa737c63 100644 --- a/beacon-chain/rpc/beaconv1/server.go +++ b/beacon-chain/rpc/beaconv1/server.go @@ -32,7 +32,6 @@ type Server struct { ChainInfoFetcher blockchain.ChainInfoFetcher DepositFetcher depositcache.DepositFetcher BlockFetcher powchain.POWBlockFetcher - PowchainInfoFetcher powchain.ChainInfoFetcher GenesisTimeFetcher blockchain.TimeFetcher BlockReceiver blockchain.BlockReceiver StateNotifier statefeed.Notifier diff --git a/beacon-chain/rpc/service.go b/beacon-chain/rpc/service.go index 5556214f20ab..b222d09ac51d 100644 --- a/beacon-chain/rpc/service.go +++ b/beacon-chain/rpc/service.go @@ -313,7 +313,6 @@ func (s *Service) Start() { ChainStartFetcher: s.chainStartFetcher, DepositFetcher: s.depositFetcher, BlockFetcher: s.powChainService, - PowchainInfoFetcher: s.powChainService, CanonicalStateChan: s.canonicalStateChan, GenesisTimeFetcher: s.timeFetcher, StateNotifier: s.stateNotifier,