Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACP-77: Allow xsvm to sign arbitrary warp messages #3448

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 additions & 0 deletions vms/example/xsvm/warp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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/snow/engine/common"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
)

// acp118Verifier allows signing all warp messages
type acp118Verifier struct{}
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved

func (acp118Verifier) Verify(context.Context, *warp.UnsignedMessage, []byte) *common.AppError {
return nil
}
Loading