util.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 tests
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/state"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. )
  28. func checkLogs(tlog []Log, logs state.Logs) error {
  29. if len(tlog) != len(logs) {
  30. return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
  31. } else {
  32. for i, log := range tlog {
  33. if common.HexToAddress(log.AddressF) != logs[i].Address {
  34. return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
  35. }
  36. if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
  37. return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
  38. }
  39. if len(log.TopicsF) != len(logs[i].Topics) {
  40. return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
  41. } else {
  42. for j, topic := range log.TopicsF {
  43. if common.HexToHash(topic) != logs[i].Topics[j] {
  44. return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
  45. }
  46. }
  47. }
  48. genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
  49. if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
  50. return fmt.Errorf("bloom mismatch")
  51. }
  52. }
  53. }
  54. return nil
  55. }
  56. type Account struct {
  57. Balance string
  58. Code string
  59. Nonce string
  60. Storage map[string]string
  61. }
  62. type Log struct {
  63. AddressF string `json:"address"`
  64. DataF string `json:"data"`
  65. TopicsF []string `json:"topics"`
  66. BloomF string `json:"bloom"`
  67. }
  68. func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) }
  69. func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) }
  70. func (self Log) RlpData() interface{} { return nil }
  71. func (self Log) Topics() [][]byte {
  72. t := make([][]byte, len(self.TopicsF))
  73. for i, topic := range self.TopicsF {
  74. t[i] = common.Hex2Bytes(topic)
  75. }
  76. return t
  77. }
  78. func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject {
  79. obj := state.NewStateObject(common.HexToAddress(addr), db)
  80. obj.SetBalance(common.Big(account.Balance))
  81. if common.IsHex(account.Code) {
  82. account.Code = account.Code[2:]
  83. }
  84. obj.SetCode(common.Hex2Bytes(account.Code))
  85. obj.SetNonce(common.Big(account.Nonce).Uint64())
  86. return obj
  87. }
  88. type VmEnv struct {
  89. CurrentCoinbase string
  90. CurrentDifficulty string
  91. CurrentGasLimit string
  92. CurrentNumber string
  93. CurrentTimestamp interface{}
  94. PreviousHash string
  95. }
  96. type VmTest struct {
  97. Callcreates interface{}
  98. //Env map[string]string
  99. Env VmEnv
  100. Exec map[string]string
  101. Transaction map[string]string
  102. Logs []Log
  103. Gas string
  104. Out string
  105. Post map[string]Account
  106. Pre map[string]Account
  107. PostStateRoot string
  108. }
  109. type Env struct {
  110. depth int
  111. state *state.StateDB
  112. skipTransfer bool
  113. initial bool
  114. Gas *big.Int
  115. origin common.Address
  116. //parent common.Hash
  117. coinbase common.Address
  118. number *big.Int
  119. time *big.Int
  120. difficulty *big.Int
  121. gasLimit *big.Int
  122. logs []vm.StructLog
  123. vmTest bool
  124. }
  125. func NewEnv(state *state.StateDB) *Env {
  126. return &Env{
  127. state: state,
  128. }
  129. }
  130. func (self *Env) StructLogs() []vm.StructLog {
  131. return self.logs
  132. }
  133. func (self *Env) AddStructLog(log vm.StructLog) {
  134. self.logs = append(self.logs, log)
  135. }
  136. func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env {
  137. env := NewEnv(state)
  138. env.origin = common.HexToAddress(exeValues["caller"])
  139. //env.parent = common.Hex2Bytes(envValues["previousHash"])
  140. env.coinbase = common.HexToAddress(envValues["currentCoinbase"])
  141. env.number = common.Big(envValues["currentNumber"])
  142. env.time = common.Big(envValues["currentTimestamp"])
  143. env.difficulty = common.Big(envValues["currentDifficulty"])
  144. env.gasLimit = common.Big(envValues["currentGasLimit"])
  145. env.Gas = new(big.Int)
  146. return env
  147. }
  148. func (self *Env) Origin() common.Address { return self.origin }
  149. func (self *Env) BlockNumber() *big.Int { return self.number }
  150. //func (self *Env) PrevHash() []byte { return self.parent }
  151. func (self *Env) Coinbase() common.Address { return self.coinbase }
  152. func (self *Env) Time() *big.Int { return self.time }
  153. func (self *Env) Difficulty() *big.Int { return self.difficulty }
  154. func (self *Env) State() *state.StateDB { return self.state }
  155. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  156. func (self *Env) VmType() vm.Type { return vm.StdVmTy }
  157. func (self *Env) GetHash(n uint64) common.Hash {
  158. return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
  159. }
  160. func (self *Env) AddLog(log *state.Log) {
  161. self.state.AddLog(log)
  162. }
  163. func (self *Env) Depth() int { return self.depth }
  164. func (self *Env) SetDepth(i int) { self.depth = i }
  165. func (self *Env) CanTransfer(from vm.Account, balance *big.Int) bool {
  166. if self.skipTransfer {
  167. if self.initial {
  168. self.initial = false
  169. return true
  170. }
  171. }
  172. return from.Balance().Cmp(balance) >= 0
  173. }
  174. func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
  175. if self.skipTransfer {
  176. return nil
  177. }
  178. return vm.Transfer(from, to, amount)
  179. }
  180. func (self *Env) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution {
  181. exec := core.NewExecution(self, addr, data, gas, price, value)
  182. return exec
  183. }
  184. func (self *Env) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  185. if self.vmTest && self.depth > 0 {
  186. caller.ReturnGas(gas, price)
  187. return nil, nil
  188. }
  189. exe := self.vm(&addr, data, gas, price, value)
  190. ret, err := exe.Call(addr, caller)
  191. self.Gas = exe.Gas
  192. return ret, err
  193. }
  194. func (self *Env) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  195. if self.vmTest && self.depth > 0 {
  196. caller.ReturnGas(gas, price)
  197. return nil, nil
  198. }
  199. caddr := caller.Address()
  200. exe := self.vm(&caddr, data, gas, price, value)
  201. return exe.Call(addr, caller)
  202. }
  203. func (self *Env) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
  204. exe := self.vm(nil, data, gas, price, value)
  205. if self.vmTest {
  206. caller.ReturnGas(gas, price)
  207. nonce := self.state.GetNonce(caller.Address())
  208. obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce))
  209. return nil, nil, obj
  210. } else {
  211. return exe.Create(caller)
  212. }
  213. }
  214. type Message struct {
  215. from common.Address
  216. to *common.Address
  217. value, gas, price *big.Int
  218. data []byte
  219. nonce uint64
  220. }
  221. func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int, nonce uint64) Message {
  222. return Message{from, to, value, gas, price, data, nonce}
  223. }
  224. func (self Message) Hash() []byte { return nil }
  225. func (self Message) From() (common.Address, error) { return self.from, nil }
  226. func (self Message) To() *common.Address { return self.to }
  227. func (self Message) GasPrice() *big.Int { return self.price }
  228. func (self Message) Gas() *big.Int { return self.gas }
  229. func (self Message) Value() *big.Int { return self.value }
  230. func (self Message) Nonce() uint64 { return self.nonce }
  231. func (self Message) Data() []byte { return self.data }