2018-06-04 21:10:59 +00:00
|
|
|
// Package txpool handles incoming transactions for a sharded Ethereum blockchain.
|
|
|
|
package txpool
|
|
|
|
|
|
|
|
import (
|
2018-06-20 03:59:02 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-07-09 02:40:34 +00:00
|
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
2018-06-20 03:59:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-07-07 17:23:19 +00:00
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/p2p"
|
2018-07-10 02:27:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-06-04 21:10:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-13 12:44:33 +00:00
|
|
|
// TXPool handles a transaction pool for a sharded system.
|
|
|
|
type TXPool struct {
|
2018-06-20 03:59:02 +00:00
|
|
|
p2p *p2p.Server
|
|
|
|
transactionsFeed *event.Feed
|
|
|
|
ticker *time.Ticker
|
2018-06-05 21:28:57 +00:00
|
|
|
}
|
2018-06-04 21:10:59 +00:00
|
|
|
|
2018-06-13 12:44:33 +00:00
|
|
|
// NewTXPool creates a new observer instance.
|
|
|
|
func NewTXPool(p2p *p2p.Server) (*TXPool, error) {
|
2018-06-20 03:59:02 +00:00
|
|
|
return &TXPool{p2p: p2p, transactionsFeed: new(event.Feed)}, nil
|
2018-06-04 21:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the main routine for a shard transaction pool.
|
2018-06-13 12:44:33 +00:00
|
|
|
func (p *TXPool) Start() {
|
2018-06-04 21:10:59 +00:00
|
|
|
log.Info("Starting shard txpool service")
|
2018-06-20 03:59:02 +00:00
|
|
|
go p.sendTestTransaction()
|
2018-06-04 21:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the main loop for a transaction pool in the shard network.
|
2018-06-13 12:44:33 +00:00
|
|
|
func (p *TXPool) Stop() error {
|
2018-06-04 21:10:59 +00:00
|
|
|
log.Info("Stopping shard txpool service")
|
2018-06-20 03:59:02 +00:00
|
|
|
p.ticker.Stop()
|
2018-06-04 21:10:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
|
|
|
|
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)
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Infof("Sent transaction %x to %d subscribers", tx.Hash(), nsent)
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 02:40:34 +00:00
|
|
|
func createTestTransaction() *gethTypes.Transaction {
|
2018-06-20 03:59:02 +00:00
|
|
|
data := make([]byte, 1024)
|
|
|
|
rand.Read(data)
|
2018-07-09 02:40:34 +00:00
|
|
|
return gethTypes.NewTransaction(0, common.HexToAddress("0x0"), nil, 0, nil, data)
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|