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