Skip to content

Commit

Permalink
wallet: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Dec 15, 2023
1 parent 53dac7c commit 3fd61f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
7 changes: 2 additions & 5 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,6 @@ func (w *SingleAddressWallet) SignTransaction(cs consensus.State, txn *types.Tra
// Redistribute returns a transaction that redistributes money in the wallet by
// selecting a minimal set of inputs to cover the creation of the requested
// outputs. It also returns a list of output IDs that need to be signed.
//
// NOTE: we can not reuse 'FundTransaction' because it randomizes the unspent
// transaction outputs it uses and we need a minimal set of inputs
func (w *SingleAddressWallet) Redistribute(cs consensus.State, outputs int, amount, feePerByte types.Currency, pool []types.Transaction) ([]types.Transaction, []types.Hash256, error) {
w.mu.Lock()
defer w.mu.Unlock()
Expand Down Expand Up @@ -372,14 +369,15 @@ func (w *SingleAddressWallet) Redistribute(cs consensus.State, outputs int, amou
Address: w.Address(),
})
}
outputs -= len(txn.SiacoinOutputs)

// estimate the fees
outputFees := feePerByte.Mul64(uint64(len(encoding.Marshal(txn.SiacoinOutputs))))
feePerInput := feePerByte.Mul64(BytesPerInput)

// collect outputs that cover the total amount
var inputs []SiacoinElement
want := amount.Mul64(uint64(outputs))
want := amount.Mul64(uint64(len(txn.SiacoinOutputs)))
var amtInUse, amtSameValue, amtNotMatured types.Currency
for _, sce := range utxos {
inUse := w.isOutputUsed(sce.ID) || inPool[sce.ID]
Expand Down Expand Up @@ -434,7 +432,6 @@ func (w *SingleAddressWallet) Redistribute(cs consensus.State, outputs int, amou
w.lastUsed[sce.ID] = time.Now()
}

outputs -= len(txn.SiacoinOutputs)
txns = append(txns, txn)
}

Expand Down
35 changes: 25 additions & 10 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wallet_test
package wallet

import (
"context"
Expand All @@ -9,23 +9,22 @@ import (
"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/wallet"
"go.uber.org/zap"
"lukechampine.com/frand"
)

// mockStore implements wallet.SingleAddressStore and allows to manipulate the
// wallet's utxos
type mockStore struct {
utxos []wallet.SiacoinElement
utxos []SiacoinElement
}

func (s *mockStore) Balance() (types.Currency, error) { return types.ZeroCurrency, nil }
func (s *mockStore) Height() uint64 { return 0 }
func (s *mockStore) UnspentSiacoinElements(bool) ([]wallet.SiacoinElement, error) {
func (s *mockStore) UnspentSiacoinElements(bool) ([]SiacoinElement, error) {
return s.utxos, nil
}
func (s *mockStore) Transactions(before, since time.Time, offset, limit int) ([]wallet.Transaction, error) {
func (s *mockStore) Transactions(before, since time.Time, offset, limit int) ([]Transaction, error) {
return nil, nil
}
func (s *mockStore) RecordWalletMetric(ctx context.Context, metrics ...api.WalletMetric) error {
Expand All @@ -47,16 +46,16 @@ func TestWalletRedistribute(t *testing.T) {
// create a wallet with one output
priv := types.GeneratePrivateKey()
pub := priv.PublicKey()
utxo := wallet.SiacoinElement{
utxo := SiacoinElement{
types.SiacoinOutput{
Value: oneSC.Mul64(20),
Address: wallet.StandardAddress(pub),
Address: StandardAddress(pub),
},
randomOutputID(),
0,
}
s := &mockStore{utxos: []wallet.SiacoinElement{utxo}}
w := wallet.NewSingleAddressWallet(priv, s, 0, zap.NewNop().Sugar())
s := &mockStore{utxos: []SiacoinElement{utxo}}
w := NewSingleAddressWallet(priv, s, 0, zap.NewNop().Sugar())

numOutputsWithValue := func(v types.Currency) (c uint64) {
utxos, _ := w.UnspentOutputs()
Expand All @@ -78,7 +77,7 @@ func TestWalletRedistribute(t *testing.T) {
}
}
for _, output := range txn.SiacoinOutputs {
s.utxos = append(s.utxos, wallet.SiacoinElement{output, randomOutputID(), 0})
s.utxos = append(s.utxos, SiacoinElement{output, randomOutputID(), 0})
}
}

Expand Down Expand Up @@ -172,6 +171,22 @@ func TestWalletRedistribute(t *testing.T) {
if cnt := numOutputsWithValue(amount); cnt != 6 {
t.Fatalf("unexpected number of 3SC outputs, %v != 6", cnt)
}

// split into 2 times the redistributeBatchSize
amount = oneSC.Div64(10)
if txns, _, err := w.Redistribute(cs, 2*redistributeBatchSize, amount, types.NewCurrency64(1), nil); err != nil {
t.Fatal(err)
} else if len(txns) != 2 {
t.Fatalf("unexpected number of txns, %v != 2", len(txns))
} else {
applyTxn(txns[0])
applyTxn(txns[1])
}

// assert number of outputs that hold 0.1SC
if cnt := numOutputsWithValue(amount); cnt != 2*redistributeBatchSize {
t.Fatalf("unexpected number of 0.1SC outputs, %v != 20", cnt)
}
}

func randomOutputID() (t types.Hash256) {
Expand Down

0 comments on commit 3fd61f6

Please sign in to comment.