Skip to content

Commit

Permalink
Fix bump_fee UTXO lookup and fix wrong change address
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Sep 11, 2024
1 parent 6901e7d commit 2385971
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
3 changes: 2 additions & 1 deletion app/appmessage/rpc_submit_transaction_replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func NewSubmitTransactionReplacementRequestMessage(transaction *RPCTransaction)
// its respective RPC message
type SubmitTransactionReplacementResponseMessage struct {
baseMessage
TransactionID string
TransactionID string
ReplacedTransaction *RPCTransaction

Error *RPCError
}
Expand Down
23 changes: 17 additions & 6 deletions cmd/kaspawallet/daemon/server/bump_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ func (s *server) BumpFee(_ context.Context, request *pb.BumpFeeRequest) (*pb.Bum
}

outpointsSet := make(map[externalapi.DomainOutpoint]struct{})
var maxUTXO *walletUTXO
for _, input := range domainTx.Inputs {
outpointsSet[input.PreviousOutpoint] = struct{}{}
}

var maxUTXO *walletUTXO
for _, utxo := range s.utxosSortedByAmount {
if _, ok := outpointsSet[*utxo.Outpoint]; !ok {
utxo, ok := s.mempoolExcludedUTXOs[input.PreviousOutpoint]
if !ok {
continue
}

Expand All @@ -52,6 +50,19 @@ func (s *server) BumpFee(_ context.Context, request *pb.BumpFeeRequest) (*pb.Bum
}
}

if maxUTXO == nil {
// If we got here it means for some reason s.mempoolExcludedUTXOs is not up to date and we need to search for the UTXOs in s.utxosSortedByAmount
for _, utxo := range s.utxosSortedByAmount {
if _, ok := outpointsSet[*utxo.Outpoint]; !ok {
continue
}

if maxUTXO == nil || utxo.UTXOEntry.Amount() > maxUTXO.UTXOEntry.Amount() {
maxUTXO = utxo
}
}
}

if maxUTXO == nil {
return nil, errors.Errorf("no UTXOs were found for transaction %s. This probably means the transaction is already accepted", request.TxID)
}
Expand Down Expand Up @@ -93,7 +104,7 @@ func (s *server) BumpFee(_ context.Context, request *pb.BumpFeeRequest) (*pb.Bum
Amount: spendValue,
}}
if changeSompi > 0 {
_, changeAddress, err := txscript.ExtractScriptPubKeyAddress(domainTx.Outputs[0].ScriptPublicKey, s.params)
changeAddress, _, err := s.changeAddress(request.UseExistingChangeAddress, fromAddresses)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/kaspawallet/daemon/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type server struct {

lock sync.RWMutex
utxosSortedByAmount []*walletUTXO
mempoolExcludedUTXOs map[externalapi.DomainOutpoint]*walletUTXO
nextSyncStartIndex uint32
keysFile *keys.File
shutdown chan struct{}
Expand Down Expand Up @@ -111,6 +112,7 @@ func Start(params *dagconfig.Params, listen, rpcServer string, keysFilePath stri
params: params,
coinbaseMaturity: coinbaseMaturity,
utxosSortedByAmount: []*walletUTXO{},
mempoolExcludedUTXOs: map[externalapi.DomainOutpoint]*walletUTXO{},
nextSyncStartIndex: 0,
keysFile: keysFile,
shutdown: make(chan struct{}),
Expand Down
22 changes: 16 additions & 6 deletions cmd/kaspawallet/daemon/server/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"

"github.com/kaspanet/kaspad/app/appmessage"
"github.com/pkg/errors"
Expand Down Expand Up @@ -240,11 +241,8 @@ func (s *server) updateUTXOSet(entries []*appmessage.UTXOsByAddressesEntry, memp
}
}

mempoolExcludedUTXOs := make(map[externalapi.DomainOutpoint]*walletUTXO)
for _, entry := range entries {
if _, ok := exclude[*entry.Outpoint]; ok {
continue
}

outpoint, err := appmessage.RPCOutpointToDomainOutpoint(entry.Outpoint)
if err != nil {
return err
Expand All @@ -260,18 +258,30 @@ func (s *server) updateUTXOSet(entries []*appmessage.UTXOsByAddressesEntry, memp
if !ok {
return errors.Errorf("Got result from address %s even though it wasn't requested", entry.Address)
}
utxos = append(utxos, &walletUTXO{

utxo := &walletUTXO{
Outpoint: outpoint,
UTXOEntry: utxoEntry,
address: address,
})
}

if _, ok := exclude[*entry.Outpoint]; ok {
mempoolExcludedUTXOs[*outpoint] = utxo
} else {
utxos = append(utxos, &walletUTXO{
Outpoint: outpoint,
UTXOEntry: utxoEntry,
address: address,
})
}
}

sort.Slice(utxos, func(i, j int) bool { return utxos[i].UTXOEntry.Amount() > utxos[j].UTXOEntry.Amount() })

s.lock.Lock()
s.startTimeOfLastCompletedRefresh = refreshStart
s.utxosSortedByAmount = utxos
s.mempoolExcludedUTXOs = mempoolExcludedUTXOs

// Cleanup expired used outpoints to avoid a memory leak
for outpoint, broadcastTime := range s.usedOutpoints {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ func (x *KaspadMessage_SubmitTransactionReplacementResponse) fromAppMessage(mess
err = &RPCError{Message: message.Error.Message}
}
x.SubmitTransactionReplacementResponse = &SubmitTransactionReplacementResponseMessage{
TransactionId: message.TransactionID,
Error: err,
TransactionId: message.TransactionID,
ReplacedTransaction: &RpcTransaction{},
Error: err,
}
if message.ReplacedTransaction != nil {
x.SubmitTransactionReplacementResponse.ReplacedTransaction.fromAppMessage(message.ReplacedTransaction)
}
return nil
}
Expand All @@ -61,8 +65,13 @@ func (x *SubmitTransactionReplacementResponseMessage) toAppMessage() (appmessage
if err != nil && !errors.Is(err, errorNil) {
return nil, err
}
replacedTx, err := x.ReplacedTransaction.toAppMessage()
if err != nil {
return nil, err
}
return &appmessage.SubmitTransactionReplacementResponseMessage{
TransactionID: x.TransactionId,
Error: rpcErr,
TransactionID: x.TransactionId,
ReplacedTransaction: replacedTx,
Error: rpcErr,
}, nil
}

0 comments on commit 2385971

Please sign in to comment.