api_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 api
  17. import (
  18. "testing"
  19. "encoding/json"
  20. "strconv"
  21. "github.com/ethereum/go-ethereum/common/compiler"
  22. "github.com/ethereum/go-ethereum/eth"
  23. "github.com/ethereum/go-ethereum/rpc/codec"
  24. "github.com/ethereum/go-ethereum/rpc/shared"
  25. "github.com/ethereum/go-ethereum/xeth"
  26. )
  27. func TestParseApiString(t *testing.T) {
  28. apis, err := ParseApiString("", codec.JSON, nil, nil)
  29. if err == nil {
  30. t.Errorf("Expected an err from parsing empty API string but got nil")
  31. }
  32. if len(apis) != 0 {
  33. t.Errorf("Expected 0 apis from empty API string")
  34. }
  35. apis, err = ParseApiString("eth", codec.JSON, nil, nil)
  36. if err != nil {
  37. t.Errorf("Expected nil err from parsing empty API string but got %v", err)
  38. }
  39. if len(apis) != 1 {
  40. t.Errorf("Expected 1 apis but got %d - %v", apis, apis)
  41. }
  42. apis, err = ParseApiString("eth,eth", codec.JSON, nil, nil)
  43. if err != nil {
  44. t.Errorf("Expected nil err from parsing empty API string but got \"%v\"", err)
  45. }
  46. if len(apis) != 2 {
  47. t.Errorf("Expected 2 apis but got %d - %v", apis, apis)
  48. }
  49. apis, err = ParseApiString("eth,invalid", codec.JSON, nil, nil)
  50. if err == nil {
  51. t.Errorf("Expected an err but got no err")
  52. }
  53. }
  54. const solcVersion = "0.9.23"
  55. func TestCompileSolidity(t *testing.T) {
  56. solc, err := compiler.New("")
  57. if solc == nil {
  58. t.Skip("no solc found: skip")
  59. } else if solc.Version() != solcVersion {
  60. t.Skip("WARNING: skipping test because of solc different version (%v, test written for %v, may need to update)", solc.Version(), solcVersion)
  61. }
  62. source := `contract test {\n` +
  63. " /// @notice Will multiply `a` by 7." + `\n` +
  64. ` function multiply(uint a) returns(uint d) {\n` +
  65. ` return a * 7;\n` +
  66. ` }\n` +
  67. `}\n`
  68. jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}`
  69. expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"
  70. expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]`
  71. expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}`
  72. expDeveloperDoc := `{"methods":{}}`
  73. expCompilerVersion := solc.Version()
  74. expLanguage := "Solidity"
  75. expLanguageVersion := "0"
  76. expSource := source
  77. eth := &eth.Ethereum{}
  78. xeth := xeth.NewTest(nil, nil)
  79. api := NewEthApi(xeth, eth, codec.JSON)
  80. var rpcRequest shared.Request
  81. json.Unmarshal([]byte(jsonstr), &rpcRequest)
  82. response, err := api.CompileSolidity(&rpcRequest)
  83. if err != nil {
  84. t.Errorf("Execution failed, %v", err)
  85. }
  86. respjson, err := json.Marshal(response)
  87. if err != nil {
  88. t.Errorf("expected no error, got %v", err)
  89. }
  90. var contracts = make(map[string]*compiler.Contract)
  91. err = json.Unmarshal(respjson, &contracts)
  92. if err != nil {
  93. t.Errorf("expected no error, got %v", err)
  94. }
  95. if len(contracts) != 1 {
  96. t.Errorf("expected one contract, got %v", len(contracts))
  97. }
  98. contract := contracts["test"]
  99. if contract.Code != expCode {
  100. t.Errorf("Expected \n%s got \n%s", expCode, contract.Code)
  101. }
  102. if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` {
  103. t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source))
  104. }
  105. if contract.Info.Language != expLanguage {
  106. t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language)
  107. }
  108. if contract.Info.LanguageVersion != expLanguageVersion {
  109. t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion)
  110. }
  111. if contract.Info.CompilerVersion != expCompilerVersion {
  112. t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion)
  113. }
  114. userdoc, err := json.Marshal(contract.Info.UserDoc)
  115. if err != nil {
  116. t.Errorf("expected no error, got %v", err)
  117. }
  118. devdoc, err := json.Marshal(contract.Info.DeveloperDoc)
  119. if err != nil {
  120. t.Errorf("expected no error, got %v", err)
  121. }
  122. abidef, err := json.Marshal(contract.Info.AbiDefinition)
  123. if err != nil {
  124. t.Errorf("expected no error, got %v", err)
  125. }
  126. if string(abidef) != expAbiDefinition {
  127. t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef))
  128. }
  129. if string(userdoc) != expUserDoc {
  130. t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc))
  131. }
  132. if string(devdoc) != expDeveloperDoc {
  133. t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc))
  134. }
  135. }