Skip to content

Commit

Permalink
go/txsource/transfer: inlcude burn transactions in transfer workload
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Mar 20, 2020
1 parent 8c42399 commit 1e42253
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 58 deletions.
1 change: 1 addition & 0 deletions .changelog/2773.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/txsource/transfer: inlcude burn transactions in transfer workload
122 changes: 64 additions & 58 deletions go/oasis-node/cmd/debug/txsource/workload/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (

const (
// NameTransfer is the name of the transfer workload.
//
// Transfer workload continiously submits transfer and burn transactions.
NameTransfer = "transfer"

transferNumAccounts = 10
transferAmount = 1
transferFundInterval = 10
transferGasCost = 10
transferNumAccounts = 10
transferAmount = 1
transferBurnAmount = 10
)

var transferLogger = logging.GetLogger("cmd/txsource/workload/transfer")
Expand Down Expand Up @@ -60,8 +61,7 @@ func (transfer) Run(
// Read all the account info up front.
stakingClient := staking.NewStakingClient(conn)
for i := range accounts {
fundAmount := transferAmount*transferFundInterval + // funds for `transferFundInterval` transfers
transferGasCost*gasPrice*transferFundInterval // gas costs for `transferFundInterval` transfers
fundAmount := transferAmount // funds for for a transfer
if err = transferFunds(ctx, transferLogger, cnsc, fundingAccount, accounts[i].signer.Public(), int64(fundAmount)); err != nil {
return fmt.Errorf("workload/transfer: account funding failure: %w", err)
}
Expand All @@ -82,20 +82,10 @@ func (transfer) Run(
accounts[i].reckonedBalance = account.General.Balance
}

fee := transaction.Fee{
Gas: transferGasCost,
}
if err = fee.Amount.FromInt64(transferGasCost * gasPrice); err != nil {
return fmt.Errorf("Fee amount error: %w", err)
}

var minBalance quantity.Quantity
if err = minBalance.FromInt64(transferAmount); err != nil {
return fmt.Errorf("min balance FromInt64 %d: %w", transferAmount, err)
}
if err = minBalance.Add(&fee.Amount); err != nil {
return fmt.Errorf("min balance %v Add fee amount %v: %w", minBalance, fee.Amount, err)
}
for {
perm := rng.Perm(transferNumAccounts)
fromPermIdx := 0
Expand All @@ -107,54 +97,70 @@ func (transfer) Run(
if fromPermIdx >= transferNumAccounts {
return fmt.Errorf("all accounts %#v have gone broke", accounts)
}
toPermIdx := (fromPermIdx + 1) % transferNumAccounts
from := &accounts[perm[fromPermIdx]]
to := &accounts[perm[toPermIdx]]

transfer := staking.Transfer{
To: to.signer.Public(),
}
if err = transfer.Tokens.FromInt64(transferAmount); err != nil {
return fmt.Errorf("transfer tokens FromInt64 %d: %w", transferAmount, err)
}
tx := staking.NewTransferTx(from.reckonedNonce, &fee, &transfer)
signedTx, err := transaction.Sign(from.signer, tx)
if err != nil {
return fmt.Errorf("transaction.Sign: %w", err)
}
transferLogger.Debug("submitting transfer",
"from", from.signer.Public(),
"to", to.signer.Public(),
)
if err = cnsc.SubmitTx(ctx, signedTx); err != nil {
return fmt.Errorf("cnsc.SubmitTx: %w", err)
}
from.reckonedNonce++
if err = from.reckonedBalance.Sub(&fee.Amount); err != nil {
return fmt.Errorf("from reckoned balance %v Sub fee amount %v: %w", from.reckonedBalance, fee.Amount, err)
}
if err = from.reckonedBalance.Sub(&transfer.Tokens); err != nil {
return fmt.Errorf("from reckoned balance %v Sub transfer tokens %v: %w", from.reckonedBalance, transfer.Tokens, err)
}
if err = to.reckonedBalance.Add(&transfer.Tokens); err != nil {
return fmt.Errorf("to reckoned balance %v Add transfer tokens %v: %w", to.reckonedBalance, transfer.Tokens, err)
}
switch rng.Intn(2) {
case 0:
// do transfer tx.
toPermIdx := (fromPermIdx + 1) % transferNumAccounts
from := &accounts[perm[fromPermIdx]]
to := &accounts[perm[toPermIdx]]

transfer := staking.Transfer{To: to.signer.Public()}
if err = transfer.Tokens.FromInt64(transferAmount); err != nil {
return fmt.Errorf("transfer tokens FromInt64 %d: %w", transferAmount, err)
}
tx := staking.NewTransferTx(from.reckonedNonce, &transaction.Fee{}, &transfer)
from.reckonedNonce++

if from.reckonedNonce%transferFundInterval == 0 {
// Re-fund account for next `transferFundInterval` transfers.
fundAmount := transferGasCost * gasPrice * transferFundInterval // gas costs for `transferFundInterval` transfers.
if err = transferFunds(ctx, parallelLogger, cnsc, fundingAccount, from.signer.Public(), int64(fundAmount)); err != nil {
return fmt.Errorf("account funding failure: %w", err)
transferLogger.Debug("Transfering tokens",
"from", from,
"to", to,
"amount", transferAmount,
)
if err = fundSignAndSubmitTx(ctx, transferLogger, cnsc, from.signer, tx, fundingAccount); err != nil {
transferLogger.Error("failed to sign and submit transfer transaction",
"tx", tx,
"signer", from.signer,
)
return fmt.Errorf("failed to sign and submit tx: %w", err)
}
var fundAmountQ quantity.Quantity
if err = fundAmountQ.FromInt64(int64(fundAmount)); err != nil {
return fmt.Errorf("fundAmountQ FromInt64(%d): %w", fundAmount, err)

if err = from.reckonedBalance.Sub(&transfer.Tokens); err != nil {
return fmt.Errorf("from reckoned balance %v Sub transfer tokens %v: %w", from.reckonedBalance, transfer.Tokens, err)
}
if err = from.reckonedBalance.Add(&fundAmountQ); err != nil {
return fmt.Errorf("to reckoned balance %v Add fund amount %v: %w", to.reckonedBalance, fundAmountQ, err)
if err = to.reckonedBalance.Add(&transfer.Tokens); err != nil {
return fmt.Errorf("to reckoned balance %v Add transfer tokens %v: %w", to.reckonedBalance, transfer.Tokens, err)
}
}
case 1:
// do burn tx
from := &accounts[perm[fromPermIdx]]

// fund account with tokens that will be burned.
if err = transferFunds(ctx, transferLogger, cnsc, fundingAccount, from.signer.Public(), int64(transferBurnAmount)); err != nil {
return fmt.Errorf("workload/transfer: account funding failure: %w", err)
}

burn := staking.Burn{}
if err = burn.Tokens.FromInt64(transferBurnAmount); err != nil {
return fmt.Errorf("burn tokens FromInt64 %d: %w", transferBurnAmount, err)
}
tx := staking.NewBurnTx(from.reckonedNonce, &transaction.Fee{}, &burn)
from.reckonedNonce++

transferLogger.Debug("Burning tokens",
"account", from,
"amount", transferBurnAmount,
)
if err = fundSignAndSubmitTx(ctx, transferLogger, cnsc, from.signer, tx, fundingAccount); err != nil {
transferLogger.Error("failed to sign and submit transfer transaction",
"tx", tx,
"signer", from.signer,
)
return fmt.Errorf("failed to sign and submit tx: %w", err)
}
default:
return fmt.Errorf("unimplemented")
}
select {
case <-gracefulExit.Done():
transferLogger.Debug("time's up")
Expand Down

0 comments on commit 1e42253

Please sign in to comment.