github.com/theQRL/go-zond@v0.1.1/core/vm/interpreter_test.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/common/math"
    26  	"github.com/theQRL/go-zond/core/rawdb"
    27  	"github.com/theQRL/go-zond/core/state"
    28  	"github.com/theQRL/go-zond/core/types"
    29  	"github.com/theQRL/go-zond/params"
    30  )
    31  
    32  var loopInterruptTests = []string{
    33  	// infinite loop using JUMP: push(2) jumpdest dup1 jump
    34  	"60025b8056",
    35  	// infinite loop using JUMPI: push(1) push(4) jumpdest dup2 dup2 jumpi
    36  	"600160045b818157",
    37  }
    38  
    39  func TestLoopInterrupt(t *testing.T) {
    40  	address := common.BytesToAddress([]byte("contract"))
    41  	vmctx := BlockContext{
    42  		Transfer: func(StateDB, common.Address, common.Address, *big.Int) {},
    43  	}
    44  
    45  	for i, tt := range loopInterruptTests {
    46  		statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    47  		statedb.CreateAccount(address)
    48  		statedb.SetCode(address, common.Hex2Bytes(tt))
    49  		statedb.Finalise(true)
    50  
    51  		evm := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{})
    52  
    53  		errChannel := make(chan error)
    54  		timeout := make(chan bool)
    55  
    56  		go func(evm *EVM) {
    57  			_, _, err := evm.Call(AccountRef(common.Address{}), address, nil, math.MaxUint64, new(big.Int))
    58  			errChannel <- err
    59  		}(evm)
    60  
    61  		go func() {
    62  			<-time.After(time.Second)
    63  			timeout <- true
    64  		}()
    65  
    66  		evm.Cancel()
    67  
    68  		select {
    69  		case <-timeout:
    70  			t.Errorf("test %d timed out", i)
    71  		case err := <-errChannel:
    72  			if err != nil {
    73  				t.Errorf("test %d failure: %v", i, err)
    74  			}
    75  		}
    76  	}
    77  }