Skip to content

Commit

Permalink
Only initialize Txs once (#2538)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
joshua-kim and StephenButtolph authored Dec 22, 2023
1 parent b7db25b commit f0c150d
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 31 deletions.
4 changes: 2 additions & 2 deletions vms/platformvm/block/abort_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewBanffAbortBlock(
},
},
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}

type ApricotAbortBlock struct {
Expand Down Expand Up @@ -78,5 +78,5 @@ func NewApricotAbortBlock(
Hght: height,
},
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}
2 changes: 1 addition & 1 deletion vms/platformvm/block/atomic_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func NewApricotAtomicBlock(
},
Tx: tx,
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}
6 changes: 4 additions & 2 deletions vms/platformvm/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ type BanffBlock interface {
Timestamp() time.Time
}

func initialize(blk Block) error {
func initialize(blk Block, commonBlk *CommonBlock) error {
// We serialize this block as a pointer so that it can be deserialized into
// a Block
bytes, err := Codec.Marshal(Version, &blk)
if err != nil {
return fmt.Errorf("couldn't marshal block: %w", err)
}
return blk.initialize(bytes)

commonBlk.initialize(bytes)
return nil
}
4 changes: 2 additions & 2 deletions vms/platformvm/block/commit_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewBanffCommitBlock(
},
},
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}

type ApricotCommitBlock struct {
Expand Down Expand Up @@ -75,5 +75,5 @@ func NewApricotCommitBlock(
Hght: height,
},
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}
4 changes: 2 additions & 2 deletions vms/platformvm/block/proposal_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewBanffProposalBlock(
Tx: proposalTx,
},
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}

type ApricotProposalBlock struct {
Expand Down Expand Up @@ -119,5 +119,5 @@ func NewApricotProposalBlock(
},
Tx: tx,
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}
6 changes: 4 additions & 2 deletions vms/platformvm/block/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ func TestBanffBlockSerialization(t *testing.T) {

for _, test := range tests {
testName := fmt.Sprintf("%T", test.block)
block := test.block
t.Run(testName, func(t *testing.T) {
require := require.New(t)

require.NoError(initialize(test.block))
require.Equal(test.bytes, test.block.Bytes())
got, err := Codec.Marshal(Version, &block)
require.NoError(err)
require.Equal(test.bytes, got)
})
}
}
4 changes: 2 additions & 2 deletions vms/platformvm/block/standard_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewBanffStandardBlock(
Transactions: txs,
},
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}

type ApricotStandardBlock struct {
Expand Down Expand Up @@ -93,5 +93,5 @@ func NewApricotStandardBlock(
},
Transactions: txs,
}
return blk, initialize(blk)
return blk, initialize(blk, &blk.CommonBlock)
}
38 changes: 22 additions & 16 deletions vms/platformvm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,23 +581,25 @@ func TestParsedStateBlock(t *testing.T) {
}

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

{
blk, err := block.NewApricotStandardBlock(ids.GenerateTestID(), 1000, []*txs.Tx{
{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
tx := &txs.Tx{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
})
}
require.NoError(tx.Initialize(txs.Codec))
blk, err := block.NewApricotStandardBlock(ids.GenerateTestID(), 1000, []*txs.Tx{tx})
require.NoError(err)
blks = append(blks, blk)
}
Expand All @@ -615,23 +617,27 @@ func TestParsedStateBlock(t *testing.T) {
}

{
blk, err := block.NewBanffProposalBlock(time.Now(), ids.GenerateTestID(), 1000, &txs.Tx{
tx := &txs.Tx{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
}, []*txs.Tx{})
}
require.NoError(tx.Initialize(txs.Codec))

blk, err := block.NewBanffProposalBlock(time.Now(), ids.GenerateTestID(), 1000, tx, []*txs.Tx{})
require.NoError(err)
blks = append(blks, blk)
}

{
blk, err := block.NewBanffStandardBlock(time.Now(), ids.GenerateTestID(), 1000, []*txs.Tx{
{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
tx := &txs.Tx{
Unsigned: &txs.RewardValidatorTx{
TxID: ids.GenerateTestID(),
},
})
}
require.NoError(tx.Initialize(txs.Codec))

blk, err := block.NewBanffStandardBlock(time.Now(), ids.GenerateTestID(), 1000, []*txs.Tx{tx})
require.NoError(err)
blks = append(blks, blk)
}
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ func TestUnverifiedParent(t *testing.T) {
firstAdvanceTimeBlk := vm.manager.NewBlock(statelessBlk)
require.NoError(firstAdvanceTimeBlk.Verify(context.Background()))

// include a tx1 to make the block be accepted
// include a tx2 to make the block be accepted
tx2 := &txs.Tx{Unsigned: &txs.ImportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: vm.ctx.NetworkID,
Expand All @@ -1675,7 +1675,7 @@ func TestUnverifiedParent(t *testing.T) {
},
}},
}}
require.NoError(tx1.Initialize(txs.Codec))
require.NoError(tx2.Initialize(txs.Codec))
nextChainTime = nextChainTime.Add(time.Second)
vm.clock.Set(nextChainTime)
statelessSecondAdvanceTimeBlk, err := block.NewBanffStandardBlock(
Expand Down

0 comments on commit f0c150d

Please sign in to comment.