From c118266d9d7f55f01f645efc79922b2737a2697f Mon Sep 17 00:00:00 2001 From: Francisco Moura Date: Wed, 7 Aug 2024 18:04:52 -0300 Subject: [PATCH] fixup! chore(evm-reader): Make code more go-like --- internal/evmreader/evmreader.go | 42 ++++++++++---------- internal/evmreader/evmreader_test.go | 58 ++++++++++++++-------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/internal/evmreader/evmreader.go b/internal/evmreader/evmreader.go index a4f9ff912..f4eb76334 100644 --- a/internal/evmreader/evmreader.go +++ b/internal/evmreader/evmreader.go @@ -9,7 +9,7 @@ import ( "log/slog" "math/big" - "github.com/cartesi/rollups-node/internal/node/model" + . "github.com/cartesi/rollups-node/internal/node/model" "github.com/cartesi/rollups-node/pkg/contracts/inputbox" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -33,24 +33,24 @@ type InputSource interface { type EvmReaderRepository interface { InsertInputsAndUpdateLastProcessedBlock( ctx context.Context, - inputs []model.Input, + inputs []Input, blockNumber uint64, appAddress common.Address, ) error GetAllRunningApplications( ctx context.Context, - ) ([]model.Application, error) + ) ([]Application, error) GetNodeConfig( ctx context.Context, - ) (*model.NodePersistentConfig, error) + ) (*NodePersistentConfig, error) GetEpoch( ctx context.Context, indexKey uint64, appAddressKey common.Address, - ) (*model.Epoch, error) + ) (*Epoch, error) InsertEpoch( ctx context.Context, - epoch *model.Epoch, + epoch *Epoch, ) (uint64, error) } @@ -86,7 +86,7 @@ type EvmReader struct { wsClient EthWsClient inputSource InputSource repository EvmReaderRepository - config model.NodePersistentConfig + config NodePersistentConfig } func (r *EvmReader) String() string { @@ -99,7 +99,7 @@ func NewEvmReader( wsClient EthWsClient, inputSource InputSource, repository EvmReaderRepository, - config model.NodePersistentConfig, + config NodePersistentConfig, ) EvmReader { return EvmReader{ client: client, @@ -228,9 +228,9 @@ func (r *EvmReader) checkForNewInputs(ctx context.Context) error { // Group Applications that have processed til the same block height func (r *EvmReader) classifyApplicationsByLastProcessedInput( - apps []model.Application, -) map[uint64][]model.Application { - result := make(map[uint64][]model.Application) + apps []Application, +) map[uint64][]Application { + result := make(map[uint64][]Application) for _, app := range apps { result[app.LastProcessedBlock] = append(result[app.LastProcessedBlock], app) } @@ -242,18 +242,18 @@ func (r *EvmReader) classifyApplicationsByLastProcessedInput( // given default block func (r *EvmReader) fetchMostRecentHeader( ctx context.Context, - defaultBlock model.DefaultBlock, + defaultBlock DefaultBlock, ) (*types.Header, error) { var defaultBlockNumber int64 switch defaultBlock { - case model.DefaultBlockStatusPending: + case DefaultBlockStatusPending: defaultBlockNumber = rpc.PendingBlockNumber.Int64() - case model.DefaultBlockStatusLatest: + case DefaultBlockStatusLatest: defaultBlockNumber = rpc.LatestBlockNumber.Int64() - case model.DefaultBlockStatusFinalized: + case DefaultBlockStatusFinalized: defaultBlockNumber = rpc.FinalizedBlockNumber.Int64() - case model.DefaultBlockStatusSafe: + case DefaultBlockStatusSafe: defaultBlockNumber = rpc.SafeBlockNumber.Int64() default: return nil, fmt.Errorf("Default block '%v' not supported", defaultBlock) @@ -278,14 +278,14 @@ func (r *EvmReader) readInputs( ctx context.Context, startBlock uint64, endBlock uint64, - apps []model.Application, + apps []Application, ) error { filter := []common.Address{} - var inputsMap = make(map[common.Address][]model.Input) + var inputsMap = make(map[common.Address][]Input) for _, app := range apps { filter = append(filter, app.ContractAddress) - inputsMap[app.ContractAddress] = []model.Input{} + inputsMap[app.ContractAddress] = []Input{} } opts := bind.FilterOpts{ @@ -304,9 +304,9 @@ func (r *EvmReader) readInputs( for _, event := range inputsEvents { slog.Debug("received input ", "app", event.AppContract, "index", event.Index) - input := model.Input{ + input := Input{ Index: event.Index.Uint64(), - CompletionStatus: model.InputStatusNone, + CompletionStatus: InputStatusNone, RawData: event.Input, BlockNumber: event.Raw.BlockNumber, AppAddress: event.AppContract, diff --git a/internal/evmreader/evmreader_test.go b/internal/evmreader/evmreader_test.go index ab859880d..567be22c8 100644 --- a/internal/evmreader/evmreader_test.go +++ b/internal/evmreader/evmreader_test.go @@ -14,7 +14,7 @@ import ( "testing" "time" - "github.com/cartesi/rollups-node/internal/node/model" + . "github.com/cartesi/rollups-node/internal/node/model" "github.com/cartesi/rollups-node/pkg/contracts/inputbox" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -116,8 +116,8 @@ func (s *EvmReaderSuite) SetupTest() { s.wsClient, s.inputBox, s.repository, - model.NodePersistentConfig{ - DefaultBlock: model.DefaultBlockStatusLatest, + NodePersistentConfig{ + DefaultBlock: DefaultBlockStatusLatest, InputBoxDeploymentBlock: 0, }, ) @@ -178,9 +178,9 @@ func (s *EvmReaderSuite) TestItReadsInputsFromNewBlocks() { &wsClient, s.inputBox, s.repository, - model.NodePersistentConfig{ + NodePersistentConfig{ InputBoxDeploymentBlock: 0x10, - DefaultBlock: model.DefaultBlockStatusLatest, + DefaultBlock: DefaultBlockStatusLatest, }, ) @@ -189,14 +189,14 @@ func (s *EvmReaderSuite) TestItReadsInputsFromNewBlocks() { s.repository.On( "GetAllRunningApplications", mock.Anything, - ).Return([]model.Application{{ + ).Return([]Application{{ ContractAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), LastProcessedBlock: 0x00, }}, nil).Once() s.repository.On( "GetAllRunningApplications", mock.Anything, - ).Return([]model.Application{{ + ).Return([]Application{{ ContractAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), LastProcessedBlock: 0x11, }}, nil).Once() @@ -286,9 +286,9 @@ func (s *EvmReaderSuite) TestItUpdatesLastProcessedBlockWhenThereIsNoInputs() { &wsClient, s.inputBox, s.repository, - model.NodePersistentConfig{ + NodePersistentConfig{ InputBoxDeploymentBlock: 0x10, - DefaultBlock: model.DefaultBlockStatusLatest, + DefaultBlock: DefaultBlockStatusLatest, }, ) @@ -297,14 +297,14 @@ func (s *EvmReaderSuite) TestItUpdatesLastProcessedBlockWhenThereIsNoInputs() { s.repository.On( "GetAllRunningApplications", mock.Anything, - ).Return([]model.Application{{ + ).Return([]Application{{ ContractAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), LastProcessedBlock: 0x00, }}, nil).Once() s.repository.On( "GetAllRunningApplications", mock.Anything, - ).Return([]model.Application{{ + ).Return([]Application{{ ContractAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), LastProcessedBlock: 0x11, }}, nil).Once() @@ -394,9 +394,9 @@ func (s *EvmReaderSuite) TestItReadsMultipleInputsFromSingleNewBlock() { &wsClient, s.inputBox, s.repository, - model.NodePersistentConfig{ + NodePersistentConfig{ InputBoxDeploymentBlock: 0x10, - DefaultBlock: model.DefaultBlockStatusLatest, + DefaultBlock: DefaultBlockStatusLatest, }, ) @@ -429,7 +429,7 @@ func (s *EvmReaderSuite) TestItReadsMultipleInputsFromSingleNewBlock() { s.repository.On( "GetAllRunningApplications", mock.Anything, - ).Return([]model.Application{{ + ).Return([]Application{{ ContractAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), LastProcessedBlock: 0x12, }}, nil).Once() @@ -440,9 +440,9 @@ func (s *EvmReaderSuite) TestItReadsMultipleInputsFromSingleNewBlock() { mock.Anything, mock.Anything, ).Once().Run(func(arguments mock.Arguments) { - var inputs []model.Input + var inputs []Input obj := arguments.Get(1) - inputs, ok := obj.([]model.Input) + inputs, ok := obj.([]Input) s.Require().True(ok) s.Assert().Equal(2, len(inputs)) }).Return(nil) @@ -484,9 +484,9 @@ func (s *EvmReaderSuite) TestItStartsWhenLasProcessedBlockIsTheMostRecentBlock() &wsClient, s.inputBox, s.repository, - model.NodePersistentConfig{ + NodePersistentConfig{ InputBoxDeploymentBlock: 0x10, - DefaultBlock: model.DefaultBlockStatusLatest, + DefaultBlock: DefaultBlockStatusLatest, }, ) @@ -503,7 +503,7 @@ func (s *EvmReaderSuite) TestItStartsWhenLasProcessedBlockIsTheMostRecentBlock() s.repository.On( "GetAllRunningApplications", mock.Anything, - ).Return([]model.Application{{ + ).Return([]Application{{ ContractAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), LastProcessedBlock: 0x11, }}, nil).Once() @@ -679,12 +679,12 @@ func newMockRepository() *MockRepository { mock.Anything, mock.Anything, mock.Anything).Return( - &model.Epoch{ + &Epoch{ Id: 1, Index: 0, FirstBlock: 0, LastBlock: math.MaxUint64, - Status: model.EpochStatusOpen, + Status: EpochStatusOpen, AppAddress: common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E"), ClaimHash: nil, TransactionHash: nil, @@ -708,7 +708,7 @@ func (m *MockRepository) Unset(methodName string) { func (m *MockRepository) InsertInputsAndUpdateLastProcessedBlock( ctx context.Context, - inputs []model.Input, + inputs []Input, blockNumber uint64, appAddress common.Address, ) error { @@ -718,30 +718,30 @@ func (m *MockRepository) InsertInputsAndUpdateLastProcessedBlock( func (m *MockRepository) GetAllRunningApplications( ctx context.Context, -) ([]model.Application, error) { +) ([]Application, error) { args := m.Called(ctx) - return args.Get(0).([]model.Application), args.Error(1) + return args.Get(0).([]Application), args.Error(1) } func (m *MockRepository) GetNodeConfig( ctx context.Context, -) (*model.NodePersistentConfig, error) { +) (*NodePersistentConfig, error) { args := m.Called(ctx) - return args.Get(0).(*model.NodePersistentConfig), args.Error(1) + return args.Get(0).(*NodePersistentConfig), args.Error(1) } func (m *MockRepository) GetEpoch( ctx context.Context, index uint64, appAddress common.Address, -) (*model.Epoch, error) { +) (*Epoch, error) { args := m.Called(ctx) - return args.Get(0).(*model.Epoch), args.Error(1) + return args.Get(0).(*Epoch), args.Error(1) } func (m *MockRepository) InsertEpoch( ctx context.Context, - epoch *model.Epoch, + epoch *Epoch, ) (uint64, error) { args := m.Called(ctx) return args.Get(0).(uint64), args.Error(1)