Skip to content

Commit

Permalink
vms/platformvm: Use snowtest.Context helper (#2515)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu committed Dec 21, 2023
1 parent 35907e0 commit 3e45123
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 205 deletions.
49 changes: 9 additions & 40 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package builder

import (
"context"
"errors"
"testing"
"time"

Expand All @@ -24,6 +22,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/snowtest"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
Expand Down Expand Up @@ -69,18 +68,13 @@ var (
defaultMinValidatorStake = 5 * units.MilliAvax
defaultBalance = 100 * defaultMinValidatorStake
preFundedKeys = secp256k1.TestKeys()
avaxAssetID = ids.ID{'y', 'e', 'e', 't'}
defaultTxFee = uint64(100)
xChainID = ids.Empty.Prefix(0)
cChainID = ids.Empty.Prefix(1)

testSubnet1 *txs.Tx
testSubnet1ControlKeys = preFundedKeys[0:3]

// Node IDs of genesis validators. Initialized in init function
genesisNodeIDs []ids.NodeID

errMissing = errors.New("missing")
)

func init() {
Expand Down Expand Up @@ -127,7 +121,14 @@ func newEnvironment(t *testing.T) *environment {
res.isBootstrapped.Set(true)

res.baseDB = versiondb.New(memdb.New())
res.ctx, res.msm = defaultCtx(res.baseDB)
atomicDB := prefixdb.New([]byte{1}, res.baseDB)
m := atomic.NewMemory(atomicDB)

res.ctx = snowtest.Context(t, snowtest.PChainID)
res.msm = &mutableSharedMemory{
SharedMemory: m.NewSharedMemory(res.ctx.ChainID),
}
res.ctx.SharedMemory = res.msm

res.ctx.Lock.Lock()
defer res.ctx.Lock.Unlock()
Expand Down Expand Up @@ -264,38 +265,6 @@ func defaultState(
return state
}

func defaultCtx(db database.Database) (*snow.Context, *mutableSharedMemory) {
ctx := snow.DefaultContextTest()
ctx.NetworkID = 10
ctx.XChainID = xChainID
ctx.CChainID = cChainID
ctx.AVAXAssetID = avaxAssetID

atomicDB := prefixdb.New([]byte{1}, db)
m := atomic.NewMemory(atomicDB)

msm := &mutableSharedMemory{
SharedMemory: m.NewSharedMemory(ctx.ChainID),
}
ctx.SharedMemory = msm

ctx.ValidatorState = &validators.TestState{
GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) {
subnetID, ok := map[ids.ID]ids.ID{
constants.PlatformChainID: constants.PrimaryNetworkID,
xChainID: constants.PrimaryNetworkID,
cChainID: constants.PrimaryNetworkID,
}[chainID]
if !ok {
return ids.Empty, errMissing
}
return subnetID, nil
},
}

return ctx, msm
}

func defaultConfig() *config.Config {
return &config.Config{
Chains: chains.TestManager,
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/builder/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestAtomicTxImports(t *testing.T) {
peerSharedMemory := m.NewSharedMemory(env.ctx.XChainID)
utxo := &avax.UTXO{
UTXOID: utxoID,
Asset: avax.Asset{ID: avaxAssetID},
Asset: avax.Asset{ID: env.ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: secp256k1fx.OutputOwners{
Expand Down
44 changes: 8 additions & 36 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package executor

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand All @@ -25,6 +23,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/snowtest"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
Expand Down Expand Up @@ -73,16 +72,12 @@ var (
preFundedKeys = secp256k1.TestKeys()
avaxAssetID = ids.ID{'y', 'e', 'e', 't'}
defaultTxFee = uint64(100)
xChainID = ids.Empty.Prefix(0)
cChainID = ids.Empty.Prefix(1)

genesisBlkID ids.ID
testSubnet1 *txs.Tx

// Node IDs of genesis validators. Initialized in init function
genesisNodeIDs []ids.NodeID

errMissing = errors.New("missing")
)

func init() {
Expand Down Expand Up @@ -138,7 +133,13 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
res.isBootstrapped.Set(true)

res.baseDB = versiondb.New(memdb.New())
res.ctx = defaultCtx(res.baseDB)
atomicDB := prefixdb.New([]byte{1}, res.baseDB)
m := atomic.NewMemory(atomicDB)

res.ctx = snowtest.Context(t, snowtest.PChainID)
res.ctx.AVAXAssetID = avaxAssetID
res.ctx.SharedMemory = m.NewSharedMemory(res.ctx.ChainID)

res.fx = defaultFx(res.clk, res.ctx.Log, res.isBootstrapped.Get())

rewardsCalc := reward.NewCalculator(res.config.RewardConfig)
Expand Down Expand Up @@ -293,35 +294,6 @@ func defaultState(
return state
}

func defaultCtx(db database.Database) *snow.Context {
ctx := snow.DefaultContextTest()
ctx.NetworkID = 10
ctx.XChainID = xChainID
ctx.CChainID = cChainID
ctx.AVAXAssetID = avaxAssetID

atomicDB := prefixdb.New([]byte{1}, db)
m := atomic.NewMemory(atomicDB)

ctx.SharedMemory = m.NewSharedMemory(ctx.ChainID)

ctx.ValidatorState = &validators.TestState{
GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) {
subnetID, ok := map[ids.ID]ids.ID{
constants.PlatformChainID: constants.PrimaryNetworkID,
xChainID: constants.PrimaryNetworkID,
cChainID: constants.PrimaryNetworkID,
}[chainID]
if !ok {
return ids.Empty, errMissing
}
return subnetID, nil
},
}

return ctx
}

func defaultConfig() *config.Config {
return &config.Config{
Chains: chains.TestManager,
Expand Down
17 changes: 11 additions & 6 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ func TestGetTxStatus(t *testing.T) {
m := atomic.NewMemory(prefixdb.New([]byte{}, service.vm.db))

sm := m.NewSharedMemory(service.vm.ctx.ChainID)
peerSharedMemory := m.NewSharedMemory(xChainID)
peerSharedMemory := m.NewSharedMemory(service.vm.ctx.XChainID)

// #nosec G404
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: ids.GenerateTestID(),
OutputIndex: rand.Uint32(),
},
Asset: avax.Asset{ID: avaxAssetID},
Asset: avax.Asset{ID: service.vm.ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: 1234567,
OutputOwners: secp256k1fx.OutputOwners{
Expand Down Expand Up @@ -220,7 +220,12 @@ func TestGetTxStatus(t *testing.T) {
oldSharedMemory := mutableSharedMemory.SharedMemory
mutableSharedMemory.SharedMemory = sm

tx, err := service.vm.txBuilder.NewImportTx(xChainID, ids.ShortEmpty, []*secp256k1.PrivateKey{recipientKey}, ids.ShortEmpty)
tx, err := service.vm.txBuilder.NewImportTx(
service.vm.ctx.XChainID,
ids.ShortEmpty,
[]*secp256k1.PrivateKey{recipientKey},
ids.ShortEmpty,
)
require.NoError(err)

mutableSharedMemory.SharedMemory = oldSharedMemory
Expand Down Expand Up @@ -399,7 +404,7 @@ func TestGetBalance(t *testing.T) {
}()

// Ensure GetStake is correct for each of the genesis validators
genesis, _ := defaultGenesis(t)
genesis, _ := defaultGenesis(t, service.vm.ctx.AVAXAssetID)
for idx, utxo := range genesis.UTXOs {
request := GetBalanceRequest{
Addresses: []string{
Expand Down Expand Up @@ -433,7 +438,7 @@ func TestGetStake(t *testing.T) {
}()

// Ensure GetStake is correct for each of the genesis validators
genesis, _ := defaultGenesis(t)
genesis, _ := defaultGenesis(t, service.vm.ctx.AVAXAssetID)
addrsStrs := []string{}
for i, validator := range genesis.Validators {
addr := fmt.Sprintf("P-%s", validator.RewardOwner.Addresses[0])
Expand Down Expand Up @@ -608,7 +613,7 @@ func TestGetCurrentValidators(t *testing.T) {
service.vm.ctx.Lock.Unlock()
}()

genesis, _ := defaultGenesis(t)
genesis, _ := defaultGenesis(t, service.vm.ctx.AVAXAssetID)

// Call getValidators
args := GetCurrentValidatorsArgs{SubnetID: constants.PrimaryNetworkID}
Expand Down
8 changes: 3 additions & 5 deletions vms/platformvm/txs/add_delegator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/snowtest"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
Expand All @@ -23,8 +23,7 @@ var preFundedKeys = secp256k1.TestKeys()
func TestAddDelegatorTxSyntacticVerify(t *testing.T) {
require := require.New(t)
clk := mockable.Clock{}
ctx := snow.DefaultContextTest()
ctx.AVAXAssetID = ids.GenerateTestID()
ctx := snowtest.Context(t, snowtest.PChainID)
signers := [][]*secp256k1.PrivateKey{preFundedKeys}

var (
Expand Down Expand Up @@ -130,8 +129,7 @@ func TestAddDelegatorTxSyntacticVerify(t *testing.T) {
func TestAddDelegatorTxSyntacticVerifyNotAVAX(t *testing.T) {
require := require.New(t)
clk := mockable.Clock{}
ctx := snow.DefaultContextTest()
ctx.AVAXAssetID = ids.GenerateTestID()
ctx := snowtest.Context(t, snowtest.PChainID)
signers := [][]*secp256k1.PrivateKey{preFundedKeys}

var (
Expand Down
8 changes: 3 additions & 5 deletions vms/platformvm/txs/add_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/snowtest"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
Expand All @@ -22,8 +22,7 @@ import (
func TestAddValidatorTxSyntacticVerify(t *testing.T) {
require := require.New(t)
clk := mockable.Clock{}
ctx := snow.DefaultContextTest()
ctx.AVAXAssetID = ids.GenerateTestID()
ctx := snowtest.Context(t, snowtest.PChainID)
signers := [][]*secp256k1.PrivateKey{preFundedKeys}

var (
Expand Down Expand Up @@ -146,8 +145,7 @@ func TestAddValidatorTxSyntacticVerify(t *testing.T) {
func TestAddValidatorTxSyntacticVerifyNotAVAX(t *testing.T) {
require := require.New(t)
clk := mockable.Clock{}
ctx := snow.DefaultContextTest()
ctx.AVAXAssetID = ids.GenerateTestID()
ctx := snowtest.Context(t, snowtest.PChainID)
signers := [][]*secp256k1.PrivateKey{preFundedKeys}

var (
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/txs/executor/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestNewExportTx(t *testing.T) {
tests := []test{
{
description: "P->X export",
destinationChainID: xChainID,
destinationChainID: env.ctx.XChainID,
sourceKeys: []*secp256k1.PrivateKey{sourceKey},
timestamp: defaultValidateStartTime,
},
{
description: "P->C export",
destinationChainID: cChainID,
destinationChainID: env.ctx.CChainID,
sourceKeys: []*secp256k1.PrivateKey{sourceKey},
timestamp: env.config.ApricotPhase5Time,
},
Expand Down
Loading

0 comments on commit 3e45123

Please sign in to comment.