prysm-pulse/sharding/txpool/service.go
Yutaro Mori 346b9ae8aa Random transaction generator (#171)
sharding: random transaction generator
Former-commit-id: b41891c474372ab54cbf1e3fa34dc7399f42c6d4 [formerly 69b7da173694490fa44748fe3cb3b2aa84f36b46]
Former-commit-id: b642be5e1b7b4aad59011983bacda4a9b1166463
2018-06-19 20:59:02 -07:00

62 lines
1.7 KiB
Go

// Package txpool handles incoming transactions for a sharded Ethereum blockchain.
package txpool
import (
"crypto/rand"
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding/p2p"
)
// TXPool handles a transaction pool for a sharded system.
type TXPool struct {
p2p *p2p.Server
transactionsFeed *event.Feed
ticker *time.Ticker
}
// NewTXPool creates a new observer instance.
func NewTXPool(p2p *p2p.Server) (*TXPool, error) {
return &TXPool{p2p: p2p, transactionsFeed: new(event.Feed)}, nil
}
// Start the main routine for a shard transaction pool.
func (p *TXPool) Start() {
log.Info("Starting shard txpool service")
go p.sendTestTransaction()
}
// Stop the main loop for a transaction pool in the shard network.
func (p *TXPool) Stop() error {
log.Info("Stopping shard txpool service")
p.ticker.Stop()
return nil
}
func (p *TXPool) TransactionsFeed() *event.Feed {
return p.transactionsFeed
}
// sendTestTransaction sends a transaction with random bytes over a 5 second interval.
// This method is for testing purposes only, and will be replaced by a more functional CLI tool.
func (p *TXPool) sendTestTransaction() {
p.ticker = time.NewTicker(5 * time.Second)
for range p.ticker.C {
tx := createTestTransaction()
nsent := p.transactionsFeed.Send(tx)
log.Info(fmt.Sprintf("Sent transaction %x to %d subscribers", tx.Hash(), nsent))
}
}
func createTestTransaction() *types.Transaction {
data := make([]byte, 1024)
rand.Read(data)
return types.NewTransaction(0, common.HexToAddress("0x0"), nil, 0, nil, data)
}