Skip to content

Commit

Permalink
ACP-77: Allow xsvm to sign arbitrary warp messages (#3448)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Oct 7, 2024
1 parent 43d5b43 commit da949d2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
44 changes: 31 additions & 13 deletions vms/example/xsvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -37,7 +39,7 @@ var (
)

type VM struct {
common.AppHandler
*p2p.Network

chainContext *snow.Context
db database.Database
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions vms/example/xsvm/warp.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit da949d2

Please sign in to comment.