diff --git a/vms/example/xsvm/vm.go b/vms/example/xsvm/vm.go index 526fc47c499..967ee7dd6a0 100644 --- a/vms/example/xsvm/vm.go +++ b/vms/example/xsvm/vm.go @@ -9,17 +9,19 @@ import ( "net/http" "github.com/gorilla/rpc/v2" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" "github.com/ava-labs/avalanchego/database" "github.com/ava-labs/avalanchego/database/versiondb" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/network/p2p" + "github.com/ava-labs/avalanchego/network/p2p/acp118" "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/consensus/snowman" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/json" - "github.com/ava-labs/avalanchego/version" "github.com/ava-labs/avalanchego/vms/example/xsvm/api" "github.com/ava-labs/avalanchego/vms/example/xsvm/builder" "github.com/ava-labs/avalanchego/vms/example/xsvm/chain" @@ -37,7 +39,7 @@ var ( ) type VM struct { - common.AppHandler + *p2p.Network chainContext *snow.Context db database.Database @@ -57,14 +59,38 @@ func (vm *VM) Initialize( _ []byte, engineChan chan<- common.Message, _ []*common.Fx, - _ common.AppSender, + appSender common.AppSender, ) error { - vm.AppHandler = common.NewNoOpAppHandler(chainContext.Log) - chainContext.Log.Info("initializing xsvm", zap.Stringer("version", Version), ) + metrics := prometheus.NewRegistry() + err := chainContext.Metrics.Register("p2p", metrics) + if err != nil { + return err + } + + vm.Network, err = p2p.NewNetwork( + chainContext.Log, + appSender, + metrics, + "", + ) + if err != nil { + return err + } + + // Allow signing of all warp messages. This is not typically safe, but is + // allowed for this example. + acp118Handler := acp118.NewHandler( + acp118Verifier{}, + chainContext.WarpSigner, + ) + if err := vm.Network.AddHandler(p2p.SignatureRequestHandlerID, acp118Handler); err != nil { + return err + } + vm.chainContext = chainContext vm.db = db g, err := genesis.Parse(genesisBytes) @@ -132,14 +158,6 @@ func (*VM) HealthCheck(context.Context) (interface{}, error) { return http.StatusOK, nil } -func (*VM) Connected(context.Context, ids.NodeID, *version.Application) error { - return nil -} - -func (*VM) Disconnected(context.Context, ids.NodeID) error { - return nil -} - func (vm *VM) GetBlock(_ context.Context, blkID ids.ID) (snowman.Block, error) { return vm.chain.GetBlock(blkID) } diff --git a/vms/example/xsvm/warp.go b/vms/example/xsvm/warp.go new file mode 100644 index 00000000000..b6f2ff59e64 --- /dev/null +++ b/vms/example/xsvm/warp.go @@ -0,0 +1,21 @@ +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package xsvm + +import ( + "context" + + "github.com/ava-labs/avalanchego/network/p2p/acp118" + "github.com/ava-labs/avalanchego/snow/engine/common" + "github.com/ava-labs/avalanchego/vms/platformvm/warp" +) + +var _ acp118.Verifier = (*acp118Verifier)(nil) + +// acp118Verifier allows signing all warp messages +type acp118Verifier struct{} + +func (acp118Verifier) Verify(context.Context, *warp.UnsignedMessage, []byte) *common.AppError { + return nil +}