solc.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2016 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 ethapi
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common/compiler"
  20. "github.com/ethereum/go-ethereum/rpc"
  21. )
  22. func makeCompilerAPIs(solcPath string) []rpc.API {
  23. c := &compilerAPI{solc: solcPath}
  24. return []rpc.API{
  25. {
  26. Namespace: "eth",
  27. Version: "1.0",
  28. Service: (*PublicCompilerAPI)(c),
  29. Public: true,
  30. },
  31. {
  32. Namespace: "admin",
  33. Version: "1.0",
  34. Service: (*CompilerAdminAPI)(c),
  35. Public: true,
  36. },
  37. }
  38. }
  39. type compilerAPI struct {
  40. // This lock guards the solc path set through the API.
  41. // It also ensures that only one solc process is used at
  42. // any time.
  43. mu sync.Mutex
  44. solc string
  45. }
  46. type CompilerAdminAPI compilerAPI
  47. // SetSolc sets the Solidity compiler path to be used by the node.
  48. func (api *CompilerAdminAPI) SetSolc(path string) (string, error) {
  49. api.mu.Lock()
  50. defer api.mu.Unlock()
  51. info, err := compiler.SolidityVersion(path)
  52. if err != nil {
  53. return "", err
  54. }
  55. api.solc = path
  56. return info.FullVersion, nil
  57. }
  58. type PublicCompilerAPI compilerAPI
  59. // CompileSolidity compiles the given solidity source.
  60. func (api *PublicCompilerAPI) CompileSolidity(source string) (map[string]*compiler.Contract, error) {
  61. api.mu.Lock()
  62. defer api.mu.Unlock()
  63. return compiler.CompileSolidityString(api.solc, source)
  64. }
  65. func (api *PublicCompilerAPI) GetCompilers() ([]string, error) {
  66. api.mu.Lock()
  67. defer api.mu.Unlock()
  68. if _, err := compiler.SolidityVersion(api.solc); err == nil {
  69. return []string{"Solidity"}, nil
  70. }
  71. return []string{}, nil
  72. }