consensus: uniform getWork endpoints

This commit is contained in:
rjl493456442 2018-09-17 18:31:30 +08:00
parent dc62da5c8c
commit 05d9cd1560
2 changed files with 17 additions and 23 deletions

View file

@ -31,27 +31,32 @@ type API struct {
ethash *Ethash // Make sure the mode of ethash is normal.
}
// getWork returns a work package for external miner.
// GetWork returns a work package for external miner.
//
// User can specify a customized extra to replace the extra field
// in the block header when the given string is not empty.
// in the block header when the given string pointer is not empty.
//
// The work package consists of 3 strings:
// result[0] - 32 bytes hex encoded current block header pow-hash
// result[1] - 32 bytes hex encoded seed hash used for DAG
// result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
func (api *API) getWork(extra string) ([3]string, error) {
func (api *API) GetWork(extra *string) ([3]string, error) {
if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
return [3]string{}, errors.New("not supported")
}
var (
extraData string
workCh = make(chan [3]string, 1)
errc = make(chan error, 1)
)
if extra != nil {
extraData = *extra
}
select {
case api.ethash.fetchWorkCh <- &sealWork{extra: extra, errc: errc, res: workCh}:
case api.ethash.fetchWorkCh <- &sealWork{extra: extraData, errc: errc, res: workCh}:
case <-api.ethash.exitCh:
return [3]string{}, errEthashStopped
}
@ -64,18 +69,6 @@ func (api *API) getWork(extra string) ([3]string, error) {
}
}
// GetWork returns a default work package for external miner.
//
// Note this function is a legacy for backward compatibility.
func (api *API) GetWork() ([3]string, error) {
return api.getWork("")
}
// GetCustomizedWork returns a customized work package for external miner.
func (api *API) GetCustomizedWork(extra string) ([3]string, error) {
return api.getWork(extra)
}
// SubmitWork can be used by external miner to submit their POW solution.
// It returns an indication if the work was accepted.
// Note either an invalid solution, a stale work a non-existent work will return false.

View file

@ -95,7 +95,7 @@ func TestRemoteSealer(t *testing.T) {
defer ethash.Close()
api := &API{ethash}
if _, err := api.GetWork(); err != errNoMiningWork {
if _, err := api.GetWork(nil); err != errNoMiningWork {
t.Error("expect to return an error indicate there is no mining work")
}
header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
@ -110,7 +110,7 @@ func TestRemoteSealer(t *testing.T) {
work [3]string
err error
)
if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
if work, err = api.GetWork(nil); err != nil || work[0] != sealhash.Hex() {
t.Error("expect to return a mining work has same hash")
}
@ -124,7 +124,7 @@ func TestRemoteSealer(t *testing.T) {
sealhash = ethash.SealHash(header)
ethash.Seal(nil, block, results, nil)
if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
if work, err = api.GetWork(nil); err != nil || work[0] != sealhash.Hex() {
t.Error("expect to return the latest pushed work")
}
}
@ -144,7 +144,8 @@ func TestCustomizedWork(t *testing.T) {
// Get customized mining work.
expect := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100), Extra: []byte("extra")}
expectSealhash := ethash.SealHash(expect)
if work, err := api.GetCustomizedWork("extra"); err != nil || work[0] != expectSealhash.Hex() {
extra := "extra"
if work, err := api.GetWork(&extra); err != nil || work[0] != expectSealhash.Hex() {
t.Errorf("expect to return a customized work, have %s, want %s", work[0], expectSealhash.Hex())
}
@ -185,7 +186,7 @@ func TestClosedRemoteSealer(t *testing.T) {
ethash.Close()
api := &API{ethash}
if _, err := api.GetWork(); err != errEthashStopped {
if _, err := api.GetWork(nil); err != errEthashStopped {
t.Error("expect to return an error to indicate ethash is stopped")
}