Skip to content

Commit

Permalink
Merge branch 'master' into options-before-verify
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Jan 20, 2024
2 parents 5a6bf3d + 7cea467 commit bc9e195
Show file tree
Hide file tree
Showing 15 changed files with 818 additions and 108 deletions.
9 changes: 9 additions & 0 deletions api/info/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var errNoChainProvided = errors.New("argument 'chain' not given")
Expand Down Expand Up @@ -416,6 +419,7 @@ func (i *Info) GetTxFee(_ *http.Request, _ *struct{}, reply *GetTxFeeResponse) e
// GetVMsReply contains the response metadata for GetVMs
type GetVMsReply struct {
VMs map[ids.ID][]string `json:"vms"`
Fxs map[ids.ID]string `json:"fxs"`
}

// GetVMs lists the virtual machines installed on the node
Expand All @@ -432,5 +436,10 @@ func (i *Info) GetVMs(_ *http.Request, _ *struct{}, reply *GetVMsReply) error {
}

reply.VMs, err = ids.GetRelevantAliases(i.VMManager, vmIDs)
reply.Fxs = map[ids.ID]string{
secp256k1fx.ID: secp256k1fx.Name,
nftfx.ID: nftfx.Name,
propertyfx.ID: propertyfx.Name,
}
return err
}
33 changes: 18 additions & 15 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/fx"
"github.com/ava-labs/avalanchego/vms/metervm"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/proposervm"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/vms/tracedvm"

timetracker "github.com/ava-labs/avalanchego/snow/networking/tracker"
Expand Down Expand Up @@ -96,6 +100,12 @@ var (
errNoPrimaryNetworkConfig = errors.New("no subnet config for primary network found")
errPartialSyncAsAValidator = errors.New("partial sync should not be configured for a validator")

fxs = map[ids.ID]fx.Factory{
secp256k1fx.ID: &secp256k1fx.Factory{},
nftfx.ID: &nftfx.Factory{},
propertyfx.ID: &propertyfx.Factory{},
}

_ Manager = (*manager)(nil)
)

Expand Down Expand Up @@ -511,23 +521,16 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
}
// TODO: Shutdown VM if an error occurs

fxs := make([]*common.Fx, len(chainParams.FxIDs))
chainFxs := make([]*common.Fx, len(chainParams.FxIDs))
for i, fxID := range chainParams.FxIDs {
// Get a factory for the fx we want to use on our chain
fxFactory, err := m.VMManager.GetFactory(fxID)
if err != nil {
return nil, fmt.Errorf("error while getting fxFactory: %w", err)
}

fx, err := fxFactory.New(chainLog)
if err != nil {
return nil, fmt.Errorf("error while creating fx: %w", err)
fxFactory, ok := fxs[fxID]
if !ok {
return nil, fmt.Errorf("fx %s not found", fxID)
}

// Create the fx
fxs[i] = &common.Fx{
chainFxs[i] = &common.Fx{
ID: fxID,
Fx: fx,
Fx: fxFactory.New(),
}
}

Expand All @@ -539,7 +542,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
chainParams.GenesisData,
m.Validators,
vm,
fxs,
chainFxs,
sb,
)
if err != nil {
Expand All @@ -557,7 +560,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
m.Validators,
beacons,
vm,
fxs,
chainFxs,
sb,
)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,12 @@ import (
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/registry"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

ipcsapi "github.com/ava-labs/avalanchego/api/ipcs"
avmconfig "github.com/ava-labs/avalanchego/vms/avm/config"
Expand Down Expand Up @@ -1226,9 +1223,6 @@ func (n *Node) initVMs() error {
},
}),
n.VMManager.RegisterFactory(context.TODO(), constants.EVMID, &coreth.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), secp256k1fx.ID, &secp256k1fx.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), nftfx.ID, &nftfx.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), propertyfx.ID, &propertyfx.Factory{}),
)
if err != nil {
return err
Expand Down
18 changes: 18 additions & 0 deletions vms/components/avax/base_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ func (t *BaseTx) Verify(ctx *snow.Context) error {
return nil
}
}

func VerifyMemoFieldLength(memo types.JSONByteSlice, isDurangoActive bool) error {
if !isDurangoActive {
// SyntacticVerify validates this field pre-Durango
return nil
}

if len(memo) != 0 {
return fmt.Errorf(
"%w: %d > %d",
ErrMemoTooLarge,
len(memo),
0,
)
}

return nil
}
9 changes: 9 additions & 0 deletions vms/fx/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package fx

// Factory returns an instance of a feature extension
type Factory interface {
New() any
}
11 changes: 6 additions & 5 deletions vms/nftfx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ package nftfx

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/fx"
)

const Name = "nftfx"

