Skip to content

Commit 5cfb0d4

Browse files
authored
Enable geth compile.solidity for rpc (#7)
1 parent 4e449e2 commit 5cfb0d4

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

common/compiler/solidity.go

+32
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ func SolidityVersion(solc string) (*Solidity, error) {
102102
return s, nil
103103
}
104104

105+
func New(solcPath string) (sol *Solidity, err error) {
106+
// set default solc
107+
if len(solcPath) == 0 {
108+
solcPath = "solc"
109+
}
110+
solcPath, err = exec.LookPath(solcPath)
111+
if err != nil {
112+
return
113+
}
114+
115+
cmd := exec.Command(solcPath, "--version")
116+
var out bytes.Buffer
117+
cmd.Stdout = &out
118+
err = cmd.Run()
119+
if err != nil {
120+
return
121+
}
122+
fullVersion := out.String()
123+
version := versionRegexp.FindString(fullVersion)
124+
125+
sol = &Solidity{
126+
Path: solcPath,
127+
Version: version,
128+
FullVersion: fullVersion}
129+
return
130+
}
131+
105132
// CompileSolidityString builds and returns all the contracts contained within a source string.
106133
func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
107134
if len(source) == 0 {
@@ -192,3 +219,8 @@ func slurpFiles(files []string) (string, error) {
192219
}
193220
return concat.String(), nil
194221
}
222+
223+
// Compile builds and returns all the contracts contained within a source string.
224+
func (sol *Solidity) Compile(source string) (map[string]*Contract, error) {
225+
return CompileSolidityString("solc", source)
226+
}

consensus/ethash/consensus.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, p
296296
// given the parent block's time and difficulty.
297297
func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
298298
next := new(big.Int).Add(parent.Number, big1)
299+
return calcDifficultyByzantium(time, parent)
299300
switch {
300301
case config.IsByzantium(next):
301302
return calcDifficultyByzantium(time, parent)
@@ -327,7 +328,7 @@ func calcDifficultyByzantium(time uint64, parent *types.Header) *big.Int {
327328
// diff = (parent_diff +
328329
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
329330
// ) + 2^(periodCount - 2)
330-
331+
return big2;
331332
bigTime := new(big.Int).SetUint64(time)
332333
bigParentTime := new(big.Int).Set(parent.Time)
333334

eth/api.go

+16
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
"math/big"
2525
"os"
2626
"strings"
27+
"errors"
2728

2829
"github.com/ethereum/go-ethereum/common"
2930
"github.com/ethereum/go-ethereum/common/hexutil"
31+
"github.com/ethereum/go-ethereum/common/compiler"
3032
"github.com/ethereum/go-ethereum/core"
3133
"github.com/ethereum/go-ethereum/core/state"
3234
"github.com/ethereum/go-ethereum/core/types"
@@ -210,6 +212,20 @@ func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI {
210212
return &PrivateAdminAPI{eth: eth}
211213
}
212214

215+
// CompileSolidity compiles the given solidity source
216+
func (s *PublicEthereumAPI) CompileSolidity(source string) (map[string]*compiler.Contract, error) {
217+
solc, err := s.e.Solc()
218+
if err != nil {
219+
return nil, err
220+
}
221+
222+
if solc == nil {
223+
return nil, errors.New("solc (solidity compiler) not found")
224+
}
225+
226+
return solc.Compile(source)
227+
}
228+
213229
// ExportChain exports the current blockchain into a local file.
214230
func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
215231
// Make sure we can create the file to export into

eth/backend.go

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/accounts"
2929
"github.com/ethereum/go-ethereum/common"
3030
"github.com/ethereum/go-ethereum/common/hexutil"
31+
"github.com/ethereum/go-ethereum/common/compiler"
3132
"github.com/ethereum/go-ethereum/consensus"
3233
"github.com/ethereum/go-ethereum/consensus/clique"
3334
"github.com/ethereum/go-ethereum/consensus/ethash"
@@ -79,6 +80,8 @@ type Ethereum struct {
7980
engine consensus.Engine
8081
accountManager *accounts.Manager
8182

83+
solc *compiler.Solidity
84+
8285
bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
8386
bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports
8487

@@ -334,6 +337,14 @@ func (self *Ethereum) SetEtherbase(etherbase common.Address) {
334337
self.miner.SetEtherbase(etherbase)
335338
}
336339

340+
func (self *Ethereum) Solc() (*compiler.Solidity, error) {
341+
var err error
342+
if self.solc == nil {
343+
self.solc, err = compiler.New("")
344+
}
345+
return self.solc, err
346+
}
347+
337348
func (s *Ethereum) StartMining(local bool) error {
338349
eb, err := s.Etherbase()
339350
if err != nil {

0 commit comments

Comments
 (0)