Skip to content
Snippets Groups Projects
Unverified Commit 65a91713 authored by Luca Moser's avatar Luca Moser Committed by GitHub
Browse files

removes sent counter as value from spammed transactions (#163)

parent b553b432
No related branches found
No related tags found
No related merge requests found
package transactionspammer package transactionspammer
import ( import (
"strings"
"sync" "sync"
"time" "time"
"github.com/iotaledger/goshimmer/packages/shutdown"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/packages/gossip" "github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/meta_transaction"
"github.com/iotaledger/goshimmer/packages/model/value_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/packages/shutdown"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/tipselection" "github.com/iotaledger/goshimmer/plugins/tipselection"
"github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
) )
const logEveryNTransactions = 5000
var log *logger.Logger var log *logger.Logger
var spamming = false var spamming = false
...@@ -23,14 +25,14 @@ var spammingMutex sync.Mutex ...@@ -23,14 +25,14 @@ var spammingMutex sync.Mutex
var shutdownSignal chan struct{} var shutdownSignal chan struct{}
var done chan struct{} var done chan struct{}
var sentCounter = uint(0)
func init() { func init() {
shutdownSignal = make(chan struct{}) shutdownSignal = make(chan struct{})
done = make(chan struct{}) done = make(chan struct{})
} }
func Start(tps uint) { var targetAddress = strings.Repeat("SPAMMMMER", 9)
func Start(tps uint64) {
log = logger.NewLogger("Transaction Spammer") log = logger.NewLogger("Transaction Spammer")
spammingMutex.Lock() spammingMutex.Lock()
spamming = true spamming = true
...@@ -38,8 +40,11 @@ func Start(tps uint) { ...@@ -38,8 +40,11 @@ func Start(tps uint) {
daemon.BackgroundWorker("Transaction Spammer", func(daemonShutdownSignal <-chan struct{}) { daemon.BackgroundWorker("Transaction Spammer", func(daemonShutdownSignal <-chan struct{}) {
start := time.Now() start := time.Now()
totalSentCounter := int64(0)
var totalSentCounter, currentSentCounter uint64
log.Infof("started spammer...will output sent count every %d transactions", logEveryNTransactions)
defer log.Infof("spammer stopped, spammed %d transactions", totalSentCounter)
for { for {
select { select {
case <-daemonShutdownSignal: case <-daemonShutdownSignal:
...@@ -50,13 +55,13 @@ func Start(tps uint) { ...@@ -50,13 +55,13 @@ func Start(tps uint) {
return return
default: default:
sentCounter++ currentSentCounter++
totalSentCounter++ totalSentCounter++
tx := value_transaction.New() tx := value_transaction.New()
tx.SetHead(true) tx.SetHead(true)
tx.SetTail(true) tx.SetTail(true)
tx.SetValue(totalSentCounter) tx.SetAddress(targetAddress)
tx.SetBranchTransactionHash(tipselection.GetRandomTip()) tx.SetBranchTransactionHash(tipselection.GetRandomTip())
tx.SetTrunkTransactionHash(tipselection.GetRandomTip()) tx.SetTrunkTransactionHash(tipselection.GetRandomTip())
tx.SetTimestamp(uint(time.Now().Unix())) tx.SetTimestamp(uint(time.Now().Unix()))
...@@ -67,7 +72,12 @@ func Start(tps uint) { ...@@ -67,7 +72,12 @@ func Start(tps uint) {
gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.GetInstance().Peer}) gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.GetInstance().Peer})
if sentCounter >= tps { if totalSentCounter%logEveryNTransactions == 0 {
log.Infof("spammed %d transactions", totalSentCounter)
}
// rate limit to the specified TPS
if currentSentCounter >= tps {
duration := time.Since(start) duration := time.Since(start)
if duration < time.Second { if duration < time.Second {
...@@ -75,8 +85,7 @@ func Start(tps uint) { ...@@ -75,8 +85,7 @@ func Start(tps uint) {
} }
start = time.Now() start = time.Now()
currentSentCounter = 0
sentCounter = 0
} }
} }
} }
......
...@@ -61,5 +61,5 @@ type Response struct { ...@@ -61,5 +61,5 @@ type Response struct {
type Request struct { type Request struct {
Cmd string `json:"cmd"` Cmd string `json:"cmd"`
Tps uint `json:"tps"` Tps uint64 `json:"tps"`
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment