github.com/MetalBlockchain/metalgo@v1.11.9/chains/linearizable_vm.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package chains 5 6 import ( 7 "context" 8 9 "github.com/MetalBlockchain/metalgo/database" 10 "github.com/MetalBlockchain/metalgo/ids" 11 "github.com/MetalBlockchain/metalgo/snow" 12 "github.com/MetalBlockchain/metalgo/snow/engine/avalanche/vertex" 13 "github.com/MetalBlockchain/metalgo/snow/engine/common" 14 "github.com/MetalBlockchain/metalgo/snow/engine/snowman/block" 15 ) 16 17 var ( 18 _ vertex.LinearizableVM = (*initializeOnLinearizeVM)(nil) 19 _ block.ChainVM = (*linearizeOnInitializeVM)(nil) 20 ) 21 22 // initializeOnLinearizeVM transforms the consensus engine's call to Linearize 23 // into a call to Initialize. This enables the proposervm to be initialized by 24 // the call to Linearize. This also provides the stopVertexID to the 25 // linearizeOnInitializeVM. 26 type initializeOnLinearizeVM struct { 27 vertex.DAGVM 28 vmToInitialize common.VM 29 vmToLinearize *linearizeOnInitializeVM 30 31 ctx *snow.Context 32 db database.Database 33 genesisBytes []byte 34 upgradeBytes []byte 35 configBytes []byte 36 toEngine chan<- common.Message 37 fxs []*common.Fx 38 appSender common.AppSender 39 } 40 41 func (vm *initializeOnLinearizeVM) Linearize(ctx context.Context, stopVertexID ids.ID) error { 42 vm.vmToLinearize.stopVertexID = stopVertexID 43 return vm.vmToInitialize.Initialize( 44 ctx, 45 vm.ctx, 46 vm.db, 47 vm.genesisBytes, 48 vm.upgradeBytes, 49 vm.configBytes, 50 vm.toEngine, 51 vm.fxs, 52 vm.appSender, 53 ) 54 } 55 56 // linearizeOnInitializeVM transforms the proposervm's call to Initialize into a 57 // call to Linearize. This enables the proposervm to provide its toEngine 58 // channel to the VM that is being linearized. 59 type linearizeOnInitializeVM struct { 60 vertex.LinearizableVMWithEngine 61 stopVertexID ids.ID 62 } 63 64 func NewLinearizeOnInitializeVM(vm vertex.LinearizableVMWithEngine) *linearizeOnInitializeVM { 65 return &linearizeOnInitializeVM{ 66 LinearizableVMWithEngine: vm, 67 } 68 } 69 70 func (vm *linearizeOnInitializeVM) Initialize( 71 ctx context.Context, 72 _ *snow.Context, 73 _ database.Database, 74 _ []byte, 75 _ []byte, 76 _ []byte, 77 toEngine chan<- common.Message, 78 _ []*common.Fx, 79 _ common.AppSender, 80 ) error { 81 return vm.Linearize(ctx, vm.stopVertexID, toEngine) 82 }