Skip to content

Commit

Permalink
replace amt-uint64 to named type UnitPrec11/UnitPrec8
Browse files Browse the repository at this point in the history
  • Loading branch information
wxf4150 committed May 19, 2022
1 parent 9697d1a commit 8a95bb2
Show file tree
Hide file tree
Showing 53 changed files with 668 additions and 690 deletions.
5 changes: 3 additions & 2 deletions amp/shard_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"encoding/binary"
"fmt"
"github.com/lightningnetwork/lnd/lnwire"
"sync"

"github.com/lightningnetwork/lnd/lntypes"
Expand Down Expand Up @@ -49,7 +50,7 @@ type ShardTracker struct {
paymentAddr [32]byte
//totalAmt lnwire.MilliSatoshi
/*obd update wxf*/
totalAmt uint64
totalAmt lnwire.UnitPrec11
assetId uint32

sharer Sharer
Expand All @@ -68,7 +69,7 @@ var _ shards.ShardTracker = (*ShardTracker)(nil)
// correctly.
func NewShardTracker(root, setID, payAddr [32]byte,
//totalAmt lnwire.MilliSatoshi) *ShardTracker {
totalAmt uint64,assetId uint32) *ShardTracker {
totalAmt lnwire.UnitPrec11,assetId uint32) *ShardTracker {

// Create a new seed sharer from this root.
rootShare := Share(root)
Expand Down
30 changes: 15 additions & 15 deletions chainreg/chainregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/lightningnetwork/lnd/lnwallet/omnicore"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -156,7 +157,7 @@ const (
// DefaultLtcChannelConstraints is the default set of channel constraints that
// are meant to be used when initially funding a Litecoin channel.
var DefaultLtcChannelConstraints = channeldb.ChannelConstraints{
DustLimit: uint64(DefaultLitecoinDustLimit),
DustLimit: DefaultLitecoinDustLimit,
MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
}

Expand Down Expand Up @@ -190,10 +191,11 @@ type PartialChainControl struct {
ChainSource chain.Interface

// RoutingPolicy is the routing policy we have decided to use.
RoutingPolicy htlcswitch.ForwardingPolicy
RoutingPolicyCfg htlcswitch.ForwardingPolicyCfg

// MinHtlcIn is the minimum HTLC we will accept.
MinHtlcIn lnwire.MilliSatoshi
AssetMinHtlcIn omnicore.Amount

// ChannelConstraints is the set of default constraints that will be
// used for any incoming or outgoing channel reservation requests.
Expand Down Expand Up @@ -240,7 +242,7 @@ func GenDefaultBtcConstraints() channeldb.ChannelConstraints {
dustLimit := lnwallet.DustLimitForSize(input.UnknownWitnessSize)

return channeldb.ChannelConstraints{
DustLimit: uint64(dustLimit),
DustLimit: dustLimit,
MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
}
}
Expand All @@ -263,26 +265,24 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) {

switch cfg.PrimaryChain() {
case BitcoinChain:
cc.RoutingPolicy = htlcswitch.ForwardingPolicy{
Cfg: htlcswitch.ForwardingPolicyCfg{
MinHTLCOut: cfg.Bitcoin.MinHTLCOut,
BaseFee: cfg.Bitcoin.BaseFee,
FeeRate: cfg.Bitcoin.FeeRate,
},
cc.RoutingPolicyCfg = htlcswitch.ForwardingPolicyCfg{
MinHTLCOut: cfg.Bitcoin.MinHTLCOut,
AssetMinHTLCOut: cfg.Bitcoin.AssetMinHTLCOut,
BaseFee: cfg.Bitcoin.BaseFee,
FeeRate: cfg.Bitcoin.FeeRate,
TimeLockDelta: cfg.Bitcoin.TimeLockDelta,
}
cc.MinHtlcIn = cfg.Bitcoin.MinHTLCIn
cc.AssetMinHtlcIn = cfg.Bitcoin.AssetMinHTLCIn
cc.FeeEstimator = chainfee.NewStaticEstimator(
DefaultBitcoinStaticFeePerKW,
DefaultBitcoinStaticMinRelayFeeRate,
)
case LitecoinChain:
cc.RoutingPolicy = htlcswitch.ForwardingPolicy{
Cfg: htlcswitch.ForwardingPolicyCfg{
MinHTLCOut: cfg.Litecoin.MinHTLCOut,
BaseFee: cfg.Litecoin.BaseFee,
FeeRate: cfg.Litecoin.FeeRate,
},
cc.RoutingPolicyCfg = htlcswitch.ForwardingPolicyCfg{
MinHTLCOut: cfg.Litecoin.MinHTLCOut,
BaseFee: cfg.Litecoin.BaseFee,
FeeRate: cfg.Litecoin.FeeRate,
TimeLockDelta: cfg.Litecoin.TimeLockDelta,
}
cc.MinHtlcIn = cfg.Litecoin.MinHTLCIn
Expand Down
10 changes: 5 additions & 5 deletions chanacceptor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ type ChannelAcceptResponse struct {
// Reserve is the amount that require the remote peer hold in reserve
// on the channel.
//Reserve btcutil.Amount
Reserve uint64
Reserve lnwire.UnitPrec8

This comment has been minimized.

Copy link
@neocarmack

neocarmack May 19, 2022

Member

OBD Ben: What if it is an asset? still use UnitPrec8?


// InFlightTotal is the maximum amount that we allow the remote peer to
// hold in outstanding htlcs.
//InFlightTotal lnwire.MilliSatoshi
InFlightTotal uint64
InFlightTotal lnwire.UnitPrec11

// HtlcLimit is the maximum number of htlcs that we allow the remote
// peer to offer us.
HtlcLimit uint16

// MinHtlcIn is the minimum incoming htlc value allowed on the channel.
//MinHtlcIn lnwire.MilliSatoshi
MinHtlcIn uint64
MinHtlcIn lnwire.UnitPrec11

// MinAcceptDepth is the minimum depth that the initiator of the
// channel should wait before considering the channel open.
Expand All @@ -73,8 +73,8 @@ type ChannelAcceptResponse struct {
// error.
func NewChannelAcceptResponse(accept bool, acceptErr error,
upfrontShutdown lnwire.DeliveryAddress, csvDelay, htlcLimit,
minDepth uint16, reserve uint64, inFlight,
minHtlcIn uint64) *ChannelAcceptResponse {
minDepth uint16, reserve lnwire.UnitPrec8, inFlight,
minHtlcIn lnwire.UnitPrec11) *ChannelAcceptResponse {

resp := &ChannelAcceptResponse{
UpfrontShutdown: upfrontShutdown,
Expand Down
62 changes: 11 additions & 51 deletions channeldb/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,71 +312,31 @@ func (c ChannelType) HasLeaseExpiration() bool {
return c&LeaseExpirationBit == LeaseExpirationBit
}

func (ccc *ChannelConstraints)LoadCfg(assetId uint32){

// get usdt value,btc/usdt ~ 30000
ccc.DustLimit=30* uint64(ccc.Cfg.DustLimit)
ccc.ChanReserve=30* uint64(ccc.Cfg.ChanReserve)
if assetId==omnicore.BtcAssetId{
ccc.DustLimit=uint64(ccc.Cfg.DustLimit)
ccc.ChanReserve=uint64(ccc.Cfg.ChanReserve)
}
ccc.MaxPendingAmount=lnwire.MstatCfgToI64(assetId,ccc.Cfg.MaxPendingAmount)
ccc.MinHTLC=lnwire.MstatCfgToI64(assetId,ccc.Cfg.MinHTLC)
}

type ChannelConstraintsCfg struct {

// DustLimit is the threshold (in satoshis) below which any outputs
// should be trimmed. When an output is trimmed, it isn't materialized
// as an actual output, but is instead burned to miner's fees.
DustLimit btcutil.Amount

// ChanReserve is an absolute reservation on the channel for the
// owner of this set of constraints. This means that the current
// settled balance for this node CANNOT dip below the reservation
// amount. This acts as a defense against costless attacks when
// either side no longer has any skin in the game.
ChanReserve btcutil.Amount

// MaxPendingAmount is the maximum pending HTLC value that the
// owner of these constraints can offer the remote node at a
// particular time.
MaxPendingAmount lnwire.MilliSatoshi

// MinHTLC is the minimum HTLC value that the owner of these
// constraints can offer the remote node. If any HTLCs below this
// amount are offered, then the HTLC will be rejected. This, in
// tandem with the dust limit allows a node to regulate the
// smallest HTLC that it deems economically relevant.
MinHTLC lnwire.MilliSatoshi
}
type ChannelConstraints struct {
Cfg ChannelConstraintsCfg

// DustLimit is the threshold (in satoshis) below which any outputs
// should be trimmed. When an output is trimmed, it isn't materialized
// as an actual output, but is instead burned to miner's fees.
DustLimit uint64
DustLimit btcutil.Amount

// ChanReserve is an absolute reservation on the channel for the
// owner of this set of constraints. This means that the current
// settled balance for this node CANNOT dip below the reservation
// amount. This acts as a defense against costless attacks when
// either side no longer has any skin in the game.
ChanReserve uint64
ChanReserve lnwire.UnitPrec8

// MaxPendingAmount is the maximum pending HTLC value that the
// owner of these constraints can offer the remote node at a
// particular time.
MaxPendingAmount uint64
MaxPendingAmount lnwire.UnitPrec11

// MinHTLC is the minimum HTLC value that the owner of these
// constraints can offer the remote node. If any HTLCs below this
// amount are offered, then the HTLC will be rejected. This, in
// tandem with the dust limit allows a node to regulate the
// smallest HTLC that it deems economically relevant.
MinHTLC uint64
MinHTLC lnwire.UnitPrec11

// MaxAcceptedHtlcs is the maximum number of HTLCs that the owner of
// this set of constraints can offer the remote node. This allows each
Expand Down Expand Up @@ -627,19 +587,19 @@ func (c ChannelStatus) String() string {

return statusStr
}
func (ch *OpenChannel) GetMsgCapForHtlc() uint64{
func (ch *OpenChannel) GetMsgCapForHtlc() lnwire.UnitPrec11{
if ch.AssetID==omnicore.BtcAssetId{
return uint64(ch.BtcCapacity)*1000
}else if ch.AssetID>1 {
return uint64(ch.AssetCapacity)
return lnwire.UnitPrec11(lnwire.NewMSatFromSatoshis(ch.BtcCapacity))
}else {
return lnwire.UnitPrec11(ch.AssetCapacity)
}
return 0
}
func (ch *OpenChannel) GetMsgCap() uint64{
func (ch *OpenChannel) GetRawCap() lnwire.UnitPrec8{
if ch.AssetID==omnicore.BtcAssetId{
return uint64(ch.BtcCapacity)
return lnwire.UnitPrec8(ch.BtcCapacity)
}else if ch.AssetID>omnicore.BtcAssetId{
return uint64(ch.AssetCapacity)
return lnwire.UnitPrec8(ch.AssetCapacity)
}
return 0
}
Expand Down
54 changes: 29 additions & 25 deletions channeldb/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3082,7 +3082,7 @@ type ChannelEdgeInfo struct {
*/
// Capacity is the total capacity of the channel, this is determined by
// the value output in the outpoint that created this channel.
Capacity uint64
Capacity lnwire.UnitPrec8
AssetId uint32

// ExtraOpaqueData is the set of data that was appended to this
Expand Down Expand Up @@ -3431,30 +3431,25 @@ type ChannelEdgePolicy struct {
*/
// MinHTLC is the smallest value HTLC this node will forward, expressed
// in millisatoshi.
MinHTLC uint64
MinHTLC lnwire.UnitPrec11

/*obd update wxf
AssetId >0 the unit is lnwire.MilliSatoshi, else omnicore.Amount
*/
// MaxHTLC is the largest value HTLC this node will forward, expressed
// in millisatoshi.
MaxHTLC uint64
MaxHTLC lnwire.UnitPrec11
/*obd add wxf*/
AssetId uint32

/*obd update wxf
AssetId >0 the unit is lnwire.MilliSatoshi, else omnicore.Amount
*/
// FeeBaseMSat is the base HTLC fee that will be charged for forwarding
// ANY HTLC, expressed in mSAT's.
FeeBaseMSat uint64
//asset will be zero
FeeBaseMSat lnwire.MilliSatoshi

/*obd update wxf
AssetId >0 the unit is lnwire.MilliSatoshi, else omnicore.Amount
*/
// FeeProportionalMillionths is the rate that the node will charge for
// HTLCs for each millionth of a satoshi forwarded.
FeeProportionalMillionths uint64
FeeProportionalMillionths lnwire.UnitPrec11

// Node is the LightningNode that this directed edge leads to. Using
// this pointer the channel graph can further be traversed.
Expand Down Expand Up @@ -3510,25 +3505,34 @@ AssetId >0 the unit is lnwire.MilliSatoshi, else omnicore.Amount
// the passed active payment channel. This value is currently computed as
// specified in BOLT07, but will likely change in the near future.
func (c *ChannelEdgePolicy) ComputeFee(
amt uint64) uint64 {

return c.FeeBaseMSat + (amt*c.FeeProportionalMillionths)/feeRateParts
amt lnwire.UnitPrec11) lnwire.UnitPrec11 {
//feeBase := lnwire.MilliSatoshi(0)
//feeRate := lnwire.MilliSatoshi(100)
//if c.AssetId == omnicore.BtcAssetId {}
feeRate := lnwire.MilliSatoshi(c.FeeProportionalMillionths)
feeBase := c.FeeBaseMSat
return lnwire.UnitPrec11(feeBase + (amt.ToMsat()*feeRate)/feeRateParts)
}

// divideCeil divides dividend by factor and rounds the result up.
func divideCeil(dividend, factor uint64) uint64 {
func divideCeil(dividend, factor lnwire.MilliSatoshi) lnwire.MilliSatoshi {
return (dividend + factor - 1) / factor
}

// ComputeFeeFromIncoming computes the fee to forward an HTLC given the incoming
// amount.
func (c *ChannelEdgePolicy) ComputeFeeFromIncoming(
incomingAmt uint64) uint64 {

return incomingAmt - divideCeil(
feeRateParts*(incomingAmt-c.FeeBaseMSat),
feeRateParts+c.FeeProportionalMillionths,
)
incomingAmt lnwire.UnitPrec11) lnwire.UnitPrec11 {
//feeBase := lnwire.MilliSatoshi(0)
//feeRate := lnwire.MilliSatoshi(100)
//if c.AssetId == omnicore.BtcAssetId {}
feeRate := lnwire.MilliSatoshi(c.FeeProportionalMillionths)
feeBase := c.FeeBaseMSat

return lnwire.UnitPrec11(incomingAmt.ToMsat() - divideCeil(
feeRateParts*(incomingAmt.ToMsat()-feeBase),
feeRateParts+feeRate,
))
}

// FetchChannelEdgesByOutpoint attempts to lookup the two directed edges for
Expand Down Expand Up @@ -4840,18 +4844,18 @@ func deserializeChanEdgePolicyRaw(r io.Reader) (*ChannelEdgePolicy, error) {
if err := binary.Read(r, byteOrder, &n); err != nil {
return nil, err
}
edge.MinHTLC = (n)
edge.MinHTLC = lnwire.UnitPrec11(n)


if err := binary.Read(r, byteOrder, &n); err != nil {
return nil, err
}
edge.FeeBaseMSat = (n)
edge.FeeBaseMSat = lnwire.MilliSatoshi(n)

if err := binary.Read(r, byteOrder, &n); err != nil {
return nil, err
}
edge.FeeProportionalMillionths = (n)
edge.FeeProportionalMillionths = lnwire.UnitPrec11(n)

var pub [33]byte
if _, err := r.Read(pub[:]); err != nil {
Expand Down Expand Up @@ -4888,7 +4892,7 @@ func deserializeChanEdgePolicyRaw(r io.Reader) (*ChannelEdgePolicy, error) {

/*obd update wxf*/
maxBtcHtlc := byteOrder.Uint64(opq[:8])
edge.MaxHTLC = maxBtcHtlc
edge.MaxHTLC = lnwire.UnitPrec11(maxBtcHtlc)

// Exclude the parsed field from the rest of the opaque data.
edge.ExtraOpaqueData = opq[8:]
Expand Down
Loading

0 comments on commit 8a95bb2

Please sign in to comment.