Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/rpc/api/miner.go

174 lines
4.6 KiB

// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
10 years ago
package api
import (
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
)
const (
10 years ago
MinerApiVersion = "1.0"
10 years ago
)
var (
10 years ago
// mapping between methods and handlers
10 years ago
MinerMapping = map[string]minerhandler{
10 years ago
"miner_hashrate": (*minerApi).Hashrate,
"miner_makeDAG": (*minerApi).MakeDAG,
"miner_setExtra": (*minerApi).SetExtra,
"miner_setGasPrice": (*minerApi).SetGasPrice,
"miner_setEtherbase": (*minerApi).SetEtherbase,
10 years ago
"miner_startAutoDAG": (*minerApi).StartAutoDAG,
"miner_start": (*minerApi).StartMiner,
"miner_stopAutoDAG": (*minerApi).StopAutoDAG,
"miner_stop": (*minerApi).StopMiner,
10 years ago
}
)
// miner callback handler
10 years ago
type minerhandler func(*minerApi, *shared.Request) (interface{}, error)
10 years ago
// miner api provider
10 years ago
type minerApi struct {
10 years ago
ethereum *eth.Ethereum
methods map[string]minerhandler
codec codec.ApiCoder
}
// create a new miner api instance
10 years ago
func NewMinerApi(ethereum *eth.Ethereum, coder codec.Codec) *minerApi {
return &minerApi{
10 years ago
ethereum: ethereum,
methods: MinerMapping,
codec: coder.New(nil),
}
}
// Execute given request
10 years ago
func (self *minerApi) Execute(req *shared.Request) (interface{}, error) {
10 years ago
if callback, ok := self.methods[req.Method]; ok {
return callback(self, req)
}
return nil, &shared.NotImplementedError{req.Method}
}
// collection with supported methods
10 years ago
func (self *minerApi) Methods() []string {
10 years ago
methods := make([]string, len(self.methods))
i := 0
for k := range self.methods {
methods[i] = k
i++
}
return methods
}
10 years ago
func (self *minerApi) Name() string {
return shared.MinerApiName
10 years ago
}
10 years ago
func (self *minerApi) ApiVersion() string {
return MinerApiVersion
}
10 years ago
func (self *minerApi) StartMiner(req *shared.Request) (interface{}, error) {
10 years ago
args := new(StartMinerArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, err
}
if args.Threads == -1 { // (not specified by user, use default)
args.Threads = self.ethereum.MinerThreads
}
self.ethereum.StartAutoDAG()
err := self.ethereum.StartMining(args.Threads)
if err == nil {
return true, nil
}
return false, err
}
10 years ago
func (self *minerApi) StopMiner(req *shared.Request) (interface{}, error) {
10 years ago
self.ethereum.StopMining()
return true, nil
}
10 years ago
func (self *minerApi) Hashrate(req *shared.Request) (interface{}, error) {
10 years ago
return self.ethereum.Miner().HashRate(), nil
}
10 years ago
func (self *minerApi) SetExtra(req *shared.Request) (interface{}, error) {
10 years ago
args := new(SetExtraArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, err
}
self.ethereum.Miner().SetExtra([]byte(args.Data))
return true, nil
}
10 years ago
func (self *minerApi) SetGasPrice(req *shared.Request) (interface{}, error) {
10 years ago
args := new(GasPriceArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return false, err
}
self.ethereum.Miner().SetGasPrice(common.String2Big(args.Price))
return true, nil
}
func (self *minerApi) SetEtherbase(req *shared.Request) (interface{}, error) {
args := new(SetEtherbaseArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return false, err
}
self.ethereum.SetEtherbase(args.Etherbase)
return nil, nil
}
10 years ago
func (self *minerApi) StartAutoDAG(req *shared.Request) (interface{}, error) {
10 years ago
self.ethereum.StartAutoDAG()
return true, nil
}
10 years ago
func (self *minerApi) StopAutoDAG(req *shared.Request) (interface{}, error) {
10 years ago
self.ethereum.StopAutoDAG()
return true, nil
}
10 years ago
func (self *minerApi) MakeDAG(req *shared.Request) (interface{}, error) {
10 years ago
args := new(MakeDAGArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, err
}
if args.BlockNumber < 0 {
return false, shared.NewValidationError("BlockNumber", "BlockNumber must be positive")
}
err := ethash.MakeDAG(uint64(args.BlockNumber), "")
if err == nil {
return true, nil
}
return false, err
10 years ago
}