Skip to content

Commit

Permalink
only initialize txs once
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Dec 22, 2023
1 parent 4458432 commit a6fa28a
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 42 deletions.
25 changes: 23 additions & 2 deletions vms/platformvm/block/abort_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package block

import (
"fmt"
"time"

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -43,7 +44,17 @@ func NewBanffAbortBlock(
},
},
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}

type ApricotAbortBlock struct {
Expand Down Expand Up @@ -78,5 +89,15 @@ func NewApricotAbortBlock(
Hght: height,
},
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}
12 changes: 11 additions & 1 deletion vms/platformvm/block/atomic_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,15 @@ func NewApricotAtomicBlock(
},
Tx: tx,
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}
11 changes: 0 additions & 11 deletions vms/platformvm/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package block

import (
"fmt"
"time"

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -35,13 +34,3 @@ type BanffBlock interface {
Block
Timestamp() time.Time
}

func initialize(blk Block) 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)
}
25 changes: 23 additions & 2 deletions vms/platformvm/block/commit_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package block

import (
"fmt"
"time"

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -43,7 +44,17 @@ func NewBanffCommitBlock(
},
},
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}

type ApricotCommitBlock struct {
Expand Down Expand Up @@ -75,5 +86,15 @@ func NewApricotCommitBlock(
Hght: height,
},
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}
24 changes: 22 additions & 2 deletions vms/platformvm/block/proposal_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ func NewBanffProposalBlock(
Tx: proposalTx,
},
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}

type ApricotProposalBlock struct {
Expand Down Expand Up @@ -124,5 +134,15 @@ func NewApricotProposalBlock(
},
Tx: tx,
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}
2 changes: 1 addition & 1 deletion vms/platformvm/block/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestBanffBlockSerialization(t *testing.T) {
t.Run(testName, func(t *testing.T) {
require := require.New(t)

require.NoError(initialize(test.block))
require.NoError(test.block.initialize(test.bytes))
require.Equal(test.bytes, test.block.Bytes())
})
}
Expand Down
24 changes: 22 additions & 2 deletions vms/platformvm/block/standard_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ func NewBanffStandardBlock(
Transactions: txs,
},
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}

type ApricotStandardBlock struct {
Expand Down Expand Up @@ -93,5 +103,15 @@ func NewApricotStandardBlock(
},
Transactions: txs,
}
return blk, initialize(blk)

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

blk.CommonBlock.initialize(bytes)
return blk, nil
}
40 changes: 23 additions & 17 deletions vms/platformvm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ func TestParsedStateBlock(t *testing.T) {
require := require.New(t)

var blks []block.Block

{
blk, err := block.NewApricotAbortBlock(ids.GenerateTestID(), 1000)
require.NoError(err)
Expand All @@ -581,23 +580,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 +616,28 @@ func TestParsedStateBlock(t *testing.T) {
}

{

Check failure on line 618 in vms/platformvm/state/state_test.go

View workflow job for this annotation

GitHub Actions / Lint

empty-lines: extra empty line at the start of a block (revive)
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
8 changes: 6 additions & 2 deletions vms/platformvm/txs/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ func NewSigned(
c codec.Manager,
signers [][]*secp256k1.PrivateKey,
) (*Tx, error) {
res := &Tx{Unsigned: unsigned}
return res, res.Sign(c, signers)
tx := &Tx{Unsigned: unsigned}
if err := tx.Sign(c, signers); err != nil {
return nil, fmt.Errorf("failed to sign: %w", err)
}

return tx, tx.Initialize(c)
}

func (tx *Tx) Initialize(c codec.Manager) error {
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 a6fa28a

Please sign in to comment.