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

test for FundManager panic to ensure it is fixed #4825

Merged
merged 1 commit into from
Nov 12, 2020
Merged
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
62 changes: 62 additions & 0 deletions chain/market/fundmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,53 @@ func TestFundManagerRestart(t *testing.T) {
checkAddMessageFields(t, msg3, s.walletAddr, acctAddr2, amt3)
}

// TestFundManagerReleaseAfterPublish verifies that release is successful in
// the following scenario:
// 1. Deal A adds 5 to addr1: reserved 0 -> 5 available 0 -> 5
// 2. Deal B adds 7 to addr1: reserved 5 -> 12 available 5 -> 12
// 3. Deal B completes, reducing addr1 by 7: reserved 12 available 12 -> 5
// 4. Deal A releases 5 from addr1: reserved 12 -> 7 available 5
func TestFundManagerReleaseAfterPublish(t *testing.T) {
s := setup(t)
defer s.fm.Stop()

// Deal A: Reserve 5
// balance: 0 -> 5
// reserved: 0 -> 5
amt := abi.NewTokenAmount(5)
sentinel, err := s.fm.Reserve(s.ctx, s.walletAddr, s.acctAddr, amt)
require.NoError(t, err)
s.mockApi.completeMsg(sentinel)

// Deal B: Reserve 7
// balance: 5 -> 12
// reserved: 5 -> 12
amt = abi.NewTokenAmount(7)
sentinel, err = s.fm.Reserve(s.ctx, s.walletAddr, s.acctAddr, amt)
require.NoError(t, err)
s.mockApi.completeMsg(sentinel)

// Deal B: Publish (removes Deal B amount from balance)
// balance: 12 -> 5
// reserved: 12
amt = abi.NewTokenAmount(7)
s.mockApi.publish(s.acctAddr, amt)

// Deal A: Release 5
// balance: 5
// reserved: 12 -> 7
amt = abi.NewTokenAmount(5)
err = s.fm.Release(s.acctAddr, amt)
require.NoError(t, err)

// Deal B: Release 7
// balance: 5
// reserved: 12 -> 7
amt = abi.NewTokenAmount(5)
err = s.fm.Release(s.acctAddr, amt)
require.NoError(t, err)
}

type scaffold struct {
ctx context.Context
ds *ds_sync.MutexDatastore
Expand Down Expand Up @@ -731,6 +778,21 @@ func (mapi *mockFundManagerAPI) getEscrow(a address.Address) abi.TokenAmount {
return escrow
}

func (mapi *mockFundManagerAPI) publish(addr address.Address, amt abi.TokenAmount) {
mapi.lk.Lock()
defer mapi.lk.Unlock()

escrow := mapi.escrow[addr]
if escrow.Nil() {
return
}
escrow = types.BigSub(escrow, amt)
if escrow.LessThan(abi.NewTokenAmount(0)) {
escrow = abi.NewTokenAmount(0)
}
mapi.escrow[addr] = escrow
}

func (mapi *mockFundManagerAPI) StateWaitMsg(ctx context.Context, c cid.Cid, confidence uint64) (*api.MsgLookup, error) {
res := &api.MsgLookup{
Message: c,
Expand Down