From 5cfb0d498e7e84f67071e2a7ff509bae85430f3c Mon Sep 17 00:00:00 2001 From: FinTechToken Date: Mon, 7 May 2018 22:19:16 -0400 Subject: [PATCH] Enable geth compile.solidity for rpc (#7) --- common/compiler/solidity.go | 32 ++++++++++++++++++++++++++++++++ consensus/ethash/consensus.go | 3 ++- eth/api.go | 16 ++++++++++++++++ eth/backend.go | 11 +++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/common/compiler/solidity.go b/common/compiler/solidity.go index 234714a2b9c3..4d04b5108c30 100644 --- a/common/compiler/solidity.go +++ b/common/compiler/solidity.go @@ -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 { @@ -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) +} \ No newline at end of file diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 62c869d2ab7b..6a4d1c30b1f7 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -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) @@ -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) diff --git a/eth/api.go b/eth/api.go index a345b57e498f..adfb7f9f0b70 100644 --- a/eth/api.go +++ b/eth/api.go @@ -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" @@ -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 diff --git a/eth/backend.go b/eth/backend.go index 94aad23101b8..826aeda9e436 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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" @@ -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 @@ -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 {