From 674cae12e67281ca2300a07e82dfe630f7b26252 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Wed, 12 Sep 2018 12:17:26 +0200 Subject: [PATCH] Fix BestHeader handling for Ethereum Classic --- bchain/coins/eth/ethrpc.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index ec3790a2..a182a6fd 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -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 }