Skip to content

Clarify break on error during ancestors lookup #1580

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

Merged
merged 7 commits into from
Jun 7, 2023
Merged
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
19 changes: 16 additions & 3 deletions snow/engine/snowman/block/batched_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"errors"
"time"

"go.uber.org/zap"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

Expand All @@ -33,6 +36,7 @@ type BatchedChainVM interface {

func GetAncestors(
ctx context.Context,
log logging.Logger,
vm Getter, // fetch blocks
blkID ids.ID, // first requested block
maxBlocksNum int, // max number of blocks to be retrieved
Expand Down Expand Up @@ -60,7 +64,7 @@ func GetAncestors(
startTime := time.Now()
blk, err := vm.GetBlock(ctx, blkID)
if err == database.ErrNotFound {
// special case ErrNotFound as an empty response: this signals
// Special case ErrNotFound as an empty response: this signals
// the client to avoid contacting this node for further ancestors
// as they may have been pruned or unavailable due to state-sync.
return nil, nil
Expand All @@ -74,8 +78,17 @@ func GetAncestors(
ancestorsBytesLen := len(blk.Bytes()) + wrappers.IntLen // length, in bytes, of all elements of ancestors

for numFetched := 1; numFetched < maxBlocksNum && time.Since(startTime) < maxBlocksRetrivalTime; numFetched++ {
blk, err = vm.GetBlock(ctx, blk.Parent())
parentID := blk.Parent()
blk, err = vm.GetBlock(ctx, parentID)
if err == database.ErrNotFound {
// After state sync we may not have the full chain
break
}
if err != nil {
log.Error("failed to get block during ancestors lookup",
zap.String("parentID", parentID.String()),
zap.Error(err),
)
break
}
blkBytes := blk.Bytes()
Expand All @@ -84,7 +97,7 @@ func GetAncestors(
// is repr. by an int.
newLen := ancestorsBytesLen + len(blkBytes) + wrappers.IntLen
if newLen > maxBlocksSize {
// reached maximum response size
// Reached maximum response size
break
}
ancestorsBytes = append(ancestorsBytes, blkBytes)
Expand Down
5 changes: 3 additions & 2 deletions snow/engine/snowman/block/batched_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/utils/logging"
)

var errTest = errors.New("non-nil error")
Expand All @@ -25,7 +26,7 @@ func TestGetAncestorsDatabaseNotFound(t *testing.T) {
require.Equal(t, someID, id)
return nil, database.ErrNotFound
}
containers, err := GetAncestors(context.Background(), vm, someID, 10, 10, 1*time.Second)
containers, err := GetAncestors(context.Background(), logging.NoLog{}, vm, someID, 10, 10, 1*time.Second)
require.NoError(t, err)
require.Empty(t, containers)
}
Expand All @@ -39,7 +40,7 @@ func TestGetAncestorsPropagatesErrors(t *testing.T) {
require.Equal(t, someID, id)
return nil, errTest
}
containers, err := GetAncestors(context.Background(), vm, someID, 10, 10, 1*time.Second)
containers, err := GetAncestors(context.Background(), logging.NoLog{}, vm, someID, 10, 10, 1*time.Second)
require.Nil(t, containers)
require.ErrorIs(t, err, errTest)
}
1 change: 1 addition & 0 deletions snow/engine/snowman/getter/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (gh *getter) GetAccepted(ctx context.Context, nodeID ids.NodeID, requestID
func (gh *getter) GetAncestors(ctx context.Context, nodeID ids.NodeID, requestID uint32, blkID ids.ID) error {
ancestorsBytes, err := block.GetAncestors(
ctx,
gh.log,
gh.vm,
blkID,
gh.cfg.AncestorsMaxContainersSent,
Expand Down
22 changes: 13 additions & 9 deletions vms/rpcchainvm/vm_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type VMServer struct {

processMetrics prometheus.Gatherer
dbManager manager.Manager
log logging.Logger

serverCloser grpcutils.ServerCloser
connCloser wrappers.Closer
Expand Down Expand Up @@ -184,6 +185,16 @@ func (vm *VMServer) Initialize(ctx context.Context, req *vmpb.InitializeRequest)
}
vm.dbManager = dbManager

// TODO: Allow the logger to be configured by the client
vm.log = logging.NewLogger(
fmt.Sprintf("<%s Chain>", chainID),
logging.NewWrappedCore(
logging.Info,
originalStderr,
logging.Colors.ConsoleEncoder(),
),
)

clientConn, err := grpcutils.Dial(
req.ServerAddr,
grpcutils.WithChainUnaryInterceptor(grpcClientMetrics.UnaryClientInterceptor()),
Expand Down Expand Up @@ -233,15 +244,7 @@ func (vm *VMServer) Initialize(ctx context.Context, req *vmpb.InitializeRequest)
CChainID: cChainID,
AVAXAssetID: avaxAssetID,

// TODO: Allow the logger to be configured by the client
Log: logging.NewLogger(
fmt.Sprintf("<%s Chain>", chainID),
logging.NewWrappedCore(
logging.Info,
originalStderr,
logging.Colors.ConsoleEncoder(),
),
),
Log: vm.log,
Keystore: keystoreClient,
SharedMemory: sharedMemoryClient,
BCLookup: bcLookupClient,
Expand Down Expand Up @@ -652,6 +655,7 @@ func (vm *VMServer) GetAncestors(ctx context.Context, req *vmpb.GetAncestorsRequ

blocks, err := block.GetAncestors(
ctx,
vm.log,
vm.vm,
blkID,
maxBlksNum,
Expand Down