From d41a07161d49fb9229a6c2d2abe2d19664e2f94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 15 Jul 2020 11:12:03 +0200 Subject: [PATCH] paychmgr: handle voucher.ChannelAddr --- node/impl/paych/paych.go | 4 +++- paychmgr/paych.go | 4 ++++ paychmgr/paych_test.go | 33 +++++++++++++++++---------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/node/impl/paych/paych.go b/node/impl/paych/paych.go index b08e07dbd..f0396f501 100644 --- a/node/impl/paych/paych.go +++ b/node/impl/paych/paych.go @@ -63,6 +63,8 @@ func (a *PaychAPI) PaychNewPayment(ctx context.Context, from, to address.Address for i, v := range vouchers { sv, err := a.paychVoucherCreate(ctx, ch.Channel, paych.SignedVoucher{ + ChannelAddr: ch.Channel, + Amount: v.Amount, Lane: uint64(lane), @@ -161,7 +163,7 @@ func (a *PaychAPI) PaychVoucherAdd(ctx context.Context, ch address.Address, sv * // actual additional value of this voucher will only be the difference between // the two. func (a *PaychAPI) PaychVoucherCreate(ctx context.Context, pch address.Address, amt types.BigInt, lane uint64) (*paych.SignedVoucher, error) { - return a.paychVoucherCreate(ctx, pch, paych.SignedVoucher{Amount: amt, Lane: lane}) + return a.paychVoucherCreate(ctx, pch, paych.SignedVoucher{ChannelAddr: pch, Amount: amt, Lane: lane}) } func (a *PaychAPI) paychVoucherCreate(ctx context.Context, pch address.Address, voucher paych.SignedVoucher) (*paych.SignedVoucher, error) { diff --git a/paychmgr/paych.go b/paychmgr/paych.go index 64ce6e3a7..d78afb381 100644 --- a/paychmgr/paych.go +++ b/paychmgr/paych.go @@ -102,6 +102,10 @@ func (pm *Manager) CheckVoucherValid(ctx context.Context, ch address.Address, sv } func (pm *Manager) checkVoucherValid(ctx context.Context, ch address.Address, sv *paych.SignedVoucher) (map[uint64]*paych.LaneState, error) { + if sv.ChannelAddr != ch { + return nil, xerrors.Errorf("voucher ChannelAddr doesn't match channel address, got %s, expected %s", sv.ChannelAddr, ch) + } + act, pchState, err := pm.loadPaychState(ctx, ch) if err != nil { return nil, err diff --git a/paychmgr/paych_test.go b/paychmgr/paych_test.go index 64e344ea7..ac9493fab 100644 --- a/paychmgr/paych_test.go +++ b/paychmgr/paych_test.go @@ -325,7 +325,7 @@ func TestCheckVoucherValid(t *testing.T) { err := mgr.TrackInboundChannel(ctx, ch) require.NoError(t, err) - sv := testCreateVoucher(t, tcase.voucherLane, tcase.voucherNonce, tcase.voucherAmount, tcase.key) + sv := testCreateVoucher(t, ch, tcase.voucherLane, tcase.voucherNonce, tcase.voucherAmount, tcase.key) err = mgr.CheckVoucherValid(ctx, ch, sv) if tcase.expectError { @@ -405,7 +405,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) { voucherLane := uint64(1) voucherNonce := uint64(2) voucherAmount := big.NewInt(6) - sv := testCreateVoucher(t, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) + sv := testCreateVoucher(t, ch, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) err = mgr.CheckVoucherValid(ctx, ch, sv) require.Error(t, err) @@ -423,7 +423,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) { // actor balance is 10 so total is ok. // voucherAmount = big.NewInt(4) - sv = testCreateVoucher(t, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) err = mgr.CheckVoucherValid(ctx, ch, sv) require.NoError(t, err) @@ -447,7 +447,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) { // voucherNonce++ voucherAmount = big.NewInt(6) - sv = testCreateVoucher(t, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) err = mgr.CheckVoucherValid(ctx, ch, sv) require.Error(t, err) @@ -465,7 +465,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) { // actor balance is 10 so total is ok. // voucherAmount = big.NewInt(5) - sv = testCreateVoucher(t, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, voucherNonce, voucherAmount, fromKeyPrivate) err = mgr.CheckVoucherValid(ctx, ch, sv) require.NoError(t, err) } @@ -482,14 +482,14 @@ func TestAddVoucherDelta(t *testing.T) { minDelta := big.NewInt(2) nonce := uint64(1) voucherAmount := big.NewInt(1) - sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv := testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.Error(t, err) // Expect success when adding a voucher whose amount is equal to minDelta nonce++ voucherAmount = big.NewInt(2) - sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) delta, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) require.EqualValues(t, delta.Int64(), 2) @@ -497,7 +497,7 @@ func TestAddVoucherDelta(t *testing.T) { // Check that delta is correct when there's an existing voucher nonce++ voucherAmount = big.NewInt(5) - sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) delta, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) require.EqualValues(t, delta.Int64(), 3) @@ -506,7 +506,7 @@ func TestAddVoucherDelta(t *testing.T) { nonce = uint64(1) voucherAmount = big.NewInt(6) voucherLane = uint64(2) - sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) delta, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) require.EqualValues(t, delta.Int64(), 6) @@ -524,7 +524,7 @@ func TestAddVoucherNextLane(t *testing.T) { // Add a voucher in lane 2 nonce := uint64(1) voucherLane := uint64(2) - sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv := testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) @@ -534,7 +534,7 @@ func TestAddVoucherNextLane(t *testing.T) { // Add a voucher in lane 1 voucherLane = uint64(1) - sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) @@ -544,7 +544,7 @@ func TestAddVoucherNextLane(t *testing.T) { // Add a voucher in lane 5 voucherLane = uint64(5) - sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv = testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) @@ -567,7 +567,7 @@ func TestAddVoucherProof(t *testing.T) { // Add a voucher with no proof var proof []byte - sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) + sv := testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, fromKeyPrivate) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) @@ -639,14 +639,14 @@ func TestNextNonceForLane(t *testing.T) { voucherLane := uint64(1) for _, nonce := range []uint64{2, 4} { voucherAmount = big.Add(voucherAmount, big.NewInt(1)) - sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, key) + sv := testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, key) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) } voucherLane = uint64(2) nonce := uint64(7) - sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, key) + sv := testCreateVoucher(t, ch, voucherLane, nonce, voucherAmount, key) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) require.NoError(t, err) @@ -704,8 +704,9 @@ func testGenerateKeyPair(t *testing.T) ([]byte, []byte) { return priv, pub } -func testCreateVoucher(t *testing.T, voucherLane uint64, nonce uint64, voucherAmount big.Int, key []byte) *paych.SignedVoucher { +func testCreateVoucher(t *testing.T, ch address.Address, voucherLane uint64, nonce uint64, voucherAmount big.Int, key []byte) *paych.SignedVoucher { sv := &paych.SignedVoucher{ + ChannelAddr: ch, Lane: voucherLane, Nonce: nonce, Amount: voucherAmount,