github.com/ur-technology/go-ur@v1.5.5/internal/ethapi/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 ethapi
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/ethereum/go-ethereum/common/compiler"
    23  	"github.com/ethereum/go-ethereum/rpc"
    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: "admin",
    37  			Version:   "1.0",
    38  			Service:   (*CompilerAdminAPI)(c),
    39  			Public:    true,
    40  		},
    41  	}
    42  }
    43  
    44  type compilerAPI struct {
    45  	// This lock guards the solc path set through the API.
    46  	// It also ensures that only one solc process is used at
    47  	// any time.
    48  	mu   sync.Mutex
    49  	solc string
    50  }
    51  
    52  type CompilerAdminAPI compilerAPI
    53  
    54  // SetSolc sets the Solidity compiler path to be used by the node.
    55  func (api *CompilerAdminAPI) SetSolc(path string) (string, error) {
    56  	api.mu.Lock()
    57  	defer api.mu.Unlock()
    58  	info, err := compiler.SolidityVersion(path)
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  	api.solc = path
    63  	return info.FullVersion, nil
    64  }
    65  
    66  type PublicCompilerAPI compilerAPI
    67  
    68  // CompileSolidity compiles the given solidity source.
    69  func (api *PublicCompilerAPI) CompileSolidity(source string) (map[string]*compiler.Contract, error) {
    70  	api.mu.Lock()
    71  	defer api.mu.Unlock()
    72  	return compiler.CompileSolidityString(api.solc, source)
    73  }
    74  
    75  func (api *PublicCompilerAPI) GetCompilers() ([]string, error) {
    76  	api.mu.Lock()
    77  	defer api.mu.Unlock()
    78  	if _, err := compiler.SolidityVersion(api.solc); err == nil {
    79  		return []string{"Solidity"}, nil
    80  	}
    81  	return []string{}, nil
    82  }