api_test.go 5.8 KB

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