Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pool: Use mock client names in tests. #399

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pool/acceptedwork_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 The Decred developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -13,10 +13,15 @@ import (

func testAcceptedWork(t *testing.T) {
// Created some valid accepted work.
const (
xClient = "cpux"
x2Client = "cpux2"
yClient = "cpuy"
)
workA := NewAcceptedWork(
"00000000000000001e2065a7248a9b4d3886fe3ca3128eebedddaf35fb26e58c",
"000000000000000007301a21efa98033e06f7eba836990394fff9f765f1556b1",
396692, yID, "dr3")
396692, yID, yClient)
err := db.persistAcceptedWork(workA)
if err != nil {
t.Fatal(err)
Expand All @@ -25,7 +30,7 @@ func testAcceptedWork(t *testing.T) {
workB := NewAcceptedWork(
"000000000000000025aa4a7ba8c3ece4608376bf84a82ec7e025991460097198",
"00000000000000001e2065a7248a9b4d3886fe3ca3128eebedddaf35fb26e58c",
396693, xID, "dr5")
396693, xID, xClient)
err = db.persistAcceptedWork(workB)
if err != nil {
t.Fatal(err)
Expand All @@ -34,7 +39,7 @@ func testAcceptedWork(t *testing.T) {
workC := NewAcceptedWork(
"0000000000000000053236ce6c274aa49a1cc6e9d906e855725c79f69c1089d3",
"000000000000000025aa4a7ba8c3ece4608376bf84a82ec7e025991460097198",
396694, xID, "dr5")
396694, xID, xClient)
err = db.persistAcceptedWork(workC)
if err != nil {
t.Fatal(err)
Expand All @@ -43,7 +48,7 @@ func testAcceptedWork(t *testing.T) {
workD := NewAcceptedWork(
"000000000000000020f9ab2b1e144a818d36a857aefda55363f5e86e01855c79",
"0000000000000000053236ce6c274aa49a1cc6e9d906e855725c79f69c1089d3",
396695, xID, "dr5")
396695, xID, xClient)
err = db.persistAcceptedWork(workD)
if err != nil {
t.Fatal(err)
Expand All @@ -52,7 +57,7 @@ func testAcceptedWork(t *testing.T) {
workE := NewAcceptedWork(
"0000000000000000032e25218be722327ae3dccf9015756facb2f98931fda7b8",
"00000000000000000476712b2f5df31bc62b9976066262af2d639a551853c056",
431611, xID, "dcr1")
431611, xID, x2Client)

// Ensure updating a non persisted accepted work returns an error.
err = db.updateAcceptedWork(workE)
Expand Down
12 changes: 8 additions & 4 deletions pool/chainstate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 The Decred developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -86,10 +86,14 @@ func testChainState(t *testing.T) {
cs := NewChainState(cCfg)

// Test pruneAcceptedWork.
const (
xClient = "cpux"
yClient = "cpuy"
)
workA := NewAcceptedWork(
"00000000000000001e2065a7248a9b4d3886fe3ca3128eebedddaf35fb26e58c",
"000000000000000007301a21efa98033e06f7eba836990394fff9f765f1556b1",
396692, yID, "dr3")
396692, yID, yClient)
workA.Confirmed = true
err = db.persistAcceptedWork(workA)
if err != nil {
Expand All @@ -99,7 +103,7 @@ func testChainState(t *testing.T) {
workB := NewAcceptedWork(
"000000000000000025aa4a7ba8c3ece4608376bf84a82ec7e025991460097198",
"00000000000000001e2065a7248a9b4d3886fe3ca3128eebedddaf35fb26e58c",
396693, xID, "dr5")
396693, xID, xClient)
err = db.persistAcceptedWork(workB)
if err != nil {
t.Fatal(err)
Expand All @@ -108,7 +112,7 @@ func testChainState(t *testing.T) {
workC := NewAcceptedWork(
zeroHash.String(),
"00000000000000001e2065a7248a9b4d3886fe3ca3128eebedddaf35fb26e58c",
396694, xID, "dr5")
396694, xID, xClient)
err = db.persistAcceptedWork(workC)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ func (c *Client) handleSubscribeRequest(req *Request, allowed bool) error {
resp = SubscribeResponse(*req.ID, nid, c.extraNonce1,
ExtraNonce2Size, nil)

case AntminerDR3, AntminerDR5:
// The DR5 and DR3 are not fully complaint with the stratum spec.
// They use an 8-byte extraNonce2 regardless of the
case AntminerDR3:
// The DR3 is not fully complaint with the stratum spec.
// It uses an 8-byte extraNonce2 regardless of the
// extraNonce2Size provided.
//
// The extraNonce1 is appended to the extraNonce2 in the
Expand Down
55 changes: 46 additions & 9 deletions pool/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 The Decred developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -1543,36 +1543,73 @@ func testClientTimeRolledWork(t *testing.T) {
}

func testClientUpgrades(t *testing.T) {
// Create mock upgrade path for CPU mining.
const clientCPU2 = CPU + "2"
activeNet := config.ActiveNet
mockPoolDiffs := func() map[string]*DifficultyInfo {
const maxGenTime = time.Millisecond * 500
genTime := new(big.Int).SetInt64(int64(maxGenTime.Seconds()))
return map[string]*DifficultyInfo{
CPU: func() *DifficultyInfo {
hashRate := minerHashes[CPU]
target, diff := calculatePoolTarget(activeNet, hashRate, genTime)
return &DifficultyInfo{
target: target,
difficulty: diff,
powLimit: new(big.Rat).SetInt(activeNet.PowLimit),
}
}(),
clientCPU2: func() *DifficultyInfo {
hashRate := new(big.Int).Mul(minerHashes[CPU], big.NewInt(2))
target, diff := calculatePoolTarget(activeNet, hashRate, genTime)
return &DifficultyInfo{
target: target,
difficulty: diff,
powLimit: new(big.Rat).SetInt(activeNet.PowLimit),
}
}(),
}
}()
fetchMinerDifficulty := func(miner string) (*DifficultyInfo, error) {
diffData, ok := mockPoolDiffs[miner]
if !ok {
desc := fmt.Sprintf("no difficulty data found for miner %s", miner)
return nil, errs.PoolError(errs.ValueNotFound, desc)
}
return diffData, nil
}

ctx := context.Background()
cfg := *config
cfg.RollWorkCycle = time.Minute * 5 // Avoiding rolled work for this test.
cfg.MaxUpgradeTries = 2
cfg.MonitorCycle = time.Millisecond * 100
cfg.ClientTimeout = time.Millisecond * 300
cfg.FetchMinerDifficulty = fetchMinerDifficulty
_, ln, client, _, _, err := setup(ctx, &cfg)
if err != nil {
ln.Close()
t.Fatalf("[setup] unexpected error: %v", err)
}

err = setMiner(client, AntminerDR3)
err = setMiner(client, CPU)
if err != nil {
ln.Close()
t.Fatalf("unexpected set miner error: %v", err)
}

minerIdx := 0
idPair := minerIDs[dr3ID]
const minerIdx = 0
idPair := newMinerIDPair(cpuID, CPU, clientCPU2)

// Trigger a client upgrade.
atomic.StoreInt64(&client.submissions, 50)

go client.monitor(minerIdx, idPair, cfg.MonitorCycle, cfg.MaxUpgradeTries)
time.Sleep(cfg.MonitorCycle + (cfg.MonitorCycle / 2))

if fetchMiner(client) != AntminerDR5 {
if fetchMiner(client) != clientCPU2 {
ln.Close()
t.Fatalf("expected a miner id of %s, got %s", AntminerDR5, client.miner)
t.Fatalf("expected a miner id of %s, got %s", clientCPU2, client.miner)
}

client.cancel()
Expand All @@ -1588,7 +1625,7 @@ func testClientUpgrades(t *testing.T) {

defer ln.Close()

err = setMiner(client, AntminerDR3)
err = setMiner(client, CPU)
if err != nil {
t.Fatalf("unexpected set miner error: %v", err)
}
Expand All @@ -1598,8 +1635,8 @@ func testClientUpgrades(t *testing.T) {
go client.monitor(minerIdx, idPair, cfg.MonitorCycle, cfg.MaxUpgradeTries)
time.Sleep(cfg.MonitorCycle + (cfg.MonitorCycle / 2))

if fetchMiner(client) == AntminerDR3 {
t.Fatalf("expected a miner of %s, got %s", AntminerDR3, client.miner)
if fetchMiner(client) == CPU {
t.Fatalf("expected a miner of %s, got %s", CPU, client.miner)
}

// Trigger a client timeout by waiting.
Expand Down
Loading