diff options
Diffstat (limited to 'plugin/evm')
-rw-r--r-- | plugin/evm/vm.go | 27 | ||||
-rw-r--r-- | plugin/evm/vm_test.go | 264 |
2 files changed, 283 insertions, 8 deletions
diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 58ab600..5d379b7 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -156,9 +156,9 @@ type VM struct { acceptedDB database.Database + txPoolStabilizedLock sync.Mutex txPoolStabilizedHead common.Hash txPoolStabilizedOk chan struct{} - txPoolStabilizedLock sync.Mutex txPoolStabilizedShutdownChan chan struct{} metalock sync.Mutex @@ -401,7 +401,10 @@ func (vm *VM) Bootstrapping() error { return vm.fx.Bootstrapping() } // Bootstrapped notifies this VM that the consensus engine has finished // bootstrapping -func (vm *VM) Bootstrapped() error { return vm.fx.Bootstrapped() } +func (vm *VM) Bootstrapped() error { + vm.ctx.Bootstrapped() + return vm.fx.Bootstrapped() +} // Shutdown implements the snowman.ChainVM interface func (vm *VM) Shutdown() error { @@ -421,10 +424,10 @@ func (vm *VM) Shutdown() error { func (vm *VM) BuildBlock() (snowman.Block, error) { vm.chain.GenBlock() block := <-vm.newBlockChan - if block == nil { - return nil, errCreateBlock - } + // reset the min block time timer + // after finishing attempt to build + // a block. vm.bdlock.Lock() vm.bdTimerState = bdTimerStateMin vm.bdGenWaitFlag = false @@ -432,7 +435,11 @@ func (vm *VM) BuildBlock() (snowman.Block, error) { vm.blockDelayTimer.SetTimeoutIn(minBlockTime) vm.bdlock.Unlock() - log.Debug(fmt.Sprintf("built block %s", block.ID())) + if block == nil { + return nil, errCreateBlock + } + + log.Debug(fmt.Sprintf("Built block %s", block.ID())) // make sure Tx Pool is updated <-vm.txPoolStabilizedOk return block, nil @@ -739,7 +746,13 @@ func (vm *VM) awaitTxPoolStabilized() { defer vm.shutdownWg.Done() for { select { - case e := <-vm.newMinedBlockSub.Chan(): + case e, ok := <-vm.newMinedBlockSub.Chan(): + if !ok { + return + } + if e == nil { + continue + } switch h := e.Data.(type) { case core.NewMinedBlockEvent: vm.txPoolStabilizedLock.Lock() diff --git a/plugin/evm/vm_test.go b/plugin/evm/vm_test.go index ecdc991..4593404 100644 --- a/plugin/evm/vm_test.go +++ b/plugin/evm/vm_test.go @@ -4,10 +4,13 @@ package evm import ( + "crypto/rand" "encoding/json" + "math/big" "testing" "github.com/ava-labs/avalanchego/api/keystore" + "github.com/ava-labs/avalanchego/cache" "github.com/ava-labs/avalanchego/chains/atomic" "github.com/ava-labs/avalanchego/database/memdb" "github.com/ava-labs/avalanchego/database/prefixdb" @@ -20,7 +23,10 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/secp256k1fx" + "github.com/ava-labs/coreth" "github.com/ava-labs/coreth/core" + "github.com/ava-labs/coreth/core/types" + "github.com/ava-labs/coreth/params" "github.com/ethereum/go-ethereum/common" ) @@ -133,6 +139,16 @@ func GenesisVM(t *testing.T, finishBootstrapping bool) (chan engCommon.Message, t.Fatal(err) } + if finishBootstrapping { + if err := vm.Bootstrapping(); err != nil { + t.Fatal(err) + } + + if err := vm.Bootstrapped(); err != nil { + t.Fatal(err) + } + } + return issuer, vm, genesisBytes, m } @@ -146,7 +162,7 @@ func TestVMGenesis(t *testing.T) { }() } -func TestIssueTxs(t *testing.T) { +func TestIssueAtomicTxs(t *testing.T) { issuer, vm, _, sharedMemory := GenesisVM(t, true) defer func() { @@ -209,6 +225,10 @@ func TestIssueTxs(t *testing.T) { t.Fatal(err) } + if err := blk.Verify(); err != nil { + t.Fatal(err) + } + if status := blk.Status(); status != choices.Processing { t.Fatalf("Expected status of built block to be %s, but found %s", choices.Processing, status) } @@ -242,6 +262,10 @@ func TestIssueTxs(t *testing.T) { t.Fatal(err) } + if err := blk2.Verify(); err != nil { + t.Fatal(err) + } + if status := blk2.Status(); status != choices.Processing { t.Fatalf("Expected status of built block to be %s, but found %s", choices.Processing, status) } @@ -259,3 +283,241 @@ func TestIssueTxs(t *testing.T) { t.Fatalf("Expected last accepted blockID to be the accepted block: %s, but found %s", blk2.ID(), lastAcceptedID) } } + +func TestBuildEthTxBlock(t *testing.T) { + issuer, vm, _, sharedMemory := GenesisVM(t, true) + + defer func() { + if err := vm.Shutdown(); err != nil { + t.Fatal(err) + } + }() + + key, err := coreth.NewKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + + importAmount := uint64(10000000) + utxoID := avax.UTXOID{ + TxID: ids.ID{ + 0x0f, 0x2f, 0x4f, 0x6f, 0x8e, 0xae, 0xce, 0xee, + 0x0d, 0x2d, 0x4d, 0x6d, 0x8c, 0xac, 0xcc, 0xec, + 0x0b, 0x2b, 0x4b, 0x6b, 0x8a, 0xaa, 0xca, 0xea, + 0x09, 0x29, 0x49, 0x69, 0x88, 0xa8, 0xc8, 0xe8, + }, + } + + utxo := &avax.UTXO{ + UTXOID: utxoID, + Asset: avax.Asset{ID: vm.ctx.AVAXAssetID}, + Out: &secp256k1fx.TransferOutput{ + Amt: importAmount, + OutputOwners: secp256k1fx.OutputOwners{ + Threshold: 1, + Addrs: []ids.ShortID{testKeys[0].PublicKey().Address()}, + }, + }, + } + utxoBytes, err := vm.codec.Marshal(codecVersion, utxo) + if err != nil { + t.Fatal(err) + } + + xChainSharedMemory := sharedMemory.NewSharedMemory(vm.ctx.XChainID) + inputID := utxo.InputID() + if err := xChainSharedMemory.Put(vm.ctx.ChainID, []*atomic.Element{{ + Key: inputID[:], + Value: utxoBytes, + Traits: [][]byte{ + testKeys[0].PublicKey().Address().Bytes(), + }, + }}); err != nil { + t.Fatal(err) + } + + importTx, err := vm.newImportTx(vm.ctx.XChainID, key.Address, []*crypto.PrivateKeySECP256K1R{testKeys[0]}) + if err != nil { + t.Fatal(err) + } + + if err := vm.issueTx(importTx); err != nil { + t.Fatal(err) + } + + <-issuer + + blk, err := vm.BuildBlock() + if err != nil { + t.Fatal(err) + } + + if err := blk.Verify(); err != nil { + t.Fatal(err) + } + + if status := blk.Status(); status != choices.Processing { + t.Fatalf("Expected status of built block to be %s, but found %s", choices.Processing, status) + } + + if err := blk.Accept(); err != nil { + t.Fatal(err) + } + + txs := make([]*types.Transaction, 10) + for i := 0; i < 10; i++ { + tx := types.NewTransaction(uint64(i), key.Address, big.NewInt(10), 21000, params.MinGasPrice, nil) + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(vm.chainID), key.PrivateKey) + if err != nil { + t.Fatal(err) + } + txs[i] = signedTx + } + errs := vm.chain.AddRemoteTxs(txs) + for i, err := range errs { + if err != nil { + t.Fatalf("Failed to add tx at index %d: %s", i, err) + } + } + + <-issuer + + blk, err = vm.BuildBlock() + if err != nil { + t.Fatal(err) + } + + if err := blk.Verify(); err != nil { + t.Fatal(err) + } + + if status := blk.Status(); status != choices.Processing { + t.Fatalf("Expected status of built block to be %s, but found %s", choices.Processing, status) + } + + if err := blk.Accept(); err != nil { + t.Fatal(err) + } + + if status := blk.Status(); status != choices.Accepted { + t.Fatalf("Expected status of accepted block to be %s, but found %s", choices.Accepted, status) + } + + lastAcceptedID := vm.LastAccepted() + if lastAcceptedID != blk.ID() { + t.Fatalf("Expected last accepted blockID to be the accepted block: %s, but found %s", blk.ID(), lastAcceptedID) + } +} + +func TestConflictingImportTxs(t *testing.T) { + issuer, vm, _, sharedMemory := GenesisVM(t, true) + + defer func() { + if err := vm.Shutdown(); err != nil { + t.Fatal(err) + } + }() + + conflictKey, err := coreth.NewKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + + xChainSharedMemory := sharedMemory.NewSharedMemory(vm.ctx.XChainID) + importTxs := make([]*Tx, 0, 3) + conflictTxs := make([]*Tx, 0, 3) + for i, key := range testKeys { + importAmount := uint64(10000000) + utxoID := avax.UTXOID{ + TxID: ids.ID{byte(i)}, + } + + utxo := &avax.UTXO{ + UTXOID: utxoID, + Asset: avax.Asset{ID: vm.ctx.AVAXAssetID}, + Out: &secp256k1fx.TransferOutput{ + Amt: importAmount, + OutputOwners: secp256k1fx.OutputOwners{ + Threshold: 1, + Addrs: []ids.ShortID{key.PublicKey().Address()}, + }, + }, + } + utxoBytes, err := vm.codec.Marshal(codecVersion, utxo) + if err != nil { + t.Fatal(err) + } + + inputID := utxo.InputID() + if err := xChainSharedMemory.Put(vm.ctx.ChainID, []*atomic.Element{{ + Key: inputID[:], + Value: utxoBytes, + Traits: [][]byte{ + key.PublicKey().Address().Bytes(), + }, + }}); err != nil { + t.Fatal(err) + } + + importTx, err := vm.newImportTx(vm.ctx.XChainID, testEthAddrs[i], []*crypto.PrivateKeySECP256K1R{key}) + if err != nil { + t.Fatal(err) + } + importTxs = append(importTxs, importTx) + + conflictTx, err := vm.newImportTx(vm.ctx.XChainID, conflictKey.Address, []*crypto.PrivateKeySECP256K1R{key}) + if err != nil { + t.Fatal(err) + } + conflictTxs = append(conflictTxs, conflictTx) + } + + expectedParentBlkID := vm.LastAccepted() + for i, tx := range importTxs { + if err := vm.issueTx(tx); err != nil { + t.Fatal(err) + } + + <-issuer + + blk, err := vm.BuildBlock() + if err != nil { + t.Fatal(err) + } + + if err := blk.Verify(); err != nil { + t.Fatal(err) + } + + if status := blk.Status(); status != choices.Processing { + t.Fatalf("Expected status of built block %d to be %s, but found %s", i, choices.Processing, status) + } + + if parentID := blk.Parent().ID(); parentID != expectedParentBlkID { + t.Fatalf("Expected parent to have blockID %s, but found %s", expectedParentBlkID, parentID) + } + + expectedParentBlkID = blk.ID() + vm.SetPreference(blk.ID()) + } + + // Shrink the atomic input cache to ensure that + // verification handles cache misses correctly. + vm.blockAtomicInputCache = cache.LRU{Size: 1} + + for i, tx := range conflictTxs { + if err := vm.issueTx(tx); err != nil { + t.Fatal(err) + } + + <-issuer + + _, err := vm.BuildBlock() + // The new block is verified in BuildBlock, so + // BuildBlock should fail due to an attempt to + // double spend an atomic UTXO. + if err == nil { + t.Fatalf("Block verification should have failed in BuildBlock %d due to double spending atomic UTXO", i) + } + } +} |