util.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2015 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 tests
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "os"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/core/vm"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/ethdb"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. "github.com/ethereum/go-ethereum/params"
  31. )
  32. var (
  33. ForceJit bool
  34. EnableJit bool
  35. )
  36. func init() {
  37. glog.SetV(0)
  38. if os.Getenv("JITVM") == "true" {
  39. ForceJit = true
  40. EnableJit = true
  41. }
  42. }
  43. func checkLogs(tlog []Log, logs vm.Logs) error {
  44. if len(tlog) != len(logs) {
  45. return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
  46. } else {
  47. for i, log := range tlog {
  48. if common.HexToAddress(log.AddressF) != logs[i].Address {
  49. return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
  50. }
  51. if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
  52. return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
  53. }
  54. if len(log.TopicsF) != len(logs[i].Topics) {
  55. return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
  56. } else {
  57. for j, topic := range log.TopicsF {
  58. if common.HexToHash(topic) != logs[i].Topics[j] {
  59. return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
  60. }
  61. }
  62. }
  63. genBloom := common.LeftPadBytes(types.LogsBloom(vm.Logs{logs[i]}).Bytes(), 256)
  64. if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
  65. return fmt.Errorf("bloom mismatch")
  66. }
  67. }
  68. }
  69. return nil
  70. }
  71. type Account struct {
  72. Balance string
  73. Code string
  74. Nonce string
  75. Storage map[string]string
  76. }
  77. type Log struct {
  78. AddressF string `json:"address"`
  79. DataF string `json:"data"`
  80. TopicsF []string `json:"topics"`
  81. BloomF string `json:"bloom"`
  82. }
  83. func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) }
  84. func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) }
  85. func (self Log) RlpData() interface{} { return nil }
  86. func (self Log) Topics() [][]byte {
  87. t := make([][]byte, len(self.TopicsF))
  88. for i, topic := range self.TopicsF {
  89. t[i] = common.Hex2Bytes(topic)
  90. }
  91. return t
  92. }
  93. func makePreState(db ethdb.Database, accounts map[string]Account) *state.StateDB {
  94. statedb, _ := state.New(common.Hash{}, db)
  95. for addr, account := range accounts {
  96. insertAccount(statedb, addr, account)
  97. }
  98. return statedb
  99. }
  100. func insertAccount(state *state.StateDB, saddr string, account Account) {
  101. if common.IsHex(account.Code) {
  102. account.Code = account.Code[2:]
  103. }
  104. addr := common.HexToAddress(saddr)
  105. state.SetCode(addr, common.Hex2Bytes(account.Code))
  106. state.SetNonce(addr, common.Big(account.Nonce).Uint64())
  107. state.SetBalance(addr, common.Big(account.Balance))
  108. for a, v := range account.Storage {
  109. state.SetState(addr, common.HexToHash(a), common.HexToHash(v))
  110. }
  111. }
  112. type VmEnv struct {
  113. CurrentCoinbase string
  114. CurrentDifficulty string
  115. CurrentGasLimit string
  116. CurrentNumber string
  117. CurrentTimestamp interface{}
  118. PreviousHash string
  119. }
  120. type VmTest struct {
  121. Callcreates interface{}
  122. //Env map[string]string
  123. Env VmEnv
  124. Exec map[string]string
  125. Transaction map[string]string
  126. Logs []Log
  127. Gas string
  128. Out string
  129. Post map[string]Account
  130. Pre map[string]Account
  131. PostStateRoot string
  132. }
  133. type RuleSet struct {
  134. HomesteadBlock *big.Int
  135. DAOForkBlock *big.Int
  136. DAOForkSupport bool
  137. HomesteadGasRepriceBlock *big.Int
  138. }
  139. func (r RuleSet) IsHomestead(n *big.Int) bool {
  140. return n.Cmp(r.HomesteadBlock) >= 0
  141. }
  142. func (r RuleSet) GasTable(num *big.Int) params.GasTable {
  143. if r.HomesteadGasRepriceBlock == nil || num == nil || num.Cmp(r.HomesteadGasRepriceBlock) < 0 {
  144. return params.GasTableHomestead
  145. }
  146. return params.GasTableHomesteadGasRepriceFork
  147. }
  148. type Env struct {
  149. ruleSet RuleSet
  150. depth int
  151. state *state.StateDB
  152. skipTransfer bool
  153. initial bool
  154. Gas *big.Int
  155. origin common.Address
  156. parent common.Hash
  157. coinbase common.Address
  158. number *big.Int
  159. time *big.Int
  160. difficulty *big.Int
  161. gasLimit *big.Int
  162. vmTest bool
  163. evm *vm.EVM
  164. }
  165. func NewEnv(ruleSet RuleSet, state *state.StateDB) *Env {
  166. env := &Env{
  167. ruleSet: ruleSet,
  168. state: state,
  169. }
  170. return env
  171. }
  172. func NewEnvFromMap(ruleSet RuleSet, state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env {
  173. env := NewEnv(ruleSet, state)
  174. env.origin = common.HexToAddress(exeValues["caller"])
  175. env.parent = common.HexToHash(envValues["previousHash"])
  176. env.coinbase = common.HexToAddress(envValues["currentCoinbase"])
  177. env.number = common.Big(envValues["currentNumber"])
  178. env.time = common.Big(envValues["currentTimestamp"])
  179. env.difficulty = common.Big(envValues["currentDifficulty"])
  180. env.gasLimit = common.Big(envValues["currentGasLimit"])
  181. env.Gas = new(big.Int)
  182. env.evm = vm.New(env, vm.Config{
  183. EnableJit: EnableJit,
  184. ForceJit: ForceJit,
  185. })
  186. return env
  187. }
  188. func (self *Env) RuleSet() vm.RuleSet { return self.ruleSet }
  189. func (self *Env) Vm() vm.Vm { return self.evm }
  190. func (self *Env) Origin() common.Address { return self.origin }
  191. func (self *Env) BlockNumber() *big.Int { return self.number }
  192. func (self *Env) Coinbase() common.Address { return self.coinbase }
  193. func (self *Env) Time() *big.Int { return self.time }
  194. func (self *Env) Difficulty() *big.Int { return self.difficulty }
  195. func (self *Env) Db() vm.Database { return self.state }
  196. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  197. func (self *Env) VmType() vm.Type { return vm.StdVmTy }
  198. func (self *Env) GetHash(n uint64) common.Hash {
  199. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  200. }
  201. func (self *Env) AddLog(log *vm.Log) {
  202. self.state.AddLog(log)
  203. }
  204. func (self *Env) Depth() int { return self.depth }
  205. func (self *Env) SetDepth(i int) { self.depth = i }
  206. func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
  207. if self.skipTransfer {
  208. if self.initial {
  209. self.initial = false
  210. return true
  211. }
  212. }
  213. return self.state.GetBalance(from).Cmp(balance) >= 0
  214. }
  215. func (self *Env) SnapshotDatabase() int {
  216. return self.state.Snapshot()
  217. }
  218. func (self *Env) RevertToSnapshot(snapshot int) {
  219. self.state.RevertToSnapshot(snapshot)
  220. }
  221. func (self *Env) Transfer(from, to vm.Account, amount *big.Int) {
  222. if self.skipTransfer {
  223. return
  224. }
  225. core.Transfer(from, to, amount)
  226. }
  227. func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  228. if self.vmTest && self.depth > 0 {
  229. caller.ReturnGas(gas, price)
  230. return nil, nil
  231. }
  232. ret, err := core.Call(self, caller, addr, data, gas, price, value)
  233. self.Gas = gas
  234. return ret, err
  235. }
  236. func (self *Env) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  237. if self.vmTest && self.depth > 0 {
  238. caller.ReturnGas(gas, price)
  239. return nil, nil
  240. }
  241. return core.CallCode(self, caller, addr, data, gas, price, value)
  242. }
  243. func (self *Env) DelegateCall(caller vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  244. if self.vmTest && self.depth > 0 {
  245. caller.ReturnGas(gas, price)
  246. return nil, nil
  247. }
  248. return core.DelegateCall(self, caller, addr, data, gas, price)
  249. }
  250. func (self *Env) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  251. if self.vmTest {
  252. caller.ReturnGas(gas, price)
  253. nonce := self.state.GetNonce(caller.Address())
  254. obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce))
  255. return nil, obj.Address(), nil
  256. } else {
  257. return core.Create(self, caller, data, gas, price, value)
  258. }
  259. }
  260. type Message struct {
  261. from common.Address
  262. to *common.Address
  263. value, gas, price *big.Int
  264. data []byte
  265. nonce uint64
  266. }
  267. func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int, nonce uint64) Message {
  268. return Message{from, to, value, gas, price, data, nonce}
  269. }
  270. func (self Message) Hash() []byte { return nil }
  271. func (self Message) From() (common.Address, error) { return self.from, nil }
  272. func (self Message) FromFrontier() (common.Address, error) { return self.from, nil }
  273. func (self Message) To() *common.Address { return self.to }
  274. func (self Message) GasPrice() *big.Int { return self.price }
  275. func (self Message) Gas() *big.Int { return self.gas }
  276. func (self Message) Value() *big.Int { return self.value }
  277. func (self Message) Nonce() uint64 { return self.nonce }
  278. func (self Message) CheckNonce() bool { return true }
  279. func (self Message) Data() []byte { return self.data }