Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

payment updates #55

Merged
merged 22 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions sdk/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
replace github.com/stellar/go => github.com/leighmcculloch/stellar--go v0.0.0-20210528222607-c2e3ef441a5d

require (
github.com/pkg/errors v0.9.1 // indirect
acharb marked this conversation as resolved.
Show resolved Hide resolved
github.com/stellar/go v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.5.1
)
3 changes: 2 additions & 1 deletion sdk/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJ
github.com/pelletier/go-toml v1.9.0/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down
15 changes: 10 additions & 5 deletions sdk/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ type Channel struct {
observationPeriodLedgerGap int64

startingSequence int64
// TODO: balances []Amount
iterationNumber int64
// TODO - leave execution out for now
// iterationNumberExecuted int64

// The balance owing from the initiator to the responder, if positive, or
// the balance owing from the responder to the initiator, if negative.
// TODO - use Balance struct
amount Amount

initiator bool
localEscrowAccount *EscrowAccount
Expand All @@ -46,6 +53,7 @@ type Config struct {
NetworkPassphrase string
ObservationPeriodTime time.Duration
ObservationPeriodLedgerGap int64
StartingSequence int64

Initiator bool

Expand All @@ -61,6 +69,7 @@ func NewChannel(c Config) *Channel {
networkPassphrase: c.NetworkPassphrase,
observationPeriodTime: c.ObservationPeriodTime,
observationPeriodLedgerGap: c.ObservationPeriodLedgerGap,
startingSequence: c.StartingSequence,
acharb marked this conversation as resolved.
Show resolved Hide resolved
initiator: c.Initiator,
localEscrowAccount: c.LocalEscrowAccount,
remoteEscrowAccount: c.RemoteEscrowAccount,
Expand Down Expand Up @@ -150,10 +159,6 @@ func (c *Channel) verifySigned(tx *txnbuild.Transaction, sigs []xdr.DecoratedSig
}
}

func (c *Channel) Payment(sendAmount int) error {
return nil
}

func (c *Channel) CloseStart(iterationNumber int) error {
return nil
}
Expand Down
124 changes: 124 additions & 0 deletions sdk/state/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package state

import (
"errors"
"fmt"

"github.com/stellar/experimental-payment-channels/sdk/txbuild"
"github.com/stellar/go/xdr"
)

type Payment struct {
IterationNumber int64
Amount int64
acharb marked this conversation as resolved.
Show resolved Hide resolved
CloseSignatures []xdr.DecoratedSignature
DeclarationSignatures []xdr.DecoratedSignature
FromInitiator bool
}

func (c *Channel) NewPayment(amount int64) (*Payment, error) {
if amount <= 0 {
return nil, errors.New("payment amount must be greater than 0")
}
newBalance := int64(0)
if c.initiator {
newBalance = c.amount.Amount + amount
} else {
newBalance = c.amount.Amount - amount
}
txC, err := txbuild.Close(txbuild.CloseParams{
ObservationPeriodTime: c.observationPeriodTime,
ObservationPeriodLedgerGap: c.observationPeriodLedgerGap,
InitiatorSigner: c.initiatorSigner(),
ResponderSigner: c.responderSigner(),
InitiatorEscrow: c.initiatorEscrowAccount().Address,
ResponderEscrow: c.responderEscrowAccount().Address,
StartSequence: c.startingSequence,
IterationNumber: c.iterationNumber,
AmountToInitiator: maxInt64(0, newBalance*-1),
AmountToResponder: maxInt64(0, newBalance),
})
if err != nil {
return nil, err
}
txC, err = txC.Sign(c.networkPassphrase, c.localSigner)
if err != nil {
return nil, err
}
return &Payment{
Amount: amount,
CloseSignatures: txC.Signatures(),
FromInitiator: c.initiator,
}, nil
}

func (c *Channel) ConfirmPayment(p *Payment) (*Payment, error) {
acharb marked this conversation as resolved.
Show resolved Hide resolved
var newCloseSignatures, newDeclarationSignatures []xdr.DecoratedSignature
var amountFromInitiator, amountFromResponder int64
if p.FromInitiator {
amountFromInitiator = p.Amount
} else {
amountFromResponder = p.Amount
}
newBalance := c.amount.Amount + amountFromInitiator - amountFromResponder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should validate the iteration number in the payment is what we expect too.

Where does the iteration number get incremented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now it's living on the Payment, so iterated by the person sending the payment. I created a ticket to maybe do differently here
#61

// validate txC, should always be signed correctly
txC, err := txbuild.Close(txbuild.CloseParams{
ObservationPeriodTime: c.observationPeriodTime,
ObservationPeriodLedgerGap: c.observationPeriodLedgerGap,
InitiatorSigner: c.initiatorSigner(),
ResponderSigner: c.responderSigner(),
InitiatorEscrow: c.initiatorEscrowAccount().Address,
ResponderEscrow: c.responderEscrowAccount().Address,
StartSequence: c.startingSequence,
IterationNumber: c.iterationNumber,
AmountToInitiator: maxInt64(0, newBalance*-1),
AmountToResponder: maxInt64(0, newBalance),
})
if err != nil {
return nil, err
}
if err := c.verifySigned(txC, p.CloseSignatures, c.remoteSigner); err != nil {
return nil, fmt.Errorf("incorrect closing transaction, the one given may have different data: %w", err)
}
// validate txD, may or may not be signed depending where in the payment step we are
signedTxD := false
txD, err := txbuild.Declaration(txbuild.DeclarationParams{
InitiatorEscrow: c.initiatorEscrowAccount().Address,
StartSequence: c.startingSequence,
IterationNumber: c.iterationNumber,
IterationNumberExecuted: 0,
})
if err != nil {
return nil, err
}
if err := c.verifySigned(txD, p.DeclarationSignatures, c.remoteSigner); err == nil {
signedTxD = true
}
// sign C_i if given a signed C_i with no D_i
if !signedTxD {
txC, err = txC.Sign(c.networkPassphrase, c.localSigner)
if err != nil {
return nil, err
}
newCloseSignatures = txC.Signatures()
}
// sign D_i always if above passes
txD, err = txD.Sign(c.networkPassphrase, c.localSigner)
if err != nil {
return nil, err
}
newDeclarationSignatures = txD.Signatures()

p.CloseSignatures = append(p.CloseSignatures, newCloseSignatures...)
p.DeclarationSignatures = append(p.DeclarationSignatures, newDeclarationSignatures...)
c.amount.Amount = newBalance
return p, nil
}

func maxInt64(x int64, y int64) int64 {
if x > y {
return x
}
return y
}
Loading