Skip to content

Commit

Permalink
chore(txs): remove utils file (#6162)
Browse files Browse the repository at this point in the history
## Motivation

Having a `utils` file is an anti-pattern. Removing it and putting the helper function next to its usage.
  • Loading branch information
acud committed Jul 19, 2024
1 parent c9726e3 commit 599b55b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 61 deletions.
52 changes: 52 additions & 0 deletions txs/conservative_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
Expand Down Expand Up @@ -221,3 +222,54 @@ func (cs *ConservativeState) GetTransactionsByAddress(
) ([]*types.MeshTransaction, error) {
return transactions.GetByAddress(cs.db, from, to, address)
}

// ShuffleWithNonceOrder perform a Fisher-Yates shuffle on the transactions.
// note that after shuffling, the original list of transactions are no longer in nonce order
// within the same principal. we simply check which principal occupies the spot after
// the shuffle and retrieve their transactions in nonce order.
func ShuffleWithNonceOrder(
logger *zap.Logger,
rng *rand.Rand,
numTXs int,
ntxs []*NanoTX,
byAddrAndNonce map[types.Address][]*NanoTX,
) []types.TransactionID {
rng.Shuffle(len(ntxs), func(i, j int) { ntxs[i], ntxs[j] = ntxs[j], ntxs[i] })
total := min(len(ntxs), numTXs)
result := make([]types.TransactionID, 0, total)
packed := make(map[types.Address][]uint64)
for _, ntx := range ntxs[:total] {
// if a spot is taken by a principal, we add its TX for the next eligible nonce
p := ntx.Principal
if _, ok := byAddrAndNonce[p]; !ok {
logger.Fatal("principal missing", zap.Stringer("address", p))
}
if len(byAddrAndNonce[p]) == 0 {
logger.Fatal("txs missing", zap.Stringer("address", p))
}
toAdd := byAddrAndNonce[p][0]
result = append(result, toAdd.ID)
if _, ok := packed[p]; !ok {
packed[p] = []uint64{toAdd.Nonce, toAdd.Nonce}
} else {
packed[p][1] = toAdd.Nonce
}
if len(byAddrAndNonce[p]) == 1 {
delete(byAddrAndNonce, p)
} else {
byAddrAndNonce[p] = byAddrAndNonce[p][1:]
}
}
logger.Debug("packed txs", zap.Array("ranges", zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
for addr, nonces := range packed {
_ = encoder.AppendObject(zapcore.ObjectMarshalerFunc(func(encoder zapcore.ObjectEncoder) error {
encoder.AddString("addr", addr.String())
encoder.AddUint64("from", nonces[0])
encoder.AddUint64("to", nonces[1])
return nil
}))
}
return nil
})))
return result
}
61 changes: 0 additions & 61 deletions txs/utils.go

This file was deleted.

0 comments on commit 599b55b

Please sign in to comment.