From 5b12d3fa21635145cb8a752b56477e22fca77a5b Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Fri, 30 Oct 2020 12:55:53 -0400 Subject: remove these ID calls: Key(), Bytes(), Equals(), NewID(), IsZero() --- plugin/evm/block.go | 4 ++-- plugin/evm/export_tx.go | 20 ++++++++++---------- plugin/evm/factory.go | 6 ++---- plugin/evm/import_tx.go | 26 +++++++++++++------------- plugin/evm/service.go | 2 +- plugin/evm/tx.go | 4 ++-- plugin/evm/user.go | 2 +- plugin/evm/vm.go | 30 +++++++++++++++--------------- 8 files changed, 46 insertions(+), 48 deletions(-) (limited to 'plugin') diff --git a/plugin/evm/block.go b/plugin/evm/block.go index 77a85b3..7c23c17 100644 --- a/plugin/evm/block.go +++ b/plugin/evm/block.go @@ -34,7 +34,7 @@ func (b *Block) Accept() error { log.Trace(fmt.Sprintf("Block %s is accepted", b.ID())) vm.updateStatus(b.id, choices.Accepted) - if err := vm.acceptedDB.Put(b.ethBlock.Number().Bytes(), b.id.Bytes()); err != nil { + if err := vm.acceptedDB.Put(b.ethBlock.Number().Bytes(), b.id[:]); err != nil { return err } @@ -68,7 +68,7 @@ func (b *Block) Status() choices.Status { // Parent implements the snowman.Block interface func (b *Block) Parent() snowman.Block { - parentID := ids.NewID(b.ethBlock.ParentHash()) + parentID := ids.ID(b.ethBlock.ParentHash()) if block := b.vm.getBlock(parentID); block != nil { return block } diff --git a/plugin/evm/export_tx.go b/plugin/evm/export_tx.go index d099eb2..74e36b6 100644 --- a/plugin/evm/export_tx.go +++ b/plugin/evm/export_tx.go @@ -17,6 +17,7 @@ import ( safemath "github.com/ava-labs/avalanchego/utils/math" "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/secp256k1fx" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" ) @@ -52,15 +53,15 @@ func (tx *UnsignedExportTx) Verify( return errNilTx case tx.syntacticallyVerified: // already passed syntactic verification return nil - case tx.DestinationChain.IsZero(): + case tx.DestinationChain == ids.Empty: return errWrongChainID - case !tx.DestinationChain.Equals(avmID): + case tx.DestinationChain != avmID: return errWrongChainID case len(tx.ExportedOutputs) == 0: return errNoExportOutputs case tx.NetworkID != ctx.NetworkID: return errWrongNetworkID - case !ctx.ChainID.Equals(tx.BlockchainID): + case ctx.ChainID != tx.BlockchainID: return errWrongBlockchainID } @@ -177,13 +178,13 @@ func (vm *VM) newExportTx( to ids.ShortID, // Address of chain recipient keys []*crypto.PrivateKeySECP256K1R, // Pay the fee and provide the tokens ) (*Tx, error) { - if !vm.ctx.XChainID.Equals(chainID) { + if vm.ctx.XChainID != chainID { return nil, errWrongChainID } var toBurn uint64 var err error - if assetID.Equals(vm.ctx.AVAXAssetID) { + if assetID == vm.ctx.AVAXAssetID { toBurn, err = safemath.Add64(amount, vm.txFee) if err != nil { return nil, errOverflowExport @@ -198,7 +199,7 @@ func (vm *VM) newExportTx( } // burn non-AVAX - if !assetID.Equals(vm.ctx.AVAXAssetID) { + if assetID != vm.ctx.AVAXAssetID { ins2, signers2, err := vm.GetSpendableFunds(keys, assetID, amount) if err != nil { return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err) @@ -242,7 +243,7 @@ func (tx *UnsignedExportTx) EVMStateTransfer(vm *VM, state *state.StateDB) error addrs := map[[20]byte]uint64{} for _, from := range tx.Ins { log.Info("crosschain C->X", "addr", from.Address, "amount", from.Amount) - if from.AssetID.Equals(vm.ctx.AVAXAssetID) { + if from.AssetID == vm.ctx.AVAXAssetID { amount := new(big.Int).Mul( new(big.Int).SetUint64(from.Amount), x2cRate) if state.GetBalance(from.Address).Cmp(amount) < 0 { @@ -251,11 +252,10 @@ func (tx *UnsignedExportTx) EVMStateTransfer(vm *VM, state *state.StateDB) error state.SubBalance(from.Address, amount) } else { amount := new(big.Int).SetUint64(from.Amount) - assetID := from.AssetID.Key() - if state.GetBalanceMultiCoin(from.Address, assetID).Cmp(amount) < 0 { + if state.GetBalanceMultiCoin(from.Address, common.Hash(from.AssetID)).Cmp(amount) < 0 { return errInsufficientFunds } - state.SubBalanceMultiCoin(from.Address, assetID, amount) + state.SubBalanceMultiCoin(from.Address, common.Hash(from.AssetID), amount) } if state.GetNonce(from.Address) != from.Nonce { return errInvalidNonce diff --git a/plugin/evm/factory.go b/plugin/evm/factory.go index 593d85b..b977117 100644 --- a/plugin/evm/factory.go +++ b/plugin/evm/factory.go @@ -3,13 +3,11 @@ package evm -import ( - "github.com/ava-labs/avalanchego/ids" -) +import "github.com/ava-labs/avalanchego/ids" // ID this VM should be referenced by var ( - ID = ids.NewID([32]byte{'e', 'v', 'm'}) + ID = ids.ID([32]byte{'e', 'v', 'm'}) ) // Factory ... diff --git a/plugin/evm/import_tx.go b/plugin/evm/import_tx.go index ae6b540..2ce8f6f 100644 --- a/plugin/evm/import_tx.go +++ b/plugin/evm/import_tx.go @@ -58,15 +58,15 @@ func (tx *UnsignedImportTx) Verify( return errNilTx case tx.syntacticallyVerified: // already passed syntactic verification return nil - case tx.SourceChain.IsZero(): + case tx.SourceChain == ids.Empty: return errWrongChainID - case !tx.SourceChain.Equals(avmID): + case tx.SourceChain != avmID: return errWrongChainID case len(tx.ImportedInputs) == 0: return errNoImportInputs case tx.NetworkID != ctx.NetworkID: return errWrongNetworkID - case !ctx.ChainID.Equals(tx.BlockchainID): + case ctx.ChainID != tx.BlockchainID: return errWrongBlockchainID } @@ -124,7 +124,8 @@ func (tx *UnsignedImportTx) SemanticVerify( utxoIDs := make([][]byte, len(tx.ImportedInputs)) for i, in := range tx.ImportedInputs { - utxoIDs[i] = in.UTXOID.InputID().Bytes() + inputID := in.UTXOID.InputID() + utxoIDs[i] = inputID[:] } // allUTXOBytes is guaranteed to be the same length as utxoIDs allUTXOBytes, err := vm.ctx.SharedMemory.Get(tx.SourceChain, utxoIDs) @@ -144,7 +145,7 @@ func (tx *UnsignedImportTx) SemanticVerify( utxoAssetID := utxo.AssetID() inAssetID := in.AssetID() - if !utxoAssetID.Equals(inAssetID) { + if utxoAssetID != inAssetID { return permError{errAssetIDMismatch} } @@ -164,7 +165,8 @@ func (tx *UnsignedImportTx) Accept(ctx *snow.Context, _ database.Batch) error { // TODO: Is any batch passed in here? utxoIDs := make([][]byte, len(tx.ImportedInputs)) for i, in := range tx.ImportedInputs { - utxoIDs[i] = in.InputID().Bytes() + inputID := in.InputID() + utxoIDs[i] = inputID[:] } return ctx.SharedMemory.Remove(tx.SourceChain, utxoIDs) } @@ -175,7 +177,7 @@ func (vm *VM) newImportTx( to common.Address, // Address of recipient keys []*crypto.PrivateKeySECP256K1R, // Keys to import the funds ) (*Tx, error) { - if !vm.ctx.XChainID.Equals(chainID) { + if vm.ctx.XChainID != chainID { return nil, errWrongChainID } @@ -204,8 +206,7 @@ func (vm *VM) newImportTx( continue } aid := utxo.AssetID() - aidKey := aid.Key() - importedAmount[aidKey], err = math.Add64(importedAmount[aidKey], input.Amount()) + importedAmount[aid], err = math.Add64(importedAmount[aid], input.Amount()) if err != nil { return nil, err } @@ -238,8 +239,7 @@ func (vm *VM) newImportTx( // This will create unique outputs (in the context of sorting) // since each output will have a unique assetID - for assetKey, amount := range importedAmount { - assetID := ids.NewID(assetKey) + for assetID, amount := range importedAmount { //if assetID.Equals(vm.ctx.AVAXAssetID) || amount == 0 { if amount == 0 { continue @@ -273,13 +273,13 @@ func (vm *VM) newImportTx( func (tx *UnsignedImportTx) EVMStateTransfer(vm *VM, state *state.StateDB) error { for _, to := range tx.Outs { log.Info("crosschain X->C", "addr", to.Address, "amount", to.Amount) - if to.AssetID.Equals(vm.ctx.AVAXAssetID) { + if to.AssetID == vm.ctx.AVAXAssetID { amount := new(big.Int).Mul( new(big.Int).SetUint64(to.Amount), x2cRate) state.AddBalance(to.Address, amount) } else { amount := new(big.Int).SetUint64(to.Amount) - state.AddBalanceMultiCoin(to.Address, to.AssetID.Key(), amount) + state.AddBalanceMultiCoin(to.Address, common.Hash(to.AssetID), amount) } } return nil diff --git a/plugin/evm/service.go b/plugin/evm/service.go index f1c30c5..3509929 100644 --- a/plugin/evm/service.go +++ b/plugin/evm/service.go @@ -232,7 +232,7 @@ type ExportArgs struct { // It must be imported on the X-Chain to complete the transfer func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.JSONTxID) error { log.Info("EVM: Export called") - if args.AssetID.IsZero() { + if args.AssetID == ids.Empty { return fmt.Errorf("assetID is required") } diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 7c2ebf1..3a2f820 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -134,7 +134,7 @@ func (ins *innerSortInputsAndSigners) Less(i, j int) bool { if addrComp != 0 { return addrComp < 0 } - return bytes.Compare(ins.inputs[i].AssetID.Bytes(), ins.inputs[j].AssetID.Bytes()) < 0 + return bytes.Compare(ins.inputs[i].AssetID[:], ins.inputs[j].AssetID[:]) < 0 } func (ins *innerSortInputsAndSigners) Len() int { return len(ins.inputs) } @@ -165,7 +165,7 @@ func (outs *innerSortEVMOutputs) Less(i, j int) bool { if addrComp != 0 { return addrComp < 0 } - return bytes.Compare(outs.outputs[i].AssetID.Bytes(), outs.outputs[j].AssetID.Bytes()) < 0 + return bytes.Compare(outs.outputs[i].AssetID[:], outs.outputs[j].AssetID[:]) < 0 } func (outs *innerSortEVMOutputs) Len() int { return len(outs.outputs) } diff --git a/plugin/evm/user.go b/plugin/evm/user.go index b751634..152156d 100644 --- a/plugin/evm/user.go +++ b/plugin/evm/user.go @@ -15,7 +15,7 @@ import ( // Key in the database whose corresponding value is the list of // addresses this user controls -var addressesKey = ids.Empty.Bytes() +var addressesKey = ids.Empty[:] var ( errDBNil = errors.New("db uninitialized") diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 1f7e501..09e2ad5 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -309,7 +309,7 @@ func (vm *VM) Initialize( log.Trace("EVM sealed a block") blk := &Block{ - id: ids.NewID(block.Hash()), + id: ids.ID(block.Hash()), ethBlock: block, vm: vm, } @@ -318,7 +318,7 @@ func (vm *VM) Initialize( return errInvalidBlock } vm.newBlockChan <- blk - vm.updateStatus(ids.NewID(block.Hash()), choices.Processing) + vm.updateStatus(ids.ID(block.Hash()), choices.Processing) vm.txPoolStabilizedLock.Lock() vm.txPoolStabilizedHead = block.Hash() vm.txPoolStabilizedLock.Unlock() @@ -385,7 +385,7 @@ func (vm *VM) Initialize( lastAccepted = chain.GetGenesisBlock() } vm.lastAccepted = &Block{ - id: ids.NewID(lastAccepted.Hash()), + id: ids.ID(lastAccepted.Hash()), ethBlock: lastAccepted, vm: vm, } @@ -437,7 +437,7 @@ func (vm *VM) BuildBlock() (snowman.Block, error) { vm.blockDelayTimer.SetTimeoutIn(minBlockTime) vm.bdlock.Unlock() - log.Debug(fmt.Sprintf("built block 0x%x", block.ID().Bytes())) + log.Debug(fmt.Sprintf("built block %s", block.ID())) // make sure Tx Pool is updated <-vm.txPoolStabilizedOk return block, nil @@ -462,7 +462,7 @@ func (vm *VM) ParseBlock(b []byte) (snowman.Block, error) { return nil, errInvalidBlock } block := &Block{ - id: ids.NewID(blockHash), + id: ids.ID(blockHash), ethBlock: ethBlock, vm: vm, } @@ -484,7 +484,7 @@ func (vm *VM) GetBlock(id ids.ID) (snowman.Block, error) { // SetPreference sets what the current tail of the chain is func (vm *VM) SetPreference(blkID ids.ID) { - err := vm.chain.SetTail(blkID.Key()) + err := vm.chain.SetTail(common.Hash(blkID)) vm.ctx.Log.AssertNoError(err) } @@ -630,7 +630,7 @@ func (vm *VM) getCachedStatus(blockID ids.ID) choices.Status { if acceptedID, err := ids.ToID(acceptedIDBytes); err != nil { log.Error(fmt.Sprintf("snowman-eth: acceptedID bytes didn't match expected value: %s", err)) } else { - if acceptedID.Equals(blockID) { + if acceptedID == blockID { vm.blockStatusCache.Put(blockID, choices.Accepted) return choices.Accepted } @@ -641,14 +641,14 @@ func (vm *VM) getCachedStatus(blockID ids.ID) choices.Status { status := vm.getUncachedStatus(blk) if status == choices.Accepted { - err := vm.acceptedDB.Put(heightKey, blockID.Bytes()) + err := vm.acceptedDB.Put(heightKey, blockID[:]) if err != nil { log.Error(fmt.Sprintf("snowman-eth: failed to write back acceptedID bytes: %s", err)) } tempBlock := wrappedBlk for tempBlock.ethBlock != nil { - parentID := ids.NewID(tempBlock.ethBlock.ParentHash()) + parentID := ids.ID(tempBlock.ethBlock.ParentHash()) tempBlock = vm.getBlock(parentID) if tempBlock == nil || tempBlock.ethBlock == nil { break @@ -660,7 +660,7 @@ func (vm *VM) getCachedStatus(blockID ids.ID) choices.Status { break } - if err := vm.acceptedDB.Put(heightKey, parentID.Bytes()); err != nil { + if err := vm.acceptedDB.Put(heightKey, parentID[:]); err != nil { log.Error(fmt.Sprintf("snowman-eth: failed to write back acceptedID bytes: %s", err)) } } @@ -681,7 +681,7 @@ func (vm *VM) getUncachedStatus(blk *types.Block) choices.Status { highBlock, lowBlock = lowBlock, highBlock } for highBlock.Number().Cmp(lowBlock.Number()) > 0 { - parentBlock := vm.getBlock(ids.NewID(highBlock.ParentHash())) + parentBlock := vm.getBlock(ids.ID(highBlock.ParentHash())) if parentBlock == nil { return choices.Processing } @@ -702,12 +702,12 @@ func (vm *VM) getBlock(id ids.ID) *Block { if blockIntf, ok := vm.blockCache.Get(id); ok { return blockIntf.(*Block) } - ethBlock := vm.chain.GetBlockByHash(id.Key()) + ethBlock := vm.chain.GetBlockByHash(common.Hash(id)) if ethBlock == nil { return nil } block := &Block{ - id: ids.NewID(ethBlock.Hash()), + id: ids.ID(ethBlock.Hash()), ethBlock: ethBlock, vm: vm, } @@ -898,10 +898,10 @@ func (vm *VM) GetSpendableFunds(keys []*crypto.PrivateKeySECP256K1R, assetID ids } addr := GetEthAddress(key) var balance uint64 - if assetID.Equals(vm.ctx.AVAXAssetID) { + if assetID == vm.ctx.AVAXAssetID { balance = new(big.Int).Div(state.GetBalance(addr), x2cRate).Uint64() } else { - balance = state.GetBalanceMultiCoin(addr, assetID.Key()).Uint64() + balance = state.GetBalanceMultiCoin(addr, common.Hash(assetID)).Uint64() } if balance == 0 { continue -- cgit v1.2.3