-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use env vars in tests * Handle progressCh as `nil` * Update README * Add a basic test (unfinished) * Add assert approx rel and abs to test helpers * Add tests for assert approx eq rel * Improve test * Add a daily test runner for the terminate package
- Loading branch information
1 parent
2c14d4f
commit cc66b24
Showing
7 changed files
with
243 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Go Daily Termination Tests | ||
|
||
on: | ||
schedule: | ||
- cron: '0 0 * * *' | ||
|
||
jobs: | ||
test: | ||
name: Run Go Tests | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.19.1 | ||
|
||
- name: Test | ||
run: go test -v ./terminate | ||
env: | ||
LOTUS_DIAL_ADDR: ${{ secrets.LOTUS_DIAL_ADDR }} | ||
LOTUS_TOKEN: ${{ secrets.LOTUS_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package terminate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/filecoin-project/lotus/chain/types" | ||
"github.com/glifio/go-pools/util" | ||
) | ||
|
||
// this test compares N random miner termination penalties computed in the most precise (time intensive) way against the quick, less precise sampling method used in the ADO | ||
var N = 10 | ||
|
||
// the wad based percentage that is acceptible for imprecision | ||
var DIFF = big.NewInt(3e16) | ||
|
||
func TestTerminationPrecision(t *testing.T) { | ||
lapi, closer := util.SetupSuite(t) | ||
defer util.TeardownSuite(closer) | ||
|
||
// get ChainHead and lookback 5 epochs | ||
head, err := lapi.ChainHead(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// TODO: this doesn't align with `@head` passed to the PreviewTerminateSectors call | ||
lookback := head.Height() - 1 | ||
ts, err := lapi.ChainGetTipSetByHeight(context.Background(), lookback, types.EmptyTSK) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
miners, err := lapi.StateListMiners(context.Background(), ts.Key()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// // make a slice the length of N | ||
// chosenMiners := make([]address.Address, N) | ||
|
||
// for i := 0; i < N; i++ { | ||
// // choose a random miner | ||
// randomIndex := rand.Intn(len(miners)) | ||
// chosenMiners[i] = miners[randomIndex] | ||
// } | ||
// eventually run this test on all 10 miners in parallel | ||
idx := 0 | ||
miner := miners[0] | ||
hasBalance := false | ||
// check if the miner has a balance | ||
for !hasBalance { | ||
sectorCount, err := lapi.StateMinerSectorCount(context.Background(), miner, ts.Key()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// if the miner has no active sectors, move to the next miner | ||
fmt.Println(miner, sectorCount.Active) | ||
if sectorCount.Active == 0 { | ||
idx++ | ||
miner = miners[idx] | ||
} else { | ||
hasBalance = true | ||
} | ||
} | ||
|
||
imprecise, err := PreviewTerminateSectorsQuick(context.Background(), lapi, miner, ts) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
errorCh := make(chan error) | ||
resultCh := make(chan *PreviewTerminateSectorsReturn) | ||
|
||
go PreviewTerminateSectors(context.Background(), lapi, miner, "@head", 0, 0, 0, false, false, false, 0, errorCh, nil, resultCh) | ||
|
||
loop: | ||
for { | ||
select { | ||
case result := <-resultCh: | ||
fmt.Println("PRECISE: ", result.SectorStats.TerminationPenalty) | ||
fmt.Println("IMPRECISE: ", imprecise.SectorStats.TerminationPenalty) | ||
if !util.AssertApproxEqRel(result.SectorStats.TerminationPenalty, imprecise.SectorStats.TerminationPenalty, DIFF) { | ||
t.Fatalf("TERMINATION PENALTIES DO NOT MATCH: precise: %v, imprecise: %v", result.SectorStats.TerminationPenalty, imprecise.SectorStats.TerminationPenalty) | ||
} | ||
break loop | ||
|
||
case err := <-errorCh: | ||
log.Fatal(err) | ||
} | ||
|
||
} | ||
|
||
// TODO: test the 10 random miners instead of just 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package util | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func TestApproxEqualRel(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
a, b, rangeWAD *big.Int | ||
want bool | ||
}{ | ||
{ | ||
name: "Equal values", | ||
a: big.NewInt(1e18), // 1 WAD | ||
b: big.NewInt(1e18), // 1 WAD | ||
rangeWAD: big.NewInt(1e17), // 10% | ||
want: true, | ||
}, | ||
{ | ||
name: "Within range", | ||
a: big.NewInt(1e18), // 1 WAD | ||
b: big.NewInt(1e18 + 5e16), // 1.05 WAD | ||
rangeWAD: big.NewInt(1e17), // 10% | ||
want: true, | ||
}, | ||
{ | ||
name: "Outside range", | ||
a: big.NewInt(1e18), // 1 WAD | ||
b: big.NewInt(1e18 + 2e17), // 1.2 WAD | ||
rangeWAD: big.NewInt(1e17), // 10% | ||
want: false, | ||
}, | ||
{ | ||
name: "A is zero", | ||
a: big.NewInt(0), // 0 WAD | ||
b: big.NewInt(1e18), // 1 WAD | ||
rangeWAD: big.NewInt(1e17), // 10% | ||
want: false, // Cannot compare if 'a' is zero | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := AssertApproxEqAbs(tc.a, tc.b, tc.rangeWAD) | ||
if got != tc.want { | ||
t.Errorf("approximatelyEqual(%v, %v, %v) = %v; want %v", tc.a, tc.b, tc.rangeWAD, got, tc.want) | ||
} | ||
}) | ||
} | ||
} |