tx-spammer/pkg/auto/tx_watcher.go
Thomas E Lackey d4436948ac
Update for testing with Merge fixturenet. (#11)
* WIP: Update for testing with Merge fixturenet.

* Switch channel to take a *Transaction rather than []byte.  This allows us to access the TX fields (eg, for logging) throughout its lifecycle.

* Tweak message

* Add code to check for TXs being put into blocks.

* 0 == unlimited

* Use -1, not 0, for unlimited.

* golang 1.19

* Read contract from file.
2022-11-02 21:33:18 -05:00

61 lines
1.6 KiB
Go

package auto
import (
"context"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/sirupsen/logrus"
"time"
)
type TxWatcher struct {
PendingTxCh chan *types.Transaction
ethClient *ethclient.Client
quitCh chan bool
startedAt time.Time
counter uint
}
func NewTxWatcher(ethClient *ethclient.Client) *TxWatcher {
return &TxWatcher{
PendingTxCh: make(chan *types.Transaction, 2500),
ethClient: ethClient,
quitCh: make(chan bool),
}
}
func (tw *TxWatcher) Start() {
tw.startedAt = time.Now()
go func() {
defer close(tw.PendingTxCh)
for {
select {
case tx := <-tw.PendingTxCh:
tw.counter += 1
if 0 == tw.counter%50 {
logrus.Debugf("TxW: checking on TX %s (%d in channel)", tx.Hash().Hex(), len(tw.PendingTxCh))
var receipt *types.Receipt = nil
sleep := time.Millisecond
start := time.Now()
for receipt == nil {
receipt, _ = tw.ethClient.TransactionReceipt(context.Background(), tx.Hash())
if nil == receipt {
time.Sleep(sleep)
sleep *= 2
} else {
elapsed := time.Now().Sub(tw.startedAt)
logrus.Debugf("TxW: TX %s found in block %s after %dms.", tx.Hash().Hex(),
receipt.BlockNumber.String(), time.Now().Sub(start).Milliseconds())
logrus.Infof("TxW: %d in %.0f seconds (%.2f/sec, %d pending)",
tw.counter, elapsed.Seconds(), float64(tw.counter)/elapsed.Seconds(), len(tw.PendingTxCh))
}
}
}
case <-tw.quitCh:
logrus.Infof("TxW: quitting with %d in channel", len(tw.PendingTxCh))
return
}
}
}()
}