From ced24892a59c2a1fc4e13b90297323b015bd6a27 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 18 Feb 2022 22:13:31 +0800 Subject: [PATCH] Cleanup Powchain Service (#10259) * add cleanup * gaz * Fix test build --- beacon-chain/deterministic-genesis/service.go | 5 -- beacon-chain/powchain/log_processing.go | 10 ++-- beacon-chain/powchain/log_processing_test.go | 6 +-- beacon-chain/powchain/service.go | 46 ------------------- beacon-chain/powchain/testing/BUILD.bazel | 1 - .../powchain/testing/mock_faulty_powchain.go | 21 --------- .../powchain/testing/mock_powchain.go | 17 ------- 7 files changed, 8 insertions(+), 98 deletions(-) diff --git a/beacon-chain/deterministic-genesis/service.go b/beacon-chain/deterministic-genesis/service.go index 5df07463da..661c70164e 100644 --- a/beacon-chain/deterministic-genesis/service.go +++ b/beacon-chain/deterministic-genesis/service.go @@ -120,11 +120,6 @@ func (_ *Service) AllDeposits(_ context.Context, _ *big.Int) []*ethpb.Deposit { return []*ethpb.Deposit{} } -// ChainStartDeposits mocks out the powchain functionality for interop. -func (s *Service) ChainStartDeposits() []*ethpb.Deposit { - return s.chainStartDeposits -} - // ChainStartEth1Data mocks out the powchain functionality for interop. func (_ *Service) ChainStartEth1Data() *ethpb.Eth1Data { return ðpb.Eth1Data{} diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index 53824e40bc..60263e0d76 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -428,27 +428,27 @@ func (s *Service) requestBatchedHeadersAndLogs(ctx context.Context) error { } func (s *Service) retrieveBlockHashAndTime(ctx context.Context, blkNum *big.Int) ([32]byte, uint64, error) { - hash, err := s.BlockHashByHeight(ctx, blkNum) + bHash, err := s.BlockHashByHeight(ctx, blkNum) if err != nil { return [32]byte{}, 0, errors.Wrap(err, "could not get eth1 block hash") } - if hash == [32]byte{} { + if bHash == [32]byte{} { return [32]byte{}, 0, errors.Wrap(err, "got empty block hash") } timeStamp, err := s.BlockTimeByHeight(ctx, blkNum) if err != nil { return [32]byte{}, 0, errors.Wrap(err, "could not get block timestamp") } - return hash, timeStamp, nil + return bHash, timeStamp, nil } // checkBlockNumberForChainStart checks the given block number for if chainstart has occurred. func (s *Service) checkBlockNumberForChainStart(ctx context.Context, blkNum *big.Int) error { - hash, timeStamp, err := s.retrieveBlockHashAndTime(ctx, blkNum) + bHash, timeStamp, err := s.retrieveBlockHashAndTime(ctx, blkNum) if err != nil { return err } - s.checkForChainstart(ctx, hash, blkNum, timeStamp) + s.checkForChainstart(ctx, bHash, blkNum, timeStamp) return nil } diff --git a/beacon-chain/powchain/log_processing_test.go b/beacon-chain/powchain/log_processing_test.go index a870b85879..7180715113 100644 --- a/beacon-chain/powchain/log_processing_test.go +++ b/beacon-chain/powchain/log_processing_test.go @@ -333,7 +333,7 @@ func TestProcessETH2GenesisLog(t *testing.T) { err = web3Service.ProcessETH1Block(context.Background(), big.NewInt(int64(logs[len(logs)-1].BlockNumber))) require.NoError(t, err) - cachedDeposits := web3Service.ChainStartDeposits() + cachedDeposits := web3Service.chainStartData.ChainstartDeposits require.Equal(t, depositsReqForChainStart, len(cachedDeposits)) // Receive the chain started event. @@ -425,7 +425,7 @@ func TestProcessETH2GenesisLog_CorrectNumOfDeposits(t *testing.T) { err = web3Service.processPastLogs(context.Background()) require.NoError(t, err) - cachedDeposits := web3Service.ChainStartDeposits() + cachedDeposits := web3Service.chainStartData.ChainstartDeposits requiredDepsForChainstart := depositsReqForChainStart + depositOffset require.Equal(t, requiredDepsForChainstart, len(cachedDeposits), "Did not cache the chain start deposits correctly") @@ -529,7 +529,7 @@ func TestProcessETH2GenesisLog_LargePeriodOfNoLogs(t *testing.T) { err = web3Service.processPastLogs(context.Background()) require.NoError(t, err) - cachedDeposits := web3Service.ChainStartDeposits() + cachedDeposits := web3Service.chainStartData.ChainstartDeposits require.Equal(t, totalNumOfDeposits, len(cachedDeposits), "Did not cache the chain start deposits correctly") // Receive the chain started event. diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 24d0e1c4d8..5c5357df7c 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -81,7 +81,6 @@ var ( // ChainStartFetcher retrieves information pertaining to the chain start event // of the beacon chain for usage across various services. type ChainStartFetcher interface { - ChainStartDeposits() []*ethpb.Deposit ChainStartEth1Data() *ethpb.Eth1Data PreGenesisState() state.BeaconState ClearPreGenesisData() @@ -271,12 +270,6 @@ func (s *Service) Stop() error { return nil } -// ChainStartDeposits returns a slice of validator deposit data processed -// by the deposit contract and cached in the powchain service. -func (s *Service) ChainStartDeposits() []*ethpb.Deposit { - return s.chainStartData.ChainstartDeposits -} - // ClearPreGenesisData clears out the stored chainstart deposits and beacon state. func (s *Service) ClearPreGenesisData() { s.chainStartData.ChainstartDeposits = []*ethpb.Deposit{} @@ -382,45 +375,6 @@ func (s *Service) ETH1ConnectionErrors() []error { return errs } -// DepositRoot returns the Merkle root of the latest deposit trie -// from the ETH1.0 deposit contract. -func (s *Service) DepositRoot() [32]byte { - return s.depositTrie.HashTreeRoot() -} - -// DepositTrie returns the sparse Merkle trie used for storing -// deposits from the ETH1.0 deposit contract. -func (s *Service) DepositTrie() *trie.SparseMerkleTrie { - return s.depositTrie -} - -// LatestBlockHeight in the ETH1.0 chain. -func (s *Service) LatestBlockHeight() *big.Int { - return big.NewInt(int64(s.latestEth1Data.BlockHeight)) -} - -// LatestBlockHash in the ETH1.0 chain. -func (s *Service) LatestBlockHash() common.Hash { - return bytesutil.ToBytes32(s.latestEth1Data.BlockHash) -} - -// AreAllDepositsProcessed determines if all the logs from the deposit contract -// are processed. -func (s *Service) AreAllDepositsProcessed() (bool, error) { - s.processingLock.RLock() - defer s.processingLock.RUnlock() - countByte, err := s.depositContractCaller.GetDepositCount(&bind.CallOpts{}) - if err != nil { - return false, errors.Wrap(err, "could not get deposit count") - } - count := bytesutil.FromBytes8(countByte) - deposits := s.cfg.depositCache.AllDeposits(s.ctx, nil) - if count != uint64(len(deposits)) { - return false, nil - } - return true, nil -} - // refers to the latest eth1 block which follows the condition: eth1_timestamp + // SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE <= current_unix_time func (s *Service) followBlockHeight(_ context.Context) (uint64, error) { diff --git a/beacon-chain/powchain/testing/BUILD.bazel b/beacon-chain/powchain/testing/BUILD.bazel index c6017fbb7a..fd77af5c21 100644 --- a/beacon-chain/powchain/testing/BUILD.bazel +++ b/beacon-chain/powchain/testing/BUILD.bazel @@ -16,7 +16,6 @@ go_library( "//beacon-chain/powchain/types:go_default_library", "//beacon-chain/state:go_default_library", "//beacon-chain/state/v1:go_default_library", - "//container/trie:go_default_library", "//encoding/bytesutil:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "@com_github_ethereum_go_ethereum//accounts/abi/bind/backends:go_default_library", diff --git a/beacon-chain/powchain/testing/mock_faulty_powchain.go b/beacon-chain/powchain/testing/mock_faulty_powchain.go index 28a7b33be1..ea111fa65d 100644 --- a/beacon-chain/powchain/testing/mock_faulty_powchain.go +++ b/beacon-chain/powchain/testing/mock_faulty_powchain.go @@ -10,7 +10,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/powchain/types" "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" - "github.com/prysmaticlabs/prysm/container/trie" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) @@ -25,11 +24,6 @@ func (_ *FaultyMockPOWChain) Eth2GenesisPowchainInfo() (uint64, *big.Int) { return 0, big.NewInt(0) } -// LatestBlockHeight -- -func (_ *FaultyMockPOWChain) LatestBlockHeight() *big.Int { - return big.NewInt(0) -} - // BlockExists -- func (f *FaultyMockPOWChain) BlockExists(_ context.Context, _ common.Hash) (bool, *big.Int, error) { if f.HashesByHeight == nil { @@ -54,21 +48,6 @@ func (_ *FaultyMockPOWChain) BlockByTimestamp(_ context.Context, _ uint64) (*typ return &types.HeaderInfo{Number: big.NewInt(0)}, nil } -// DepositRoot -- -func (_ *FaultyMockPOWChain) DepositRoot() [32]byte { - return [32]byte{} -} - -// DepositTrie -- -func (_ *FaultyMockPOWChain) DepositTrie() *trie.SparseMerkleTrie { - return &trie.SparseMerkleTrie{} -} - -// ChainStartDeposits -- -func (_ *FaultyMockPOWChain) ChainStartDeposits() []*ethpb.Deposit { - return []*ethpb.Deposit{} -} - // ChainStartEth1Data -- func (_ *FaultyMockPOWChain) ChainStartEth1Data() *ethpb.Eth1Data { return ðpb.Eth1Data{} diff --git a/beacon-chain/powchain/testing/mock_powchain.go b/beacon-chain/powchain/testing/mock_powchain.go index 706ec12729..9bcf9f2d03 100644 --- a/beacon-chain/powchain/testing/mock_powchain.go +++ b/beacon-chain/powchain/testing/mock_powchain.go @@ -16,7 +16,6 @@ import ( "github.com/prysmaticlabs/prysm/async/event" "github.com/prysmaticlabs/prysm/beacon-chain/powchain/types" "github.com/prysmaticlabs/prysm/beacon-chain/state" - "github.com/prysmaticlabs/prysm/container/trie" "github.com/prysmaticlabs/prysm/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) @@ -58,11 +57,6 @@ func (m *POWChain) Eth2GenesisPowchainInfo() (uint64, *big.Int) { return uint64(GenesisTime), blk } -// DepositTrie -- -func (_ *POWChain) DepositTrie() *trie.SparseMerkleTrie { - return &trie.SparseMerkleTrie{} -} - // BlockExists -- func (m *POWChain) BlockExists(_ context.Context, hash common.Hash) (bool, *big.Int, error) { // Reverse the map of heights by hash. @@ -107,17 +101,6 @@ func (m *POWChain) BlockByTimestamp(_ context.Context, time uint64) (*types.Head return &types.HeaderInfo{Number: chosenNumber, Time: chosenTime}, nil } -// DepositRoot -- -func (_ *POWChain) DepositRoot() [32]byte { - root := []byte("depositroot") - return bytesutil.ToBytes32(root) -} - -// ChainStartDeposits -- -func (_ *POWChain) ChainStartDeposits() []*ethpb.Deposit { - return []*ethpb.Deposit{} -} - // ChainStartEth1Data -- func (m *POWChain) ChainStartEth1Data() *ethpb.Eth1Data { return m.Eth1Data