small fixes

This commit is contained in:
Ori Newman 2023-12-27 14:14:50 +02:00
parent 057cc85c8c
commit 265d76a393
5 changed files with 24 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package server
import ( import (
"context" "context"
"github.com/pkg/errors"
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb" "github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet" "github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet"
@ -14,13 +15,15 @@ func (s *server) GetBalance(_ context.Context, _ *pb.GetBalanceRequest) (*pb.Get
s.lock.RLock() s.lock.RLock()
defer s.lock.RUnlock() defer s.lock.RUnlock()
if !s.isSynced() {
return nil, errors.Errorf("wallet daemon is not synced yet, %s", s.formatSyncStateReport())
}
dagInfo, err := s.rpcClient.GetBlockDAGInfo() dagInfo, err := s.rpcClient.GetBlockDAGInfo()
if err != nil { if err != nil {
return nil, err return nil, err
} }
daaScore := dagInfo.VirtualDAAScore daaScore := dagInfo.VirtualDAAScore
maturity := s.params.BlockCoinbaseMaturity
balancesMap := make(balancesMapType, 0) balancesMap := make(balancesMapType, 0)
for _, entry := range s.utxosSortedByAmount { for _, entry := range s.utxosSortedByAmount {
amount := entry.UTXOEntry.Amount() amount := entry.UTXOEntry.Amount()
@ -30,7 +33,7 @@ func (s *server) GetBalance(_ context.Context, _ *pb.GetBalanceRequest) (*pb.Get
balances = new(balancesType) balances = new(balancesType)
balancesMap[address] = balances balancesMap[address] = balances
} }
if isUTXOSpendable(entry, daaScore, maturity) { if s.isUTXOSpendable(entry, daaScore) {
balances.available += amount balances.available += amount
} else { } else {
balances.pending += amount balances.pending += amount
@ -64,9 +67,9 @@ func (s *server) GetBalance(_ context.Context, _ *pb.GetBalanceRequest) (*pb.Get
}, nil }, nil
} }
func isUTXOSpendable(entry *walletUTXO, virtualDAAScore uint64, coinbaseMaturity uint64) bool { func (s *server) isUTXOSpendable(entry *walletUTXO, virtualDAAScore uint64) bool {
if !entry.UTXOEntry.IsCoinbase() { if !entry.UTXOEntry.IsCoinbase() {
return true return true
} }
return entry.UTXOEntry.BlockDAAScore()+coinbaseMaturity < virtualDAAScore return entry.UTXOEntry.BlockDAAScore()+s.coinbaseMaturity < virtualDAAScore
} }

View File

@ -98,14 +98,9 @@ func (s *server) selectUTXOs(spendAmount uint64, isSendAll bool, feePerInput uin
return nil, 0, 0, err return nil, 0, 0, err
} }
coinbaseMaturity := s.params.BlockCoinbaseMaturity
if dagInfo.NetworkName == "kaspa-testnet-11" {
coinbaseMaturity = 1000
}
for _, utxo := range s.utxosSortedByAmount { for _, utxo := range s.utxosSortedByAmount {
if (fromAddresses != nil && !walletAddressesContain(fromAddresses, utxo.address)) || if (fromAddresses != nil && !walletAddressesContain(fromAddresses, utxo.address)) ||
!isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, coinbaseMaturity) { !s.isUTXOSpendable(utxo, dagInfo.VirtualDAAScore) {
continue continue
} }

View File

@ -33,6 +33,7 @@ type server struct {
rpcClient *rpcclient.RPCClient // RPC client for ongoing user requests rpcClient *rpcclient.RPCClient // RPC client for ongoing user requests
backgroundRPCClient *rpcclient.RPCClient // RPC client dedicated for address and UTXO background fetching backgroundRPCClient *rpcclient.RPCClient // RPC client dedicated for address and UTXO background fetching
params *dagconfig.Params params *dagconfig.Params
coinbaseMaturity uint64 // Is different from default if we use testnet-11
lock sync.RWMutex lock sync.RWMutex
utxosSortedByAmount []*walletUTXO utxosSortedByAmount []*walletUTXO
@ -94,10 +95,21 @@ func Start(params *dagconfig.Params, listen, rpcServer string, keysFilePath stri
return err return err
} }
dagInfo, err := rpcClient.GetBlockDAGInfo()
if err != nil {
return nil
}
coinbaseMaturity := params.BlockCoinbaseMaturity
if dagInfo.NetworkName == "kaspa-testnet-11" {
coinbaseMaturity = 1000
}
serverInstance := &server{ serverInstance := &server{
rpcClient: rpcClient, rpcClient: rpcClient,
backgroundRPCClient: backgroundRPCClient, backgroundRPCClient: backgroundRPCClient,
params: params, params: params,
coinbaseMaturity: coinbaseMaturity,
utxosSortedByAmount: []*walletUTXO{}, utxosSortedByAmount: []*walletUTXO{},
nextSyncStartIndex: 0, nextSyncStartIndex: 0,
keysFile: keysFile, keysFile: keysFile,

View File

@ -264,7 +264,7 @@ func (s *server) moreUTXOsForMergeTransaction(alreadySelectedUTXOs []*libkaspawa
if _, ok := alreadySelectedUTXOsMap[*utxo.Outpoint]; ok { if _, ok := alreadySelectedUTXOsMap[*utxo.Outpoint]; ok {
continue continue
} }
if !isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, s.params.BlockCoinbaseMaturity) { if !s.isUTXOSpendable(utxo, dagInfo.VirtualDAAScore) {
continue continue
} }
additionalUTXOs = append(additionalUTXOs, &libkaspawallet.UTXO{ additionalUTXOs = append(additionalUTXOs, &libkaspawallet.UTXO{

View File

@ -285,9 +285,9 @@ func (s *server) updateUTXOSet(entries []*appmessage.UTXOsByAddressesEntry, memp
func (s *server) refreshUTXOs() error { func (s *server) refreshUTXOs() error {
refreshStart := time.Now() refreshStart := time.Now()
s.lock.Lock()
// No need to lock for reading since the only writer of this set is on `syncLoop` on the same goroutine.
addresses := s.addressSet.strings() addresses := s.addressSet.strings()
s.lock.Unlock()
// It's important to check the mempool before calling `GetUTXOsByAddresses`: // It's important to check the mempool before calling `GetUTXOsByAddresses`:
// If we would do it the other way around an output can be spent in the mempool // If we would do it the other way around an output can be spent in the mempool
// and not in consensus, and between the calls its spending transaction will be // and not in consensus, and between the calls its spending transaction will be