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 3, 2024
1 parent 3ea3966 commit 790e4aa
Show file tree
Hide file tree
Showing 7 changed files with 893 additions and 737 deletions.
32 changes: 8 additions & 24 deletions internal/evmreader/claimreader.go → internal/evmreader/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func (r *EvmReader) checkForClaimStatus(
ctx context.Context,
apps []Application,
apps []application,
mostRecentBlockNumber uint64,
) {

Expand Down Expand Up @@ -66,7 +66,7 @@ func (r *EvmReader) checkForClaimStatus(

func (r *EvmReader) readAndUpdateClaims(
ctx context.Context,
apps []Application,
apps []application,
lastClaimCheck, mostRecentBlockNumber uint64,
) {

Expand All @@ -80,14 +80,12 @@ func (r *EvmReader) readAndUpdateClaims(

appAddresses := appToAddresses(apps)

consensusContract, err := r.contractFactory.NewIConsensus(iConsensusAddress)
if err != nil {
slog.Error("Error instantiating IConsensus",
"apps", apps,
"IConsensus", iConsensusAddress,
"error", err)
// All these apps shares the same IConsensus
// So we can grab the first one
if len(apps) == 0 {
continue
}
consensusContract := apps[0].consensusContract

// Retrieve Claim Acceptance Events from blockchain
appClaimAcceptanceEventMap, err := r.readClaimAcceptanceFromBlockchain(
Expand Down Expand Up @@ -204,27 +202,13 @@ func (r *EvmReader) readClaimAcceptanceFromBlockchain(
return appClaimAcceptanceMap, nil
}

// Index applications given a key extractor function
func indexApps[K comparable](
keyExtractor func(Application) K,
apps []Application,
) map[K][]Application {

result := make(map[K][]Application)
for _, item := range apps {
key := keyExtractor(item)
result[key] = append(result[key], item)
}
return result
}

// LastClaimCheck key extractor function intended to be used with `indexApps` function
func keyByLastClaimCheck(app Application) uint64 {
func keyByLastClaimCheck(app application) uint64 {
return app.LastClaimCheckBlock
}

// IConsensus address key extractor function intended to be used with `indexApps` function
func keyByIConsensus(app Application) Address {
func keyByIConsensus(app application) Address {
return app.IConsensusAddress
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,53 @@ func (s *EvmReaderSuite) TestNoClaimsAcceptance() {
LastClaimCheckBlock: 0x11,
}}, nil).Once()

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
).Once().Run(func(arguments mock.Arguments) {
obj := arguments.Get(1)
claims, ok := obj.([]*Epoch)
s.Require().True(ok)
s.Require().Equal(0, len(claims))

}).Return(nil)

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
).Once().Run(func(arguments mock.Arguments) {
obj := arguments.Get(1)
claims, ok := obj.([]*Epoch)
s.Require().True(ok)
s.Require().Equal(0, len(claims))

obj = arguments.Get(2)
lastClaimCheck, ok := obj.(uint64)
s.Require().True(ok)
s.Require().Equal(uint64(17), lastClaimCheck)

}).Return(nil)
s.repository.On("StoreClaimsTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
).Once().Run(func(arguments mock.Arguments) {
obj := arguments.Get(1)
claims, ok := obj.([]*Epoch)
s.Require().True(ok)
s.Require().Equal(0, len(claims))

obj = arguments.Get(2)
lastClaimCheck, ok := obj.(uint64)
s.Require().True(ok)
s.Require().Equal(uint64(18), lastClaimCheck)

}).Return(nil)

//No Inputs
s.inputBox.Unset("RetrieveInputs")
s.inputBox.On("RetrieveInputs",
Expand Down Expand Up @@ -101,7 +148,7 @@ func (s *EvmReaderSuite) TestNoClaimsAcceptance() {

}

func (s *EvmReaderSuite) TestReadClaimsAcceptance() {
func (s *EvmReaderSuite) TestReadClaimAcceptance() {

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

Expand Down Expand Up @@ -240,16 +287,6 @@ func (s *EvmReaderSuite) TestReadClaimsAcceptance() {
mock.Anything,
mock.Anything,
).Return(&header0, nil).Once()
s.client.On(
"HeaderByNumber",
mock.Anything,
mock.Anything,
).Return(&header1, nil).Once()
s.client.On(
"HeaderByNumber",
mock.Anything,
mock.Anything,
).Return(&header2, nil).Once()

// Start service
ready := make(chan struct{}, 1)
Expand Down
Loading

0 comments on commit 790e4aa

Please sign in to comment.