Skip to content

Commit 07a95ce

Browse files
authored
les/checkpointoracle: don't lookup checkpoint more than once per minute (#21285)
* les/checkpointoracle: don't lookup checkpoint more than once per second * les/checkpoint/oracle: change oracle checktime to 1 minute
1 parent 04c4e50 commit 07a95ce

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎les/checkpointoracle/oracle.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ package checkpointoracle
2121

2222
import (
2323
"encoding/binary"
24+
"sync"
2425
"sync/atomic"
26+
"time"
2527

2628
"github.com/ethereum/go-ethereum/accounts/abi/bind"
2729
"github.com/ethereum/go-ethereum/common"
@@ -40,6 +42,11 @@ type CheckpointOracle struct {
4042

4143
running int32 // Flag whether the contract backend is set or not
4244
getLocal func(uint64) params.TrustedCheckpoint // Function used to retrieve local checkpoint
45+
46+
checkMu sync.Mutex // Mutex to sync access to the fields below
47+
lastCheckTime time.Time // Time we last checked the checkpoint
48+
lastCheckPoint *params.TrustedCheckpoint // The last stable checkpoint
49+
lastCheckPointHeight uint64 // The height of last stable checkpoint
4350
}
4451

4552
// New creates a checkpoint oracle handler with given configs and callback.
@@ -88,6 +95,12 @@ func (oracle *CheckpointOracle) Contract() *checkpointoracle.CheckpointOracle {
8895
// StableCheckpoint returns the stable checkpoint which was generated by local
8996
// indexers and announced by trusted signers.
9097
func (oracle *CheckpointOracle) StableCheckpoint() (*params.TrustedCheckpoint, uint64) {
98+
oracle.checkMu.Lock()
99+
defer oracle.checkMu.Unlock()
100+
if time.Since(oracle.lastCheckTime) < 1*time.Minute {
101+
return oracle.lastCheckPoint, oracle.lastCheckPointHeight
102+
}
103+
// Look it up properly
91104
// Retrieve the latest checkpoint from the contract, abort if empty
92105
latest, hash, height, err := oracle.contract.Contract().GetLatestCheckpoint(nil)
93106
if err != nil || (latest == 0 && hash == [32]byte{}) {
@@ -103,6 +116,9 @@ func (oracle *CheckpointOracle) StableCheckpoint() (*params.TrustedCheckpoint, u
103116
//
104117
// In both cases, no stable checkpoint will be returned.
105118
if local.HashEqual(hash) {
119+
oracle.lastCheckTime = time.Now()
120+
oracle.lastCheckPointHeight = height.Uint64()
121+
oracle.lastCheckPoint = &local
106122
return &local, height.Uint64()
107123
}
108124
return nil, 0

0 commit comments

Comments
 (0)