Skip to content

Commit 8ff6a1b

Browse files
authored
Fix/fee debt assumptions (#113)
* Fix broken assumptions * Add tests * Fix failling test
1 parent 251c3bd commit 8ff6a1b

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

econ/terminate.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func SampleSectors(sectors []uint64, samples int) []uint64 {
157157
if sectorCount <= samples {
158158
return sectors
159159
}
160-
160+
161161
// Compute the step size for evenly spaced sampling
162162
step := float64(sectorCount-1) / float64(samples-1)
163163

@@ -306,10 +306,10 @@ func TerminateSectors(ctx context.Context, api *lotusapi.FullNodeStruct, minerAd
306306

307307
// Assumptions:
308308
// 1. there cannot be negative fee debt
309-
// 2. there cannot be positive fee debt with a positive balance
309+
// 2. there cannot be positive fee debt with a positive balance - BROKEN ASSUMPTION
310310
// 3. there cannot be positive fee debt with a positive available balance
311-
// 4. there cannot be a positive fee debt with a positive initial pledge
312-
// 5. there cannot be a positive fee debt with a positive vesting funds
311+
// 4. there cannot be a positive fee debt with a positive initial pledge - BROKEN ASSUMPTION
312+
// 5. there cannot be a positive fee debt with a positive vesting funds - BROKEN ASSUMPTION
313313
// 6. there cannot be a negative termination fee
314314
// 7. there cannot be a negative initial pledge
315315
// 8. with no fee debt, available balance should always be positive (or 0)
@@ -328,22 +328,10 @@ func failOnBrokenAssumptions(minerAddr address.Address, result *TerminateSectorR
328328

329329
// fee debt exists
330330
if result.FeeDebt.Cmp(big.NewInt(0)) > 0 {
331-
// 2.there cannot be positive fee debt with a positive balance
332-
if result.TotalBalance.Cmp(big.NewInt(0)) > 0 {
333-
return xerrors.Errorf("miner %s: positive fee debt with positive balance: %v", minerAddr, result.FeeDebt)
334-
}
335331
// 3. there cannot be positive fee debt with a positive available balance
336332
if result.AvailableBalance.Cmp(big.NewInt(0)) > 0 {
337333
return xerrors.Errorf("miner %s: positive fee debt with positive available balance: %v", minerAddr, result.FeeDebt)
338334
}
339-
// 4. there cannot be a positive fee debt with a positive initial pledge
340-
if result.InitialPledge.Cmp(big.NewInt(0)) > 0 {
341-
return xerrors.Errorf("miner %s: positive fee debt with positive initial pledge: %v", minerAddr, result.FeeDebt)
342-
}
343-
// 5. there cannot be a positive fee debt with a positive vesting funds
344-
if result.VestingFunds.Cmp(big.NewInt(0)) > 0 {
345-
return xerrors.Errorf("miner %s: positive fee debt with positive vesting funds: %v", minerAddr, result.FeeDebt)
346-
}
347335
} else if result.FeeDebt.Cmp(big.NewInt(0)) == 0 {
348336
// no fee debt
349337
// 8. with no fee debt, available balance should always be positive (or 0)

econ/terminate_test.go

+32-12
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,15 @@ func TestEstimateTerminationPenalty2(t *testing.T) {
331331
}
332332
}
333333

334-
var BLOCK_HEIGHT = big.NewInt(4198539)
334+
var BASE_BLOCK_HEIGHT = big.NewInt(4198539)
335335

336336
type baseFiTest struct {
337337
*BaseFi
338338

339339
maxBorrowAndSeal *big.Int
340340
maxBorrowAndWithdraw *big.Int
341341
liquidationValue *big.Int
342+
height *big.Int
342343
}
343344

344345
var baseFiTests = []struct {
@@ -360,6 +361,7 @@ var baseFiTests = []struct {
360361
maxBorrowAndSeal: bigFromStr("50375782116425480034999"),
361362
maxBorrowAndWithdraw: bigFromStr("12593945529106370008750"),
362363
liquidationValue: bigFromStr("16791927372141826678333"),
364+
height: BASE_BLOCK_HEIGHT,
363365
}},
364366
{"miner with fee debt", "f01836766", baseFiTest{
365367
BaseFi: &BaseFi{
@@ -375,6 +377,7 @@ var baseFiTests = []struct {
375377
maxBorrowAndSeal: big.NewInt(0),
376378
maxBorrowAndWithdraw: big.NewInt(0),
377379
liquidationValue: big.NewInt(0),
380+
height: BASE_BLOCK_HEIGHT,
378381
}},
379382
{"inactive miner (all 0s)", "f01156", baseFiTest{
380383
BaseFi: &BaseFi{
@@ -390,6 +393,7 @@ var baseFiTests = []struct {
390393
maxBorrowAndSeal: big.NewInt(0),
391394
maxBorrowAndWithdraw: big.NewInt(0),
392395
liquidationValue: big.NewInt(0),
396+
height: BASE_BLOCK_HEIGHT,
393397
}},
394398
{"normal miner", "f01344987", baseFiTest{
395399
BaseFi: &BaseFi{
@@ -405,6 +409,23 @@ var baseFiTests = []struct {
405409
maxBorrowAndSeal: bigFromStr("492527766798191726655114"),
406410
maxBorrowAndWithdraw: bigFromStr("123131941699547931663779"),
407411
liquidationValue: bigFromStr("164175922266063908885038"),
412+
height: BASE_BLOCK_HEIGHT,
413+
}},
414+
{"miner with fee debt and positive balance", "f01931245", baseFiTest{
415+
BaseFi: &BaseFi{
416+
Balance: bigFromStr("65041872611791874551"),
417+
AvailableBalance: bigFromStr("0"),
418+
LockedRewards: bigFromStr("137666159110180561"),
419+
InitialPledge: bigFromStr("64904206452681693990"),
420+
FeeDebt: bigFromStr("957933257513729319"),
421+
TerminationFee: bigFromStr("12832724091624933434"),
422+
LiveSectors: big.NewInt(323),
423+
FaultySectors: big.NewInt(323),
424+
},
425+
maxBorrowAndSeal: bigFromStr("156627445560500823351"),
426+
maxBorrowAndWithdraw: bigFromStr("39156861390125205838"),
427+
liquidationValue: bigFromStr("52209148520166941117"),
428+
height: big.NewInt(4299545),
408429
}},
409430
}
410431

@@ -420,14 +441,13 @@ func TestMinerFi(t *testing.T) {
420441
}
421442
defer closer()
422443

423-
ts, err := lapi.ChainGetTipSetByHeight(context.Background(), abi.ChainEpoch(BLOCK_HEIGHT.Int64()), types.EmptyTSK)
424-
if err != nil {
425-
t.Fatal(err)
426-
}
427-
428444
for _, tt := range baseFiTests {
429445
testname := fmt.Sprintf("%s:%s", tt.name, tt.miner)
430446
t.Run(testname, func(t *testing.T) {
447+
ts, err := lapi.ChainGetTipSetByHeight(context.Background(), abi.ChainEpoch(tt.want.height.Int64()), types.EmptyTSK)
448+
if err != nil {
449+
t.Fatal(err)
450+
}
431451
minerAddr, err := address.NewFromString(tt.miner)
432452
if err != nil {
433453
t.Fatal(err)
@@ -490,7 +510,7 @@ var agentTests = []struct {
490510
want agentFiTest
491511
}{
492512
// agent 59
493-
{"agent many miners", "0x64Ea1aD49A78B6A6878c4975566b8953b1Ef1e79", BLOCK_HEIGHT, agentFiTest{
513+
{"agent many miners", "0x64Ea1aD49A78B6A6878c4975566b8953b1Ef1e79", BASE_BLOCK_HEIGHT, agentFiTest{
494514
AgentFi: &AgentFi{
495515
BaseFi: BaseFi{
496516
Balance: bigFromStr("2090733730144435384596917"),
@@ -517,7 +537,7 @@ var agentTests = []struct {
517537
dtl: 0.574289111476492565,
518538
}},
519539
// agent 27
520-
{"agent no miners", "0xDBa96B0FDbc87C7eEb641Ee37EAFC55B355079E4", BLOCK_HEIGHT, agentFiTest{
540+
{"agent no miners", "0xDBa96B0FDbc87C7eEb641Ee37EAFC55B355079E4", BASE_BLOCK_HEIGHT, agentFiTest{
521541
AgentFi: EmptyAgentFi(),
522542
balance: big.NewInt(0),
523543
maxBorrowAndSeal: big.NewInt(0),
@@ -530,7 +550,7 @@ var agentTests = []struct {
530550
dtl: 0,
531551
}},
532552
// agent 2
533-
{"agent single miner", "0xf0F1ceCCF78D411EeD9Ca190ca7F157140cCB2d3", BLOCK_HEIGHT, agentFiTest{
553+
{"agent single miner", "0xf0F1ceCCF78D411EeD9Ca190ca7F157140cCB2d3", BASE_BLOCK_HEIGHT, agentFiTest{
534554
AgentFi: &AgentFi{
535555
BaseFi: BaseFi{
536556
Balance: bigFromStr("133252309755096579751"),
@@ -557,7 +577,7 @@ var agentTests = []struct {
557577
dtl: 0.016608593934123496,
558578
}},
559579
// agent 91
560-
{"agent miner with fee debt", "0xFF65F5f3D309fEA7aA2d4cB2727E918FAb0aE7F7", BLOCK_HEIGHT, agentFiTest{
580+
{"agent miner with fee debt", "0xFF65F5f3D309fEA7aA2d4cB2727E918FAb0aE7F7", BASE_BLOCK_HEIGHT, agentFiTest{
561581
AgentFi: &AgentFi{
562582
BaseFi: BaseFi{
563583
Balance: bigFromStr("0"),
@@ -675,8 +695,8 @@ func TestComputePerc(t *testing.T) {
675695
i := bigFromStr("28202982363374106229")
676696

677697
perc := computePerc(new(big.Int).Add(p, i), lv)
678-
exp := big.NewFloat(0.394356164017979036)
679-
if perc.Cmp(exp) != 0 {
698+
exp, _ := new(big.Float).SetString("0.394356164017979036")
699+
if perc.String() != exp.String() {
680700
t.Fatalf("Expected perc: %v, actual: %v", exp, perc)
681701
}
682702
}

0 commit comments

Comments
 (0)