Skip to content

Commit

Permalink
fixup! chore(evm-reader): Make code more go-like
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Aug 9, 2024
1 parent 0e93cbd commit f277774
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
42 changes: 21 additions & 21 deletions internal/evmreader/evmreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand Down Expand Up @@ -86,7 +86,7 @@ type EvmReader struct {
wsClient EthWsClient
inputSource InputSource
repository EvmReaderRepository
config model.NodePersistentConfig
config NodePersistentConfig
}

func (r *EvmReader) String() string {
Expand All @@ -99,7 +99,7 @@ func NewEvmReader(
wsClient EthWsClient,
inputSource InputSource,
repository EvmReaderRepository,
config model.NodePersistentConfig,
config NodePersistentConfig,
) EvmReader {
return EvmReader{
client: client,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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,
Expand Down
58 changes: 29 additions & 29 deletions internal/evmreader/evmreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -116,8 +116,8 @@ func (s *EvmReaderSuite) SetupTest() {
s.wsClient,
s.inputBox,
s.repository,
model.NodePersistentConfig{
DefaultBlock: model.DefaultBlockStatusLatest,
NodePersistentConfig{
DefaultBlock: DefaultBlockStatusLatest,
InputBoxDeploymentBlock: 0,
},
)
Expand Down Expand Up @@ -178,9 +178,9 @@ func (s *EvmReaderSuite) TestItReadsInputsFromNewBlocks() {
&wsClient,
s.inputBox,
s.repository,
model.NodePersistentConfig{
NodePersistentConfig{
InputBoxDeploymentBlock: 0x10,
DefaultBlock: model.DefaultBlockStatusLatest,
DefaultBlock: DefaultBlockStatusLatest,
},
)

Expand All @@ -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()
Expand Down Expand Up @@ -286,9 +286,9 @@ func (s *EvmReaderSuite) TestItUpdatesLastProcessedBlockWhenThereIsNoInputs() {
&wsClient,
s.inputBox,
s.repository,
model.NodePersistentConfig{
NodePersistentConfig{
InputBoxDeploymentBlock: 0x10,
DefaultBlock: model.DefaultBlockStatusLatest,
DefaultBlock: DefaultBlockStatusLatest,
},
)

Expand All @@ -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()
Expand Down Expand Up @@ -394,9 +394,9 @@ func (s *EvmReaderSuite) TestItReadsMultipleInputsFromSingleNewBlock() {
&wsClient,
s.inputBox,
s.repository,
model.NodePersistentConfig{
NodePersistentConfig{
InputBoxDeploymentBlock: 0x10,
DefaultBlock: model.DefaultBlockStatusLatest,
DefaultBlock: DefaultBlockStatusLatest,
},
)

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -484,9 +484,9 @@ func (s *EvmReaderSuite) TestItStartsWhenLasProcessedBlockIsTheMostRecentBlock()
&wsClient,
s.inputBox,
s.repository,
model.NodePersistentConfig{
NodePersistentConfig{
InputBoxDeploymentBlock: 0x10,
DefaultBlock: model.DefaultBlockStatusLatest,
DefaultBlock: DefaultBlockStatusLatest,
},
)

Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit f277774

Please sign in to comment.