Skip to content

Commit

Permalink
pool: Use mock upgrade path in tests.
Browse files Browse the repository at this point in the history
This modifies the tests for client upgrades to used mock test data
instead of relying on the existence of specific models of ASICs which no
longer exist.
  • Loading branch information
davecgh committed Sep 28, 2023
1 parent 3d37801 commit 1895be0
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions pool/client_test.go
Original file line number Diff line number Diff line change
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

0 comments on commit 1895be0

Please sign in to comment.