Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block-generator: Improve payment algorithm to allow sending many more transactions. #864

Merged
merged 9 commits into from
Feb 9, 2022
38 changes: 25 additions & 13 deletions cmd/block-generator/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ type generator struct {
config GenerationConfig

// payment transaction metadata
numPayments uint64
numPayments uint64
paymentOffset uint64

// Number of algorand accounts
numAccounts uint64
Expand Down Expand Up @@ -450,33 +451,44 @@ func (g *generator) generatePaymentTxn(round uint64, intra uint64) (transactions
return g.generatePaymentTxnInternal(selection.(TxTypeID), round, intra)
}

const minbal = uint64(100000)
winder marked this conversation as resolved.
Show resolved Hide resolved

func (g *generator) generatePaymentTxnInternal(selection TxTypeID, round uint64, intra uint64) (transactions.SignedTxn, transactions.ApplyData, error) {
defer g.recordData(track(selection))

// amounts
amount := minbal
fee := uint64(1000)
total := amount + fee

// Select a receiver
var receiveIndex uint64
switch selection {
case paymentTx:
receiveIndex = rand.Uint64() % g.numAccounts
case paymentAcctCreateTx:
// give new accounts extra algos for sending other transactions (optin/optout)
amount = minbal * 100
g.balances = append(g.balances, 0)
g.numAccounts++
receiveIndex = g.numAccounts - 1
receiveIndex = g.numAccounts
// increment at the end in case it needs to be referenced later.
defer func() {
g.numAccounts++
}()
}

// Always send from a genesis account.
sendIndex := g.numPayments % g.config.NumGenesisAccounts
// Select a sender from genesis account
sendIndex := (g.numPayments + g.paymentOffset) % g.config.NumGenesisAccounts
winder marked this conversation as resolved.
Show resolved Hide resolved
// if the genesis account has insufficient balance... start checking others
winder marked this conversation as resolved.
Show resolved Hide resolved
for n := uint64(1); g.balances[sendIndex] < (total + minbal); n++ {
g.paymentOffset++
sendIndex = (g.numPayments + g.paymentOffset) % g.numAccounts
fmt.Printf(" (%d) the sender account does not have enough algos for the transfer. idx %d, payment number %d\n", n, sendIndex, g.numPayments)
winder marked this conversation as resolved.
Show resolved Hide resolved
}

sender := indexToAccount(sendIndex)
receiver := indexToAccount(receiveIndex)

amount := uint64(1000000)
fee := uint64(1000)
total := amount + fee
if g.balances[sendIndex] < total {
fmt.Printf("\n\nthe sender account does not have enough algos for the transfer. idx %d, payment number %d\n\n", sendIndex, g.numPayments)
os.Exit(1)
}

g.balances[sendIndex] -= total
g.balances[receiveIndex] += amount

Expand Down