From 61479754d935e36993f7144d70d7c518cdb26a36 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 25 Nov 2020 11:20:36 -0500 Subject: Add avax API client and upgrade to avalanchego v1.0.5-client --- plugin/evm/vm.go | 57 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) (limited to 'plugin/evm/vm.go') diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 284a772..aaf7f10 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -70,6 +70,7 @@ const ( batchSize = 250 maxUTXOsToFetch = 1024 blockCacheSize = 1 << 10 // 1024 + codecVersion = uint16(0) ) const ( @@ -110,26 +111,29 @@ func maxDuration(x, y time.Duration) time.Duration { } // Codec does serialization and deserialization -var Codec codec.Codec +var Codec codec.Manager func init() { - Codec = codec.NewDefault() + Codec = codec.NewDefaultManager() + c := codec.NewDefault() errs := wrappers.Errs{} errs.Add( - Codec.RegisterType(&UnsignedImportTx{}), - Codec.RegisterType(&UnsignedExportTx{}), + c.RegisterType(&UnsignedImportTx{}), + c.RegisterType(&UnsignedExportTx{}), ) - Codec.Skip(3) + c.Skip(3) errs.Add( - Codec.RegisterType(&secp256k1fx.TransferInput{}), - Codec.RegisterType(&secp256k1fx.MintOutput{}), - Codec.RegisterType(&secp256k1fx.TransferOutput{}), - Codec.RegisterType(&secp256k1fx.MintOperation{}), - Codec.RegisterType(&secp256k1fx.Credential{}), - Codec.RegisterType(&secp256k1fx.Input{}), - Codec.RegisterType(&secp256k1fx.OutputOwners{}), + c.RegisterType(&secp256k1fx.TransferInput{}), + c.RegisterType(&secp256k1fx.MintOutput{}), + c.RegisterType(&secp256k1fx.TransferOutput{}), + c.RegisterType(&secp256k1fx.MintOperation{}), + c.RegisterType(&secp256k1fx.Credential{}), + c.RegisterType(&secp256k1fx.Input{}), + c.RegisterType(&secp256k1fx.OutputOwners{}), ) + + errs.Add(Codec.RegisterCodec(0, c)) if errs.Errored() { panic(errs.Err) } @@ -139,8 +143,7 @@ func init() { type VM struct { ctx *snow.Context - CLIConfig CommandLineConfig - encodingManager formatting.EncodingManager + CLIConfig CommandLineConfig chainID *big.Int networkID uint64 @@ -173,7 +176,8 @@ type VM struct { txSubmitChan <-chan struct{} atomicTxSubmitChan chan struct{} shutdownSubmitChan chan struct{} - codec codec.Codec + baseCodec codec.Codec + codec codec.Manager clock timer.Clock txFee uint64 pendingAtomicTxs chan *Tx @@ -187,7 +191,7 @@ type VM struct { func (vm *VM) getAtomicTx(block *types.Block) *Tx { extdata := block.ExtraData() atx := new(Tx) - if err := vm.codec.Unmarshal(extdata, atx); err != nil { + if _, err := vm.codec.Unmarshal(extdata, atx); err != nil { return nil } atx.Sign(vm.codec, nil) @@ -195,7 +199,10 @@ func (vm *VM) getAtomicTx(block *types.Block) *Tx { } // Codec implements the secp256k1fx interface -func (vm *VM) Codec() codec.Codec { return codec.NewDefault() } +func (vm *VM) Codec() codec.Manager { return vm.codec } + +// CodecRegistry implements the secp256k1fx interface +func (vm *VM) CodecRegistry() codec.Registry { return vm.baseCodec } // Clock implements the secp256k1fx interface func (vm *VM) Clock() *timer.Clock { return &vm.clock } @@ -221,12 +228,6 @@ func (vm *VM) Initialize( return vm.CLIConfig.ParsingError } - encodingManager, err := formatting.NewEncodingManager(formatting.CB58Encoding) - if err != nil { - return fmt.Errorf("problem creating encoding manager: %w", err) - } - vm.encodingManager = encodingManager - if len(fxs) > 0 { return errUnsupportedFXs } @@ -284,7 +285,7 @@ func (vm *VM) Initialize( vm.newBlockChan <- nil return nil, err } - raw, _ := vm.codec.Marshal(atx) + raw, _ := vm.codec.Marshal(codecVersion, atx) return raw, nil default: if len(txs) == 0 { @@ -382,10 +383,14 @@ func (vm *VM) Initialize( vm.genesisHash = chain.GetGenesisBlock().Hash() log.Info(fmt.Sprintf("lastAccepted = %s", vm.lastAccepted.ethBlock.Hash().Hex())) - // TODO: shutdown this go routine vm.shutdownWg.Add(1) go vm.ctx.Log.RecoverAndPanic(vm.awaitSubmittedTxs) vm.codec = Codec + // The Codec explicitly registers the types it requires from the secp256k1fx + // so [vm.baseCodec] is a dummy codec use to fulfill the secp256k1fx VM + // interface. The fx will register all of its types, which can be safely + // ignored by the VM's codec. + vm.baseCodec = codec.NewDefault() return vm.fx.Initialize(vm) } @@ -857,7 +862,7 @@ func (vm *VM) GetAtomicUTXOs( utxos := make([]*avax.UTXO, len(allUTXOBytes)) for i, utxoBytes := range allUTXOBytes { utxo := &avax.UTXO{} - if err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil { + if _, err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil { return nil, ids.ShortID{}, ids.ID{}, fmt.Errorf("error parsing UTXO: %w", err) } utxos[i] = utxo -- cgit v1.2.3 From 627bc7f7fd75d385e623c3c5177c5aeb639a0e52 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Sun, 29 Nov 2020 14:38:44 -0500 Subject: Address comments --- plugin/evm/vm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugin/evm/vm.go') diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index aaf7f10..58ab600 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -131,9 +131,9 @@ func init() { c.RegisterType(&secp256k1fx.Credential{}), c.RegisterType(&secp256k1fx.Input{}), c.RegisterType(&secp256k1fx.OutputOwners{}), + Codec.RegisterCodec(codecVersion, c), ) - errs.Add(Codec.RegisterCodec(0, c)) if errs.Errored() { panic(errs.Err) } -- cgit v1.2.3