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 386079b commit 47de075
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
19 changes: 11 additions & 8 deletions internal/evmreader/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func (r *EvmReader) checkForClaimStatus(

for lastClaimCheck, apps := range appsIndexedByLastCheck {

//Check lastClaimCheck against most recent one!!!!!!

appAddresses := appToAddresses(apps)

// Safeguard: Only check blocks starting from the block where the InputBox
Expand Down Expand Up @@ -81,7 +79,6 @@ func (r *EvmReader) readAndUpdateClaims(
appAddresses := appToAddresses(apps)

// All these apps shares the same IConsensus
// So we can grab the first one
if len(apps) == 0 {
continue
}
Expand All @@ -106,14 +103,15 @@ func (r *EvmReader) readAndUpdateClaims(
epochs := []*Epoch{}
for _, claimAcceptance := range claimAcceptances {

//Get Previous claims and update their statuses
// Get Previous claims and update their statuses
// rejecting all
previousClaims, err := r.repository.GetPreviousSubmittedClaims(
ctx, app, claimAcceptance.LastProcessedBlockNumber.Uint64())
if err != nil {
slog.Error("Error retrieving previous submitted claims",
"app", app, "error", err)
}
// This should not be huge....

for _, previousClaim := range previousClaims {
previousClaim.Status = EpochStatusClaimRejected
epochs = append(epochs, &previousClaim)
Expand All @@ -132,6 +130,7 @@ func (r *EvmReader) readAndUpdateClaims(
if err != nil {
slog.Error("Error retrieving claim", "app", app, "error", err)
}

// Check Claim
if claim == nil {
slog.Error("Got unknown claim event",
Expand All @@ -141,6 +140,7 @@ func (r *EvmReader) readAndUpdateClaims(
continue
}

// Update claim status
if claimAcceptance.Claim != *claim.ClaimHash {
slog.Warn("Claim Rejected",
"app", app,
Expand Down Expand Up @@ -195,23 +195,26 @@ func (r *EvmReader) readClaimAcceptanceFromBlockchain(
return nil, err
}
for _, event := range claimAcceptanceEvents {
// Insert Sorted
appClaimAcceptanceMap[event.AppContract] = insertSorted(
sortByLastBlockNumber, appClaimAcceptanceMap[event.AppContract], event)
}
return appClaimAcceptanceMap, nil
}

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

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

// sortByLastBlockNumber is a ClaimAcceptance's by last block number sorting function.
// Intended to be used with insertSorted function, see insertSorted()
func sortByLastBlockNumber(a, b *iconsensus.IConsensusClaimAcceptance) int {
return cmp.Compare(a.LastProcessedBlockNumber.Uint64(), b.LastProcessedBlockNumber.Uint64())
}
4 changes: 1 addition & 3 deletions internal/evmreader/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() {

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

// Want two run loops
wsClient := FakeWSEhtClient{}

// Contract Factory

consensusContract := &MockIConsensusContract{}
Expand All @@ -167,6 +164,7 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() {
).Return(consensusContract, nil)

//New EVM Reader
wsClient := FakeWSEhtClient{}
evmReader := NewEvmReader(
s.client,
&wsClient,
Expand Down
21 changes: 12 additions & 9 deletions internal/evmreader/evmreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (e *SubscriptionError) Error() string {
return fmt.Sprintf("Subscription error : %v", e.Cause)
}

// Internal struct to hold application and it's
// contracts together
type application struct {
Application
applicationContract ApplicationContract
Expand Down Expand Up @@ -129,7 +131,6 @@ func NewEvmReader(
func (r *EvmReader) Run(ctx context.Context, ready chan<- struct{}) error {

// Initialize epochLength cache
// This should be better managed, and remove unused entries
r.epochLengthCache = make(map[Address]uint64)

for {
Expand All @@ -144,7 +145,7 @@ func (r *EvmReader) Run(ctx context.Context, ready chan<- struct{}) error {
}
}

// Watch for new blocks and reads new inputs based on the
// watchForNewBlocks watches for new blocks and reads new inputs based on the
// default block configuration, which have not been processed yet.
func (r *EvmReader) watchForNewBlocks(ctx context.Context, ready chan<- struct{}) error {
headers := make(chan *types.Header)
Expand Down Expand Up @@ -214,7 +215,7 @@ func (r *EvmReader) watchForNewBlocks(ctx context.Context, ready chan<- struct{}
}
}

// Fetch the most recent header up till the
// fetchMostRecentHeader fetches the most recent header up till the
// given default block
func (r *EvmReader) fetchMostRecentHeader(
ctx context.Context,
Expand Down Expand Up @@ -249,8 +250,8 @@ func (r *EvmReader) fetchMostRecentHeader(
return header, nil
}

// Retrieve ApplicationContract and ConsensusContract for a given Application.
// Also validates if IConsensus configuration matches the ApplicationContract one
// getAppContracts retrieves the ApplicationContract and ConsensusContract for a given Application.
// Also validates if IConsensus configuration matches the blockchain registered one
func (r *EvmReader) getAppContracts(app Application,
) (ApplicationContract, ConsensusContract, error) {
applicationContract, err := r.contractFactory.NewApplication(app.ContractAddress)
Expand Down Expand Up @@ -287,7 +288,7 @@ func (r *EvmReader) getAppContracts(app Application,
return applicationContract, consensus, nil
}

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

epochLengthRaw, err := consensus.GetEpochLength(nil)
Expand All @@ -303,7 +304,8 @@ func getEpochLengthFromContract(consensus ConsensusContract) (uint64, error) {

// Util functions

// Calculates the epoch index given the input block number
// calculateEpochIndex calculates the epoch index given the input block number
// and epoch length
func calculateEpochIndex(epochLength uint64, blockNumber uint64) uint64 {
return blockNumber / epochLength
}
Expand All @@ -316,8 +318,9 @@ func appToAddresses(apps []application) []Address {
return addresses
}

// index Inputs by its index property.
// to be used with `insertSorted`
// sortByInputIndex is a compare function that orders Inputs
// by it's index field. It is intended to be used with
// `insertSorted`, see insertSorted()
func sortByInputIndex(a, b *Input) int {
return cmp.Compare(a.Index, b.Index)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/evmreader/evmreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ func (s *EvmReaderSuite) TestItWrongIConsensus() {
wsClient.fireNewHead(&header0)
time.Sleep(time.Second)

// Do not process inputs
// Should not advance input processing
s.inputBox.AssertNumberOfCalls(s.T(), "RetrieveInputs", 0)
s.repository.AssertNumberOfCalls(
s.T(),
"StoreEpochAndInputsTransaction",
0,
)

// Do not process claims
// Should not advance claim acceptance processing
s.inputBox.AssertNumberOfCalls(s.T(), "RetrieveClaimAcceptanceEvents", 0)
s.repository.AssertNumberOfCalls(
s.T(),
Expand Down
11 changes: 6 additions & 5 deletions internal/evmreader/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ethereum/go-ethereum/common"
)

// Check if is there new Inputs for all running Applications
// checkForNewInputs checks if is there new Inputs for all running Applications
func (r *EvmReader) checkForNewInputs(
ctx context.Context,
apps []application,
Expand Down Expand Up @@ -74,7 +74,8 @@ func (r *EvmReader) checkForNewInputs(
}
}

// Read and store inputs from the InputSource given specific filter options.
// readAndStoreInputs reads, inputs from the InputSource given specific filter options, indexes
// them into epochs and store the indexed inputs and epochs
func (r *EvmReader) readAndStoreInputs(
ctx context.Context,
startBlock uint64,
Expand Down Expand Up @@ -230,8 +231,8 @@ func (r *EvmReader) readAndStoreInputs(
return nil
}

// Checks the epoch length cache and read epoch length from IConsensus
// and add it to the cache if needed
// addAppEpochLengthIntoCache checks the epoch length cache and read epoch length from IConsensus
// contract and add it to the cache if needed
func (r *EvmReader) addAppEpochLengthIntoCache(app application) error {

epochLength, ok := r.epochLengthCache[app.ContractAddress]
Expand All @@ -257,7 +258,7 @@ func (r *EvmReader) addAppEpochLengthIntoCache(app application) error {
return nil
}

// Read inputs from the blockchain ordered by Input index
// readInputsFromBlockchain read the inputs from the blockchain ordered by Input index
func (r *EvmReader) readInputsFromBlockchain(
ctx context.Context,
appsAddresses []Address,
Expand Down

0 comments on commit 47de075

Please sign in to comment.