Skip to content

Commit

Permalink
Enable geth compile.solidity for rpc (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
TuitionCoin authored May 8, 2018
1 parent 4e449e2 commit 5cfb0d4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
32 changes: 32 additions & 0 deletions common/compiler/solidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ func SolidityVersion(solc string) (*Solidity, error) {
return s, nil
}

func New(solcPath string) (sol *Solidity, err error) {
// set default solc
if len(solcPath) == 0 {
solcPath = "solc"
}
solcPath, err = exec.LookPath(solcPath)
if err != nil {
return
}

cmd := exec.Command(solcPath, "--version")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return
}
fullVersion := out.String()
version := versionRegexp.FindString(fullVersion)

sol = &Solidity{
Path: solcPath,
Version: version,
FullVersion: fullVersion}
return
}

// CompileSolidityString builds and returns all the contracts contained within a source string.
func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
if len(source) == 0 {
Expand Down Expand Up @@ -192,3 +219,8 @@ func slurpFiles(files []string) (string, error) {
}
return concat.String(), nil
}

// Compile builds and returns all the contracts contained within a source string.
func (sol *Solidity) Compile(source string) (map[string]*Contract, error) {
return CompileSolidityString("solc", source)
}
3 changes: 2 additions & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, p
// given the parent block's time and difficulty.
func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
next := new(big.Int).Add(parent.Number, big1)
return calcDifficultyByzantium(time, parent)
switch {
case config.IsByzantium(next):
return calcDifficultyByzantium(time, parent)
Expand Down Expand Up @@ -327,7 +328,7 @@ func calcDifficultyByzantium(time uint64, parent *types.Header) *big.Int {
// diff = (parent_diff +
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
// ) + 2^(periodCount - 2)

return big2;
bigTime := new(big.Int).SetUint64(time)
bigParentTime := new(big.Int).Set(parent.Time)

Expand Down
16 changes: 16 additions & 0 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"math/big"
"os"
"strings"
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -210,6 +212,20 @@ func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI {
return &PrivateAdminAPI{eth: eth}
}

// CompileSolidity compiles the given solidity source
func (s *PublicEthereumAPI) CompileSolidity(source string) (map[string]*compiler.Contract, error) {
solc, err := s.e.Solc()
if err != nil {
return nil, err
}

if solc == nil {
return nil, errors.New("solc (solidity compiler) not found")
}

return solc.Compile(source)
}

// ExportChain exports the current blockchain into a local file.
func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
// Make sure we can create the file to export into
Expand Down
11 changes: 11 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
Expand Down Expand Up @@ -79,6 +80,8 @@ type Ethereum struct {
engine consensus.Engine
accountManager *accounts.Manager

solc *compiler.Solidity

bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports

Expand Down Expand Up @@ -334,6 +337,14 @@ func (self *Ethereum) SetEtherbase(etherbase common.Address) {
self.miner.SetEtherbase(etherbase)
}

func (self *Ethereum) Solc() (*compiler.Solidity, error) {
var err error
if self.solc == nil {
self.solc, err = compiler.New("")
}
return self.solc, err
}

func (s *Ethereum) StartMining(local bool) error {
eb, err := s.Etherbase()
if err != nil {
Expand Down

0 comments on commit 5cfb0d4

Please sign in to comment.