jit_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2014 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. package vm
  17. import (
  18. "math/big"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. )
  24. const maxRun = 1000
  25. func TestCompiling(t *testing.T) {
  26. prog := NewProgram([]byte{0x60, 0x10})
  27. err := CompileProgram(prog)
  28. if err != nil {
  29. t.Error("didn't expect compile error")
  30. }
  31. if len(prog.instructions) != 1 {
  32. t.Error("exected 1 compiled instruction, got", len(prog.instructions))
  33. }
  34. }
  35. func TestResetInput(t *testing.T) {
  36. var sender account
  37. env := NewEnv()
  38. contract := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
  39. contract.CodeAddr = &common.Address{}
  40. program := NewProgram([]byte{})
  41. RunProgram(program, env, contract, []byte{0xbe, 0xef})
  42. if contract.Input != nil {
  43. t.Errorf("expected input to be nil, got %x", contract.Input)
  44. }
  45. }
  46. func TestPcMappingToInstruction(t *testing.T) {
  47. program := NewProgram([]byte{byte(PUSH2), 0xbe, 0xef, byte(ADD)})
  48. CompileProgram(program)
  49. if program.mapping[3] != 1 {
  50. t.Error("expected mapping PC 4 to me instr no. 2, got", program.mapping[4])
  51. }
  52. }
  53. var benchmarks = map[string]vmBench{
  54. "pushes": vmBench{
  55. false, false, false,
  56. common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
  57. },
  58. }
  59. func BenchmarkPushes(b *testing.B) {
  60. runVmBench(benchmarks["pushes"], b)
  61. }
  62. type vmBench struct {
  63. precompile bool // compile prior to executing
  64. nojit bool // ignore jit (sets DisbaleJit = true
  65. forcejit bool // forces the jit, precompile is ignored
  66. code []byte
  67. input []byte
  68. }
  69. type account struct{}
  70. func (account) SubBalance(amount *big.Int) {}
  71. func (account) AddBalance(amount *big.Int) {}
  72. func (account) SetBalance(*big.Int) {}
  73. func (account) SetNonce(uint64) {}
  74. func (account) Balance() *big.Int { return nil }
  75. func (account) Address() common.Address { return common.Address{} }
  76. func (account) ReturnGas(*big.Int, *big.Int) {}
  77. func (account) SetCode([]byte) {}
  78. func runVmBench(test vmBench, b *testing.B) {
  79. var sender account
  80. if test.precompile && !test.forcejit {
  81. NewProgram(test.code)
  82. }
  83. env := NewEnv()
  84. EnableJit = !test.nojit
  85. ForceJit = test.forcejit
  86. b.ResetTimer()
  87. for i := 0; i < b.N; i++ {
  88. context := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
  89. context.Code = test.code
  90. context.CodeAddr = &common.Address{}
  91. _, err := New(env).Run(context, test.input)
  92. if err != nil {
  93. b.Error(err)
  94. b.FailNow()
  95. }
  96. }
  97. }
  98. type Env struct {
  99. gasLimit *big.Int
  100. depth int
  101. }
  102. func NewEnv() *Env {
  103. return &Env{big.NewInt(10000), 0}
  104. }
  105. func (self *Env) Origin() common.Address { return common.Address{} }
  106. func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
  107. func (self *Env) AddStructLog(log StructLog) {
  108. }
  109. func (self *Env) StructLogs() []StructLog {
  110. return nil
  111. }
  112. //func (self *Env) PrevHash() []byte { return self.parent }
  113. func (self *Env) Coinbase() common.Address { return common.Address{} }
  114. func (self *Env) MakeSnapshot() Database { return nil }
  115. func (self *Env) SetSnapshot(Database) {}
  116. func (self *Env) Time() *big.Int { return big.NewInt(time.Now().Unix()) }
  117. func (self *Env) Difficulty() *big.Int { return big.NewInt(0) }
  118. func (self *Env) Db() Database { return nil }
  119. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  120. func (self *Env) VmType() Type { return StdVmTy }
  121. func (self *Env) GetHash(n uint64) common.Hash {
  122. return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
  123. }
  124. func (self *Env) AddLog(log *Log) {
  125. }
  126. func (self *Env) Depth() int { return self.depth }
  127. func (self *Env) SetDepth(i int) { self.depth = i }
  128. func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
  129. return true
  130. }
  131. func (self *Env) Transfer(from, to Account, amount *big.Int) error {
  132. return nil
  133. }
  134. func (self *Env) Call(caller ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  135. return nil, nil
  136. }
  137. func (self *Env) CallCode(caller ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  138. return nil, nil
  139. }
  140. func (self *Env) Create(caller ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  141. return nil, common.Address{}, nil
  142. }