aboutsummaryrefslogtreecommitdiffstats
path: root/consensus/ethash/ethash_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'consensus/ethash/ethash_test.go')
-rw-r--r--consensus/ethash/ethash_test.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/consensus/ethash/ethash_test.go b/consensus/ethash/ethash_test.go
index 31116da43..ccdd30fb0 100644
--- a/consensus/ethash/ethash_test.go
+++ b/consensus/ethash/ethash_test.go
@@ -23,7 +23,10 @@ import (
"os"
"sync"
"testing"
+ "time"
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)
@@ -32,6 +35,7 @@ func TestTestMode(t *testing.T) {
head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
ethash := NewTester()
+ defer ethash.Close()
block, err := ethash.Seal(nil, types.NewBlockWithHeader(head), nil)
if err != nil {
t.Fatalf("failed to seal block: %v", err)
@@ -52,6 +56,7 @@ func TestCacheFileEvict(t *testing.T) {
}
defer os.RemoveAll(tmpdir)
e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest})
+ defer e.Close()
workers := 8
epochs := 100
@@ -77,3 +82,90 @@ func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) {
e.VerifySeal(nil, head)
}
}
+
+func TestRemoteSealer(t *testing.T) {
+ ethash := NewTester()
+ defer ethash.Close()
+ api := &API{ethash}
+ if _, err := api.GetWork(); err != errNoMiningWork {
+ t.Error("expect to return an error indicate there is no mining work")
+ }
+
+ head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
+ block := types.NewBlockWithHeader(head)
+
+ // Push new work.
+ ethash.Seal(nil, block, nil)
+
+ var (
+ work [3]string
+ err error
+ )
+ if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() {
+ t.Error("expect to return a mining work has same hash")
+ }
+
+ if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res {
+ t.Error("expect to return false when submit a fake solution")
+ }
+
+ // Push new block with same block number to replace the original one.
+ head = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
+ block = types.NewBlockWithHeader(head)
+ ethash.Seal(nil, block, nil)
+
+ if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() {
+ t.Error("expect to return the latest pushed work")
+ }
+
+ // Push block with higher block number.
+ newHead := &types.Header{Number: big.NewInt(2), Difficulty: big.NewInt(100)}
+ newBlock := types.NewBlockWithHeader(newHead)
+ ethash.Seal(nil, newBlock, nil)
+
+ if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res {
+ t.Error("expect to return false when submit a stale solution")
+ }
+}
+
+func TestHashRate(t *testing.T) {
+ var (
+ ethash = NewTester()
+ api = &API{ethash}
+ hashrate = []hexutil.Uint64{100, 200, 300}
+ expect uint64
+ ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
+ )
+
+ defer ethash.Close()
+
+ if tot := ethash.Hashrate(); tot != 0 {
+ t.Error("expect the result should be zero")
+ }
+
+ for i := 0; i < len(hashrate); i += 1 {
+ if res := api.SubmitHashRate(hashrate[i], ids[i]); !res {
+ t.Error("remote miner submit hashrate failed")
+ }
+ expect += uint64(hashrate[i])
+ }
+ if tot := ethash.Hashrate(); tot != float64(expect) {
+ t.Error("expect total hashrate should be same")
+ }
+}
+
+func TestClosedRemoteSealer(t *testing.T) {
+ ethash := NewTester()
+ // Make sure exit channel has been listened
+ time.Sleep(1 * time.Second)
+ ethash.Close()
+
+ api := &API{ethash}
+ if _, err := api.GetWork(); err != errEthashStopped {
+ t.Error("expect to return an error to indicate ethash is stopped")
+ }
+
+ if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res {
+ t.Error("expect to return false when submit hashrate to a stopped ethash")
+ }
+}