api_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. xeth := xeth.NewTest(&eth.Ethereum{}, nil)
  63. api := NewEthApi(xeth, codec.JSON)
  64. var rpcRequest shared.Request
  65. json.Unmarshal([]byte(jsonstr), &rpcRequest)
  66. response, err := api.CompileSolidity(&rpcRequest)
  67. if err != nil {
  68. t.Errorf("Execution failed, %v", err)
  69. }
  70. respjson, err := json.Marshal(response)
  71. if err != nil {
  72. t.Errorf("expected no error, got %v", err)
  73. }
  74. var contracts = make(map[string]*compiler.Contract)
  75. err = json.Unmarshal(respjson, &contracts)
  76. if err != nil {
  77. t.Errorf("expected no error, got %v", err)
  78. }
  79. if len(contracts) != 1 {
  80. t.Errorf("expected one contract, got %v", len(contracts))
  81. }
  82. contract := contracts["test"]
  83. if contract.Code != expCode {
  84. t.Errorf("Expected \n%s got \n%s", expCode, contract.Code)
  85. }
  86. if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` {
  87. t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source))
  88. }
  89. if contract.Info.Language != expLanguage {
  90. t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language)
  91. }
  92. if contract.Info.LanguageVersion != expLanguageVersion {
  93. t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion)
  94. }
  95. if contract.Info.CompilerVersion != expCompilerVersion {
  96. t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion)
  97. }
  98. userdoc, err := json.Marshal(contract.Info.UserDoc)
  99. if err != nil {
  100. t.Errorf("expected no error, got %v", err)
  101. }
  102. devdoc, err := json.Marshal(contract.Info.DeveloperDoc)
  103. if err != nil {
  104. t.Errorf("expected no error, got %v", err)
  105. }
  106. abidef, err := json.Marshal(contract.Info.AbiDefinition)
  107. if err != nil {
  108. t.Errorf("expected no error, got %v", err)
  109. }
  110. if string(abidef) != expAbiDefinition {
  111. t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef))
  112. }
  113. if string(userdoc) != expUserDoc {
  114. t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc))
  115. }
  116. if string(devdoc) != expDeveloperDoc {
  117. t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc))
  118. }
  119. }