From ba70b74fb7ee5ad7ab69ec3f2e52d5d61d4df6a5 Mon Sep 17 00:00:00 2001
From: atvanguard <93arpit@gmail.com>
Date: Tue, 19 May 2020 09:47:54 +0530
Subject: [PATCH] fetch events from [lastSync, (block-sprint).Time)

---
 consensus/bor/bor.go               | 16 +++++++++-------
 consensus/bor/bor_test/bor_test.go | 11 +++--------
 2 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go
index d3fde7875..66e04dbab 100644
--- a/consensus/bor/bor.go
+++ b/consensus/bor/bor.go
@@ -653,9 +653,7 @@ func (c *Bor) Prepare(chain consensus.ChainReader, header *types.Header) error {
 // Finalize implements consensus.Engine, ensuring no uncles are set, nor block
 // rewards given.
 func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
-	// commit span
 	headerNumber := header.Number.Uint64()
-
 	if headerNumber%c.config.Sprint == 0 {
 		cx := chainContext{Chain: chain, Bor: c}
 		// check and commit span
@@ -663,6 +661,7 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state
 			log.Error("Error while committing span", "error", err)
 			return
 		}
+
 		// commit statees
 		if err := c.CommitStates(state, header, cx); err != nil {
 			log.Error("Error while committing states", "error", err)
@@ -678,8 +677,8 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state
 // FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
 // nor block rewards given, and returns the final block.
 func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
-	// commit span
-	if header.Number.Uint64()%c.config.Sprint == 0 {
+	headerNumber := header.Number.Uint64()
+	if headerNumber%c.config.Sprint == 0 {
 		cx := chainContext{Chain: chain, Bor: c}
 
 		// check and commit span
@@ -1082,7 +1081,10 @@ func (c *Bor) CommitStates(
 		return err
 	}
 	from := lastSync.Add(time.Second * 1) // querying the interval [from, to)
-	to := time.Unix(int64(chain.Chain.GetHeaderByNumber(number-1).Time), 0)
+	to := time.Unix(int64(chain.Chain.GetHeaderByNumber(number-c.config.Sprint).Time), 0)
+	if !from.Before(to) {
+		return nil
+	}
 	log.Info(
 		"Fetching state updates from Heimdall",
 		"from", from.Format(time.RFC3339),
@@ -1150,8 +1152,8 @@ func (c *Bor) CommitStates(
 }
 
 func validateEventRecord(eventRecord *EventRecordWithTime, number uint64, from, to time.Time) error {
-	inRange := (eventRecord.Time.Equal(from) || eventRecord.Time.After(from)) && eventRecord.Time.Before(to)
-	if !inRange {
+	// event should lie in the range [from, to)
+	if eventRecord.Time.Before(from) || !eventRecord.Time.Before(to) {
 		return &InvalidStateReceivedError{number, &from, &to, eventRecord}
 	}
 	return nil
diff --git a/consensus/bor/bor_test/bor_test.go b/consensus/bor/bor_test/bor_test.go
index d9391f178..8cbdbfa8a 100644
--- a/consensus/bor/bor_test/bor_test.go
+++ b/consensus/bor/bor_test/bor_test.go
@@ -36,16 +36,12 @@ func TestInsertingSpanSizeBlocks(t *testing.T) {
 
 	db := init.ethereum.ChainDb()
 	block := init.genesis.ToBlock(db)
-	var to int64
+	to := int64(block.Header().Time)
 
 	// Insert sprintSize # of blocks so that span is fetched at the start of a new sprint
 	for i := uint64(1); i <= spanSize; i++ {
 		block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor)
 		insertNewBlock(t, chain, block)
-		if i == sprintSize-1 {
-			// at # sprintSize, events are fetched for the internal [from, (block-1).Time)
-			to = int64(block.Header().Time)
-		}
 	}
 
 	assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, ""))
@@ -71,7 +67,6 @@ func TestFetchStateSyncEvents(t *testing.T) {
 	// A. Insert blocks for 0th sprint
 	db := init.ethereum.ChainDb()
 	block := init.genesis.ToBlock(db)
-
 	// Insert sprintSize # of blocks so that span is fetched at the start of a new sprint
 	for i := uint64(1); i < sprintSize; i++ {
 		block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor)
@@ -100,9 +95,9 @@ func TestFetchStateSyncEvents(t *testing.T) {
 		t.Fatalf("%s", err)
 	}
 
-	// at # sprintSize, events are fetched for the internal [from, (block-1).Time)
+	// at # sprintSize, events are fetched for the interval [from, (block-sprint).Time)
 	from := 1
-	to := int64(block.Header().Time)
+	to := int64(chain.GetHeaderByNumber(0).Time)
 	page := 1
 	query1Params := fmt.Sprintf(clerkQueryParams, from, to, page)
 	h.On("FetchWithRetry", clerkPath, query1Params).Return(&response, nil)
-- 
GitLab