Skip to content

Commit

Permalink
Only return accepted blocks in GetStatelessBlock (ava-labs#1724)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Jul 18, 2023
1 parent b9355c1 commit cccba11
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 93 deletions.
4 changes: 2 additions & 2 deletions vms/platformvm/blocks/executor/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func (b *backend) GetBlock(blkID ids.ID) (blocks.Block, error) {
if blk, ok := b.blkIDToState[blkID]; ok {
return blk.statelessBlock, nil
}

// The block isn't in memory. Check the database.
statelessBlk, _, err := b.state.GetStatelessBlock(blkID)
return statelessBlk, err
return b.state.GetStatelessBlock(blkID)
}

func (b *backend) LastAccepted() ids.ID {
Expand Down
5 changes: 2 additions & 3 deletions vms/platformvm/blocks/executor/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
)
Expand Down Expand Up @@ -98,15 +97,15 @@ func TestBackendGetBlock(t *testing.T) {
{
// Case: block isn't in the map or database.
blkID := ids.GenerateTestID()
state.EXPECT().GetStatelessBlock(blkID).Return(nil, choices.Unknown, database.ErrNotFound)
state.EXPECT().GetStatelessBlock(blkID).Return(nil, database.ErrNotFound)
_, err := b.GetBlock(blkID)
require.Equal(database.ErrNotFound, err)
}

{
// Case: block isn't in the map and is in database.
blkID := ids.GenerateTestID()
state.EXPECT().GetStatelessBlock(blkID).Return(statelessBlk, choices.Accepted, nil)
state.EXPECT().GetStatelessBlock(blkID).Return(statelessBlk, nil)
gotBlk, err := b.GetBlock(blkID)
require.NoError(err)
require.Equal(statelessBlk, gotBlk)
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/blocks/executor/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func (b *Block) Status() choices.Status {
return choices.Processing
}
// Block isn't in memory. Check in the database.
_, status, err := b.manager.state.GetStatelessBlock(blkID)
_, err := b.manager.state.GetStatelessBlock(blkID)
switch err {
case nil:
return status
return choices.Accepted

case database.ErrNotFound:
// choices.Unknown means we don't have the bytes of the block.
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/blocks/executor/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestStatus(t *testing.T) {
statelessBlk.EXPECT().ID().Return(blkID)

state := state.NewMockState(ctrl)
state.EXPECT().GetStatelessBlock(blkID).Return(statelessBlk, choices.Accepted, nil)
state.EXPECT().GetStatelessBlock(blkID).Return(statelessBlk, nil)

manager := &manager{
backend: &backend{
Expand All @@ -98,7 +98,7 @@ func TestStatus(t *testing.T) {
statelessBlk.EXPECT().ID().Return(blkID)

state := state.NewMockState(ctrl)
state.EXPECT().GetStatelessBlock(blkID).Return(nil, choices.Unknown, database.ErrNotFound)
state.EXPECT().GetStatelessBlock(blkID).Return(nil, database.ErrNotFound)

manager := &manager{
backend: &backend{
Expand Down
5 changes: 2 additions & 3 deletions vms/platformvm/blocks/executor/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
)
Expand All @@ -34,13 +33,13 @@ func TestGetBlock(t *testing.T) {

{
// Case: block isn't in memory or database
state.EXPECT().GetStatelessBlock(statelessBlk.ID()).Return(nil, choices.Unknown, database.ErrNotFound).Times(1)
state.EXPECT().GetStatelessBlock(statelessBlk.ID()).Return(nil, database.ErrNotFound).Times(1)
_, err := manager.GetBlock(statelessBlk.ID())
require.ErrorIs(err, database.ErrNotFound)
}
{
// Case: block isn't in memory but is in database.
state.EXPECT().GetStatelessBlock(statelessBlk.ID()).Return(statelessBlk, choices.Accepted, nil).Times(1)
state.EXPECT().GetStatelessBlock(statelessBlk.ID()).Return(statelessBlk, nil).Times(1)
gotBlk, err := manager.GetBlock(statelessBlk.ID())
require.NoError(err)
require.Equal(statelessBlk.ID(), gotBlk.ID())
Expand Down
21 changes: 10 additions & 11 deletions vms/platformvm/blocks/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
Expand Down Expand Up @@ -183,11 +182,11 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
env.blkManager.(*manager).lastAccepted = parentID
env.mockedState.EXPECT().GetLastAccepted().Return(parentID).AnyTimes()
env.mockedState.EXPECT().GetStatelessBlock(gomock.Any()).DoAndReturn(
func(blockID ids.ID) (blocks.Block, choices.Status, error) {
func(blockID ids.ID) (blocks.Block, error) {
if blockID == parentID {
return banffParentBlk, choices.Accepted, nil
return banffParentBlk, nil
}
return nil, choices.Rejected, database.ErrNotFound
return nil, database.ErrNotFound
}).AnyTimes()

// setup state to validate proposal block transaction
Expand Down Expand Up @@ -660,7 +659,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
// build proposal block moving ahead chain time
// as well as rewarding staker0
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err := blocks.NewBanffProposalBlock(
newTime,
Expand Down Expand Up @@ -815,7 +814,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {

// build proposal block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err := blocks.NewBanffProposalBlock(
subnetVdr1EndTime,
Expand Down Expand Up @@ -927,7 +926,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {

// build proposal block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err := blocks.NewBanffProposalBlock(
subnetVdr1StartTime,
Expand Down Expand Up @@ -1012,7 +1011,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {

// build proposal block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err := blocks.NewBanffProposalBlock(
pendingValidatorStartTime,
Expand Down Expand Up @@ -1105,7 +1104,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {

// Advance Time
preferredID = env.state.GetLastAccepted()
parentBlk, _, err = env.state.GetStatelessBlock(preferredID)
parentBlk, err = env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err = blocks.NewBanffProposalBlock(
pendingDelegatorStartTime,
Expand Down Expand Up @@ -1196,7 +1195,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {

// build proposal block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err := blocks.NewBanffProposalBlock(
pendingValidatorStartTime,
Expand Down Expand Up @@ -1288,7 +1287,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {

// Advance Time
preferredID = env.state.GetLastAccepted()
parentBlk, _, err = env.state.GetStatelessBlock(preferredID)
parentBlk, err = env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessProposalBlock, err = blocks.NewBanffProposalBlock(
pendingDelegatorStartTime,
Expand Down
12 changes: 6 additions & 6 deletions vms/platformvm/blocks/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestBanffStandardBlockUpdatePrimaryNetworkStakers(t *testing.T) {

// build standard block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessStandardBlock, err := blocks.NewBanffStandardBlock(
pendingValidatorStartTime,
Expand Down Expand Up @@ -543,7 +543,7 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {

// build standard block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessStandardBlock, err := blocks.NewBanffStandardBlock(
newTime,
Expand Down Expand Up @@ -659,7 +659,7 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
env.clk.Set(subnetVdr1EndTime)
// build standard block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessStandardBlock, err := blocks.NewBanffStandardBlock(
subnetVdr1EndTime,
Expand Down Expand Up @@ -731,7 +731,7 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) {

// build standard block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessStandardBlock, err := blocks.NewBanffStandardBlock(
subnetVdr1StartTime,
Expand Down Expand Up @@ -776,7 +776,7 @@ func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) {

// build standard block moving ahead chain time
preferredID := env.state.GetLastAccepted()
parentBlk, _, err := env.state.GetStatelessBlock(preferredID)
parentBlk, err := env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessStandardBlock, err := blocks.NewBanffStandardBlock(
pendingValidatorStartTime,
Expand Down Expand Up @@ -828,7 +828,7 @@ func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) {

// Advance Time
preferredID = env.state.GetLastAccepted()
parentBlk, _, err = env.state.GetStatelessBlock(preferredID)
parentBlk, err = env.state.GetStatelessBlock(preferredID)
require.NoError(err)
statelessStandardBlock, err = blocks.NewBanffStandardBlock(
pendingDelegatorStartTime,
Expand Down
3 changes: 1 addition & 2 deletions vms/platformvm/blocks/executor/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
Expand Down Expand Up @@ -482,7 +481,7 @@ func TestVerifyUnverifiedParent(t *testing.T) {

// Set expectations for dependencies.
s.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
s.EXPECT().GetStatelessBlock(parentID).Return(nil, choices.Unknown, database.ErrNotFound).Times(1)
s.EXPECT().GetStatelessBlock(parentID).Return(nil, database.ErrNotFound).Times(1)

// Verify the block.
err = blk.Visit(verifier)
Expand Down
8 changes: 3 additions & 5 deletions vms/platformvm/state/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cccba11

Please sign in to comment.