From 55a21a96f4bd2ff89c23676058a7b0aadb0552db Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Mon, 30 Jan 2023 10:52:31 -0500 Subject: [PATCH] Always provide App messages to the VM (#2545) --- .../avalanche/bootstrap/bootstrapper.go | 2 +- snow/engine/avalanche/transitive.go | 47 ++----------------- snow/engine/snowman/bootstrap/bootstrapper.go | 2 +- snow/engine/snowman/syncer/state_syncer.go | 15 +----- snow/engine/snowman/transitive.go | 47 ++----------------- vms/avm/vm.go | 36 ++------------ vms/platformvm/blocks/builder/builder.go | 5 ++ 7 files changed, 22 insertions(+), 132 deletions(-) diff --git a/snow/engine/avalanche/bootstrap/bootstrapper.go b/snow/engine/avalanche/bootstrap/bootstrapper.go index b55efe6792a8..1b35b5df144f 100644 --- a/snow/engine/avalanche/bootstrap/bootstrapper.go +++ b/snow/engine/avalanche/bootstrap/bootstrapper.go @@ -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}, diff --git a/snow/engine/avalanche/transitive.go b/snow/engine/avalanche/transitive.go index c10a3b3381b9..65a50dbee111 100644 --- a/snow/engine/avalanche/transitive.go +++ b/snow/engine/avalanche/transitive.go @@ -6,7 +6,6 @@ package avalanche import ( "context" "fmt" - "time" "go.uber.org/zap" @@ -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) @@ -44,6 +43,8 @@ type Transitive struct { common.AcceptedFrontierHandler common.AcceptedHandler common.AncestorsHandler + common.AppHandler + validators.Connector RequestID uint32 @@ -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, @@ -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 } diff --git a/snow/engine/snowman/bootstrap/bootstrapper.go b/snow/engine/snowman/bootstrap/bootstrapper.go index 3c846b3c476d..80b588820aae 100644 --- a/snow/engine/snowman/bootstrap/bootstrapper.go +++ b/snow/engine/snowman/bootstrap/bootstrapper.go @@ -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, diff --git a/snow/engine/snowman/syncer/state_syncer.go b/snow/engine/snowman/syncer/state_syncer.go index 10d234fa072d..1c3b1fca9a9b 100644 --- a/snow/engine/snowman/syncer/state_syncer.go +++ b/snow/engine/snowman/syncer/state_syncer.go @@ -6,7 +6,6 @@ package syncer import ( "context" "fmt" - "time" stdmath "math" @@ -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, } @@ -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", diff --git a/snow/engine/snowman/transitive.go b/snow/engine/snowman/transitive.go index 174abb9723ce..30cfdcbbf2ad 100644 --- a/snow/engine/snowman/transitive.go +++ b/snow/engine/snowman/transitive.go @@ -6,7 +6,6 @@ package snowman import ( "context" "fmt" - "time" "go.uber.org/zap" @@ -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 @@ -46,6 +45,8 @@ type Transitive struct { common.AcceptedFrontierHandler common.AcceptedHandler common.AncestorsHandler + common.AppHandler + validators.Connector RequestID uint32 @@ -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, @@ -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 } diff --git a/vms/avm/vm.go b/vms/avm/vm.go index b2411ddb6197..3f3f043cac7e 100644 --- a/vms/avm/vm.go +++ b/vms/avm/vm.go @@ -71,6 +71,8 @@ var ( ) type VM struct { + common.AppHandler + Factory metrics avax.AddressManager @@ -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 { @@ -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) diff --git a/vms/platformvm/blocks/builder/builder.go b/vms/platformvm/blocks/builder/builder.go index fec8180de0e5..2a2fdc8ca8f3 100644 --- a/vms/platformvm/blocks/builder/builder.go +++ b/vms/platformvm/blocks/builder/builder.go @@ -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 { @@ -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