Skip to content

Commit

Permalink
add asset transter at SettleWorkflow in lnwallet
Browse files Browse the repository at this point in the history
  • Loading branch information
wxf4150 committed Apr 20, 2022
1 parent 1e511be commit 7e84e9e
Show file tree
Hide file tree
Showing 36 changed files with 1,631 additions and 679 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ profile.tmp

# Coverage test
coverage.txt
*.bak
6 changes: 5 additions & 1 deletion chanbackup/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ type Single struct {

// Capacity is the size of the original channel.
Capacity btcutil.Amount
/*
obd update
todo add BtcCapacity AssetCapacity
*/

// LocalChanCfg is our local channel configuration. It contains all the
// information we need to re-derive the keys we used within the
Expand Down Expand Up @@ -187,7 +191,7 @@ func NewSingle(channel *channeldb.OpenChannel,
ShortChannelID: chanID,
RemoteNodePub: channel.IdentityPub,
Addresses: nodeAddrs,
Capacity: channel.Capacity,
Capacity: channel.BtcCapacity,
LocalChanCfg: channel.LocalChanCfg,
RemoteChanCfg: channel.RemoteChanCfg,
ShaChainRootDesc: shaChainRootDesc,
Expand Down
133 changes: 97 additions & 36 deletions channeldb/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/lightningnetwork/lnd/lnwallet/omnicore"
"io"
"net"
"strconv"
Expand Down Expand Up @@ -438,14 +439,21 @@ type ChannelCommitment struct {
//
// NOTE: This is the balance *after* subtracting any commitment fee,
// AND anchor output values.
LocalBalance lnwire.MilliSatoshi
LocalBtcBalance lnwire.MilliSatoshi

// RemoteBalance is the current available settled balance within the
// channel directly spendable by the remote node.
//
// NOTE: This is the balance *after* subtracting any commitment fee,
// AND anchor output values.
RemoteBalance lnwire.MilliSatoshi
RemoteBtcBalance lnwire.MilliSatoshi

/*
obd add wxf
*/
LocalAssetBalance omnicore.Amount
RemoteAssetBalance omnicore.Amount


// CommitFee is the amount calculated to be paid in fees for the
// current set of commitment transactions. The fee amount is persisted
Expand Down Expand Up @@ -646,16 +654,27 @@ type OpenChannel struct {
// channel has been established with.
IdentityPub *btcec.PublicKey

// Capacity is the total capacity of this channel.
Capacity btcutil.Amount
/*
obd update wxf
*/
//// Capacity is the total capacity of this channel.
//Capacity btcutil.Amount
BtcCapacity btcutil.Amount
AssetCapacity omnicore.Amount
AssetID uint32

// TotalMSatSent is the total number of milli-satoshis we've sent
// within this channel.
TotalMSatSent lnwire.MilliSatoshi

//
// TotalMSatReceived is the total number of milli-satoshis we've
// received within this channel.
TotalMSatReceived lnwire.MilliSatoshi
/*
obd add wxf
*/
TotalAssetSent omnicore.Amount
TotalAssetReceived omnicore.Amount

// LocalChanCfg is the channel configuration for the local node.
LocalChanCfg ChannelConfig
Expand Down Expand Up @@ -1481,6 +1500,13 @@ func (c *OpenChannel) SyncPending(addr net.Addr, pendingHeight uint32) error {
c.Lock()
defer c.Unlock()

/*
obd update wxf
*/
if c.AssetCapacity>0 && c.AssetID<0{
return fmt.Errorf("OpenChannel SyncPending err :miss AssetID")
}

c.FundingBroadcastHeight = pendingHeight

return kvdb.Update(c.Db.backend, func(tx kvdb.RwTx) error {
Expand Down Expand Up @@ -1649,36 +1675,39 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment,
// NOTE: these are our balances *after* subtracting the commitment fee and
// anchor outputs.
func (c *OpenChannel) BalancesAtHeight(height uint64) (lnwire.MilliSatoshi,
lnwire.MilliSatoshi, error) {
lnwire.MilliSatoshi,omnicore.Amount,
omnicore.Amount, error) {

if height > c.LocalCommitment.CommitHeight &&
height > c.RemoteCommitment.CommitHeight {

return 0, 0, errHeightNotReached
return 0, 0, 0, 0, errHeightNotReached
}

// If our current commit is as the desired height, we can return our
// current balances.
if c.LocalCommitment.CommitHeight == height {
return c.LocalCommitment.LocalBalance,
c.LocalCommitment.RemoteBalance, nil
return c.LocalCommitment.LocalBtcBalance,
c.LocalCommitment.RemoteBtcBalance, c.LocalCommitment.LocalAssetBalance,
c.LocalCommitment.RemoteAssetBalance, nil
}

// If our current remote commit is at the desired height, we can return
// the current balances.
if c.RemoteCommitment.CommitHeight == height {
return c.RemoteCommitment.LocalBalance,
c.RemoteCommitment.RemoteBalance, nil
return c.RemoteCommitment.LocalBtcBalance,
c.RemoteCommitment.RemoteBtcBalance, c.RemoteCommitment.LocalAssetBalance,
c.RemoteCommitment.RemoteAssetBalance, nil
}

// If we are not currently on the height requested, we need to look up
// the previous height to obtain our balances at the given height.
commit, err := c.FindPreviousState(height)
if err != nil {
return 0, 0, err
return 0, 0, 0, 0, err
}

return commit.LocalBalance, commit.RemoteBalance, nil
return commit.LocalBtcBalance, commit.RemoteBtcBalance,commit.LocalAssetBalance, commit.RemoteAssetBalance, nil
}

// ActiveHtlcs returns a slice of HTLC's which are currently active on *both*
Expand Down Expand Up @@ -1729,7 +1758,11 @@ type HTLC struct {
RHash [32]byte

// Amt is the amount of milli-satoshis this HTLC escrows.
Amt lnwire.MilliSatoshi
BtcAmt lnwire.MilliSatoshi
/*
obd update wxf
*/
AssetAmt omnicore.Amount

// RefundTimeout is the absolute timeout on the HTLC that the sender
// must wait before reclaiming the funds in limbo.
Expand Down Expand Up @@ -1772,7 +1805,7 @@ func SerializeHtlcs(b io.Writer, htlcs ...HTLC) error {

for _, htlc := range htlcs {
if err := WriteElements(b,
htlc.Signature, htlc.RHash, htlc.Amt, htlc.RefundTimeout,
htlc.Signature, htlc.RHash, htlc.BtcAmt,htlc.AssetAmt, htlc.RefundTimeout,
htlc.OutputIndex, htlc.Incoming, htlc.OnionBlob[:],
htlc.HtlcIndex, htlc.LogIndex,
); err != nil {
Expand Down Expand Up @@ -1803,7 +1836,8 @@ func DeserializeHtlcs(r io.Reader) ([]HTLC, error) {
htlcs = make([]HTLC, numHtlcs)
for i := uint16(0); i < numHtlcs; i++ {
if err := ReadElements(r,
&htlcs[i].Signature, &htlcs[i].RHash, &htlcs[i].Amt,
&htlcs[i].Signature, &htlcs[i].RHash, &htlcs[i].BtcAmt,
&htlcs[i].AssetAmt,
&htlcs[i].RefundTimeout, &htlcs[i].OutputIndex,
&htlcs[i].Incoming, &htlcs[i].OnionBlob,
&htlcs[i].HtlcIndex, &htlcs[i].LogIndex,
Expand All @@ -1819,7 +1853,8 @@ func DeserializeHtlcs(r io.Reader) ([]HTLC, error) {
func (h *HTLC) Copy() HTLC {
clone := HTLC{
Incoming: h.Incoming,
Amt: h.Amt,
BtcAmt: h.BtcAmt,
AssetAmt: h.AssetAmt,
RefundTimeout: h.RefundTimeout,
OutputIndex: h.OutputIndex,
}
Expand Down Expand Up @@ -2798,7 +2833,11 @@ type ChannelCloseSummary struct {
RemotePub *btcec.PublicKey

// Capacity was the total capacity of the channel.
Capacity btcutil.Amount
BtcCapacity btcutil.Amount
/*
obd add wxf
*/
AssetCapacity omnicore.Amount

// CloseHeight is the height at which the funding transaction was
// spent.
Expand All @@ -2807,15 +2846,20 @@ type ChannelCloseSummary struct {
// SettledBalance is our total balance settled balance at the time of
// channel closure. This _does not_ include the sum of any outputs that
// have been time-locked as a result of the unilateral channel closure.
SettledBalance btcutil.Amount
SettledBtcBalance btcutil.Amount
/*
obd add wxf
*/
SettledAssetBalance omnicore.Amount
TimeLockedAssetBalance omnicore.Amount

// TimeLockedBalance is the sum of all the time-locked outputs at the
// time of channel closure. If we triggered the force closure of this
// channel, then this value will be non-zero if our settled output is
// above the dust limit. If we were on the receiving side of a channel
// force closure, then this value will be non-zero if we had any
// outstanding outgoing HTLC's at the time of channel closure.
TimeLockedBalance btcutil.Amount
TimeLockedBtcBalance btcutil.Amount

This comment has been minimized.

Copy link
@neocarmack

neocarmack May 18, 2022

Member

// Ben: Where is the timeLockedAssetBalance? @wxf4150


// CloseType details exactly _how_ the channel was closed. Five closure
// types are possible: cooperative, local force, remote force, breach
Expand Down Expand Up @@ -3023,8 +3067,8 @@ type ChannelSnapshot struct {
// within.
ChainHash chainhash.Hash

// Capacity is the total capacity of the channel.
Capacity btcutil.Amount
//// Capacity is the total capacity of the channel.
//Capacity btcutil.Amount

// TotalMSatSent is the total number of milli-satoshis we've sent
// within this channel.
Expand All @@ -3034,6 +3078,15 @@ type ChannelSnapshot struct {
// received within this channel.
TotalMSatReceived lnwire.MilliSatoshi

/*
obd add wxf
*/
BtcCapacity btcutil.Amount
AssetCapacity omnicore.Amount
TotalAssetSent omnicore.Amount
TotalAssetReceived omnicore.Amount
AssetID uint32

// ChannelCommitment is the current up-to-date commitment for the
// target channel.
ChannelCommitment
Expand All @@ -3050,13 +3103,20 @@ func (c *OpenChannel) Snapshot() *ChannelSnapshot {
snapshot := &ChannelSnapshot{
RemoteIdentity: *c.IdentityPub,
ChannelPoint: c.FundingOutpoint,
Capacity: c.Capacity,
/*obd update wxf*/
BtcCapacity: c.BtcCapacity,
AssetCapacity: c.AssetCapacity,
AssetID: c.AssetID,
TotalAssetSent: c.TotalAssetSent,
TotalAssetReceived: c.TotalAssetReceived,
TotalMSatSent: c.TotalMSatSent,
TotalMSatReceived: c.TotalMSatReceived,
ChainHash: c.ChainHash,
ChannelCommitment: ChannelCommitment{
LocalBalance: localCommit.LocalBalance,
RemoteBalance: localCommit.RemoteBalance,
LocalBtcBalance: localCommit.LocalBtcBalance,
RemoteBtcBalance: localCommit.RemoteBtcBalance,
LocalAssetBalance: localCommit.LocalAssetBalance,
RemoteAssetBalance: localCommit.RemoteAssetBalance,
CommitHeight: localCommit.CommitHeight,
CommitFee: localCommit.CommitFee,
},
Expand Down Expand Up @@ -3163,8 +3223,8 @@ func putChannelCloseSummary(tx kvdb.RwTx, chanID []byte,
func serializeChannelCloseSummary(w io.Writer, cs *ChannelCloseSummary) error {
err := WriteElements(w,
cs.ChanPoint, cs.ShortChanID, cs.ChainHash, cs.ClosingTXID,
cs.CloseHeight, cs.RemotePub, cs.Capacity, cs.SettledBalance,
cs.TimeLockedBalance, cs.CloseType, cs.IsPending,
cs.CloseHeight, cs.RemotePub, cs.BtcCapacity, cs.AssetCapacity, cs.SettledBtcBalance, cs.SettledAssetBalance,
cs.TimeLockedBtcBalance, cs.TimeLockedAssetBalance, cs.CloseType, cs.IsPending,
)
if err != nil {
return err
Expand Down Expand Up @@ -3224,8 +3284,8 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {

err := ReadElements(r,
&c.ChanPoint, &c.ShortChanID, &c.ChainHash, &c.ClosingTXID,
&c.CloseHeight, &c.RemotePub, &c.Capacity, &c.SettledBalance,
&c.TimeLockedBalance, &c.CloseType, &c.IsPending,
&c.CloseHeight, &c.RemotePub, &c.BtcCapacity, &c.AssetCapacity, &c.SettledBtcBalance, &c.SettledAssetBalance,
&c.TimeLockedBtcBalance, &c.TimeLockedAssetBalance, &c.CloseType, &c.IsPending,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -3326,8 +3386,8 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
channel.ShortChannelID, channel.IsPending, channel.IsInitiator,
channel.chanStatus, channel.FundingBroadcastHeight,
channel.NumConfsRequired, channel.ChannelFlags,
channel.IdentityPub, channel.Capacity, channel.TotalMSatSent,
channel.TotalMSatReceived,
channel.IdentityPub, channel.BtcCapacity, channel.AssetCapacity, channel.AssetID, channel.TotalMSatSent,
channel.TotalMSatReceived, channel.TotalAssetSent, channel.TotalAssetReceived,
); err != nil {
return err
}
Expand Down Expand Up @@ -3420,8 +3480,9 @@ func getOptionalUpfrontShutdownScript(chanBucket kvdb.RBucket, key []byte,
func serializeChanCommit(w io.Writer, c *ChannelCommitment) error {
if err := WriteElements(w,
c.CommitHeight, c.LocalLogIndex, c.LocalHtlcIndex,
c.RemoteLogIndex, c.RemoteHtlcIndex, c.LocalBalance,
c.RemoteBalance, c.CommitFee, c.FeePerKw, c.CommitTx,
c.RemoteLogIndex, c.RemoteHtlcIndex, c.LocalBtcBalance,
c.RemoteBtcBalance, c.LocalAssetBalance,
c.RemoteAssetBalance, c.CommitFee, c.FeePerKw, c.CommitTx,
c.CommitSig,
); err != nil {
return err
Expand Down Expand Up @@ -3514,8 +3575,8 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
&channel.ShortChannelID, &channel.IsPending, &channel.IsInitiator,
&channel.chanStatus, &channel.FundingBroadcastHeight,
&channel.NumConfsRequired, &channel.ChannelFlags,
&channel.IdentityPub, &channel.Capacity, &channel.TotalMSatSent,
&channel.TotalMSatReceived,
&channel.IdentityPub, &channel.BtcCapacity, &channel.AssetCapacity, &channel.AssetID, &channel.TotalMSatSent,
&channel.TotalMSatReceived,&channel.TotalAssetSent, &channel.TotalAssetReceived,
); err != nil {
return err
}
Expand Down Expand Up @@ -3579,7 +3640,7 @@ func deserializeChanCommit(r io.Reader) (ChannelCommitment, error) {

err := ReadElements(r,
&c.CommitHeight, &c.LocalLogIndex, &c.LocalHtlcIndex, &c.RemoteLogIndex,
&c.RemoteHtlcIndex, &c.LocalBalance, &c.RemoteBalance,
&c.RemoteHtlcIndex, &c.LocalBtcBalance, &c.RemoteBtcBalance, &c.LocalAssetBalance, &c.RemoteAssetBalance,
&c.CommitFee, &c.FeePerKw, &c.CommitTx, &c.CommitSig,
)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions channeldb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/lightningnetwork/lnd/lnwallet/omnicore"
"io"
"net"

Expand Down Expand Up @@ -142,6 +143,14 @@ func WriteElement(w io.Writer, element interface{}) error {
return err
}

/*
*obd add wxf: same as btcutil.Amount
*/
case omnicore.Amount:
if err := binary.Write(w, byteOrder, uint64(e)); err != nil {
return err
}

case lnwire.MilliSatoshi:
if err := binary.Write(w, byteOrder, uint64(e)); err != nil {
return err
Expand Down Expand Up @@ -332,6 +341,16 @@ func ReadElement(r io.Reader, element interface{}) error {

*e = btcutil.Amount(a)

/*obd add wxf: same as btcutil.Amount
*/
case *omnicore.Amount:
var a uint64
if err := binary.Read(r, byteOrder, &a); err != nil {
return err
}

*e = omnicore.Amount(a)

case *lnwire.MilliSatoshi:
var a uint64
if err := binary.Read(r, byteOrder, &a); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions channeldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,10 @@ func (c *ChannelStateDB) AbandonChannel(chanPoint *wire.OutPoint,
ChainHash: dbChan.ChainHash,
CloseHeight: bestHeight,
RemotePub: dbChan.IdentityPub,
Capacity: dbChan.Capacity,
SettledBalance: dbChan.LocalCommitment.LocalBalance.ToSatoshis(),
BtcCapacity: dbChan.BtcCapacity,
AssetCapacity: dbChan.AssetCapacity,
SettledBtcBalance: dbChan.LocalCommitment.LocalBtcBalance.ToSatoshis(),
SettledAssetBalance: dbChan.LocalCommitment.LocalAssetBalance,
ShortChanID: dbChan.ShortChanID(),
RemoteCurrentRevocation: dbChan.RemoteCurrentRevocation,
RemoteNextRevocation: dbChan.RemoteNextRevocation,
Expand Down
Loading

0 comments on commit 7e84e9e

Please sign in to comment.