From d80e7eda932a2a28ec6c5cd6fdb0acdc1c22c966 Mon Sep 17 00:00:00 2001 From: Ingar Shu Date: Fri, 9 Oct 2020 13:18:43 -0700 Subject: [PATCH] Revert cached "available" amount if the AddFunds message send fails --- chain/market/fundmgr.go | 6 ++++-- chain/market/fundmgr_test.go | 38 +++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/chain/market/fundmgr.go b/chain/market/fundmgr.go index aef3b98ebf7..8b5f85a125d 100644 --- a/chain/market/fundmgr.go +++ b/chain/market/fundmgr.go @@ -120,9 +120,10 @@ func (fm *FundMgr) EnsureAvailable(ctx context.Context, addr, wallet address.Add return cid.Undef, err } fm.lk.Lock() + defer fm.lk.Unlock() + bal, err := fm.api.StateMarketBalance(ctx, addr, types.EmptyTSK) if err != nil { - fm.lk.Unlock() return cid.Undef, err } @@ -138,7 +139,6 @@ func (fm *FundMgr) EnsureAvailable(ctx context.Context, addr, wallet address.Add toAdd = types.NewInt(0) } fm.available[idAddr] = big.Add(avail, toAdd) - fm.lk.Unlock() log.Infof("Funds operation w/ Expected Balance: %s, In State: %s, Requested: %s, Adding: %s", avail.String(), stateAvail.String(), amt.String(), toAdd.String()) @@ -148,6 +148,7 @@ func (fm *FundMgr) EnsureAvailable(ctx context.Context, addr, wallet address.Add params, err := actors.SerializeParams(&addr) if err != nil { + fm.available[idAddr] = avail return cid.Undef, err } @@ -159,6 +160,7 @@ func (fm *FundMgr) EnsureAvailable(ctx context.Context, addr, wallet address.Add Params: params, }, nil) if err != nil { + fm.available[idAddr] = avail return cid.Undef, err } diff --git a/chain/market/fundmgr_test.go b/chain/market/fundmgr_test.go index b05db55d87a..f5936f73d5e 100644 --- a/chain/market/fundmgr_test.go +++ b/chain/market/fundmgr_test.go @@ -57,9 +57,10 @@ func addFundsMsg(toAdd abi.TokenAmount, addr address.Address, wallet address.Add } type expectedResult struct { - addAmt abi.TokenAmount - shouldAdd bool - err error + addAmt abi.TokenAmount + shouldAdd bool + err error + cachedAvailable abi.TokenAmount } func TestAddFunds(t *testing.T) { @@ -88,8 +89,9 @@ func TestAddFunds(t *testing.T) { addAmounts: []abi.TokenAmount{abi.NewTokenAmount(100)}, expectedResults: []expectedResult{ { - shouldAdd: false, - err: nil, + shouldAdd: false, + err: nil, + cachedAvailable: abi.NewTokenAmount(100), }, }, }, @@ -102,18 +104,21 @@ func TestAddFunds(t *testing.T) { err: nil, }, { - addAmt: abi.NewTokenAmount(100), - shouldAdd: true, - err: nil, + addAmt: abi.NewTokenAmount(100), + shouldAdd: true, + err: nil, + cachedAvailable: abi.NewTokenAmount(200), }, { - addAmt: abi.NewTokenAmount(50), - shouldAdd: true, - err: nil, + addAmt: abi.NewTokenAmount(50), + shouldAdd: true, + err: nil, + cachedAvailable: abi.NewTokenAmount(250), }, { - shouldAdd: false, - err: nil, + shouldAdd: false, + err: nil, + cachedAvailable: abi.NewTokenAmount(250), }, }, }, @@ -132,7 +137,8 @@ func TestAddFunds(t *testing.T) { addAmounts: []abi.TokenAmount{abi.NewTokenAmount(100)}, expectedResults: []expectedResult{ { - err: errors.New("something went wrong"), + err: errors.New("something went wrong"), + cachedAvailable: abi.NewTokenAmount(0), }, }, }, @@ -183,6 +189,10 @@ func TestAddFunds(t *testing.T) { } else { require.EqualError(t, err, expected.err.Error()) } + + if !expected.cachedAvailable.Nil() { + require.Equal(t, expected.cachedAvailable, fundMgr.available[addr]) + } } }) }