Updated coinset interfaces to use btcutil.Amount for coin value
This commit is contained in:
parent
bcb009075b
commit
3e403ed6c0
2 changed files with 26 additions and 24 deletions
|
@ -12,7 +12,7 @@ import (
|
||||||
type Coin interface {
|
type Coin interface {
|
||||||
Hash() *btcwire.ShaHash
|
Hash() *btcwire.ShaHash
|
||||||
Index() uint32
|
Index() uint32
|
||||||
Value() int64
|
Value() btcutil.Amount
|
||||||
PkScript() []byte
|
PkScript() []byte
|
||||||
NumConfs() int64
|
NumConfs() int64
|
||||||
ValueAge() int64
|
ValueAge() int64
|
||||||
|
@ -33,7 +33,7 @@ type Coins interface {
|
||||||
// the CoinSet, otherwise the cached values will be incorrect.
|
// the CoinSet, otherwise the cached values will be incorrect.
|
||||||
type CoinSet struct {
|
type CoinSet struct {
|
||||||
coinList *list.List
|
coinList *list.List
|
||||||
totalValue int64
|
totalValue btcutil.Amount
|
||||||
totalValueAge int64
|
totalValueAge int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ func (cs *CoinSet) Coins() []Coin {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TotalValue returns the total value of the coins in the set.
|
// TotalValue returns the total value of the coins in the set.
|
||||||
func (cs *CoinSet) TotalValue() (value int64) {
|
func (cs *CoinSet) TotalValue() (value btcutil.Amount) {
|
||||||
return cs.totalValue
|
return cs.totalValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +143,7 @@ var (
|
||||||
|
|
||||||
// satisfiesTargetValue checks that the totalValue is either exactly the targetValue
|
// satisfiesTargetValue checks that the totalValue is either exactly the targetValue
|
||||||
// or is greater than the targetValue by at least the minChange amount.
|
// or is greater than the targetValue by at least the minChange amount.
|
||||||
func satisfiesTargetValue(targetValue, minChange, totalValue int64) bool {
|
func satisfiesTargetValue(targetValue, minChange, totalValue btcutil.Amount) bool {
|
||||||
return (totalValue == targetValue || totalValue >= targetValue+minChange)
|
return (totalValue == targetValue || totalValue >= targetValue+minChange)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ func satisfiesTargetValue(targetValue, minChange, totalValue int64) bool {
|
||||||
// It is important to note that the Coins being used as inputs need to have
|
// It is important to note that the Coins being used as inputs need to have
|
||||||
// a constant ValueAge() during the execution of CoinSelect.
|
// a constant ValueAge() during the execution of CoinSelect.
|
||||||
type CoinSelector interface {
|
type CoinSelector interface {
|
||||||
CoinSelect(targetValue int64, coins []Coin) (Coins, error)
|
CoinSelect(targetValue btcutil.Amount, coins []Coin) (Coins, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MinIndexCoinSelector is a CoinSelector that attempts to construct a
|
// MinIndexCoinSelector is a CoinSelector that attempts to construct a
|
||||||
|
@ -167,12 +167,12 @@ type CoinSelector interface {
|
||||||
// any number of lower indexes (as in the ordered array) over higher ones.
|
// any number of lower indexes (as in the ordered array) over higher ones.
|
||||||
type MinIndexCoinSelector struct {
|
type MinIndexCoinSelector struct {
|
||||||
MaxInputs int
|
MaxInputs int
|
||||||
MinChangeAmount int64
|
MinChangeAmount btcutil.Amount
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoinSelect will attempt to select coins using the algorithm described
|
// CoinSelect will attempt to select coins using the algorithm described
|
||||||
// in the MinIndexCoinSelector struct.
|
// in the MinIndexCoinSelector struct.
|
||||||
func (s MinIndexCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Coins, error) {
|
func (s MinIndexCoinSelector) CoinSelect(targetValue btcutil.Amount, coins []Coin) (Coins, error) {
|
||||||
cs := NewCoinSet(nil)
|
cs := NewCoinSet(nil)
|
||||||
for n := 0; n < len(coins) && n < s.MaxInputs; n++ {
|
for n := 0; n < len(coins) && n < s.MaxInputs; n++ {
|
||||||
cs.PushCoin(coins[n])
|
cs.PushCoin(coins[n])
|
||||||
|
@ -188,12 +188,12 @@ func (s MinIndexCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Coins
|
||||||
// that uses as few of the inputs as possible.
|
// that uses as few of the inputs as possible.
|
||||||
type MinNumberCoinSelector struct {
|
type MinNumberCoinSelector struct {
|
||||||
MaxInputs int
|
MaxInputs int
|
||||||
MinChangeAmount int64
|
MinChangeAmount btcutil.Amount
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoinSelect will attempt to select coins using the algorithm described
|
// CoinSelect will attempt to select coins using the algorithm described
|
||||||
// in the MinNumberCoinSelector struct.
|
// in the MinNumberCoinSelector struct.
|
||||||
func (s MinNumberCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Coins, error) {
|
func (s MinNumberCoinSelector) CoinSelect(targetValue btcutil.Amount, coins []Coin) (Coins, error) {
|
||||||
sortedCoins := make([]Coin, 0, len(coins))
|
sortedCoins := make([]Coin, 0, len(coins))
|
||||||
sortedCoins = append(sortedCoins, coins...)
|
sortedCoins = append(sortedCoins, coins...)
|
||||||
sort.Sort(sort.Reverse(byAmount(sortedCoins)))
|
sort.Sort(sort.Reverse(byAmount(sortedCoins)))
|
||||||
|
@ -212,12 +212,12 @@ func (s MinNumberCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Coin
|
||||||
// block.
|
// block.
|
||||||
type MaxValueAgeCoinSelector struct {
|
type MaxValueAgeCoinSelector struct {
|
||||||
MaxInputs int
|
MaxInputs int
|
||||||
MinChangeAmount int64
|
MinChangeAmount btcutil.Amount
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoinSelect will attempt to select coins using the algorithm described
|
// CoinSelect will attempt to select coins using the algorithm described
|
||||||
// in the MaxValueAgeCoinSelector struct.
|
// in the MaxValueAgeCoinSelector struct.
|
||||||
func (s MaxValueAgeCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Coins, error) {
|
func (s MaxValueAgeCoinSelector) CoinSelect(targetValue btcutil.Amount, coins []Coin) (Coins, error) {
|
||||||
sortedCoins := make([]Coin, 0, len(coins))
|
sortedCoins := make([]Coin, 0, len(coins))
|
||||||
sortedCoins = append(sortedCoins, coins...)
|
sortedCoins = append(sortedCoins, coins...)
|
||||||
sort.Sort(sort.Reverse(byValueAge(sortedCoins)))
|
sort.Sort(sort.Reverse(byValueAge(sortedCoins)))
|
||||||
|
@ -239,13 +239,13 @@ func (s MaxValueAgeCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Co
|
||||||
//
|
//
|
||||||
type MinPriorityCoinSelector struct {
|
type MinPriorityCoinSelector struct {
|
||||||
MaxInputs int
|
MaxInputs int
|
||||||
MinChangeAmount int64
|
MinChangeAmount btcutil.Amount
|
||||||
MinAvgValueAgePerInput int64
|
MinAvgValueAgePerInput int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoinSelect will attempt to select coins using the algorithm described
|
// CoinSelect will attempt to select coins using the algorithm described
|
||||||
// in the MinPriorityCoinSelector struct.
|
// in the MinPriorityCoinSelector struct.
|
||||||
func (s MinPriorityCoinSelector) CoinSelect(targetValue int64, coins []Coin) (Coins, error) {
|
func (s MinPriorityCoinSelector) CoinSelect(targetValue btcutil.Amount, coins []Coin) (Coins, error) {
|
||||||
possibleCoins := make([]Coin, 0, len(coins))
|
possibleCoins := make([]Coin, 0, len(coins))
|
||||||
possibleCoins = append(possibleCoins, coins...)
|
possibleCoins = append(possibleCoins, coins...)
|
||||||
|
|
||||||
|
@ -368,8 +368,8 @@ func (c *SimpleCoin) txOut() *btcwire.TxOut {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the value of the Coin
|
// Value returns the value of the Coin
|
||||||
func (c *SimpleCoin) Value() int64 {
|
func (c *SimpleCoin) Value() btcutil.Amount {
|
||||||
return c.txOut().Value
|
return btcutil.Amount(c.txOut().Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PkScript returns the outpoint script of the Coin.
|
// PkScript returns the outpoint script of the Coin.
|
||||||
|
@ -390,5 +390,5 @@ func (c *SimpleCoin) NumConfs() int64 {
|
||||||
// ValueAge returns the product of the value and the number of confirmations. This is
|
// ValueAge returns the product of the value and the number of confirmations. This is
|
||||||
// used as an input to calculate the priority of the transaction.
|
// used as an input to calculate the priority of the transaction.
|
||||||
func (c *SimpleCoin) ValueAge() int64 {
|
func (c *SimpleCoin) ValueAge() int64 {
|
||||||
return c.TxNumConfs * c.Value()
|
return c.TxNumConfs * int64(c.Value())
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,18 +14,18 @@ import (
|
||||||
type TestCoin struct {
|
type TestCoin struct {
|
||||||
TxHash *btcwire.ShaHash
|
TxHash *btcwire.ShaHash
|
||||||
TxIndex uint32
|
TxIndex uint32
|
||||||
TxValue int64
|
TxValue btcutil.Amount
|
||||||
TxNumConfs int64
|
TxNumConfs int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TestCoin) Hash() *btcwire.ShaHash { return c.TxHash }
|
func (c *TestCoin) Hash() *btcwire.ShaHash { return c.TxHash }
|
||||||
func (c *TestCoin) Index() uint32 { return c.TxIndex }
|
func (c *TestCoin) Index() uint32 { return c.TxIndex }
|
||||||
func (c *TestCoin) Value() int64 { return c.TxValue }
|
func (c *TestCoin) Value() btcutil.Amount { return c.TxValue }
|
||||||
func (c *TestCoin) PkScript() []byte { return nil }
|
func (c *TestCoin) PkScript() []byte { return nil }
|
||||||
func (c *TestCoin) NumConfs() int64 { return c.TxNumConfs }
|
func (c *TestCoin) NumConfs() int64 { return c.TxNumConfs }
|
||||||
func (c *TestCoin) ValueAge() int64 { return c.TxValue * c.TxNumConfs }
|
func (c *TestCoin) ValueAge() int64 { return int64(c.TxValue) * c.TxNumConfs }
|
||||||
|
|
||||||
func NewCoin(index, value, numConfs int64) coinset.Coin {
|
func NewCoin(index int64, value btcutil.Amount, numConfs int64) coinset.Coin {
|
||||||
h := fastsha256.New()
|
h := fastsha256.New()
|
||||||
h.Write([]byte(fmt.Sprintf("%d", index)))
|
h.Write([]byte(fmt.Sprintf("%d", index)))
|
||||||
hash, _ := btcwire.NewShaHash(h.Sum(nil))
|
hash, _ := btcwire.NewShaHash(h.Sum(nil))
|
||||||
|
@ -41,7 +41,7 @@ func NewCoin(index, value, numConfs int64) coinset.Coin {
|
||||||
type coinSelectTest struct {
|
type coinSelectTest struct {
|
||||||
selector coinset.CoinSelector
|
selector coinset.CoinSelector
|
||||||
inputCoins []coinset.Coin
|
inputCoins []coinset.Coin
|
||||||
targetValue int64
|
targetValue btcutil.Amount
|
||||||
expectedCoins []coinset.Coin
|
expectedCoins []coinset.Coin
|
||||||
expectedError error
|
expectedError error
|
||||||
}
|
}
|
||||||
|
@ -216,9 +216,11 @@ func TestMinPrioritySelector(t *testing.T) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// should be two outpoints, with 1st one having 0.035BTC value.
|
// should be two outpoints, with 1st one having 0.035BTC value.
|
||||||
|
testSimpleCoinNumConfs = int64(1)
|
||||||
testSimpleCoinTxHash = "9b5965c86de51d5dc824e179a05cf232db78c80ae86ca9d7cb2a655b5e19c1e2"
|
testSimpleCoinTxHash = "9b5965c86de51d5dc824e179a05cf232db78c80ae86ca9d7cb2a655b5e19c1e2"
|
||||||
testSimpleCoinTxHex = "0100000001a214a110f79e4abe073865ea5b3745c6e82c913bad44be70652804a5e4003b0a010000008c493046022100edd18a69664efa57264be207100c203e6cade1888cbb88a0ad748548256bb2f0022100f1027dc2e6c7f248d78af1dd90027b5b7d8ec563bb62aa85d4e74d6376f3868c0141048f3757b65ed301abd1b0e8942d1ab5b50594d3314cff0299f300c696376a0a9bf72e74710a8af7a5372d4af4bb519e2701a094ef48c8e48e3b65b28502452dceffffffff02e0673500000000001976a914686dd149a79b4a559d561fbc396d3e3c6628b98d88ace86ef102000000001976a914ac3f995655e81b875b38b64351d6f896ddbfc68588ac00000000"
|
testSimpleCoinTxHex = "0100000001a214a110f79e4abe073865ea5b3745c6e82c913bad44be70652804a5e4003b0a010000008c493046022100edd18a69664efa57264be207100c203e6cade1888cbb88a0ad748548256bb2f0022100f1027dc2e6c7f248d78af1dd90027b5b7d8ec563bb62aa85d4e74d6376f3868c0141048f3757b65ed301abd1b0e8942d1ab5b50594d3314cff0299f300c696376a0a9bf72e74710a8af7a5372d4af4bb519e2701a094ef48c8e48e3b65b28502452dceffffffff02e0673500000000001976a914686dd149a79b4a559d561fbc396d3e3c6628b98d88ace86ef102000000001976a914ac3f995655e81b875b38b64351d6f896ddbfc68588ac00000000"
|
||||||
testSimpleCoinTxValue0 = int64(3500000)
|
testSimpleCoinTxValue0 = btcutil.Amount(3500000)
|
||||||
|
testSimpleCoinTxValueAge0 = int64(testSimpleCoinTxValue0) * testSimpleCoinNumConfs
|
||||||
testSimpleCoinTxPkScript0Hex = "76a914686dd149a79b4a559d561fbc396d3e3c6628b98d88ac"
|
testSimpleCoinTxPkScript0Hex = "76a914686dd149a79b4a559d561fbc396d3e3c6628b98d88ac"
|
||||||
testSimpleCoinTxPkScript0Bytes, _ = hex.DecodeString(testSimpleCoinTxPkScript0Hex)
|
testSimpleCoinTxPkScript0Bytes, _ = hex.DecodeString(testSimpleCoinTxPkScript0Hex)
|
||||||
testSimpleCoinTxBytes, _ = hex.DecodeString(testSimpleCoinTxHex)
|
testSimpleCoinTxBytes, _ = hex.DecodeString(testSimpleCoinTxHex)
|
||||||
|
@ -226,7 +228,7 @@ var (
|
||||||
testSimpleCoin = &coinset.SimpleCoin{
|
testSimpleCoin = &coinset.SimpleCoin{
|
||||||
Tx: testSimpleCoinTx,
|
Tx: testSimpleCoinTx,
|
||||||
TxIndex: 0,
|
TxIndex: 0,
|
||||||
TxNumConfs: 1,
|
TxNumConfs: testSimpleCoinNumConfs,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -246,7 +248,7 @@ func TestSimpleCoin(t *testing.T) {
|
||||||
if testSimpleCoin.NumConfs() != 1 {
|
if testSimpleCoin.NumConfs() != 1 {
|
||||||
t.Error("Differet value of num confs than expected")
|
t.Error("Differet value of num confs than expected")
|
||||||
}
|
}
|
||||||
if testSimpleCoin.ValueAge() != testSimpleCoinTxValue0 {
|
if testSimpleCoin.ValueAge() != testSimpleCoinTxValueAge0 {
|
||||||
t.Error("Different value of coin value * age than expected")
|
t.Error("Different value of coin value * age than expected")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue