Skip to content

Commit

Permalink
fixup! feat(evm-reader): Read claim acceptance
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Sep 5, 2024
1 parent e37c2e3 commit f6d934d
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 11 deletions.
7 changes: 5 additions & 2 deletions internal/evmreader/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (r *EvmReader) readAndUpdateClaims(
}

// Check events against Epochs
APP_LOOP:
for app, claimAcceptances := range appClaimAcceptanceEventMap {

epochs := []*Epoch{}
Expand All @@ -110,11 +111,12 @@ func (r *EvmReader) readAndUpdateClaims(
if err != nil {
slog.Error("Error retrieving previous submitted claims",
"app", app, "error", err)
continue APP_LOOP
}

for _, previousClaim := range previousClaims {
previousClaim.Status = EpochStatusClaimRejected
epochs = append(epochs, &previousClaim)
epochs = append(epochs, previousClaim)
slog.Warn("Claim rejected",
"app", app,
"lastBlock", previousClaim.LastBlock,
Expand All @@ -129,6 +131,7 @@ func (r *EvmReader) readAndUpdateClaims(
app)
if err != nil {
slog.Error("Error retrieving claim", "app", app, "error", err)
continue APP_LOOP
}

// Check Claim
Expand All @@ -137,7 +140,7 @@ func (r *EvmReader) readAndUpdateClaims(
"app", app,
"claim last block", claimAcceptance.LastProcessedBlockNumber,
"hash", claimAcceptance.Claim)
continue
continue APP_LOOP
}

// Update claim status
Expand Down
294 changes: 293 additions & 1 deletion internal/evmreader/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package evmreader

import (
"context"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -235,7 +237,7 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() {
mock.Anything,
mock.Anything,
mock.Anything,
).Return([]Epoch{*claim0}, nil)
).Return([]*Epoch{claim0}, nil)

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
Expand Down Expand Up @@ -298,3 +300,293 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() {
)

}

func (s *EvmReaderSuite) TestCheckClaimFails() {
s.Run("whenRetrievePreviousEpochsFails", func() {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

appAddress := common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E")

// Contract Factory

consensusContract := &MockIConsensusContract{}

contractFactory := newEmvReaderContractFactory()

contractFactory.Unset("NewIConsensus")
contractFactory.On("NewIConsensus",
mock.Anything,
).Return(consensusContract, nil)

//New EVM Reader
wsClient := FakeWSEhtClient{}
evmReader := NewEvmReader(
s.client,
&wsClient,
s.inputBox,
s.repository,
0x00,
DefaultBlockStatusLatest,
contractFactory,
)

// Prepare Claims Acceptance Events

claimEvent0 := &iconsensus.IConsensusClaimAcceptance{
AppContract: appAddress,
LastProcessedBlockNumber: big.NewInt(3),
Claim: common.HexToHash("0xdeadbeef"),
}

claimEvents := []*iconsensus.IConsensusClaimAcceptance{claimEvent0}
consensusContract.On("RetrieveClaimAcceptanceEvents",
mock.Anything,
mock.Anything,
).Return(claimEvents, nil).Once()
consensusContract.On("RetrieveClaimAcceptanceEvents",
mock.Anything,
mock.Anything,
).Return([]*iconsensus.IConsensusClaimAcceptance{}, nil)

// Epoch Length
consensusContract.On("GetEpochLength",
mock.Anything,
).Return(big.NewInt(1), nil).Once()

// Prepare repository
s.repository.Unset("GetAllRunningApplications")
s.repository.On(
"GetAllRunningApplications",
mock.Anything,
).Return([]Application{{
ContractAddress: appAddress,
IConsensusAddress: common.HexToAddress("0xdeadbeef"),
LastClaimCheckBlock: 0x10,
}}, nil).Once()
s.repository.On(
"GetAllRunningApplications",
mock.Anything,
).Return([]Application{{
ContractAddress: appAddress,
IConsensusAddress: common.HexToAddress("0xdeadbeef"),
LastClaimCheckBlock: 0x11,
}}, nil).Once()

claim1Hash := common.HexToHash("0xdeadbeef")
claim1 := &Epoch{
Index: 3,
FirstBlock: 3,
LastBlock: 3,
AppAddress: appAddress,
Status: EpochStatusClaimSubmitted,
ClaimHash: &claim1Hash,
}

s.repository.Unset("GetEpoch")
s.repository.On("GetEpoch",
mock.Anything,
mock.Anything,
mock.Anything).Return(claim1, nil)

s.repository.Unset("GetPreviousSubmittedClaims")
s.repository.On("GetPreviousSubmittedClaims",
mock.Anything,
mock.Anything,
mock.Anything,
).Return([]*Epoch{}, fmt.Errorf("No previous epochs for you"))

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)

//No Inputs
s.inputBox.Unset("RetrieveInputs")
s.inputBox.On("RetrieveInputs",
mock.Anything,
mock.Anything,
mock.Anything,
).Return([]inputbox.InputBoxInputAdded{}, nil)

// Prepare Client
s.client.Unset("HeaderByNumber")
s.client.On(
"HeaderByNumber",
mock.Anything,
mock.Anything,
).Return(&header0, nil).Once()

// Start service
ready := make(chan struct{}, 1)
errChannel := make(chan error, 1)

go func() {
errChannel <- evmReader.Run(ctx, ready)
}()

select {
case <-ready:
break
case err := <-errChannel:
s.FailNow("unexpected error signal", err)
}

wsClient.fireNewHead(&header0)
time.Sleep(1 * time.Second)

s.repository.AssertNumberOfCalls(
s.T(),
"StoreClaimsTransaction",
0,
)

})

s.Run("whenGetEpochsFails", func() {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

appAddress := common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E")

// Contract Factory

consensusContract := &MockIConsensusContract{}

contractFactory := newEmvReaderContractFactory()

contractFactory.Unset("NewIConsensus")
contractFactory.On("NewIConsensus",
mock.Anything,
).Return(consensusContract, nil)

//New EVM Reader
wsClient := FakeWSEhtClient{}
evmReader := NewEvmReader(
s.client,
&wsClient,
s.inputBox,
s.repository,
0x00,
DefaultBlockStatusLatest,
contractFactory,
)

// Prepare Claims Acceptance Events

claimEvent0 := &iconsensus.IConsensusClaimAcceptance{
AppContract: appAddress,
LastProcessedBlockNumber: big.NewInt(3),
Claim: common.HexToHash("0xdeadbeef"),
}

claimEvents := []*iconsensus.IConsensusClaimAcceptance{claimEvent0}
consensusContract.On("RetrieveClaimAcceptanceEvents",
mock.Anything,
mock.Anything,
).Return(claimEvents, nil).Once()
consensusContract.On("RetrieveClaimAcceptanceEvents",
mock.Anything,
mock.Anything,
).Return([]*iconsensus.IConsensusClaimAcceptance{}, nil)

// Epoch Length
consensusContract.On("GetEpochLength",
mock.Anything,
).Return(big.NewInt(1), nil).Once()

// Prepare repository
s.repository.Unset("GetAllRunningApplications")
s.repository.On(
"GetAllRunningApplications",
mock.Anything,
).Return([]Application{{
ContractAddress: appAddress,
IConsensusAddress: common.HexToAddress("0xdeadbeef"),
LastClaimCheckBlock: 0x10,
}}, nil).Once()
s.repository.On(
"GetAllRunningApplications",
mock.Anything,
).Return([]Application{{
ContractAddress: appAddress,
IConsensusAddress: common.HexToAddress("0xdeadbeef"),
LastClaimCheckBlock: 0x11,
}}, nil).Once()

claim0Hash := common.HexToHash("0xdeadbeef")
claim0 := &Epoch{
Index: 1,
FirstBlock: 1,
LastBlock: 1,
AppAddress: appAddress,
Status: EpochStatusClaimSubmitted,
ClaimHash: &claim0Hash,
}

s.repository.Unset("GetEpoch")
s.repository.On("GetEpoch",
mock.Anything,
mock.Anything,
mock.Anything).Return(nil, fmt.Errorf("No epoch for you"))

s.repository.Unset("GetPreviousSubmittedClaims")
s.repository.On("GetPreviousSubmittedClaims",
mock.Anything,
mock.Anything,
mock.Anything,
).Return([]*Epoch{claim0}, nil)

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)

//No Inputs
s.inputBox.Unset("RetrieveInputs")
s.inputBox.On("RetrieveInputs",
mock.Anything,
mock.Anything,
mock.Anything,
).Return([]inputbox.InputBoxInputAdded{}, nil)

// Prepare Client
s.client.Unset("HeaderByNumber")
s.client.On(
"HeaderByNumber",
mock.Anything,
mock.Anything,
).Return(&header0, nil).Once()

// Start service
ready := make(chan struct{}, 1)
errChannel := make(chan error, 1)

go func() {
errChannel <- evmReader.Run(ctx, ready)
}()

select {
case <-ready:
break
case err := <-errChannel:
s.FailNow("unexpected error signal", err)
}

wsClient.fireNewHead(&header0)
time.Sleep(1 * time.Second)

s.repository.AssertNumberOfCalls(
s.T(),
"StoreClaimsTransaction",
0,
)

})
}
2 changes: 1 addition & 1 deletion internal/evmreader/evmreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type EvmReaderRepository interface {
GetAllRunningApplications(ctx context.Context) ([]Application, error)
GetNodeConfig(ctx context.Context) (*NodePersistentConfig, error)
GetEpoch(ctx context.Context, indexKey uint64, appAddressKey Address) (*Epoch, error)
GetPreviousSubmittedClaims(ctx context.Context, app Address, lastBlock uint64) ([]Epoch, error)
GetPreviousSubmittedClaims(ctx context.Context, app Address, lastBlock uint64) ([]*Epoch, error)
StoreClaimsTransaction(ctx context.Context,
app Address,
claims []*Epoch,
Expand Down
14 changes: 11 additions & 3 deletions internal/evmreader/evmreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ func (m *MockRepository) GetEpoch(
appAddress common.Address,
) (*Epoch, error) {
args := m.Called(ctx, index, appAddress)
return args.Get(0).(*Epoch), args.Error(1)
obj := args.Get(0)
if obj == nil {
return nil, args.Error(1)
}
return obj.(*Epoch), args.Error(1)
}

func (m *MockRepository) InsertEpoch(
Expand All @@ -506,9 +510,13 @@ func (m *MockRepository) GetPreviousSubmittedClaims(
ctx context.Context,
app Address,
lastBlock uint64,
) ([]Epoch, error) {
) ([]*Epoch, error) {
args := m.Called(ctx, app, lastBlock)
return args.Get(0).([]Epoch), args.Error(1)
obj := args.Get(0)
if obj == nil {
return nil, args.Error(1)
}
return obj.([]*Epoch), args.Error(1)

}
func (m *MockRepository) StoreClaimsTransaction(ctx context.Context,
Expand Down
Loading

0 comments on commit f6d934d

Please sign in to comment.