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

Support both stateBlks and Blocks in blockDB #1748

Merged
merged 4 commits into from
Jul 22, 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
42 changes: 31 additions & 11 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type State interface {
Close() error
}

// TODO: Remove after v1.11.x is activated
type stateBlk struct {
Blk blocks.Block
Bytes []byte `serialize:"true"`
Expand Down Expand Up @@ -1564,24 +1565,18 @@ func (s *state) GetStatelessBlock(blockID ids.ID) (blocks.Block, error) {
return nil, err
}

// Note: stored blocks are verified, so it's safe to unmarshal them with GenesisCodec
blkState := stateBlk{}
if _, err := blocks.GenesisCodec.Unmarshal(blkBytes, &blkState); err != nil {
blk, status, _, err := parseStoredBlock(blkBytes)
if err != nil {
return nil, err
}

if blkState.Status != choices.Accepted {
if status != choices.Accepted {
s.blockCache.Put(blockID, nil)
return nil, database.ErrNotFound
}

blkState.Blk, err = blocks.Parse(blocks.GenesisCodec, blkState.Bytes)
if err != nil {
return nil, err
}

s.blockCache.Put(blockID, blkState.Blk)
return blkState.Blk, nil
s.blockCache.Put(blockID, blk)
return blk, nil
}

func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error {
Expand Down Expand Up @@ -1982,3 +1977,28 @@ func (s *state) writeMetadata() error {
}
return nil
}

// Returns the block, status of the block, and whether it is a [stateBlk].
// Invariant: blkBytes is safe to parse with blocks.GenesisCodec
//
// TODO: Remove after v1.11.x is activated
func parseStoredBlock(blkBytes []byte) (blocks.Block, choices.Status, bool, error) {
// Attempt to parse as blocks.Block
blk, err := blocks.Parse(blocks.GenesisCodec, blkBytes)
if err == nil {
return blk, choices.Accepted, false, nil
}

// Fallback to [stateBlk]
blkState := stateBlk{}
if _, err := blocks.GenesisCodec.Unmarshal(blkBytes, &blkState); err != nil {
return nil, choices.Processing, false, err
}

blkState.Blk, err = blocks.Parse(blocks.GenesisCodec, blkState.Bytes)
if err != nil {
return nil, choices.Processing, false, err
}

return blkState.Blk, blkState.Status, true, nil
}
106 changes: 106 additions & 0 deletions vms/platformvm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
Expand Down Expand Up @@ -837,3 +838,108 @@ func TestStateAddRemoveValidator(t *testing.T) {
require.Equal(diff.expectedPublicKeyDiff, gotPublicKeyDiffs)
}
}

func TestParsedStateBlock(t *testing.T) {
require := require.New(t)

var blks []blocks.Block

{
blk, err := blocks.NewApricotAbortBlock(ids.GenerateTestID(), 1000)
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewApricotAtomicBlock(ids.GenerateTestID(), 1000, &txs.Tx{
Unsigned: &txs.AdvanceTimeTx{
Time: 1000,
},
})
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewApricotCommitBlock(ids.GenerateTestID(), 1000)
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewApricotProposalBlock(ids.GenerateTestID(), 1000, &txs.Tx{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
})
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewApricotStandardBlock(ids.GenerateTestID(), 1000, []*txs.Tx{
{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
},
})
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewBanffAbortBlock(time.Now(), ids.GenerateTestID(), 1000)
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewBanffCommitBlock(time.Now(), ids.GenerateTestID(), 1000)
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewBanffProposalBlock(time.Now(), ids.GenerateTestID(), 1000, &txs.Tx{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
})
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := blocks.NewBanffStandardBlock(time.Now(), ids.GenerateTestID(), 1000, []*txs.Tx{
{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
},
})
require.NoError(err)
blks = append(blks, blk)
}

for _, blk := range blks {
stBlk := stateBlk{
Blk: blk,
Bytes: blk.Bytes(),
Status: choices.Accepted,
}

stBlkBytes, err := blocks.GenesisCodec.Marshal(blocks.Version, &stBlk)
require.NoError(err)

gotBlk, _, isStateBlk, err := parseStoredBlock(stBlkBytes)
require.NoError(err)
require.True(isStateBlk)
require.Equal(blk.ID(), gotBlk.ID())

gotBlk, _, isStateBlk, err = parseStoredBlock(blk.Bytes())
require.NoError(err)
require.False(isStateBlk)
require.Equal(blk.ID(), gotBlk.ID())
}
}