Fix BestHeader handling for Ethereum Classic
This commit is contained in:
parent
8bdf4b0ae3
commit
674cae12e6
@ -47,6 +47,7 @@ type EthereumRPC struct {
|
|||||||
Mempool *bchain.NonUTXOMempool
|
Mempool *bchain.NonUTXOMempool
|
||||||
bestHeaderMu sync.Mutex
|
bestHeaderMu sync.Mutex
|
||||||
bestHeader *ethtypes.Header
|
bestHeader *ethtypes.Header
|
||||||
|
bestHeaderTime time.Time
|
||||||
chanNewBlock chan *ethtypes.Header
|
chanNewBlock chan *ethtypes.Header
|
||||||
newBlockSubscription *rpc.ClientSubscription
|
newBlockSubscription *rpc.ClientSubscription
|
||||||
chanNewTx chan ethcommon.Hash
|
chanNewTx chan ethcommon.Hash
|
||||||
@ -95,6 +96,7 @@ func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.Notification
|
|||||||
// update best header to the new header
|
// update best header to the new header
|
||||||
s.bestHeaderMu.Lock()
|
s.bestHeaderMu.Lock()
|
||||||
s.bestHeader = h
|
s.bestHeader = h
|
||||||
|
s.bestHeaderTime = time.Now()
|
||||||
s.bestHeaderMu.Unlock()
|
s.bestHeaderMu.Unlock()
|
||||||
// notify blockbook
|
// notify blockbook
|
||||||
pushHandler(bchain.NotificationNewBlock)
|
pushHandler(bchain.NotificationNewBlock)
|
||||||
@ -187,7 +189,7 @@ func (b *EthereumRPC) Initialize() error {
|
|||||||
return nil
|
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 {
|
func (b *EthereumRPC) subscribe(f func() (*rpc.ClientSubscription, error)) error {
|
||||||
s, err := f()
|
s, err := f()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -273,6 +275,12 @@ func (b *EthereumRPC) GetBlockChainInfo() (string, error) {
|
|||||||
func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
|
func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
|
||||||
b.bestHeaderMu.Lock()
|
b.bestHeaderMu.Lock()
|
||||||
defer b.bestHeaderMu.Unlock()
|
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 {
|
if b.bestHeader == nil {
|
||||||
var err error
|
var err error
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
|
||||||
@ -281,6 +289,7 @@ func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
b.bestHeaderTime = time.Now()
|
||||||
}
|
}
|
||||||
return b.bestHeader, nil
|
return b.bestHeader, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user