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 f6d934d commit 9692f4d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 88 deletions.
51 changes: 26 additions & 25 deletions internal/evmreader/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,67 +104,68 @@ func (r *EvmReader) readAndUpdateClaims(
epochs := []*Epoch{}
for _, claimAcceptance := range claimAcceptances {

// Get Previous claims and update their statuses
// rejecting all
previousClaims, err := r.repository.GetPreviousSubmittedClaims(
// Get Previous Epochs with submitted claims, that got no acceptance event
// and update their statuses rejecting them all
previousEpochs, err := r.repository.GetPreviousSubmittedClaimsEpochs(
ctx, app, claimAcceptance.LastProcessedBlockNumber.Uint64())
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)
for _, previousEpoch := range previousEpochs {
previousEpoch.Status = EpochStatusClaimRejected
epochs = append(epochs, previousEpoch)
slog.Warn("Claim rejected",
"app", app,
"lastBlock", previousClaim.LastBlock,
"hash", previousClaim.ClaimHash)
"lastBlock", previousEpoch.LastBlock,
"hash", previousEpoch.ClaimHash)
}

// Get Claim
claim, err := r.repository.GetEpoch(
// Get the Epoch for the current Claim Acceptance Event
epoch, err := r.repository.GetEpoch(
ctx, calculateEpochIndex(
r.epochLengthCache[app],
claimAcceptance.LastProcessedBlockNumber.Uint64()),
app)
if err != nil {
slog.Error("Error retrieving claim", "app", app, "error", err)
slog.Error("Error retrieving Epoch", "app", app, "error", err)
continue APP_LOOP
}

// Check Claim
if claim == nil {
slog.Error("Got unknown claim event",
// Check Epoch
if epoch == nil {
slog.Error("Got claim acceptance event for an unknown epoch",
"app", app,
"claim last block", claimAcceptance.LastProcessedBlockNumber,
"hash", claimAcceptance.Claim)
continue APP_LOOP
}

// Update claim status
if claimAcceptance.Claim != *claim.ClaimHash {
// Update Epoch claim status
if claimAcceptance.Claim != *epoch.ClaimHash ||
claimAcceptance.LastProcessedBlockNumber.Uint64() != epoch.LastBlock {
slog.Warn("Claim Rejected",
"app", app,
"lastBlock", claim.LastBlock,
"hash", claim.ClaimHash)
"lastBlock", epoch.LastBlock,
"hash", epoch.ClaimHash)

claim.Status = EpochStatusClaimRejected
epochs = append(epochs, claim)
epoch.Status = EpochStatusClaimRejected
epochs = append(epochs, epoch)
} else {
slog.Info("Claim Accepted",
"app", app,
"lastBlock", claim.LastBlock,
"hash", claim.ClaimHash)
"lastBlock", epoch.LastBlock,
"hash", epoch.ClaimHash)

claim.Status = EpochStatusClaimAccepted
epochs = append(epochs, claim)
epoch.Status = EpochStatusClaimAccepted
epochs = append(epochs, epoch)
}
}

// Store everything
err = r.repository.StoreClaimsTransaction(
err = r.repository.UpdateEpochsClaimStatusTransaction(
ctx, app, epochs, mostRecentBlockNumber)
if err != nil {
slog.Error("Error storing claims", "app", app, "error", err)
Expand Down
38 changes: 19 additions & 19 deletions internal/evmreader/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (s *EvmReaderSuite) TestNoClaimsAcceptance() {
LastClaimCheckBlock: 0x11,
}}, nil).Once()

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
s.repository.Unset("UpdateEpochsClaimStatusTransaction")
s.repository.On("UpdateEpochsClaimStatusTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
Expand All @@ -67,7 +67,7 @@ func (s *EvmReaderSuite) TestNoClaimsAcceptance() {
s.Require().Equal(uint64(17), lastClaimCheck)

}).Return(nil)
s.repository.On("StoreClaimsTransaction",
s.repository.On("UpdateEpochsClaimStatusTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *EvmReaderSuite) TestNoClaimsAcceptance() {

s.repository.AssertNumberOfCalls(
s.T(),
"StoreClaimsTransaction",
"UpdateEpochsClaimStatusTransaction",
2,
)

Expand Down Expand Up @@ -232,15 +232,15 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() {
mock.Anything,
mock.Anything).Return(claim1, nil)

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

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
s.repository.Unset("UpdateEpochsClaimStatusTransaction")
s.repository.On("UpdateEpochsClaimStatusTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -295,7 +295,7 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() {

s.repository.AssertNumberOfCalls(
s.T(),
"StoreClaimsTransaction",
"UpdateEpochsClaimStatusTransaction",
1,
)

Expand Down Expand Up @@ -390,15 +390,15 @@ func (s *EvmReaderSuite) TestCheckClaimFails() {
mock.Anything,
mock.Anything).Return(claim1, nil)

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

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
s.repository.Unset("UpdateEpochsClaimStatusTransaction")
s.repository.On("UpdateEpochsClaimStatusTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -440,7 +440,7 @@ func (s *EvmReaderSuite) TestCheckClaimFails() {

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

Expand Down Expand Up @@ -534,15 +534,15 @@ func (s *EvmReaderSuite) TestCheckClaimFails() {
mock.Anything,
mock.Anything).Return(nil, fmt.Errorf("No epoch for you"))

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

s.repository.Unset("StoreClaimsTransaction")
s.repository.On("StoreClaimsTransaction",
s.repository.Unset("UpdateEpochsClaimStatusTransaction")
s.repository.On("UpdateEpochsClaimStatusTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -584,7 +584,7 @@ func (s *EvmReaderSuite) TestCheckClaimFails() {

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

Expand Down
3 changes: 1 addition & 2 deletions internal/evmreader/consensus_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func (c *ConsensusContractAdapter) RetrieveClaimAcceptanceEvents(
claimAcceptanceEvent := itr.Event
events = append(events, claimAcceptanceEvent)
}
err = itr.Error()
if err != nil {
if err = itr.Error(); err != nil {
return nil, err
}
return events, nil
Expand Down
24 changes: 5 additions & 19 deletions internal/evmreader/evmreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ 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)
StoreClaimsTransaction(ctx context.Context,
GetPreviousSubmittedClaimsEpochs(ctx context.Context, app Address, lastBlock uint64) ([]*Epoch, error)
UpdateEpochsClaimStatusTransaction(ctx context.Context,
app Address,
claims []*Epoch,
mostRecentBlockNumber uint64,
Expand Down Expand Up @@ -91,7 +91,7 @@ type application struct {
consensusContract ConsensusContract
}

// EvmReader reads inputs from the blockchain
// EvmReader reads Input Added and Claim Submitted events from the blockchain
type EvmReader struct {
client EthClient
wsClient EthWsClient
Expand Down Expand Up @@ -170,7 +170,7 @@ func (r *EvmReader) watchForNewBlocks(ctx context.Context, ready chan<- struct{}
// Get All Applications
runningApps, err := r.repository.GetAllRunningApplications(ctx)
if err != nil {
slog.Error("Error retrieving running applications for new inputs",
slog.Error("Error retrieving running applications",
"error",
err,
)
Expand All @@ -191,7 +191,7 @@ func (r *EvmReader) watchForNewBlocks(ctx context.Context, ready chan<- struct{}
}

if len(apps) == 0 {
slog.Info("No running consistent applications")
slog.Info("No correctly configured applications running")
continue
}

Expand Down Expand Up @@ -288,20 +288,6 @@ func (r *EvmReader) getAppContracts(app Application,
return applicationContract, consensus, nil
}

// getEpochLengthFromContract reads the application epoch length given it's consensus contract
func getEpochLengthFromContract(consensus ConsensusContract) (uint64, error) {

epochLengthRaw, err := consensus.GetEpochLength(nil)
if err != nil {
return 0, errors.Join(
fmt.Errorf("error retrieving application epoch length"),
err,
)
}

return epochLengthRaw.Uint64(), nil
}

// Util functions

// calculateEpochIndex calculates the epoch index given the input block number
Expand Down
10 changes: 5 additions & 5 deletions internal/evmreader/evmreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (s *EvmReaderSuite) TestItWrongIConsensus() {
s.inputBox.AssertNumberOfCalls(s.T(), "RetrieveClaimAcceptanceEvents", 0)
s.repository.AssertNumberOfCalls(
s.T(),
"StoreClaimsTransaction",
"UpdateEpochsClaimStatusTransaction",
0,
)
}
Expand Down Expand Up @@ -437,12 +437,12 @@ func newMockRepository() *MockRepository {
mock.Anything,
mock.Anything).Return(1, nil)

repo.On("GetPreviousSubmittedClaims",
repo.On("GetPreviousSubmittedClaimsEpochs",
mock.Anything,
mock.Anything,
).Return([]Epoch{}, nil)

repo.On("StoreClaimsTransaction",
repo.On("UpdateEpochsClaimStatusTransaction",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -506,7 +506,7 @@ func (m *MockRepository) InsertEpoch(
return args.Get(0).(uint64), args.Error(1)
}

func (m *MockRepository) GetPreviousSubmittedClaims(
func (m *MockRepository) GetPreviousSubmittedClaimsEpochs(
ctx context.Context,
app Address,
lastBlock uint64,
Expand All @@ -519,7 +519,7 @@ func (m *MockRepository) GetPreviousSubmittedClaims(
return obj.([]*Epoch), args.Error(1)

}
func (m *MockRepository) StoreClaimsTransaction(ctx context.Context,
func (m *MockRepository) UpdateEpochsClaimStatusTransaction(ctx context.Context,
app Address,
epochs []*Epoch,
mostRecentBlockNumber uint64,
Expand Down
14 changes: 14 additions & 0 deletions internal/evmreader/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,17 @@ func (r *EvmReader) readInputsFromBlockchain(
func byLastProcessedBlock(app application) uint64 {
return app.LastProcessedBlock
}

// getEpochLengthFromContract reads the application epoch length given it's consensus contract
func getEpochLengthFromContract(consensus ConsensusContract) (uint64, error) {

epochLengthRaw, err := consensus.GetEpochLength(nil)
if err != nil {
return 0, errors.Join(
fmt.Errorf("error retrieving application epoch length"),
err,
)
}

return epochLengthRaw.Uint64(), nil
}
3 changes: 1 addition & 2 deletions internal/evmreader/inputsource_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func (i *InputSourceAdapter) RetrieveInputs(
inputAddedEvent := itr.Event
events = append(events, *inputAddedEvent)
}
err = itr.Error()
if err != nil {
if err = itr.Error(); err != nil {
return nil, err
}
return events, nil
Expand Down
Loading

0 comments on commit 9692f4d

Please sign in to comment.