plugin.go 1.55 KiB
package statusscreen_tps
import (
"strconv"
"sync/atomic"
"time"
"github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/plugins/statusscreen"
"github.com/iotaledger/goshimmer/plugins/tangle"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/node"
)
var receivedTpsCounter uint64
var solidTpsCounter uint64
var receivedTps uint64
var solidTps uint64
var PLUGIN = node.NewPlugin("Statusscreen TPS", node.Enabled, func(plugin *node.Plugin) {
gossip.Event.NewTransaction.Attach(events.NewClosure(func(_ *gossip.NewTransactionEvent) {
atomic.AddUint64(&receivedTpsCounter, 1)
}))
tangle.Events.TransactionSolid.Attach(events.NewClosure(func(_ *value_transaction.ValueTransaction) {
atomic.AddUint64(&solidTpsCounter, 1)
}))
statusscreen.AddHeaderInfo(func() (s string, s2 string) {
return "TPS", strconv.FormatUint(atomic.LoadUint64(&receivedTps), 10) + " received / " + strconv.FormatUint(atomic.LoadUint64(&solidTps), 10) + " new"
})
}, func(plugin *node.Plugin) {
daemon.BackgroundWorker("Statusscreen TPS Tracker", func() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-daemon.ShutdownSignal:
return
case <-ticker.C:
atomic.StoreUint64(&receivedTps, atomic.LoadUint64(&receivedTpsCounter))
atomic.StoreUint64(&solidTps, atomic.LoadUint64(&solidTpsCounter))
atomic.StoreUint64(&receivedTpsCounter, 0)
atomic.StoreUint64(&solidTpsCounter, 0)
}
}
})
})