Fix BestHeader handling for Ethereum Classic

pull/53/head v0.0.6
Martin Boehm 2018-09-12 12:17:26 +02:00
parent 8bdf4b0ae3
commit 674cae12e6
1 changed files with 10 additions and 1 deletions

View File

@ -47,6 +47,7 @@ type EthereumRPC struct {
Mempool *bchain.NonUTXOMempool
bestHeaderMu sync.Mutex
bestHeader *ethtypes.Header
bestHeaderTime time.Time
chanNewBlock chan *ethtypes.Header
newBlockSubscription *rpc.ClientSubscription
chanNewTx chan ethcommon.Hash
@ -95,6 +96,7 @@ func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.Notification
// update best header to the new header
s.bestHeaderMu.Lock()
s.bestHeader = h
s.bestHeaderTime = time.Now()
s.bestHeaderMu.Unlock()
// notify blockbook
pushHandler(bchain.NotificationNewBlock)
@ -187,7 +189,7 @@ func (b *EthereumRPC) Initialize() error {
return nil
}
// subscribeNewBlocks subscribes to new blocks notification
// subscribe subscribes notification and tries to resubscribe in case of error
func (b *EthereumRPC) subscribe(f func() (*rpc.ClientSubscription, error)) error {
s, err := f()
if err != nil {
@ -273,6 +275,12 @@ func (b *EthereumRPC) GetBlockChainInfo() (string, error) {
func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
b.bestHeaderMu.Lock()
defer b.bestHeaderMu.Unlock()
// ETC does not have newBlocks subscription, bestHeader must be updated very often (each 1 second)
if b.isETC {
if b.bestHeaderTime.Add(1 * time.Second).Before(time.Now()) {
b.bestHeader = nil
}
}
if b.bestHeader == nil {
var err error
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
@ -281,6 +289,7 @@ func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
if err != nil {
return nil, err
}
b.bestHeaderTime = time.Now()
}
return b.bestHeader, nil
}