util.go 7.3 KB

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