aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-10-21 16:34:25 -0400
committerDeterminant <[email protected]>2019-10-21 16:34:25 -0400
commit190b04fcbcadf4807c5c2bb0ef9e528bbff62e32 (patch)
treed9302f7a9b877c3f6d7b5903d91a84a116337c53
parent7212dbd90f4cb8fe907617f804c8940db1ce657e (diff)
add NewTxPoolHeadEvent
-rw-r--r--core/events.go3
-rw-r--r--core/tx_pool.go8
-rw-r--r--coreth.go6
-rw-r--r--examples/counter/main.go10
-rw-r--r--examples/payments/main.go6
5 files changed, 28 insertions, 5 deletions
diff --git a/core/events.go b/core/events.go
index 09e9180..aa58d08 100644
--- a/core/events.go
+++ b/core/events.go
@@ -24,6 +24,9 @@ import (
// NewTxsEvent is posted when a batch of transactions enter the transaction pool.
type NewTxsEvent struct{ Txs []*types.Transaction }
+// NewTxPoolHeadEvent is posted when the pool head is updated.
+type NewTxPoolHeadEvent struct{ block *types.Block }
+
// PendingLogsEvent is posted pre mining and notifies of pending logs.
type PendingLogsEvent struct {
Logs []*types.Log
diff --git a/core/tx_pool.go b/core/tx_pool.go
index caabd5c..3454936 100644
--- a/core/tx_pool.go
+++ b/core/tx_pool.go
@@ -213,6 +213,7 @@ type TxPool struct {
chain blockChain
gasPrice *big.Int
txFeed event.Feed
+ headFeed event.Feed
scope event.SubscriptionScope
signer types.Signer
mu sync.RWMutex
@@ -328,6 +329,7 @@ func (pool *TxPool) loop() {
if ev.Block != nil {
pool.requestReset(head.Header(), ev.Block.Header())
head = ev.Block
+ pool.headFeed.Send(NewTxPoolHeadEvent{head})
}
// System shutdown.
@@ -398,6 +400,12 @@ func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- NewTxsEvent) event.Subscripti
return pool.scope.Track(pool.txFeed.Subscribe(ch))
}
+// SubscribeNewHeadEvent registers a subscription of NewHeadEvent and
+// starts sending event to the given channel.
+func (pool *TxPool) SubscribeNewHeadEvent(ch chan<- NewTxPoolHeadEvent) event.Subscription {
+ return pool.scope.Track(pool.headFeed.Subscribe(ch))
+}
+
// GasPrice returns the current gas price enforced by the transaction pool.
func (pool *TxPool) GasPrice() *big.Int {
pool.mu.RLock()
diff --git a/coreth.go b/coreth.go
index ca2a5de..8cc5f77 100644
--- a/coreth.go
+++ b/coreth.go
@@ -7,6 +7,7 @@ import (
"os"
"github.com/ava-labs/coreth/consensus/dummy"
+ "github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/coreth/miner"
"github.com/ava-labs/coreth/node"
@@ -180,10 +181,15 @@ func (self *ETHChain) AttachEthService(handler *rpc.Server, namespaces []string)
}
}
+// TODO: use SubscribeNewTxsEvent()
func (self *ETHChain) GetTxSubmitCh() <-chan struct{} {
return self.backend.GetTxSubmitCh()
}
+func (self *ETHChain) GetTxPool() *core.TxPool {
+ return self.backend.TxPool()
+}
+
type Key struct {
Address common.Address
PrivateKey *ecdsa.PrivateKey
diff --git a/examples/counter/main.go b/examples/counter/main.go
index e802a33..86e839a 100644
--- a/examples/counter/main.go
+++ b/examples/counter/main.go
@@ -6,11 +6,10 @@ import (
"encoding/json"
"fmt"
"github.com/ava-labs/coreth"
+ "github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/compiler"
- //"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core"
"github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/log"
@@ -94,6 +93,7 @@ func main() {
blockCount := 0
chain := coreth.NewETHChain(&config, nil, nil, nil)
+ newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1)
log.Info(chain.GetGenesisBlock().Hash().Hex())
firstBlock := false
var contractAddr common.Address
@@ -145,11 +145,15 @@ func main() {
if postGen(block) {
return nil
}
- chain.GenBlock()
+ go func() {
+ <-newTxPoolHeadChan
+ chain.GenBlock()
+ }()
return nil
})
// start the chain
+ chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan)
chain.Start()
_ = contract
diff --git a/examples/payments/main.go b/examples/payments/main.go
index 1eab70b..5076737 100644
--- a/examples/payments/main.go
+++ b/examples/payments/main.go
@@ -4,10 +4,10 @@ import (
"crypto/rand"
"fmt"
"github.com/ava-labs/coreth"
+ "github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core"
"github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/params"
@@ -83,11 +83,13 @@ func main() {
header.Extra = append(header.Extra, hid...)
})
newBlockChan := make(chan *types.Block)
+ newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1)
chain.SetOnSealFinish(func(block *types.Block) error {
newBlockChan <- block
return nil
})
+ chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan)
// start the chain
chain.Start()
for i := 0; i < 42; i++ {
@@ -98,7 +100,7 @@ func main() {
nonce++
chain.GenBlock()
block := <-newBlockChan
- chain.SetTail(block.Hash())
+ <-newTxPoolHeadChan
log.Info("finished generating block, starting the next iteration", "height", block.Number())
}
showBalance()