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

[vms/platformvm] Rename txstest.Builder to txstest.WalletFactory #2890

Merged
merged 18 commits into from
Jun 6, 2024
38 changes: 25 additions & 13 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
Expand All @@ -29,6 +28,7 @@ import (

blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor"
txexecutor "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
walletsigner "github.com/ava-labs/avalanchego/wallet/chain/p/signer"
)

func TestBuildBlockBasic(t *testing.T) {
Expand All @@ -39,15 +39,17 @@ func TestBuildBlockBasic(t *testing.T) {
defer env.ctx.Lock.Unlock()

// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
builder, signer := env.factory.MakeWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1])
utx, err := builder.NewCreateChainTx(
testSubnet1.ID(),
nil,
constants.AVMID,
nil,
"chain name",
[]*secp256k1.PrivateKey{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)
txID := tx.ID()

// Issue the transaction
Expand Down Expand Up @@ -109,7 +111,8 @@ func TestBuildBlockShouldReward(t *testing.T) {
require.NoError(err)

// Create a valid [AddPermissionlessValidatorTx]
tx, err := env.txBuilder.NewAddPermissionlessValidatorTx(
builder, txSigner := env.factory.MakeWallet(preFundedKeys[0])
utx, err := builder.NewAddPermissionlessValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: nodeID,
Expand All @@ -130,13 +133,14 @@ func TestBuildBlockShouldReward(t *testing.T) {
Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()},
},
reward.PercentDenominator,
[]*secp256k1.PrivateKey{preFundedKeys[0]},
common.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()},
}),
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), txSigner, utx)
require.NoError(err)
txID := tx.ID()

// Issue the transaction
Expand Down Expand Up @@ -247,15 +251,17 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) {
defer env.ctx.Lock.Unlock()

// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
builder, signer := env.factory.MakeWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1])
utx, err := builder.NewCreateChainTx(
testSubnet1.ID(),
nil,
constants.AVMID,
nil,
"chain name",
[]*secp256k1.PrivateKey{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)
txID := tx.ID()

// Issue the transaction
Expand Down Expand Up @@ -314,7 +320,8 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) {
sk, err := bls.NewSecretKey()
require.NoError(err)

tx1, err := env.txBuilder.NewAddPermissionlessValidatorTx(
builder1, signer1 := env.factory.MakeWallet(preFundedKeys[0])
utx1, err := builder1.NewAddPermissionlessValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: ids.GenerateTestNodeID(),
Expand All @@ -335,13 +342,14 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) {
Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()},
},
reward.PercentDenominator,
[]*secp256k1.PrivateKey{preFundedKeys[0]},
common.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()},
}),
)
require.NoError(err)
tx1, err := walletsigner.SignUnsigned(context.Background(), signer1, utx1)
require.NoError(err)
require.NoError(env.mempool.Add(tx1))
tx1ID := tx1.ID()
_, ok := env.mempool.Get(tx1ID)
Expand All @@ -353,7 +361,8 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) {
sk, err = bls.NewSecretKey()
require.NoError(err)

tx2, err := env.txBuilder.NewAddPermissionlessValidatorTx(
builder2, signer2 := env.factory.MakeWallet(preFundedKeys[2])
utx2, err := builder2.NewAddPermissionlessValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: ids.GenerateTestNodeID(),
Expand All @@ -374,13 +383,14 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) {
Addrs: []ids.ShortID{preFundedKeys[2].PublicKey().Address()},
},
reward.PercentDenominator,
[]*secp256k1.PrivateKey{preFundedKeys[2]},
common.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{preFundedKeys[2].PublicKey().Address()},
}),
)
require.NoError(err)
tx2, err := walletsigner.SignUnsigned(context.Background(), signer2, utx2)
require.NoError(err)
require.NoError(env.mempool.Add(tx2))
tx2ID := tx2.ID()
_, ok = env.mempool.Get(tx2ID)
Expand Down Expand Up @@ -416,15 +426,17 @@ func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) {
defer env.ctx.Lock.Unlock()

// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
builder, signer := env.factory.MakeWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1])
utx, err := builder.NewCreateChainTx(
testSubnet1.ID(),
nil,
constants.AVMID,
nil,
"chain name",
[]*secp256k1.PrivateKey{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)
txID := tx.ID()

// Transaction should not be marked as dropped before being added to the
Expand Down
19 changes: 7 additions & 12 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor"
txexecutor "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
pvalidators "github.com/ava-labs/avalanchego/vms/platformvm/validators"
walletsigner "github.com/ava-labs/avalanchego/wallet/chain/p/signer"
walletcommon "github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

Expand Down Expand Up @@ -118,7 +119,7 @@ type environment struct {
state state.State
uptimes uptime.Manager
utxosVerifier utxo.Verifier
txBuilder *txstest.Builder
factory *txstest.WalletFactory
backend txexecutor.Backend
}

Expand Down Expand Up @@ -152,12 +153,7 @@ func newEnvironment(t *testing.T, f fork) *environment { //nolint:unparam

res.uptimes = uptime.NewManager(res.state, res.clk)
res.utxosVerifier = utxo.NewVerifier(res.ctx, res.clk, res.fx)

res.txBuilder = txstest.NewBuilder(
res.ctx,
res.config,
res.state,
)
res.factory = txstest.NewWalletFactory(res.ctx, res.config, res.state)

genesisID := res.state.GetLastAccepted()
res.backend = txexecutor.Backend{
Expand Down Expand Up @@ -240,9 +236,8 @@ func newEnvironment(t *testing.T, f fork) *environment { //nolint:unparam
func addSubnet(t *testing.T, env *environment) {
require := require.New(t)

// Create a subnet
var err error
testSubnet1, err = env.txBuilder.NewCreateSubnetTx(
builder, signer := env.factory.MakeWallet(preFundedKeys[0])
utx, err := builder.NewCreateSubnetTx(
&secp256k1fx.OutputOwners{
Threshold: 2,
Addrs: []ids.ShortID{
Expand All @@ -251,15 +246,15 @@ func addSubnet(t *testing.T, env *environment) {
preFundedKeys[2].PublicKey().Address(),
},
},
[]*secp256k1.PrivateKey{preFundedKeys[0]},
walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()},
}),
)
require.NoError(err)
testSubnet1, err = walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

// store it
genesisID := env.state.GetLastAccepted()
stateDiff, err := state.NewDiff(genesisID, env.blkManager)
require.NoError(err)
Expand Down
9 changes: 6 additions & 3 deletions vms/platformvm/block/builder/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

walletsigner "github.com/ava-labs/avalanchego/wallet/chain/p/signer"
)

func TestAtomicTxImports(t *testing.T) {
Expand Down Expand Up @@ -62,15 +63,17 @@ func TestAtomicTxImports(t *testing.T) {
}}},
}))

