diff --git a/internal/evmreader/claim.go b/internal/evmreader/claim.go index 7a24f1df..345c73c2 100644 --- a/internal/evmreader/claim.go +++ b/internal/evmreader/claim.go @@ -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 @@ -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 } @@ -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) @@ -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", @@ -141,6 +140,7 @@ func (r *EvmReader) readAndUpdateClaims( continue } + // Update claim status if claimAcceptance.Claim != *claim.ClaimHash { slog.Warn("Claim Rejected", "app", app, @@ -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()) } diff --git a/internal/evmreader/claim_test.go b/internal/evmreader/claim_test.go index fac6728e..ab47f610 100644 --- a/internal/evmreader/claim_test.go +++ b/internal/evmreader/claim_test.go @@ -152,9 +152,6 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() { appAddress := common.HexToAddress("0x2E663fe9aE92275242406A185AA4fC8174339D3E") - // Want two run loops - wsClient := FakeWSEhtClient{} - // Contract Factory consensusContract := &MockIConsensusContract{} @@ -167,6 +164,7 @@ func (s *EvmReaderSuite) TestReadClaimAcceptance() { ).Return(consensusContract, nil) //New EVM Reader + wsClient := FakeWSEhtClient{} evmReader := NewEvmReader( s.client, &wsClient, diff --git a/internal/evmreader/evmreader.go b/internal/evmreader/evmreader.go index 77ef3c06..6f4b7ad2 100644 --- a/internal/evmreader/evmreader.go +++ b/internal/evmreader/evmreader.go @@ -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 @@ -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 { @@ -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) @@ -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, @@ -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) @@ -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) @@ -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 } @@ -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) } diff --git a/internal/evmreader/evmreader_test.go b/internal/evmreader/evmreader_test.go index 6976b9ce..216ee00d 100644 --- a/internal/evmreader/evmreader_test.go +++ b/internal/evmreader/evmreader_test.go @@ -240,7 +240,7 @@ 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(), @@ -248,7 +248,7 @@ func (s *EvmReaderSuite) TestItWrongIConsensus() { 0, ) - // Do not process claims + // Should not advance claim acceptance processing s.inputBox.AssertNumberOfCalls(s.T(), "RetrieveClaimAcceptanceEvents", 0) s.repository.AssertNumberOfCalls( s.T(), diff --git a/internal/evmreader/input.go b/internal/evmreader/input.go index c8881cdb..ab5768b7 100644 --- a/internal/evmreader/input.go +++ b/internal/evmreader/input.go @@ -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, @@ -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, @@ -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] @@ -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,