Skip to content

Commit

Permalink
lint and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samliok committed Dec 18, 2024
1 parent be4e122 commit 41aaaaa
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 196 deletions.
4 changes: 2 additions & 2 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
ID: snowtest.AVAXAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: 0, // set to 0?
Amt: 1,
},
}
utxoID := utxo.InputID()
Expand All @@ -164,7 +164,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
UTXOID: utxo.UTXOID,
Asset: utxo.Asset,
In: &secp256k1fx.TransferInput{
Amt: 0,
Amt: 1,
},
}},
}},
Expand Down
9 changes: 2 additions & 7 deletions vms/platformvm/block/executor/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ func (v *verifier) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
return err
}

var (
// timestamp = onCommitState.GetTimestamp() // Equal to parent timestamp
feeCalculator = txfee.NewSimpleCalculator(0)
)
feeCalculator := txfee.NewSimpleCalculator(0)
return v.proposalBlock(
b,
b.Tx,
Expand All @@ -202,9 +199,7 @@ func (v *verifier) ApricotStandardBlock(b *block.ApricotStandardBlock) error {
return err
}

var (
feeCalculator = txfee.NewSimpleCalculator(0)
)
feeCalculator := txfee.NewSimpleCalculator(0)
return v.standardBlock(
b,
b.Transactions,
Expand Down
30 changes: 15 additions & 15 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ var (
}
)

func defaultService(t *testing.T, fork upgradetest.Fork) (*Service, *mutableSharedMemory) {
vm, _, mutableSharedMemory := defaultVM(t, fork)
func defaultService(t *testing.T) (*Service, *mutableSharedMemory) {
vm, _, mutableSharedMemory := defaultVM(t, upgradetest.Latest)
return &Service{
vm: vm,
addrManager: avax.NewAddressManager(vm.ctx),
Expand All @@ -94,7 +94,7 @@ func defaultService(t *testing.T, fork upgradetest.Fork) (*Service, *mutableShar

func TestGetProposedHeight(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

reply := api.GetHeightResponse{}
require.NoError(service.GetProposedHeight(&http.Request{}, nil, &reply))
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGetProposedHeight(t *testing.T) {
func TestExportKey(t *testing.T) {
require := require.New(t)

service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)
service.vm.ctx.Lock.Lock()

ks := keystore.New(logging.NoLog{}, memdb.New())
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestExportKey(t *testing.T) {
// Test issuing a tx and accepted
func TestGetTxStatus(t *testing.T) {
require := require.New(t)
service, mutableSharedMemory := defaultService(t, upgradetest.Latest)
service, mutableSharedMemory := defaultService(t)
service.vm.ctx.Lock.Lock()

recipientKey, err := secp256k1.NewPrivateKey()
Expand Down Expand Up @@ -374,7 +374,7 @@ func TestGetTx(t *testing.T) {
)
t.Run(testName, func(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

service.vm.ctx.Lock.Lock()
tx := test.createTx(t, service)
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestGetTx(t *testing.T) {

func TestGetBalance(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Durango)
service, _ := defaultService(t)

feeCalculator := state.PickFeeCalculator(&service.vm.Internal, service.vm.state)
createSubnetFee, err := feeCalculator.CalculateFee(testSubnet1.Unsigned)
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestGetBalance(t *testing.T) {

func TestGetStake(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

// Ensure GetStake is correct for each of the genesis validators
genesis := genesistest.New(t, genesistest.Config{})
Expand Down Expand Up @@ -675,7 +675,7 @@ func TestGetStake(t *testing.T) {

func TestGetCurrentValidators(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

genesis := genesistest.New(t, genesistest.Config{})

Expand Down Expand Up @@ -821,7 +821,7 @@ func TestGetCurrentValidators(t *testing.T) {

func TestGetValidatorsAt(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

genesis := genesistest.New(t, genesistest.Config{})

Expand Down Expand Up @@ -957,7 +957,7 @@ func TestGetValidatorsAtArgsMarshalling(t *testing.T) {

func TestGetTimestamp(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

reply := GetTimestampReply{}
require.NoError(service.GetTimestamp(nil, nil, &reply))
Expand Down Expand Up @@ -993,7 +993,7 @@ func TestGetBlock(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)
service.vm.ctx.Lock.Lock()

service.vm.Internal.CreateAssetTxFee = 100 * defaultTxFee
Expand Down Expand Up @@ -1283,7 +1283,7 @@ func TestServiceGetBlockByHeight(t *testing.T) {

func TestServiceGetSubnets(t *testing.T) {
require := require.New(t)
service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

testSubnet1ID := testSubnet1.ID()

Expand Down Expand Up @@ -1355,7 +1355,7 @@ func TestGetFeeConfig(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)
service.vm.Internal.UpgradeConfig.EtnaTime = test.etnaTime

var reply gas.Config
Expand All @@ -1369,7 +1369,7 @@ func FuzzGetFeeState(f *testing.F) {
f.Fuzz(func(t *testing.T, capacity, excess uint64) {
require := require.New(t)

service, _ := defaultService(t, upgradetest.Latest)
service, _ := defaultService(t)

var (
expectedState = gas.State{
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/state/chain_time_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ func getNextL1ValidatorEvictionTime(
return nextTime, nil
}

// PickFeeCalculator creates either a static or a dynamic fee calculator,
// PickFeeCalculator creates either a simple or a dynamic fee calculator,
// depending on the active upgrade.
//
// PickFeeCalculator does not modify [state].
func PickFeeCalculator(config *config.Internal, state Chain) txfee.Calculator {
timestamp := state.GetTimestamp()
if !config.UpgradeConfig.IsEtnaActivated(timestamp) {
return txfee.NewSimpleCalculator(config.TxFee)
return txfee.NewSimpleCalculator(0)
}

feeState := state.GetFeeState()
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/state/chain_time_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ func TestPickFeeCalculator(t *testing.T) {
}{
{
fork: upgradetest.ApricotPhase2,
expected: txfee.NewSimpleCalculator(txFee),
expected: txfee.NewSimpleCalculator(0),
},
{
fork: upgradetest.ApricotPhase3,
expected: txfee.NewSimpleCalculator(txFee),
expected: txfee.NewSimpleCalculator(0),
},
{
fork: upgradetest.Etna,
Expand Down
8 changes: 0 additions & 8 deletions vms/platformvm/txs/executor/create_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import (
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

Expand Down Expand Up @@ -195,12 +193,6 @@ func TestCreateChainTxAP3FeeChange(t *testing.T) {
fee: 0,
expectedError: nil,
},
{
name: "post-fork - incorrectly priced",
time: ap3Time,
fee: 100*defaultTxFee - 1*units.NanoAvax,
expectedError: utxo.ErrInsufficientUnlockedFunds,
},
{
name: "post-fork - correctly priced",
time: ap3Time,
Expand Down
147 changes: 0 additions & 147 deletions vms/platformvm/txs/executor/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,167 +6,20 @@ package executor
import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

"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/upgrade/upgradetest"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
)

var fundedSharedMemoryCalls byte

func TestNewImportTx(t *testing.T) {
env := newEnvironment(t, upgradetest.ApricotPhase5)

type test struct {
description string
sourceChainID ids.ID
sharedMemory atomic.SharedMemory
timestamp time.Time
expectedErr error
}

sourceKey, err := secp256k1.NewPrivateKey()
require.NoError(t, err)

customAssetID := ids.GenerateTestID()
// generate a constant random source generator.
randSrc := rand.NewSource(0)
tests := []test{
{
description: "can't pay fee",
sourceChainID: env.ctx.XChainID,
sharedMemory: fundedSharedMemory(
t,
env,
sourceKey,
env.ctx.XChainID,
map[ids.ID]uint64{
env.ctx.AVAXAssetID: env.config.TxFee - 1,
},
randSrc,
),
expectedErr: builder.ErrInsufficientFunds,
},
{
description: "can barely pay fee",
sourceChainID: env.ctx.XChainID,
sharedMemory: fundedSharedMemory(
t,
env,
sourceKey,
env.ctx.XChainID,
map[ids.ID]uint64{
env.ctx.AVAXAssetID: env.config.TxFee,
},
randSrc,
),
expectedErr: nil,
},
{
description: "attempting to import from C-chain",
sourceChainID: env.ctx.CChainID,
sharedMemory: fundedSharedMemory(
t,
env,
sourceKey,
env.ctx.CChainID,
map[ids.ID]uint64{
env.ctx.AVAXAssetID: env.config.TxFee,
},
randSrc,
),
timestamp: env.config.UpgradeConfig.ApricotPhase5Time,
expectedErr: nil,
},
{
description: "attempting to import non-avax from X-chain",
sourceChainID: env.ctx.XChainID,
sharedMemory: fundedSharedMemory(
t,
env,
sourceKey,
env.ctx.XChainID,
map[ids.ID]uint64{
env.ctx.AVAXAssetID: env.config.TxFee,
customAssetID: 1,
},
randSrc,
),
timestamp: env.config.UpgradeConfig.ApricotPhase5Time,
expectedErr: nil,
},
}

to := &secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
require := require.New(t)

env.msm.SharedMemory = tt.sharedMemory

wallet := newWallet(t, env, walletConfig{
keys: []*secp256k1.PrivateKey{sourceKey},
chainIDs: []ids.ID{tt.sourceChainID},
})

tx, err := wallet.IssueImportTx(
tt.sourceChainID,
to,
)
require.ErrorIs(err, tt.expectedErr)
if tt.expectedErr != nil {
return
}

unsignedTx := tx.Unsigned.(*txs.ImportTx)
require.NotEmpty(unsignedTx.ImportedInputs)
numInputs := len(unsignedTx.Ins) + len(unsignedTx.ImportedInputs)
require.Equal(len(tx.Creds), numInputs, "should have the same number of credentials as inputs")

totalIn := uint64(0)
for _, in := range unsignedTx.Ins {
totalIn += in.Input().Amount()
}
for _, in := range unsignedTx.ImportedInputs {
totalIn += in.Input().Amount()
}
totalOut := uint64(0)
for _, out := range unsignedTx.Outs {
totalOut += out.Out.Amount()
}

require.Equal(env.config.TxFee, totalIn-totalOut)

stateDiff, err := state.NewDiff(lastAcceptedID, env)
require.NoError(err)

stateDiff.SetTimestamp(tt.timestamp)

feeCalculator := state.PickFeeCalculator(env.config, stateDiff)
_, _, _, err = StandardTx(
&env.backend,
feeCalculator,
tx,
stateDiff,
)
require.NoError(err)
})
}
}

// Returns a shared memory where GetDatabase returns a database
// where [recipientKey] has a balance of [amt]
func fundedSharedMemory(
Expand Down
3 changes: 3 additions & 0 deletions vms/platformvm/txs/fee/simple_calculator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package fee

import "github.com/ava-labs/avalanchego/vms/platformvm/txs"
Expand Down
Loading

0 comments on commit 41aaaaa

Please sign in to comment.