tx, err := env.txBuilder.NewImportTx(
builder, signer := env.factory.MakeWallet(recipientKey)
utx, err := builder.NewImportTx(
env.ctx.XChainID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{recipientKey.PublicKey().Address()},
},
[]*secp256k1.PrivateKey{recipientKey},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

require.NoError(env.Builder.Add(tx))
b, err := env.Builder.BuildBlock(context.Background())
Expand Down
28 changes: 17 additions & 11 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package executor

import (
"context"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -52,6 +53,7 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

pvalidators "github.com/ava-labs/avalanchego/vms/platformvm/validators"
walletsigner "github.com/ava-labs/avalanchego/wallet/chain/p/signer"
walletcommon "github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

Expand Down Expand Up @@ -130,7 +132,7 @@ type environment struct {
mockedState *state.MockState
uptimes uptime.Manager
utxosVerifier utxo.Verifier
txBuilder *txstest.Builder
factory *txstest.WalletFactory
backend *executor.Backend
}

Expand Down Expand Up @@ -158,7 +160,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f fork) *environment
res.state = defaultState(res.config, res.ctx, res.baseDB, rewardsCalc)
res.uptimes = uptime.NewManager(res.state, res.clk)
res.utxosVerifier = utxo.NewVerifier(res.ctx, res.clk, res.fx)
res.txBuilder = txstest.NewBuilder(
res.factory = txstest.NewWalletFactory(
res.ctx,
res.config,
res.state,
Expand All @@ -168,8 +170,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f fork) *environment
res.mockedState = state.NewMockState(ctrl)
res.uptimes = uptime.NewManager(res.mockedState, res.clk)
res.utxosVerifier = utxo.NewVerifier(res.ctx, res.clk, res.fx)

res.txBuilder = txstest.NewBuilder(
res.factory = txstest.NewWalletFactory(
res.ctx,
res.config,
res.mockedState,
Expand Down Expand Up @@ -251,9 +252,8 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f fork) *environment
}

func addSubnet(env *environment) {
// Create a subnet
var err error
testSubnet1, err = env.txBuilder.NewCreateSubnetTx(
builder, signer := env.factory.MakeWallet(preFundedKeys[0])
utx, err := builder.NewCreateSubnetTx(
&secp256k1fx.OutputOwners{
Threshold: 2,
Addrs: []ids.ShortID{
Expand All @@ -262,7 +262,6 @@ func addSubnet(env *environment) {
preFundedKeys[2].PublicKey().Address(),
},
},
[]*secp256k1.PrivateKey{preFundedKeys[0]},
walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()},
Expand All @@ -271,8 +270,11 @@ func addSubnet(env *environment) {
if err != nil {
panic(err)
}
testSubnet1, err = walletsigner.SignUnsigned(context.Background(), signer, utx)
if err != nil {
panic(err)
}

// store it
genesisID := env.state.GetLastAccepted()
stateDiff, err := state.NewDiff(genesisID, env.blkManager)
if err != nil {
Expand Down Expand Up @@ -495,7 +497,8 @@ func addPendingValidator(
rewardAddress ids.ShortID,
keys []*secp256k1.PrivateKey,
) (*txs.Tx, error) {
addPendingValidatorTx, err := env.txBuilder.NewAddValidatorTx(
builder, signer := env.factory.MakeWallet(keys...)
utx, err := builder.NewAddValidatorTx(
&txs.Validator{
NodeID: nodeID,
Start: uint64(startTime.Unix()),
Expand All @@ -507,11 +510,14 @@ func addPendingValidator(
Addrs: []ids.ShortID{rewardAddress},
},
reward.PercentDenominator,
keys,
)
if err != nil {
return nil, err
}
addPendingValidatorTx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
if err != nil {
return nil, err
}

staker, err := state.NewPendingStaker(
addPendingValidatorTx.ID(),
Expand Down
Loading
Loading