api_test.go 4.4 KB

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