Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanvians committed Nov 23, 2023
1 parent 4e5f65b commit 891477b
Show file tree
Hide file tree
Showing 111 changed files with 2,273 additions and 1,170 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
uses: actions/checkout@v1

- name: Set up Go 1.19
uses: actions/setup-go@v3
uses: actions/setup-go@v1
with:
go-version: 1.19
id: go
Expand All @@ -27,16 +27,20 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}-v2
restore-keys: |
${{ runner.os }}-go-
- name: Install linter
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.53.3

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: go mod download

- name: Test and lint
- name: Lint
run: |
./run_tests.sh
export PATH=$PATH:~/go/bin
./bin/golangci-lint run --deadline=10m \
--disable-all --enable govet \
--enable staticcheck --enable gosimple \
--enable unconvert --enable ineffassign \
--enable revive --enable goimports \
--enable unparam
20 changes: 0 additions & 20 deletions .golangci.yml

This file was deleted.

8 changes: 4 additions & 4 deletions libwallet/assets/btc/rescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// SetBlocksRescanProgressListener sets the blocks rescan progress listener.
func (asset *Asset) SetBlocksRescanProgressListener(blocksRescanProgressListener *sharedW.BlocksRescanProgressListener) {
func (asset *Asset) SetBlocksRescanProgressListener(blocksRescanProgressListener sharedW.BlocksRescanProgressListener) {
asset.blocksRescanProgressListener = blocksRescanProgressListener
}

Expand Down Expand Up @@ -140,15 +140,15 @@ func (asset *Asset) rescanAsync() error {

// Attempt to drop the the tx history. See the btcwallet/cmd/dropwtxmgr app
// for more information. Because of how often a forces rescan will be triggered,
// dropping the transaction history in every one of those occasions won't make
// dropping the transaction history in every one of those ocassions won't make
// much difference. Its recommended that on the manually triggered rescan that
// is when dropping transaction history can be done.
log.Infof("(%v) Dropping transaction history to perform full rescan...", asset.GetWalletName())

err := w.DropTransactionHistory(asset.Internal().BTC.Database(), false)
if err != nil {
log.Errorf("Failed to drop wallet transaction history: %v", err)
// continue with the rescan despite the error occurring
// continue with the rescan despite the error occuring
}

log.Info("Starting wallet...")
Expand Down Expand Up @@ -396,7 +396,7 @@ func (asset *Asset) getblockStamp(height int32) (*waddrmgr.BlockStamp, error) {
// regardless thus avoid handling the possible scenario where btcwallet might miss
// the syncedto store trigger event.
func (asset *Asset) updateSyncedToBlock(height int32) {
// Ignore blocks notifications received during the wallet recovery phase.
// Ignore blocks notifications recieved during the wallet recovery phase.
if !asset.IsSynced() || asset.IsRescanning() {
return
}
Expand Down
22 changes: 8 additions & 14 deletions libwallet/assets/btc/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const (
// syncIntervalGap defines the interval at which to publish and log progress
// without unnecessarily spamming the receiver.
// without unnecessarily spamming the reciever.
syncIntervalGap = time.Second * 3

// start helps to synchronously execute compare-and-swap operation when
Expand Down Expand Up @@ -48,7 +48,7 @@ type SyncData struct {
wg sync.WaitGroup

// Listeners
syncProgressListeners map[string]*sharedW.SyncProgressListener
syncProgressListeners map[string]sharedW.SyncProgressListener

*activeSyncData
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (asset *Asset) resetSyncProgressData() {
}

// AddSyncProgressListener registers a sync progress listener to the asset.
func (asset *Asset) AddSyncProgressListener(syncProgressListener *sharedW.SyncProgressListener, uniqueIdentifier string) error {
func (asset *Asset) AddSyncProgressListener(syncProgressListener sharedW.SyncProgressListener, uniqueIdentifier string) error {
asset.syncData.mu.Lock()
defer asset.syncData.mu.Unlock()

Expand Down Expand Up @@ -194,9 +194,7 @@ func (asset *Asset) updateSyncProgress(rawBlockHeight int32) {

// publish the sync progress results to all listeners.
for _, listener := range asset.syncData.syncProgressListeners {
if listener.OnHeadersFetchProgress != nil {
listener.OnHeadersFetchProgress(&asset.syncData.headersFetchProgress)
}
listener.OnHeadersFetchProgress(&asset.syncData.headersFetchProgress)
}
}

Expand All @@ -212,9 +210,7 @@ func (asset *Asset) publishHeadersFetchComplete() {

func (asset *Asset) handleSyncUIUpdate() {
for _, listener := range asset.syncData.syncProgressListeners {
if listener.OnSyncCompleted != nil {
listener.OnSyncCompleted()
}
listener.OnSyncCompleted()
}
}

Expand Down Expand Up @@ -544,7 +540,7 @@ func (asset *Asset) IsConnectedToBitcoinNetwork() bool {
// startWallet initializes the *btcwallet.Wallet and its supporting players and
// starts syncing.
func (asset *Asset) startWallet() (err error) {
// If this is an imported wallet and address discovery has not been performed,
// If this is an imported wallet and address dicovery has not been performed,
// We want to set the assets birtday to the genesis block.
if asset.IsRestored && !asset.ContainsDiscoveredAccounts() {
asset.forceRescan()
Expand Down Expand Up @@ -611,15 +607,13 @@ func (asset *Asset) SpvSync() (err error) {
asset.syncData.mu.Unlock()

for _, listener := range asset.syncData.syncProgressListeners {
if listener.OnSyncStarted != nil {
listener.OnSyncStarted()
}
listener.OnSyncStarted()
}

go func() {
err = asset.startWallet()
if err != nil {
log.Warn("error occurred when starting BTC sync: ", err)
log.Warn("error occured when starting BTC sync: ", err)
}
}()

Expand Down
24 changes: 12 additions & 12 deletions libwallet/assets/btc/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
type txCache struct {
blockHeight int32

unminedTxs []*sharedW.Transaction
minedTxs []*sharedW.Transaction
unminedTxs []sharedW.Transaction
minedTxs []sharedW.Transaction

mu sync.RWMutex
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (asset *Asset) GetTransactionRaw(txHash string) (*sharedW.Transaction, erro
transactions, err := asset.getTransactionsRaw(0, 0, true)
for _, tx := range transactions {
if tx.Hash == txHash {
return tx, nil
return &tx, nil
}
}
return nil, err
Expand Down Expand Up @@ -104,7 +104,7 @@ func (asset *Asset) GetTransactions(offset, limit, txFilter int32, newestFirst b
// get all transactions then return transactions that match the input limit and offset.
// If offset and limit are 0, it will return all transactions
// If newestFirst is true, it will return transactions from newest to oldest
func (asset *Asset) GetTransactionsRaw(offset, limit, txFilter int32, newestFirst bool) ([]*sharedW.Transaction, error) {
func (asset *Asset) GetTransactionsRaw(offset, limit, txFilter int32, newestFirst bool) ([]sharedW.Transaction, error) {
if !asset.WalletOpened() {
return nil, utils.ErrBTCNotInitialized
}
Expand All @@ -120,7 +120,7 @@ func (asset *Asset) GetTransactionsRaw(offset, limit, txFilter int32, newestFirs
start := offset
end := offset + limit
if start >= totalTxs {
return []*sharedW.Transaction{}, nil
return []sharedW.Transaction{}, nil
}

if end >= totalTxs {
Expand All @@ -145,18 +145,18 @@ func (asset *Asset) btcSupportedTxFilter(txFilter int32) int32 {

// the offset is the height of start block
// limit is number of blocks will take from offset to get transactions
func (asset *Asset) filterTxs(offset, limit, txFilter int32, newestFirst bool) ([]*sharedW.Transaction, error) {
func (asset *Asset) filterTxs(offset, limit, txFilter int32, newestFirst bool) ([]sharedW.Transaction, error) {
txType := asset.btcSupportedTxFilter(txFilter)
transactions, err := asset.getTransactionsRaw(offset, limit, newestFirst)
if err != nil {
return []*sharedW.Transaction{}, nil
return []sharedW.Transaction{}, nil
}

if txType == txhelper.TxDirectionAll {
return transactions, err
}

txsCopy := make([]*sharedW.Transaction, 0, len(transactions))
txsCopy := make([]sharedW.Transaction, 0, len(transactions))
for _, tx := range transactions {
if tx.Direction == txType {
txsCopy = append(txsCopy, tx)
Expand All @@ -170,7 +170,7 @@ func (asset *Asset) filterTxs(offset, limit, txFilter int32, newestFirst bool) (
// of the offset and the limit values.
// If startblock is less that the endblock the list return is in ascending order
// (starts with the oldest) otherwise its in descending (starts with the newest) order.
func (asset *Asset) getTransactionsRaw(offset, limit int32, newestFirst bool) ([]*sharedW.Transaction, error) {
func (asset *Asset) getTransactionsRaw(offset, limit int32, newestFirst bool) ([]sharedW.Transaction, error) {
asset.txs.mu.RLock()
allTxs := append(asset.txs.unminedTxs, asset.txs.minedTxs...)
txCacheHeight := asset.txs.blockHeight
Expand Down Expand Up @@ -214,7 +214,7 @@ func (asset *Asset) getTransactionsRaw(offset, limit int32, newestFirst bool) ([
return nil, err
}

unminedTxs := make([]*sharedW.Transaction, 0)
unminedTxs := make([]sharedW.Transaction, 0)
for _, transaction := range txResult.UnminedTransactions {
unminedTx := asset.decodeTransactionWithTxSummary(sharedW.UnminedTxHeight, transaction)
unminedTxs = append(unminedTxs, unminedTx)
Expand Down Expand Up @@ -242,8 +242,8 @@ func (asset *Asset) getTransactionsRaw(offset, limit int32, newestFirst bool) ([
return append(unminedTxs, minedTxs...), nil
}

func (asset *Asset) extractTxs(blocks []wallet.Block) []*sharedW.Transaction {
txs := make([]*sharedW.Transaction, 0)
func (asset *Asset) extractTxs(blocks []wallet.Block) []sharedW.Transaction {
txs := make([]sharedW.Transaction, 0)
for _, block := range blocks {
for _, transaction := range block.Transactions {
decodedTx := asset.decodeTransactionWithTxSummary(block.Height, transaction)
Expand Down
42 changes: 19 additions & 23 deletions libwallet/assets/btc/txandblocknotifications.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package btc

import (
"encoding/json"
"sync/atomic"

"decred.org/dcrwallet/v3/errors"
Expand All @@ -25,7 +26,7 @@ notificationsLoop:
break notificationsLoop
}

txToCache := make([]*sharedW.Transaction, len(n.UnminedTransactions))
txToCache := make([]sharedW.Transaction, len(n.UnminedTransactions))

// handle txs hitting the mempool.
for i, tx := range n.UnminedTransactions {
Expand All @@ -35,8 +36,13 @@ notificationsLoop:
// decodeTxs
txToCache[i] = asset.decodeTransactionWithTxSummary(sharedW.UnminedTxHeight, tx)

// publish mempool tx.
asset.mempoolTransactionNotification(txToCache[i])
result, err := json.Marshal(txToCache[i])
if err != nil {
log.Error(err)
} else {
// publish mempool tx.
asset.mempoolTransactionNotification(string(result))
}
}

if len(n.UnminedTransactions) > 0 {
Expand Down Expand Up @@ -85,8 +91,8 @@ notificationsLoop:
// until all notification handlers finish processing the notification. If a
// notification handler were to try to access such features, it would result
// in a deadlock.
func (asset *Asset) AddTxAndBlockNotificationListener(txAndBlockNotificationListener *sharedW.TxAndBlockNotificationListener,
uniqueIdentifier string,
func (asset *Asset) AddTxAndBlockNotificationListener(txAndBlockNotificationListener sharedW.TxAndBlockNotificationListener,
async bool, uniqueIdentifier string,
) error {
asset.notificationListenersMu.Lock()
defer asset.notificationListenersMu.Unlock()
Expand All @@ -95,22 +101,12 @@ func (asset *Asset) AddTxAndBlockNotificationListener(txAndBlockNotificationList
return errors.New(utils.ErrListenerAlreadyExist)
}

asset.txAndBlockNotificationListeners[uniqueIdentifier] = &sharedW.TxAndBlockNotificationListener{
OnTransaction: func(transaction *sharedW.Transaction) {
if txAndBlockNotificationListener.OnTransaction != nil {
go txAndBlockNotificationListener.OnTransaction(transaction)
}
},
OnBlockAttached: func(walletID int, blockHeight int32) {
if txAndBlockNotificationListener.OnBlockAttached != nil {
go txAndBlockNotificationListener.OnBlockAttached(walletID, blockHeight)
}
},
OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) {
if txAndBlockNotificationListener.OnTransactionConfirmed != nil {
txAndBlockNotificationListener.OnTransactionConfirmed(walletID, hash, blockHeight)
}
},
if async {
asset.txAndBlockNotificationListeners[uniqueIdentifier] = &sharedW.AsyncTxAndBlockNotificationListener{
TxAndBlockNotificationListener: txAndBlockNotificationListener,
}
} else {
asset.txAndBlockNotificationListeners[uniqueIdentifier] = txAndBlockNotificationListener
}
return nil
}
Expand All @@ -125,7 +121,7 @@ func (asset *Asset) RemoveTxAndBlockNotificationListener(uniqueIdentifier string
}

// mempoolTransactionNotification publishes the txs that hit the mempool for the first time.
func (asset *Asset) mempoolTransactionNotification(transaction *sharedW.Transaction) {
func (asset *Asset) mempoolTransactionNotification(transaction string) {
asset.notificationListenersMu.RLock()
defer asset.notificationListenersMu.RUnlock()

Expand All @@ -146,7 +142,7 @@ func (asset *Asset) publishTransactionConfirmed(txHash string, blockHeight int32
}
}

// publishBlockAttached once the initial sync is complete all the new blocks received
// publishBlockAttached once the initial sync is complete all the new blocks recieved
// are published through this method.
func (asset *Asset) publishBlockAttached(blockHeight int32) {
asset.notificationListenersMu.RLock()
Expand Down
2 changes: 1 addition & 1 deletion libwallet/assets/btc/txauthor.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (asset *Asset) Broadcast(privatePassphrase, transactionLabel string) ([]byt
return nil, errors.New(utils.ErrInvalidPassphrase)
}

// To discourage fee sniping, LockTime is explicitly set in the raw tx.
// To discourage fee sniping, LockTime is explicity set in the raw tx.
// More documentation on this:
// https://bitcoin.stackexchange.com/questions/48384/why-bitcoin-core-creates-time-locked-transactions-by-default
msgTx.LockTime = uint32(asset.GetBestBlockHeight())
Expand Down
6 changes: 4 additions & 2 deletions libwallet/assets/btc/txparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/crypto-power/cryptopower/libwallet/txhelper"
)

func (asset *Asset) decodeTransactionWithTxSummary(blockheight int32, txsummary w.TransactionSummary) *sharedW.Transaction {
func (asset *Asset) decodeTransactionWithTxSummary(blockheight int32, txsummary w.TransactionSummary) sharedW.Transaction {
txHex := fmt.Sprintf("%x", txsummary.Transaction)
decodedTx, _ := asset.decodeTxHex(txHex)
txSize := decodedTx.SerializeSize()
Expand All @@ -28,7 +28,8 @@ func (asset *Asset) decodeTransactionWithTxSummary(blockheight int32, txsummary
outputs, totalOutputsAmount := asset.decodeTxOutputs(decodedTx, txsummary.MyOutputs)
amount, direction := txhelper.TransactionAmountAndDirection(totalInputsAmount, totalOutputsAmount, int64(txsummary.Fee))

return &sharedW.Transaction{
tx := sharedW.Transaction{
WalletID: asset.GetWalletID(),
Hash: txsummary.Hash.String(),
Type: txType,
Hex: txHex,
Expand All @@ -47,4 +48,5 @@ func (asset *Asset) decodeTransactionWithTxSummary(blockheight int32, txsummary
Inputs: inputs,
Outputs: outputs,
}
return tx
}
Loading

0 comments on commit 891477b

Please sign in to comment.