@@ -21,7 +21,9 @@ package checkpointoracle
2121
2222import (
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.
9097func (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