Skip to content

Commit

Permalink
Added target wallets selection
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Nov 28, 2023
1 parent 266b2bd commit 83a0779
Showing 1 changed file with 96 additions and 25 deletions.
121 changes: 96 additions & 25 deletions pkg/tbtc/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tbtc
import (
"context"
"fmt"
"math/big"
"runtime"
"sort"
"time"
Expand Down Expand Up @@ -569,7 +570,10 @@ func Initialize(

_ = chain.OnMovingFundsCommitmentInitiated(func(blockNumber uint64) {
go func() {
logger.Info("Moving funds commitment initiated at block %d", blockNumber)
logger.Info(
"Moving funds commitment initiated at block %d",
blockNumber,
)

events, err := chain.PastNewWalletRegisteredEvents(nil)
if err != nil {
Expand All @@ -580,23 +584,11 @@ func Initialize(
return
}

// Take the oldest first
sort.SliceStable(events, func(i, j int) bool {
return events[i].BlockNumber < events[j].BlockNumber
})

var walletPublicKeyHashes [][20]byte
for _, event := range events {
walletPublicKeyHashes = append(
walletPublicKeyHashes,
event.WalletPublicKeyHash,
)
}

movingFundsWallets := make(map[[20]byte]*WalletChainData)
liveWallets := make(map[[20]byte]*WalletChainData)
liveWallets := make([][20]byte, 0)

for _, walletPublicKeyHash := range walletPublicKeyHashes {
for _, event := range events {
walletPublicKeyHash := event.WalletPublicKeyHash
wallet, err := chain.GetWallet(walletPublicKeyHash)
if err != nil {
logger.Errorf(
Expand All @@ -611,22 +603,72 @@ func Initialize(
if wallet.State == StateMovingFunds {
movingFundsWallets[walletPublicKeyHash] = wallet
} else if wallet.State == StateLive {
liveWallets[walletPublicKeyHash] = wallet
liveWallets = append(liveWallets, walletPublicKeyHash)
}
}

// Sort the live wallets according to their numerical representation
// as the on-chain contract expects.
sort.Slice(liveWallets, func(i, j int) bool {
bigIntI := new(big.Int).SetBytes(liveWallets[i][:])
bigIntJ := new(big.Int).SetBytes(liveWallets[j][:])
return bigIntI.Cmp(bigIntJ) < 0
})

logger.Infof(
"found [%v] wallets with state MovingFunds and [%v] wallets "+
"with state Live [%v]",
len(movingFundsWallets),
len(liveWallets),
)

// TODO: Read `walletMaxBtcTransfer` and use it for target wallet
// selection.
_, _, _, _, _, _, _, _ = chain.GetWalletParameters()
// Make sure all the live wallets data has been retrieved by
// comparing the number of live wallets to the on-chain counter.
liveWalletsCount, err := chain.GetLiveWalletsCount()
if err != nil {
logger.Errorf("failed to get live wallets count: [%v]", err)
return
}

if len(liveWallets) != int(liveWalletsCount) {
logger.Errorf(
"mismatch between the number of retrieved live wallets "+
"and the on-chain live wallet count [%v:%v]",
len(liveWallets),
int(liveWalletsCount),
)
return
}

if len(liveWallets) == 0 {
logger.Infof(
"skipping moving funds commitments due to lack of live " +
"wallets",
)
return
}

_, _, _, _, _, walletMaxBtcTransfer, _, _ := chain.GetWalletParameters()

for walletPublicKeyHash, walletChainData := range movingFundsWallets {
logger.Infof(
"checking moving funds commitment eligibility for wallet "+
"with PKH [0x%x]",
walletPublicKeyHash,
)

_, found := node.walletRegistry.getWalletByPublicKeyHash(
walletPublicKeyHash,
)
if !found {
logger.Infof(
"skipping moving funds commitment for wallet with PKH"+
"[0x%x] as the node does not control it",
walletPublicKeyHash,
)
continue
}

for walletPublicKeyHash, wallet := range movingFundsWallets {
walletMainUtxo, err := DetermineWalletMainUtxo(
walletPublicKeyHash,
chain,
Expand All @@ -646,10 +688,10 @@ func Initialize(

// Check if the wallet meets the condition for moving funds
// commitment.
if wallet.State != StateMovingFunds ||
wallet.PendingRedemptionsValue > 0 ||
wallet.PendingMovedFundsSweepRequestsCount > 0 ||
wallet.MovingFundsTargetWalletsCommitmentHash != [32]byte{} ||
if walletChainData.State != StateMovingFunds ||
walletChainData.PendingRedemptionsValue > 0 ||
walletChainData.PendingMovedFundsSweepRequestsCount > 0 ||
walletChainData.MovingFundsTargetWalletsCommitmentHash != [32]byte{} ||
walletBalance <= 0 {
logger.Infof(
"skipping moving funds commitment for wallet with PKH "+
Expand All @@ -659,7 +701,36 @@ func Initialize(
continue
}

logger.Infof(
"proceeding with moving funds commitment for wallet with "+
"PKH [0x%x]",
walletPublicKeyHash,
)

ceilingDivide := func(x, y uint64) uint64 {
return (x + y - 1) / y
}
min := func(x, y uint64) uint64 {
if x < y {
return x
}
return y
}

targetWalletsCount := min(
uint64(liveWalletsCount),
ceilingDivide(uint64(walletBalance), walletMaxBtcTransfer),
)

targetWallets := liveWallets[0:targetWalletsCount]
logger.Infof("Target wallets length [%v]", len(targetWallets))

// TODO: Continue with moving funds commitment

logger.Infof(
"Finished moving funds commitment for wallet with PKH [0x%x]",
walletPublicKeyHash,
)
}
}()
})
Expand Down

0 comments on commit 83a0779

Please sign in to comment.