mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
web3 upgrade + integration of miner api in console
This commit is contained in:
parent
b036853917
commit
13390cb78b
7 changed files with 8449 additions and 25 deletions
|
|
@ -107,9 +107,14 @@ func (js *jsre) apiBindings(ipcpath string) {
|
||||||
utils.Fatalf("Error loading bignumber.js: %v", err)
|
utils.Fatalf("Error loading bignumber.js: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = js.re.Compile("ethereum.js", re.Ethereum_JS)
|
// err = js.re.Compile("ethereum.js", re.Ethereum_JS)
|
||||||
|
// if err != nil {
|
||||||
|
// utils.Fatalf("Error loading ethereum.js: %v", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
err = js.re.Compile("ethereum.js", re.Web3_JS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Error loading ethereum.js: %v", err)
|
utils.Fatalf("Error loading web3.js: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = js.re.Eval("var web3 = require('web3');")
|
_, err = js.re.Eval("var web3 = require('web3');")
|
||||||
|
|
@ -117,6 +122,11 @@ func (js *jsre) apiBindings(ipcpath string) {
|
||||||
utils.Fatalf("Error requiring web3: %v", err)
|
utils.Fatalf("Error requiring web3: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = js.re.Compile("miner.js", re.Miner_JS)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Error loading miner.js: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err = js.re.Eval("web3.setProvider(jeth)")
|
_, err = js.re.Eval("web3.setProvider(jeth)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Error setting web3 provider: %v", err)
|
utils.Fatalf("Error setting web3 provider: %v", err)
|
||||||
|
|
|
||||||
|
|
@ -211,7 +211,7 @@ var (
|
||||||
IPCApiFlag = cli.StringFlag{
|
IPCApiFlag = cli.StringFlag{
|
||||||
Name: "ipcapi",
|
Name: "ipcapi",
|
||||||
Usage: "Specify the API's which are offered over this interface",
|
Usage: "Specify the API's which are offered over this interface",
|
||||||
Value: api.DEFAULT_IPC_APIS,
|
Value: api.DefaultIpcApis,
|
||||||
}
|
}
|
||||||
IPCPathFlag = cli.StringFlag{
|
IPCPathFlag = cli.StringFlag{
|
||||||
Name: "ipcpath",
|
Name: "ipcpath",
|
||||||
|
|
@ -393,7 +393,7 @@ func StartIPC(eth *eth.Ethereum, ctx *cli.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
glog.V(logger.Error).Infof("IPC api's: %s", ctx.GlobalString(IPCApiFlag.Name))
|
glog.V(logger.Debug).Infof("IPC api's: %s", ctx.GlobalString(IPCApiFlag.Name))
|
||||||
|
|
||||||
cfg := comms.IpcConfig{
|
cfg := comms.IpcConfig{
|
||||||
Endpoint: ctx.GlobalString(IPCPathFlag.Name),
|
Endpoint: ctx.GlobalString(IPCPathFlag.Name),
|
||||||
|
|
|
||||||
74
jsre/miner_js.go
Normal file
74
jsre/miner_js.go
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
package jsre
|
||||||
|
|
||||||
|
const Miner_JS = `
|
||||||
|
web3.extend({
|
||||||
|
property: 'miner',
|
||||||
|
methods:
|
||||||
|
[
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'start',
|
||||||
|
call: 'miner_start',
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [web3.extend.formatters.formatInputInt],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'stop',
|
||||||
|
call: 'miner_stop',
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [web3.extend.formatters.formatInputInt],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'getHashrate',
|
||||||
|
call: 'miner_hashrate',
|
||||||
|
params: 0,
|
||||||
|
inputFormatter: [],
|
||||||
|
outputFormatter: web3.extend.utils.toDecimal
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'setExtra',
|
||||||
|
call: 'miner_setExtra',
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [web3.extend.utils.formatInputString],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'setGasPrice',
|
||||||
|
call: 'miner_setGasPrice',
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [web3.extend.utils.formatInputString],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'startAutoDAG',
|
||||||
|
call: 'miner_startAutoDAG',
|
||||||
|
params: 0,
|
||||||
|
inputFormatter: [],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'stopAutoDAG',
|
||||||
|
call: 'miner_stopAutoDAG',
|
||||||
|
params: 0,
|
||||||
|
inputFormatter: [],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
}),
|
||||||
|
new web3.extend.Method({
|
||||||
|
name: 'makeDAG',
|
||||||
|
call: 'miner_makeDAG',
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [web3.extend.formatters.inputDefaultBlockNumberFormatter],
|
||||||
|
outputFormatter: web3.extend.formatters.formatOutputBool
|
||||||
|
})
|
||||||
|
],
|
||||||
|
properties:
|
||||||
|
[
|
||||||
|
new web3.extend.Property({
|
||||||
|
name: 'hashrate',
|
||||||
|
getter: 'miner_hashrate',
|
||||||
|
outputFormatter: web3.extend.utils.toDecimal
|
||||||
|
})
|
||||||
|
]
|
||||||
|
});
|
||||||
|
`
|
||||||
8290
jsre/web3_js.go
Normal file
8290
jsre/web3_js.go
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -11,8 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DEFAULT_IPC_APIS = "admin,debug,eth,miner,net,web3"
|
DefaultIpcApis = "admin,debug,eth,miner,net,web3"
|
||||||
DEFAULT_HTTP_APIS = "web3,eth"
|
DefaultHttpApiS = "web3,eth"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Descriptor for all API implementations
|
// Descriptor for all API implementations
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ func (self *miner) StartMiner(req *shared.Request) (interface{}, error) {
|
||||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if args.Threads == -1 { // (-1 is set in unmarshalljson when not specified in req)
|
if args.Threads == -1 { // (not specified by user, use default)
|
||||||
args.Threads = self.ethereum.MinerThreads
|
args.Threads = self.ethereum.MinerThreads
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,21 +111,21 @@ func (self *miner) SetExtra(req *shared.Request) (interface{}, error) {
|
||||||
func (self *miner) SetGasPrice(req *shared.Request) (interface{}, error) {
|
func (self *miner) SetGasPrice(req *shared.Request) (interface{}, error) {
|
||||||
args := new(GasPriceArgs)
|
args := new(GasPriceArgs)
|
||||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||||
return nil, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
self.ethereum.Miner().SetGasPrice(common.String2Big(args.Price))
|
self.ethereum.Miner().SetGasPrice(common.String2Big(args.Price))
|
||||||
return nil, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *miner) StartAutoDAG(req *shared.Request) (interface{}, error) {
|
func (self *miner) StartAutoDAG(req *shared.Request) (interface{}, error) {
|
||||||
self.ethereum.StartAutoDAG()
|
self.ethereum.StartAutoDAG()
|
||||||
return nil, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *miner) StopAutoDAG(req *shared.Request) (interface{}, error) {
|
func (self *miner) StopAutoDAG(req *shared.Request) (interface{}, error) {
|
||||||
self.ethereum.StopAutoDAG()
|
self.ethereum.StopAutoDAG()
|
||||||
return nil, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *miner) MakeDAG(req *shared.Request) (interface{}, error) {
|
func (self *miner) MakeDAG(req *shared.Request) (interface{}, error) {
|
||||||
|
|
@ -134,7 +134,11 @@ func (self *miner) MakeDAG(req *shared.Request) (interface{}, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err := ethash.MakeDAG(args.BlockNumber, "")
|
if args.BlockNumber < 0 {
|
||||||
|
return false, shared.NewValidationError("BlockNumber", "BlockNumber must be positive")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := ethash.MakeDAG(uint64(args.BlockNumber), "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,14 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/rpc/shared"
|
"github.com/ethereum/go-ethereum/rpc/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StartMinerArgs struct {
|
type StartMinerArgs struct {
|
||||||
Threads int `json:"threads"`
|
Threads int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (args *StartMinerArgs) UnmarshalJSON(b []byte) (err error) {
|
func (args *StartMinerArgs) UnmarshalJSON(b []byte) (err error) {
|
||||||
|
|
@ -17,31 +18,76 @@ func (args *StartMinerArgs) UnmarshalJSON(b []byte) (err error) {
|
||||||
return shared.NewDecodeParamError(err.Error())
|
return shared.NewDecodeParamError(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(obj) == 0 {
|
if len(obj) == 0 || obj[0] == nil {
|
||||||
args.Threads = -1
|
args.Threads = -1
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var arg0 *big.Int
|
var num *big.Int
|
||||||
if arg0, err = numString(obj[0]); err != nil {
|
if num, err = numString(obj[0]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
args.Threads = int(num.Int64())
|
||||||
if arg0.Int64() >= 0 && arg0.Int64() <= 256 {
|
return nil
|
||||||
args.Threads = int(arg0.Int64())
|
|
||||||
}
|
|
||||||
|
|
||||||
return shared.NewValidationError("threads", "must be in range [0...256]")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetExtraArgs struct {
|
type SetExtraArgs struct {
|
||||||
Data string `json:"data"`
|
Data string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (args *SetExtraArgs) UnmarshalJSON(b []byte) (err error) {
|
||||||
|
var obj []interface{}
|
||||||
|
if err := json.Unmarshal(b, &obj); err != nil {
|
||||||
|
return shared.NewDecodeParamError(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
extrastr, ok := obj[0].(string)
|
||||||
|
if !ok {
|
||||||
|
return shared.NewInvalidTypeError("Price", "not a string")
|
||||||
|
}
|
||||||
|
args.Data = extrastr
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type GasPriceArgs struct {
|
type GasPriceArgs struct {
|
||||||
Price string `json:"price"`
|
Price string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (args *GasPriceArgs) UnmarshalJSON(b []byte) (err error) {
|
||||||
|
var obj []interface{}
|
||||||
|
if err := json.Unmarshal(b, &obj); err != nil {
|
||||||
|
return shared.NewDecodeParamError(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
pricestr, ok := obj[0].(string)
|
||||||
|
if !ok {
|
||||||
|
return shared.NewInvalidTypeError("Price", "not a string")
|
||||||
|
}
|
||||||
|
args.Price = pricestr
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type MakeDAGArgs struct {
|
type MakeDAGArgs struct {
|
||||||
BlockNumber uint64 `json:"blockNumber"`
|
BlockNumber int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (args *MakeDAGArgs) UnmarshalJSON(b []byte) (err error) {
|
||||||
|
args.BlockNumber = -1
|
||||||
|
var obj []interface{}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, &obj); err != nil {
|
||||||
|
return shared.NewDecodeParamError(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(obj) < 1 {
|
||||||
|
return shared.NewInsufficientParamsError(len(obj), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue