Skip to content

Commit

Permalink
vms/platformvm: Add decisionTxs parameter to `NewBanffProposalBlo…
Browse files Browse the repository at this point in the history
…ck` (#2411)

Co-authored-by: Dan Laine <[email protected]>
  • Loading branch information
dhrubabasu and Dan Laine authored Dec 5, 2023
1 parent 439dc1e commit 004a23e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 26 deletions.
1 change: 1 addition & 0 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func buildBlock(
parentID,
height,
rewardValidatorTx,
[]*txs.Tx{}, // TODO: Populate with StandardBlock txs
)
}

Expand Down
1 change: 1 addition & 0 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ func TestBuildBlock(t *testing.T) {
parentID,
height,
tx,
[]*txs.Tx{},
)
require.NoError(err)
return expectedBlk
Expand Down
14 changes: 14 additions & 0 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height(),
blkTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand Down Expand Up @@ -287,6 +288,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height()+1,
blkTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand All @@ -304,6 +306,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height()+1,
blkTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand All @@ -321,6 +324,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height()+1,
blkTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand All @@ -342,6 +346,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height()+1,
invalidTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand All @@ -357,6 +362,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height()+1,
blkTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand All @@ -373,6 +379,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentID,
banffParentBlk.Height()+1,
blkTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand Down Expand Up @@ -662,6 +669,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand Down Expand Up @@ -819,6 +827,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)
propBlk := env.blkManager.NewBlock(statelessProposalBlock)
Expand Down Expand Up @@ -931,6 +940,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)
propBlk := env.blkManager.NewBlock(statelessProposalBlock)
Expand Down Expand Up @@ -1017,6 +1027,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)
propBlk := env.blkManager.NewBlock(statelessProposalBlock)
Expand Down Expand Up @@ -1108,6 +1119,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand Down Expand Up @@ -1198,6 +1210,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)
propBlk := env.blkManager.NewBlock(statelessProposalBlock)
Expand Down Expand Up @@ -1288,6 +1301,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
parentBlk.ID(),
parentBlk.Height()+1,
s0RewardTx,
[]*txs.Tx{},
)
require.NoError(err)
propBlk = env.blkManager.NewBlock(statelessProposalBlock)
Expand Down
1 change: 1 addition & 0 deletions vms/platformvm/block/executor/rejector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestRejectBlock(t *testing.T) {
},
Creds: []verify.Verifiable{},
},
[]*txs.Tx{},
)
},
rejectFunc: func(r *rejector, b block.Block) error {
Expand Down
33 changes: 33 additions & 0 deletions vms/platformvm/block/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func TestProposalBlocks(t *testing.T) {
height := uint64(2022)
proposalTx, err := testProposalTx()
require.NoError(err)
decisionTxs, err := testDecisionTxs()
require.NoError(err)

for _, cdc := range []codec.Manager{Codec, GenesisCodec} {
// build block
Expand Down Expand Up @@ -109,6 +111,7 @@ func TestProposalBlocks(t *testing.T) {
parentID,
height,
proposalTx,
[]*txs.Tx{},
)
require.NoError(err)

Expand All @@ -130,6 +133,36 @@ func TestProposalBlocks(t *testing.T) {

// backward compatibility check
require.Equal(parsedApricotProposalBlk.Txs(), parsedBanffProposalBlk.Txs())

// check that banff proposal block with decisionTxs can be built and parsed
banffProposalBlkWithDecisionTxs, err := NewBanffProposalBlock(
blkTimestamp,
parentID,
height,
proposalTx,
decisionTxs,
)
require.NoError(err)

// parse block
parsed, err = Parse(cdc, banffProposalBlkWithDecisionTxs.Bytes())
require.NoError(err)

// compare content
require.Equal(banffProposalBlkWithDecisionTxs.ID(), parsed.ID())
require.Equal(banffProposalBlkWithDecisionTxs.Bytes(), parsed.Bytes())
require.Equal(banffProposalBlkWithDecisionTxs.Parent(), parsed.Parent())
require.Equal(banffProposalBlkWithDecisionTxs.Height(), parsed.Height())
require.IsType(&BanffProposalBlock{}, parsed)
parsedBanffProposalBlkWithDecisionTxs := parsed.(*BanffProposalBlock)

l := len(decisionTxs)
expectedTxs := make([]*txs.Tx, l+1)
copy(expectedTxs, decisionTxs)
expectedTxs[l] = proposalTx
require.Equal(expectedTxs, parsedBanffProposalBlkWithDecisionTxs.Txs())

require.Equal(banffProposalBlkWithDecisionTxs.Timestamp(), parsedBanffProposalBlkWithDecisionTxs.Timestamp())
}
}

Expand Down
8 changes: 5 additions & 3 deletions vms/platformvm/block/proposal_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ func NewBanffProposalBlock(
timestamp time.Time,
parentID ids.ID,
height uint64,
tx *txs.Tx,
proposalTx *txs.Tx,
decisionTxs []*txs.Tx,
) (*BanffProposalBlock, error) {
blk := &BanffProposalBlock{
Time: uint64(timestamp.Unix()),
Transactions: decisionTxs,
Time: uint64(timestamp.Unix()),
ApricotProposalBlock: ApricotProposalBlock{
CommonBlock: CommonBlock{
PrntID: parentID,
Hght: height,
},
Tx: tx,
Tx: proposalTx,
},
}
return blk, initialize(blk)
Expand Down
88 changes: 66 additions & 22 deletions vms/platformvm/block/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,70 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)

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

timestamp := time.Now().Truncate(time.Second)
parentID := ids.GenerateTestID()
height := uint64(1337)
proposalTx, err := testProposalTx()
require.NoError(err)
require.NoError(t, err)
decisionTxs, err := testDecisionTxs()
require.NoError(t, err)

blk, err := NewBanffProposalBlock(
timestamp,
parentID,
height,
proposalTx,
)
require.NoError(err)
type test struct {
name string
proposalTx *txs.Tx
decisionTxs []*txs.Tx
}

// Make sure the block and tx are initialized
require.NotEmpty(blk.Bytes())
require.NotEmpty(blk.Tx.Bytes())
require.NotEqual(ids.Empty, blk.Tx.ID())
require.Equal(proposalTx.Bytes(), blk.Tx.Bytes())
require.Equal(timestamp, blk.Timestamp())
require.Equal(parentID, blk.Parent())
require.Equal(height, blk.Height())
tests := []test{
{
name: "no decision txs",
proposalTx: proposalTx,
decisionTxs: []*txs.Tx{},
},
{
name: "decision txs",
proposalTx: proposalTx,
decisionTxs: decisionTxs,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

blk, err := NewBanffProposalBlock(
timestamp,
parentID,
height,
test.proposalTx,
test.decisionTxs,
)
require.NoError(err)

require.NotEmpty(blk.Bytes())
require.Equal(parentID, blk.Parent())
require.Equal(height, blk.Height())
require.Equal(timestamp, blk.Timestamp())

l := len(test.decisionTxs)
expectedTxs := make([]*txs.Tx, l+1)
copy(expectedTxs, test.decisionTxs)
expectedTxs[l] = test.proposalTx

blkTxs := blk.Txs()
require.Equal(expectedTxs, blkTxs)
for i, blkTx := range blkTxs {
expectedTx := expectedTxs[i]
require.NotEmpty(blkTx.Bytes())
require.NotEqual(ids.Empty, blkTx.ID())
require.Equal(expectedTx.Bytes(), blkTx.Bytes())
}
})
}
}

func TestNewApricotProposalBlock(t *testing.T) {
Expand All @@ -54,11 +91,18 @@ func TestNewApricotProposalBlock(t *testing.T) {
)
require.NoError(err)

// Make sure the block and tx are initialized
require.NotEmpty(blk.Bytes())
require.NotEmpty(blk.Tx.Bytes())
require.NotEqual(ids.Empty, blk.Tx.ID())
require.Equal(proposalTx.Bytes(), blk.Tx.Bytes())
require.Equal(parentID, blk.Parent())
require.Equal(height, blk.Height())

expectedTxs := []*txs.Tx{proposalTx}

blkTxs := blk.Txs()
require.Equal(blkTxs, expectedTxs)
for i, blkTx := range blkTxs {
expectedTx := expectedTxs[i]
require.NotEmpty(blkTx.Bytes())
require.NotEqual(ids.Empty, blkTx.ID())
require.Equal(expectedTx.Bytes(), blkTx.Bytes())
}
}
2 changes: 1 addition & 1 deletion vms/platformvm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func TestParsedStateBlock(t *testing.T) {
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
})
}, []*txs.Tx{})
require.NoError(err)
blks = append(blks, blk)
}
Expand Down

0 comments on commit 004a23e

Please sign in to comment.