-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pool: round default min chan amt to nearest unit #151
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ type SupplyUnit uint64 | |
const ( | ||
// BaseSupplyUnit is the smallest channel that can be bought or sold in | ||
// the system. These units are expressed in satoshis. | ||
BaseSupplyUnit SupplyUnit = 100_000 | ||
BaseSupplyUnit btcutil.Amount = 100_000 | ||
) | ||
|
||
// NewSupplyFromSats calculates the number of supply units that can be bought or | ||
|
@@ -21,6 +21,12 @@ func NewSupplyFromSats(sats btcutil.Amount) SupplyUnit { | |
return SupplyUnit(uint64(sats) / uint64(BaseSupplyUnit)) | ||
} | ||
|
||
// RoundToNextSupplyUnit computes and rounds to the next whole number of supply | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the TODO on L19 or is there still something more to do, @Roasbeef? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope this should be good, was just a hunch to revisit to ensure things are consistent. I think we need to do a sweep w.r.t where we use either of these methods, we may want to unify and ensure we always round up. |
||
// units from the given amount in satoshis. | ||
func RoundToNextSupplyUnit(sats btcutil.Amount) SupplyUnit { | ||
return SupplyUnit((sats + BaseSupplyUnit - 1) / BaseSupplyUnit) | ||
} | ||
|
||
// ToSatoshis maps a set number of supply units to the corresponding number of | ||
// satoshis. | ||
func (s SupplyUnit) ToSatoshis() btcutil.Amount { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package order_test | ||
|
||
import ( | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
"testing/quick" | ||
|
||
"github.com/btcsuite/btcutil" | ||
"github.com/lightninglabs/pool/order" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestRoundToNextSupplyUnit runs a quick check scenario to ensure the | ||
// correctness of RoundToNextSupplyUnit. | ||
func TestRoundToNextSupplyUnit(t *testing.T) { | ||
t.Parallel() | ||
|
||
f := func(sats btcutil.Amount) bool { | ||
units := order.RoundToNextSupplyUnit(sats) | ||
if sats%order.BaseSupplyUnit == 0 { | ||
return units == order.NewSupplyFromSats(sats) | ||
} | ||
return units == order.NewSupplyFromSats(sats)+1 | ||
} | ||
|
||
cfg := &quick.Config{ | ||
Values: func(v []reflect.Value, r *rand.Rand) { | ||
v[0] = reflect.ValueOf(btcutil.Amount(r.Int63())) | ||
}, | ||
} | ||
|
||
require.NoError(t, quick.Check(f, cfg)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change? To cutdown on casting somewhere else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just seemed strange to define it as
SupplyUnit
as you could then invokeToSatoshis
on it resulting in100000^2
.