fix: paych check voucher should get inbound channel from state

This commit is contained in:
Dirk McCormick 2020-08-19 09:16:54 -04:00
parent b25215e31b
commit d317835535
2 changed files with 23 additions and 26 deletions

View File

@ -178,9 +178,12 @@ func (pm *Manager) GetChannelInfo(addr address.Address) (*ChannelInfo, error) {
return ca.getChannelInfo(addr) return ca.getChannelInfo(addr)
} }
// CheckVoucherValid checks if the given voucher is valid (is or could become spendable at some point) // CheckVoucherValid checks if the given voucher is valid (is or could become spendable at some point).
// If the channel is not in the store, fetches the channel from state (and checks that
// the channel To address is owned by the wallet).
func (pm *Manager) CheckVoucherValid(ctx context.Context, ch address.Address, sv *paych.SignedVoucher) error { func (pm *Manager) CheckVoucherValid(ctx context.Context, ch address.Address, sv *paych.SignedVoucher) error {
ca, err := pm.accessorByAddress(ch) // Get an accessor for the channel, creating it from state if necessary
ca, err := pm.inboundChannelAccessor(ctx, ch)
if err != nil { if err != nil {
return err return err
} }
@ -213,21 +216,29 @@ func (pm *Manager) AddVoucherOutbound(ctx context.Context, ch address.Address, s
// If the channel is not in the store, fetches the channel from state (and checks that // If the channel is not in the store, fetches the channel from state (and checks that
// the channel To address is owned by the wallet). // the channel To address is owned by the wallet).
func (pm *Manager) AddVoucherInbound(ctx context.Context, ch address.Address, sv *paych.SignedVoucher, proof []byte, minDelta types.BigInt) (types.BigInt, error) { func (pm *Manager) AddVoucherInbound(ctx context.Context, ch address.Address, sv *paych.SignedVoucher, proof []byte, minDelta types.BigInt) (types.BigInt, error) {
// Get an accessor for the channel, creating it from state if necessary
ca, err := pm.inboundChannelAccessor(ctx, ch)
if err != nil {
return types.BigInt{}, err
}
return ca.addVoucher(ctx, ch, sv, proof, minDelta)
}
// inboundChannelAccessor gets an accessor for the given channel. The channel
// must either exist in the store, or be an inbound channel that can be created
// from state.
func (pm *Manager) inboundChannelAccessor(ctx context.Context, ch address.Address) (*channelAccessor, error) {
// Make sure channel is in store, or can be fetched from state, and that // Make sure channel is in store, or can be fetched from state, and that
// the channel To address is owned by the wallet // the channel To address is owned by the wallet
ci, err := pm.trackInboundChannel(ctx, ch) ci, err := pm.trackInboundChannel(ctx, ch)
if err != nil { if err != nil {
return types.BigInt{}, err return nil, err
} }
// This is an inbound channel, so To is the Control address (this node) // This is an inbound channel, so To is the Control address (this node)
from := ci.Target from := ci.Target
to := ci.Control to := ci.Control
ca, err := pm.accessorByFromTo(from, to) return pm.accessorByFromTo(from, to)
if err != nil {
return types.BigInt{}, err
}
return ca.addVoucher(ctx, ch, sv, proof, minDelta)
} }
func (pm *Manager) trackInboundChannel(ctx context.Context, ch address.Address) (*ChannelInfo, error) { func (pm *Manager) trackInboundChannel(ctx context.Context, ch address.Address) (*ChannelInfo, error) {

View File

@ -222,15 +222,8 @@ func TestCheckVoucherValid(t *testing.T) {
mgr, err := newManager(store, mock) mgr, err := newManager(store, mock)
require.NoError(t, err) require.NoError(t, err)
// Create the channel in the manager's store // Add channel To address to wallet
ci := &ChannelInfo{ mock.addWalletAddress(to)
Channel: &ch,
Control: toAcct,
Target: fromAcct,
Direction: DirInbound,
}
err = mgr.store.putChannelInfo(ci)
require.NoError(t, err)
// Create a signed voucher // Create a signed voucher
sv := createTestVoucher(t, ch, tcase.voucherLane, tcase.voucherNonce, tcase.voucherAmount, tcase.key) sv := createTestVoucher(t, ch, tcase.voucherLane, tcase.voucherNonce, tcase.voucherAmount, tcase.key)
@ -298,15 +291,8 @@ func TestCheckVoucherValidCountingAllLanes(t *testing.T) {
mgr, err := newManager(store, mock) mgr, err := newManager(store, mock)
require.NoError(t, err) require.NoError(t, err)
// Create the channel in the manager's store // Add channel To address to wallet
ci := &ChannelInfo{ mock.addWalletAddress(to)
Channel: &ch,
Control: toAcct,
Target: fromAcct,
Direction: DirInbound,
}
err = mgr.store.putChannelInfo(ci)
require.NoError(t, err)
// //
// Should not be possible to add a voucher with a value such that // Should not be possible to add a voucher with a value such that