Skip to content

Commit

Permalink
Always provide App messages to the VM (ava-labs#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Jan 30, 2023
1 parent 518d45f commit 55a21a9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 132 deletions.
2 changes: 1 addition & 1 deletion snow/engine/avalanche/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func New(ctx context.Context, config Config, onFinished func(ctx context.Context
PutHandler: common.NewNoOpPutHandler(config.Ctx.Log),
QueryHandler: common.NewNoOpQueryHandler(config.Ctx.Log),
ChitsHandler: common.NewNoOpChitsHandler(config.Ctx.Log),
AppHandler: common.NewNoOpAppHandler(config.Ctx.Log),
AppHandler: config.VM,

processedCache: &cache.LRU[ids.ID, struct{}]{Size: cacheSize},
Fetcher: common.Fetcher{OnFinished: onFinished},
Expand Down
47 changes: 5 additions & 42 deletions snow/engine/avalanche/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package avalanche
import (
"context"
"fmt"
"time"

"go.uber.org/zap"

Expand All @@ -20,10 +19,10 @@ import (
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/common/tracker"
"github.com/ava-labs/avalanchego/snow/events"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/sampler"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
)

var _ Engine = (*Transitive)(nil)
Expand All @@ -44,6 +43,8 @@ type Transitive struct {
common.AcceptedFrontierHandler
common.AcceptedHandler
common.AncestorsHandler
common.AppHandler
validators.Connector

RequestID uint32

Expand Down Expand Up @@ -92,6 +93,8 @@ func newTransitive(config Config) (*Transitive, error) {
AcceptedFrontierHandler: common.NewNoOpAcceptedFrontierHandler(config.Ctx.Log),
AcceptedHandler: common.NewNoOpAcceptedHandler(config.Ctx.Log),
AncestorsHandler: common.NewNoOpAncestorsHandler(config.Ctx.Log),
AppHandler: config.VM,
Connector: config.VM,
acceptedFrontiers: acceptedFrontiers,
polls: poll.NewSet(factory,
config.Ctx.Log,
Expand Down Expand Up @@ -249,46 +252,6 @@ func (t *Transitive) QueryFailed(ctx context.Context, nodeID ids.NodeID, request
return t.Chits(ctx, nodeID, requestID, lastAccepted, lastAccepted)
}

func (t *Transitive) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte) error {
return t.VM.CrossChainAppRequest(ctx, chainID, requestID, deadline, request)
}

func (t *Transitive) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32) error {
return t.VM.CrossChainAppRequestFailed(ctx, chainID, requestID)
}

func (t *Transitive) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error {
return t.VM.CrossChainAppResponse(ctx, chainID, requestID, response)
}

func (t *Transitive) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error {
// Notify the VM of this request
return t.VM.AppRequest(ctx, nodeID, requestID, deadline, request)
}

func (t *Transitive) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
// Notify the VM that a request it made failed
return t.VM.AppRequestFailed(ctx, nodeID, requestID)
}

func (t *Transitive) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error {
// Notify the VM of a response to its request
return t.VM.AppResponse(ctx, nodeID, requestID, response)
}

func (t *Transitive) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error {
// Notify the VM of this message which has been gossiped to it
return t.VM.AppGossip(ctx, nodeID, msg)
}

func (t *Transitive) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
return t.VM.Connected(ctx, nodeID, nodeVersion)
}

func (t *Transitive) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
return t.VM.Disconnected(ctx, nodeID)
}

func (*Transitive) Timeout(context.Context) error {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/snowman/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func New(ctx context.Context, config Config, onFinished func(ctx context.Context
PutHandler: common.NewNoOpPutHandler(config.Ctx.Log),
QueryHandler: common.NewNoOpQueryHandler(config.Ctx.Log),
ChitsHandler: common.NewNoOpChitsHandler(config.Ctx.Log),
AppHandler: common.NewNoOpAppHandler(config.Ctx.Log),
AppHandler: config.VM,

Fetcher: common.Fetcher{
OnFinished: onFinished,
Expand Down
15 changes: 1 addition & 14 deletions snow/engine/snowman/syncer/state_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package syncer
import (
"context"
"fmt"
"time"

stdmath "math"

Expand Down Expand Up @@ -102,7 +101,7 @@ func New(
PutHandler: common.NewNoOpPutHandler(cfg.Ctx.Log),
QueryHandler: common.NewNoOpQueryHandler(cfg.Ctx.Log),
ChitsHandler: common.NewNoOpChitsHandler(cfg.Ctx.Log),
AppHandler: common.NewNoOpAppHandler(cfg.Ctx.Log),
AppHandler: cfg.VM,
stateSyncVM: ssVM,
onDoneStateSyncing: onDoneStateSyncing,
}
Expand Down Expand Up @@ -526,18 +525,6 @@ func (ss *stateSyncer) sendGetAcceptedStateSummaries(ctx context.Context) {
}
}

func (ss *stateSyncer) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error {
return ss.VM.AppRequest(ctx, nodeID, requestID, deadline, request)
}

func (ss *stateSyncer) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error {
return ss.VM.AppResponse(ctx, nodeID, requestID, response)
}

func (ss *stateSyncer) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
return ss.VM.AppRequestFailed(ctx, nodeID, requestID)
}

func (ss *stateSyncer) Notify(ctx context.Context, msg common.Message) error {
if msg != common.StateSyncDone {
ss.Ctx.Log.Warn("received an unexpected message from the VM",
Expand Down
47 changes: 5 additions & 42 deletions snow/engine/snowman/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package snowman
import (
"context"
"fmt"
"time"

"go.uber.org/zap"

Expand All @@ -21,9 +20,9 @@ import (
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/common/tracker"
"github.com/ava-labs/avalanchego/snow/events"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
)

const nonVerifiedCacheSize = 128
Expand All @@ -46,6 +45,8 @@ type Transitive struct {
common.AcceptedFrontierHandler
common.AcceptedHandler
common.AncestorsHandler
common.AppHandler
validators.Connector

RequestID uint32

Expand Down Expand Up @@ -106,6 +107,8 @@ func newTransitive(config Config) (*Transitive, error) {
AcceptedFrontierHandler: common.NewNoOpAcceptedFrontierHandler(config.Ctx.Log),
AcceptedHandler: common.NewNoOpAcceptedHandler(config.Ctx.Log),
AncestorsHandler: common.NewNoOpAncestorsHandler(config.Ctx.Log),
AppHandler: config.VM,
Connector: config.VM,
pending: make(map[ids.ID]snowman.Block),
nonVerifieds: NewAncestorTree(),
nonVerifiedCache: nonVerifiedCache,
Expand Down Expand Up @@ -303,46 +306,6 @@ func (t *Transitive) QueryFailed(ctx context.Context, nodeID ids.NodeID, request
return t.buildBlocks(ctx)
}

func (t *Transitive) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte) error {
return t.VM.CrossChainAppRequest(ctx, chainID, requestID, deadline, request)
}

func (t *Transitive) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32) error {
return t.VM.CrossChainAppRequestFailed(ctx, chainID, requestID)
}

func (t *Transitive) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error {
return t.VM.CrossChainAppResponse(ctx, chainID, requestID, response)
}

func (t *Transitive) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error {
// Notify the VM of this request
return t.VM.AppRequest(ctx, nodeID, requestID, deadline, request)
}

func (t *Transitive) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
// Notify the VM that a request it made failed
return t.VM.AppRequestFailed(ctx, nodeID, requestID)
}

func (t *Transitive) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error {
// Notify the VM of a response to its request
return t.VM.AppResponse(ctx, nodeID, requestID, response)
}

func (t *Transitive) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error {
// Notify the VM of this message which has been gossiped to it
return t.VM.AppGossip(ctx, nodeID, msg)
}

func (t *Transitive) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
return t.VM.Connected(ctx, nodeID, nodeVersion)
}

func (t *Transitive) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
return t.VM.Disconnected(ctx, nodeID)
}

func (*Transitive) Timeout(context.Context) error {
return nil
}
Expand Down
36 changes: 4 additions & 32 deletions vms/avm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var (
)

type VM struct {
common.AppHandler

Factory
metrics
avax.AddressManager
Expand Down Expand Up @@ -148,6 +150,8 @@ func (vm *VM) Initialize(
fxs []*common.Fx,
_ common.AppSender,
) error {
vm.AppHandler = common.NewNoOpAppHandler(ctx.Log)

avmConfig := Config{}
if len(configBytes) > 0 {
if err := stdjson.Unmarshal(configBytes, &avmConfig); err != nil {
Expand Down Expand Up @@ -1081,38 +1085,6 @@ func (vm *VM) lookupAssetID(asset string) (ids.ID, error) {
return ids.ID{}, fmt.Errorf("asset '%s' not found", asset)
}

func (*VM) CrossChainAppRequest(context.Context, ids.ID, uint32, time.Time, []byte) error {
return nil
}

func (*VM) CrossChainAppRequestFailed(context.Context, ids.ID, uint32) error {
return nil
}

func (*VM) CrossChainAppResponse(context.Context, ids.ID, uint32, []byte) error {
return nil
}

// This VM doesn't (currently) have any app-specific messages
func (*VM) AppRequest(context.Context, ids.NodeID, uint32, time.Time, []byte) error {
return nil
}

// This VM doesn't (currently) have any app-specific messages
func (*VM) AppResponse(context.Context, ids.NodeID, uint32, []byte) error {
return nil
}

// This VM doesn't (currently) have any app-specific messages
func (*VM) AppRequestFailed(context.Context, ids.NodeID, uint32) error {
return nil
}

// This VM doesn't (currently) have any app-specific messages
func (*VM) AppGossip(context.Context, ids.NodeID, []byte) error {
return nil
}

// UniqueTx de-duplicates the transaction.
func (vm *VM) DeduplicateTx(tx *UniqueTx) *UniqueTx {
return vm.uniqueTxs.Deduplicate(tx)
Expand Down
5 changes: 5 additions & 0 deletions vms/platformvm/blocks/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (

errEndOfTime = errors.New("program time is suspiciously far in the future")
errNoPendingBlocks = errors.New("no pending blocks")
errChainNotSynced = errors.New("chain not synced")
)

type Builder interface {
Expand Down Expand Up @@ -124,6 +125,10 @@ func (b *builder) Preferred() (snowman.Block, error) {

// AddUnverifiedTx verifies a transaction and attempts to add it to the mempool
func (b *builder) AddUnverifiedTx(tx *txs.Tx) error {
if !b.txExecutorBackend.Bootstrapped.Get() {
return errChainNotSynced
}

txID := tx.ID()
if b.Mempool.Has(txID) {
// If the transaction is already in the mempool - then it looks the same
Expand Down

0 comments on commit 55a21a9

Please sign in to comment.