var (
_ vms.Factory = (*Factory)(nil)
_ fx.Factory = (*Factory)(nil)

// ID that this Fx uses when labeled
ID = ids.ID{'n', 'f', 't', 'f', 'x'}
)

type Factory struct{}

func (*Factory) New(logging.Logger) (interface{}, error) {
return &Fx{}, nil
func (*Factory) New() any {
return &Fx{}
}
6 changes: 1 addition & 5 deletions vms/nftfx/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/logging"
)

func TestFactory(t *testing.T) {
require := require.New(t)

factory := Factory{}
fx, err := factory.New(logging.NoLog{})
require.NoError(err)
require.NotNil(fx)
require.Equal(&Fx{}, factory.New())
}
63 changes: 50 additions & 13 deletions vms/platformvm/txs/executor/staker_tx_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ func verifyAddValidatorTx(
var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
startTime = currentTimestamp
)
if err := avax.VerifyMemoFieldLength(tx.Memo, isDurangoActive); err != nil {
return nil, err
}

startTime := currentTimestamp
if !isDurangoActive {
startTime = tx.StartTime()
}

duration := tx.EndTime().Sub(startTime)
switch {
case tx.Validator.Wght < backend.Config.MinValidatorStake:
Expand Down Expand Up @@ -194,8 +199,12 @@ func verifyAddSubnetValidatorTx(
var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
startTime = currentTimestamp
)
if err := avax.VerifyMemoFieldLength(tx.Memo, isDurangoActive); err != nil {
return err
}

startTime := currentTimestamp
if !isDurangoActive {
startTime = tx.StartTime()
}
Expand Down Expand Up @@ -283,6 +292,14 @@ func verifyRemoveSubnetValidatorTx(
return nil, false, err
}

var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
)
if err := avax.VerifyMemoFieldLength(tx.Memo, isDurangoActive); err != nil {
return nil, false, err
}

isCurrentValidator := true
vdr, err := chainState.GetCurrentValidator(tx.Subnet, tx.NodeID)
if err == database.ErrNotFound {
Expand Down Expand Up @@ -351,8 +368,14 @@ func verifyAddDelegatorTx(
var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
endTime = tx.EndTime()
startTime = currentTimestamp
)
if err := avax.VerifyMemoFieldLength(tx.Memo, isDurangoActive); err != nil {
return nil, err
}

var (
endTime = tx.EndTime()
startTime = currentTimestamp
)
if !isDurangoActive {
startTime = tx.StartTime()
Expand Down Expand Up @@ -458,15 +481,19 @@ func verifyAddPermissionlessValidatorTx(
return err
}

if !backend.Bootstrapped.Get() {
return nil
}

var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
startTime = currentTimestamp
)
if err := avax.VerifyMemoFieldLength(tx.Memo, isDurangoActive); err != nil {
return err
}

if !backend.Bootstrapped.Get() {
return nil
}

startTime := currentTimestamp
if !isDurangoActive {
startTime = tx.StartTime()
}
Expand Down Expand Up @@ -578,15 +605,21 @@ func verifyAddPermissionlessDelegatorTx(
return err
}

var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
)
if err := avax.VerifyMemoFieldLength(tx.Memo, isDurangoActive); err != nil {
return err
}

if !backend.Bootstrapped.Get() {
return nil
}

var (
currentTimestamp = chainState.GetTimestamp()
isDurangoActive = backend.Config.IsDurangoActivated(currentTimestamp)
endTime = tx.EndTime()
startTime = currentTimestamp
endTime = tx.EndTime()
startTime = currentTimestamp
)
if !isDurangoActive {
startTime = tx.StartTime()
Expand Down Expand Up @@ -728,6 +761,10 @@ func verifyTransferSubnetOwnershipTx(
return err
}

if err := avax.VerifyMemoFieldLength(tx.Memo, true /*=isDurangoActive*/); err != nil {
return err
}

if !backend.Bootstrapped.Get() {
// Not bootstrapped yet -- don't need to do full verification.
return nil
Expand Down
6 changes: 4 additions & 2 deletions vms/platformvm/txs/executor/staker_tx_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ func TestVerifyAddPermissionlessValidatorTx(t *testing.T) {
}
},
stateF: func(ctrl *gomock.Controller) state.Chain {
return nil
mockState := state.NewMockChain(ctrl)
mockState.EXPECT().GetTimestamp().Return(now) // chain time is after Durango fork activation since now.After(activeForkTime)
return mockState
},
sTxF: func() *txs.Tx {
return &verifiedSignedTx
},
txF: func() *txs.AddPermissionlessValidatorTx {
return nil
return &txs.AddPermissionlessValidatorTx{}
},
expectedErr: nil,
},
Expand Down
Loading

0 comments on commit bc9e195

Please sign in to comment.