Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context for fetch process #57

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash co
hash := checkpoint.Hash()

// get validators and current span
validators, err := c.GetCurrentValidators(hash, number+1)
validators, err := c.GetCurrentValidators(context.Background(), hash, number+1)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -647,7 +647,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e

// get validator set if number
if (number+1)%c.config.Sprint == 0 {
newValidators, err := c.GetCurrentValidators(header.ParentHash, number+1)
newValidators, err := c.GetCurrentValidators(context.Background(), header.ParentHash, number+1)
if err != nil {
return errors.New("unknown validators")
}
Expand Down Expand Up @@ -695,9 +695,10 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header,
var err error
headerNumber := header.Number.Uint64()
if headerNumber%c.config.Sprint == 0 {
ctx := context.Background()
cx := chainContext{Chain: chain, Bor: c}
// check and commit span
if err := c.checkAndCommitSpan(state, header, cx); err != nil {
if err := c.checkAndCommitSpan(ctx, state, header, cx); err != nil {
log.Error("Error while committing span", "error", err)
return
}
Expand Down Expand Up @@ -761,10 +762,11 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ

headerNumber := header.Number.Uint64()
if headerNumber%c.config.Sprint == 0 {
ctx := context.Background()
cx := chainContext{Chain: chain, Bor: c}

// check and commit span
err := c.checkAndCommitSpan(state, header, cx)
err := c.checkAndCommitSpan(ctx, state, header, cx)
if err != nil {
log.Error("Error while committing span", "error", err)
return nil, err
Expand Down Expand Up @@ -927,14 +929,14 @@ func (c *Bor) Close() error {
}

// GetCurrentSpan get current span from contract
func (c *Bor) GetCurrentSpan(headerHash common.Hash) (*Span, error) {
func (c *Bor) GetCurrentSpan(ctx context.Context, headerHash common.Hash) (*Span, error) {
// block
blockNr := rpc.BlockNumberOrHashWithHash(headerHash, false)

// method
method := "getCurrentSpan"

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

data, err := c.validatorSetABI.Pack(method)
Expand Down Expand Up @@ -976,14 +978,14 @@ func (c *Bor) GetCurrentSpan(headerHash common.Hash) (*Span, error) {
}

// GetCurrentValidators get current validators
func (c *Bor) GetCurrentValidators(headerHash common.Hash, blockNumber uint64) ([]*Validator, error) {
func (c *Bor) GetCurrentValidators(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*Validator, error) {
// block
blockNr := rpc.BlockNumberOrHashWithHash(headerHash, false)

// method
method := "getBorValidators"

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

data, err := c.validatorSetABI.Pack(method, big.NewInt(0).SetUint64(blockNumber))
Expand Down Expand Up @@ -1084,17 +1086,18 @@ func (c *Bor) GetCurrentValidatorsByBlockNrOrHash(ctx context.Context, blockNrOr
}

func (c *Bor) checkAndCommitSpan(
ctx context.Context,
state *state.StateDB,
header *types.Header,
chain core.ChainContext,
) error {
headerNumber := header.Number.Uint64()
span, err := c.GetCurrentSpan(header.ParentHash)
span, err := c.GetCurrentSpan(ctx, header.ParentHash)
if err != nil {
return err
}
if c.needToCommitSpan(span, headerNumber) {
err := c.fetchAndCommitSpan(span.ID+1, state, header, chain)
err := c.fetchAndCommitSpan(ctx, span.ID+1, state, header, chain)
return err
}
return nil
Expand All @@ -1120,6 +1123,7 @@ func (c *Bor) needToCommitSpan(span *Span, headerNumber uint64) bool {
}

func (c *Bor) fetchAndCommitSpan(
ctx context.Context,
newSpanID uint64,
state *state.StateDB,
header *types.Header,
Expand All @@ -1128,7 +1132,7 @@ func (c *Bor) fetchAndCommitSpan(
var heimdallSpan HeimdallSpan

if c.WithoutHeimdall {
s, err := c.getNextHeimdallSpanForTest(newSpanID, state, header, chain)
s, err := c.getNextHeimdallSpanForTest(ctx, newSpanID, state, header, chain)
if err != nil {
return err
}
Expand Down Expand Up @@ -1277,13 +1281,14 @@ func (c *Bor) SetHeimdallClient(h IHeimdallClient) {
//

func (c *Bor) getNextHeimdallSpanForTest(
ctx context.Context,
newSpanID uint64,
state *state.StateDB,
header *types.Header,
chain core.ChainContext,
) (*HeimdallSpan, error) {
headerNumber := header.Number.Uint64()
span, err := c.GetCurrentSpan(header.ParentHash)
span, err := c.GetCurrentSpan(ctx, header.ParentHash)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion tests/bor/bor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bor

import (
"context"
"encoding/hex"
"encoding/json"
"math/big"
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestInsertingSpanSizeBlocks(t *testing.T) {
}

assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, ""))
validators, err := _bor.GetCurrentValidators(block.Hash(), spanSize) // check validator set at the first block of new span
validators, err := _bor.GetCurrentValidators(context.Background(), block.Hash(), spanSize) // check validator set at the first block of new span
if err != nil {
t.Fatalf("%s", err)
}
Expand Down