fetch events from [lastSync, (block-sprint).Time)

This commit is contained in:
atvanguard 2020-05-19 09:47:54 +05:30
parent 0e8b5ca4b7
commit ba70b74fb7
2 changed files with 12 additions and 15 deletions

View file

@ -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

View file

@ -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)