paychmgr: handle voucher.ChannelAddr

This commit is contained in:
Łukasz Magiera 2020-07-15 11:12:03 +02:00
parent fc13794f5f
commit d41a07161d
3 changed files with 24 additions and 17 deletions

View File

@ -63,6 +63,8 @@ func (a *PaychAPI) PaychNewPayment(ctx context.Context, from, to address.Address
for i, v := range vouchers { for i, v := range vouchers {
sv, err := a.paychVoucherCreate(ctx, ch.Channel, paych.SignedVoucher{ sv, err := a.paychVoucherCreate(ctx, ch.Channel, paych.SignedVoucher{
ChannelAddr: ch.Channel,
Amount: v.Amount, Amount: v.Amount,
Lane: uint64(lane), 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 // actual additional value of this voucher will only be the difference between
// the two. // the two.
func (a *PaychAPI) PaychVoucherCreate(ctx context.Context, pch address.Address, amt types.BigInt, lane uint64) (*paych.SignedVoucher, error) { 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) { func (a *PaychAPI) paychVoucherCreate(ctx context.Context, pch address.Address, voucher paych.SignedVoucher) (*paych.SignedVoucher, error) {

View File

@ -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) { 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) act, pchState, err := pm.loadPaychState(ctx, ch)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -325,7 +325,7 @@ func TestCheckVoucherValid(t *testing.T) {
err := mgr.TrackInboundChannel(ctx, ch) err := mgr.TrackInboundChannel(ctx, ch)
require.NoError(t, err) 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) err = mgr.CheckVoucherValid(ctx, ch, sv)
if tcase.expectError { if tcase.expectError {
@ -405,7 +405,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) {
voucherLane := uint64(1) voucherLane := uint64(1)
voucherNonce := uint64(2) voucherNonce := uint64(2)
voucherAmount := big.NewInt(6) 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) err = mgr.CheckVoucherValid(ctx, ch, sv)
require.Error(t, err) require.Error(t, err)
@ -423,7 +423,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) {
// actor balance is 10 so total is ok. // actor balance is 10 so total is ok.
// //
voucherAmount = big.NewInt(4) 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) err = mgr.CheckVoucherValid(ctx, ch, sv)
require.NoError(t, err) require.NoError(t, err)
@ -447,7 +447,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) {
// //
voucherNonce++ voucherNonce++
voucherAmount = big.NewInt(6) 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) err = mgr.CheckVoucherValid(ctx, ch, sv)
require.Error(t, err) require.Error(t, err)
@ -465,7 +465,7 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) {
// actor balance is 10 so total is ok. // actor balance is 10 so total is ok.
// //
voucherAmount = big.NewInt(5) 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) err = mgr.CheckVoucherValid(ctx, ch, sv)
require.NoError(t, err) require.NoError(t, err)
} }
@ -482,14 +482,14 @@ func TestAddVoucherDelta(t *testing.T) {
minDelta := big.NewInt(2) minDelta := big.NewInt(2)
nonce := uint64(1) nonce := uint64(1)
voucherAmount := big.NewInt(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) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.Error(t, err) require.Error(t, err)
// Expect success when adding a voucher whose amount is equal to minDelta // Expect success when adding a voucher whose amount is equal to minDelta
nonce++ nonce++
voucherAmount = big.NewInt(2) 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) delta, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, delta.Int64(), 2) 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 // Check that delta is correct when there's an existing voucher
nonce++ nonce++
voucherAmount = big.NewInt(5) 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) delta, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, delta.Int64(), 3) require.EqualValues(t, delta.Int64(), 3)
@ -506,7 +506,7 @@ func TestAddVoucherDelta(t *testing.T) {
nonce = uint64(1) nonce = uint64(1)
voucherAmount = big.NewInt(6) voucherAmount = big.NewInt(6)
voucherLane = uint64(2) 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) delta, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, delta.Int64(), 6) require.EqualValues(t, delta.Int64(), 6)
@ -524,7 +524,7 @@ func TestAddVoucherNextLane(t *testing.T) {
// Add a voucher in lane 2 // Add a voucher in lane 2
nonce := uint64(1) nonce := uint64(1)
voucherLane := uint64(2) 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) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
@ -534,7 +534,7 @@ func TestAddVoucherNextLane(t *testing.T) {
// Add a voucher in lane 1 // Add a voucher in lane 1
voucherLane = uint64(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) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
@ -544,7 +544,7 @@ func TestAddVoucherNextLane(t *testing.T) {
// Add a voucher in lane 5 // Add a voucher in lane 5
voucherLane = uint64(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) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
@ -567,7 +567,7 @@ func TestAddVoucherProof(t *testing.T) {
// Add a voucher with no proof // Add a voucher with no proof
var proof []byte 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) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
@ -639,14 +639,14 @@ func TestNextNonceForLane(t *testing.T) {
voucherLane := uint64(1) voucherLane := uint64(1)
for _, nonce := range []uint64{2, 4} { for _, nonce := range []uint64{2, 4} {
voucherAmount = big.Add(voucherAmount, big.NewInt(1)) 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) _, err := mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
} }
voucherLane = uint64(2) voucherLane = uint64(2)
nonce := uint64(7) 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) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
@ -704,8 +704,9 @@ func testGenerateKeyPair(t *testing.T) ([]byte, []byte) {
return priv, pub 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{ sv := &paych.SignedVoucher{
ChannelAddr: ch,
Lane: voucherLane, Lane: voucherLane,
Nonce: nonce, Nonce: nonce,
Amount: voucherAmount, Amount: voucherAmount,