Skip to content

Commit

Permalink
fix: logic and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
metacertain committed Jun 14, 2021
1 parent 12cff49 commit 8006e25
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/accounting/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func (a *Accounting) peerLatentDebt(peer swarm.Address) (*big.Int, error) {
return zero, nil
}

return peerDebt, nil
return peerLatentDebt, nil
}

// shadowBalance returns the current debt reduced by any potentially debitable amount stored in shadowReservedBalance
Expand Down Expand Up @@ -980,7 +980,7 @@ func (d *debitAction) Apply() error {

disconnectFor, err := a.blocklistUntil(d.peer, 1)
if err != nil {
return p2p.NewBlockPeerError(24*time.Hour, ErrDisconnectThresholdExceeded)
return p2p.NewBlockPeerError(1*time.Minute, ErrDisconnectThresholdExceeded)
}
return p2p.NewBlockPeerError(time.Duration(disconnectFor), ErrDisconnectThresholdExceeded)

Expand Down Expand Up @@ -1029,7 +1029,7 @@ func (a *Accounting) blocklist(peer swarm.Address, multiplier int64) error {

disconnectFor, err := a.blocklistUntil(peer, multiplier)
if err != nil {
return a.p2p.Blocklist(peer, 1*time.Hour)
return a.p2p.Blocklist(peer, 1*time.Minute)
}

return a.p2p.Blocklist(peer, time.Duration(disconnectFor)*time.Second)
Expand Down
59 changes: 59 additions & 0 deletions pkg/accounting/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,3 +1211,62 @@ func TestAccountingCallPaymentFailureRetries(t *testing.T) {

acc.Release(peer1Addr, 1)
}

func TestAccountingReconnectBlocklistOverdraft(t *testing.T) {
logger := logging.New(ioutil.Discard, 0)

store := mock.NewStateStore()
defer store.Close()

var blocklistTime int64

paymentThresholdInRefreshmentSeconds := new(big.Int).Div(testPaymentThreshold, big.NewInt(testRefreshRate)).Uint64()

f := func(s swarm.Address, t time.Duration) error {
blocklistTime = int64(t.Seconds())
return nil
}

acc, err := accounting.NewAccounting(testPaymentThreshold, testPaymentTolerance, testPaymentEarly, logger, store, nil, big.NewInt(testRefreshRate), p2pmock.New(p2pmock.WithBlocklistFunc(f)))
if err != nil {
t.Fatal(err)
}

ts := int64(1000)
acc.SetTime(ts)

peer, err := swarm.ParseHexAddress("00112233")
if err != nil {
t.Fatal(err)
}

requestPrice := testPaymentThreshold.Uint64()

debitActionNormal := acc.PrepareDebit(peer, requestPrice)
err = debitActionNormal.Apply()
if err != nil {
t.Fatal(err)
}
debitActionNormal.Cleanup()

// debit ghost balance
debitActionGhost := acc.PrepareDebit(peer, requestPrice)
debitActionGhost.Cleanup()

// increase shadow reserve
debitActionShadow := acc.PrepareDebit(peer, requestPrice)
_ = debitActionShadow

if blocklistTime != 0 {
t.Fatal("unexpected blocklist")
}

// ghost overdraft triggering blocklist
debitAction4 := acc.PrepareDebit(peer, requestPrice)
debitAction4.Cleanup()

if blocklistTime != int64(5*paymentThresholdInRefreshmentSeconds) {
t.Fatalf("unexpected blocklisting time, got %v expected %v", blocklistTime, 5*paymentThresholdInRefreshmentSeconds)
}

}

0 comments on commit 8006e25

Please sign in to comment.