api_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package rpc
  2. import (
  3. "encoding/json"
  4. // "sync"
  5. "testing"
  6. // "time"
  7. // "fmt"
  8. "io/ioutil"
  9. "strconv"
  10. "github.com/ethereum/go-ethereum/common/compiler"
  11. "github.com/ethereum/go-ethereum/xeth"
  12. )
  13. func TestWeb3Sha3(t *testing.T) {
  14. jsonstr := `{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`
  15. expected := "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
  16. api := &EthereumApi{}
  17. var req RpcRequest
  18. json.Unmarshal([]byte(jsonstr), &req)
  19. var response interface{}
  20. _ = api.GetRequestReply(&req, &response)
  21. if response.(string) != expected {
  22. t.Errorf("Expected %s got %s", expected, response)
  23. }
  24. }
  25. const solcVersion = "0.9.23"
  26. func TestCompileSolidity(t *testing.T) {
  27. solc, err := compiler.New("")
  28. if solc == nil {
  29. t.Skip("no solc found: skip")
  30. } else if solc.Version() != solcVersion {
  31. t.Logf("WARNING: solc different version found (%v, test written for %v, may need to update)", solc.Version(), solcVersion)
  32. }
  33. source := `contract test {\n` +
  34. " /// @notice Will multiply `a` by 7." + `\n` +
  35. ` function multiply(uint a) returns(uint d) {\n` +
  36. ` return a * 7;\n` +
  37. ` }\n` +
  38. `}\n`
  39. jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}`
  40. expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"
  41. expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]`
  42. expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}`
  43. expDeveloperDoc := `{"methods":{}}`
  44. expCompilerVersion := solc.Version()
  45. expLanguage := "Solidity"
  46. expLanguageVersion := "0"
  47. expSource := source
  48. api := NewEthereumApi(&xeth.XEth{})
  49. var req RpcRequest
  50. json.Unmarshal([]byte(jsonstr), &req)
  51. var response interface{}
  52. err = api.GetRequestReply(&req, &response)
  53. if err != nil {
  54. t.Errorf("expected no error, got %v", err)
  55. }
  56. respjson, err := json.Marshal(response)
  57. if err != nil {
  58. t.Errorf("expected no error, got %v", err)
  59. }
  60. var contract = compiler.Contract{}
  61. err = json.Unmarshal(respjson, &contract)
  62. if err != nil {
  63. t.Errorf("expected no error, got %v", err)
  64. }
  65. if contract.Code != expCode {
  66. t.Errorf("Expected \n%s got \n%s", expCode, contract.Code)
  67. }
  68. if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` {
  69. t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source))
  70. }
  71. if contract.Info.Language != expLanguage {
  72. t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language)
  73. }
  74. if contract.Info.LanguageVersion != expLanguageVersion {
  75. t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion)
  76. }
  77. if contract.Info.CompilerVersion != expCompilerVersion {
  78. t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion)
  79. }
  80. userdoc, err := json.Marshal(contract.Info.UserDoc)
  81. if err != nil {
  82. t.Errorf("expected no error, got %v", err)
  83. }
  84. devdoc, err := json.Marshal(contract.Info.DeveloperDoc)
  85. if err != nil {
  86. t.Errorf("expected no error, got %v", err)
  87. }
  88. abidef, err := json.Marshal(contract.Info.AbiDefinition)
  89. if err != nil {
  90. t.Errorf("expected no error, got %v", err)
  91. }
  92. if string(abidef) != expAbiDefinition {
  93. t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef))
  94. }
  95. ioutil.WriteFile("/tmp/abidef", []byte(string(abidef)), 0700)
  96. ioutil.WriteFile("/tmp/expabidef", []byte(expAbiDefinition), 0700)
  97. if string(userdoc) != expUserDoc {
  98. t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc))
  99. }
  100. if string(devdoc) != expDeveloperDoc {
  101. t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc))
  102. }
  103. }
  104. // func TestDbStr(t *testing.T) {
  105. // jsonput := `{"jsonrpc":"2.0","method":"db_putString","params":["testDB","myKey","myString"],"id":64}`
  106. // jsonget := `{"jsonrpc":"2.0","method":"db_getString","params":["testDB","myKey"],"id":64}`
  107. // expected := "myString"
  108. // xeth := &xeth.XEth{}
  109. // api := NewEthereumApi(xeth)
  110. // var response interface{}
  111. // var req RpcRequest
  112. // json.Unmarshal([]byte(jsonput), &req)
  113. // _ = api.GetRequestReply(&req, &response)
  114. // json.Unmarshal([]byte(jsonget), &req)
  115. // _ = api.GetRequestReply(&req, &response)
  116. // if response.(string) != expected {
  117. // t.Errorf("Expected %s got %s", expected, response)
  118. // }
  119. // }
  120. // func TestDbHexStr(t *testing.T) {
  121. // jsonput := `{"jsonrpc":"2.0","method":"db_putHex","params":["testDB","beefKey","0xbeef"],"id":64}`
  122. // jsonget := `{"jsonrpc":"2.0","method":"db_getHex","params":["testDB","beefKey"],"id":64}`
  123. // expected := "0xbeef"
  124. // xeth := &xeth.XEth{}
  125. // api := NewEthereumApi(xeth)
  126. // defer api.db.Close()
  127. // var response interface{}
  128. // var req RpcRequest
  129. // json.Unmarshal([]byte(jsonput), &req)
  130. // _ = api.GetRequestReply(&req, &response)
  131. // json.Unmarshal([]byte(jsonget), &req)
  132. // _ = api.GetRequestReply(&req, &response)
  133. // if response.(string) != expected {
  134. // t.Errorf("Expected %s got %s", expected, response)
  135. // }
  136. // }
  137. // func TestFilterClose(t *testing.T) {
  138. // t.Skip()
  139. // api := &EthereumApi{
  140. // logs: make(map[int]*logFilter),
  141. // messages: make(map[int]*whisperFilter),
  142. // quit: make(chan struct{}),
  143. // }
  144. // filterTickerTime = 1
  145. // api.logs[0] = &logFilter{}
  146. // api.messages[0] = &whisperFilter{}
  147. // var wg sync.WaitGroup
  148. // wg.Add(1)
  149. // go api.start()
  150. // go func() {
  151. // select {
  152. // case <-time.After(500 * time.Millisecond):
  153. // api.stop()
  154. // wg.Done()
  155. // }
  156. // }()
  157. // wg.Wait()
  158. // if len(api.logs) != 0 {
  159. // t.Error("expected logs to be empty")
  160. // }
  161. // if len(api.messages) != 0 {
  162. // t.Error("expected messages to be empty")
  163. // }
  164. // }