Skip to content

Commit 4838ca3

Browse files
committed
Use logger methods with context
Signed-off-by: Takeshi Arabiki <takeshi.arabiki@datachain.jp>
1 parent b9c54d7 commit 4838ca3

File tree

7 files changed

+38
-37
lines changed

7 files changed

+38
-37
lines changed

module/fork_spec.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func FindTargetForkSpec(forkSpecs []*ForkSpec, height uint64, timestamp uint64)
209209

210210
var boundaryHeightCache = make(map[uint64]uint64)
211211

212-
func GetBoundaryHeight(headerFn getHeaderFn, currentHeight uint64, currentForkSpec ForkSpec) (*BoundaryHeight, error) {
212+
func GetBoundaryHeight(ctx context.Context, headerFn getHeaderFn, currentHeight uint64, currentForkSpec ForkSpec) (*BoundaryHeight, error) {
213213
logger := log.GetLogger()
214214
boundaryHeight := uint64(0)
215215
if condition, ok := currentForkSpec.GetHeightOrTimestamp().(*ForkSpec_Height); ok {
@@ -219,20 +219,20 @@ func GetBoundaryHeight(headerFn getHeaderFn, currentHeight uint64, currentForkSp
219219
if v, ok := boundaryHeightCache[ts]; ok {
220220
boundaryHeight = v
221221
} else {
222-
logger.Debug("seek fork height", "currentHeight", currentHeight, "ts", ts)
222+
logger.DebugContext(ctx, "seek fork height", "currentHeight", currentHeight, "ts", ts)
223223
for i := int64(currentHeight); i >= 0; i-- {
224-
h, err := headerFn(context.Background(), uint64(i))
224+
h, err := headerFn(ctx, uint64(i))
225225
if err != nil {
226226
return nil, err
227227
}
228228
if MilliTimestamp(h) == ts {
229229
boundaryHeight = h.Number.Uint64()
230-
logger.Debug("seek fork height found", "currentHeight", currentHeight, "ts", ts, "boundaryHeight", boundaryHeight)
230+
logger.DebugContext(ctx, "seek fork height found", "currentHeight", currentHeight, "ts", ts, "boundaryHeight", boundaryHeight)
231231
boundaryHeightCache[ts] = boundaryHeight
232232
break
233233
} else if MilliTimestamp(h) < ts {
234234
boundaryHeight = h.Number.Uint64() + 1
235-
logger.Debug("seek fork height found", "currentHeight", currentHeight, "ts", ts, "boundaryHeight", boundaryHeight)
235+
logger.DebugContext(ctx, "seek fork height found", "currentHeight", currentHeight, "ts", ts, "boundaryHeight", boundaryHeight)
236236
boundaryHeightCache[ts] = boundaryHeight
237237
break
238238
}

module/fork_spec_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (ts *ForkSpecTestSuite) Test_GetBoundaryHeight_ValidHeight() {
132132
currentHeight := uint64(100)
133133
currentForkSpec := ForkSpec{HeightOrTimestamp: &ForkSpec_Height{Height: 50}}
134134

135-
boundaryHeight, err := GetBoundaryHeight(headerFn, currentHeight, currentForkSpec)
135+
boundaryHeight, err := GetBoundaryHeight(context.Background(), headerFn, currentHeight, currentForkSpec)
136136

137137
ts.NoError(err)
138138
ts.Equal(uint64(50), boundaryHeight.Height)
@@ -145,7 +145,7 @@ func (ts *ForkSpecTestSuite) Test_GetBoundaryHeight_ValidTimestamp() {
145145
currentHeight := uint64(100)
146146
currentForkSpec := ForkSpec{HeightOrTimestamp: &ForkSpec_Timestamp{Timestamp: 1000 * 1000}} // msec
147147

148-
boundaryHeight, err := GetBoundaryHeight(headerFn, currentHeight, currentForkSpec)
148+
boundaryHeight, err := GetBoundaryHeight(context.Background(), headerFn, currentHeight, currentForkSpec)
149149

150150
ts.NoError(err)
151151
ts.Equal(uint64(100), boundaryHeight.Height)
@@ -158,7 +158,7 @@ func (ts *ForkSpecTestSuite) Test_GetBoundaryHeight_ValidTimestampMultiHeader()
158158
currentHeight := uint64(1100)
159159
currentForkSpec := ForkSpec{HeightOrTimestamp: &ForkSpec_Timestamp{Timestamp: 1000_000}} // msec
160160

161-
boundaryHeight, err := GetBoundaryHeight(headerFn, currentHeight, currentForkSpec)
161+
boundaryHeight, err := GetBoundaryHeight(context.Background(), headerFn, currentHeight, currentForkSpec)
162162

163163
ts.NoError(err)
164164
ts.Equal(uint64(1000), boundaryHeight.Height)
@@ -171,7 +171,7 @@ func (ts *ForkSpecTestSuite) Test_GetBoundaryHeight_ValidTimestampMultiHeaderNot
171171
currentHeight := uint64(1100)
172172
currentForkSpec := ForkSpec{HeightOrTimestamp: &ForkSpec_Timestamp{Timestamp: 999_999}} // msec
173173

174-
boundaryHeight, err := GetBoundaryHeight(headerFn, currentHeight, currentForkSpec)
174+
boundaryHeight, err := GetBoundaryHeight(context.Background(), headerFn, currentHeight, currentForkSpec)
175175

176176
ts.NoError(err)
177177
ts.Equal(uint64(1000), boundaryHeight.Height)
@@ -184,7 +184,7 @@ func (ts *ForkSpecTestSuite) Test_GetBoundaryHeight_TimestampNotFound() {
184184
currentHeight := uint64(100)
185185
currentForkSpec := ForkSpec{HeightOrTimestamp: &ForkSpec_Timestamp{Timestamp: 1000}}
186186

187-
boundaryHeight, err := GetBoundaryHeight(headerFn, currentHeight, currentForkSpec)
187+
boundaryHeight, err := GetBoundaryHeight(context.Background(), headerFn, currentHeight, currentForkSpec)
188188

189189
ts.NoError(err)
190190
ts.Equal(uint64(0), boundaryHeight.Height)
@@ -197,7 +197,7 @@ func (ts *ForkSpecTestSuite) Test_GetBoundaryHeight_HeaderFnError() {
197197
currentHeight := uint64(100)
198198
currentForkSpec := ForkSpec{HeightOrTimestamp: &ForkSpec_Timestamp{Timestamp: 1000}}
199199

200-
_, err := GetBoundaryHeight(headerFn, currentHeight, currentForkSpec)
200+
_, err := GetBoundaryHeight(context.Background(), headerFn, currentHeight, currentForkSpec)
201201

202202
ts.Error(err)
203203
}

module/header_query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func queryLatestFinalizedHeader(ctx context.Context, getHeader getHeaderFn, late
2727
}
2828
probablyFinalized := vote.Data.SourceNumber
2929

30-
logger.Debug("Try to seek verifying headers to finalize", "probablyFinalized", probablyFinalized, "latest", latestBlockNumber)
30+
logger.DebugContext(ctx, "Try to seek verifying headers to finalize", "probablyFinalized", probablyFinalized, "latest", latestBlockNumber)
3131

3232
headers, err := queryFinalizedHeader(ctx, getHeader, probablyFinalized, latestBlockNumber)
3333
if err != nil {
@@ -36,7 +36,7 @@ func queryLatestFinalizedHeader(ctx context.Context, getHeader getHeaderFn, late
3636
if headers != nil {
3737
return probablyFinalized, headers, nil
3838
}
39-
logger.Debug("Failed to seek verifying headers to finalize. So seek previous finalized header.", "probablyFinalized", probablyFinalized, "latest", latestBlockNumber)
39+
logger.DebugContext(ctx, "Failed to seek verifying headers to finalize. So seek previous finalized header.", "probablyFinalized", probablyFinalized, "latest", latestBlockNumber)
4040
}
4141
return 0, nil, fmt.Errorf("no finalized header found: %d", latestBlockNumber)
4242
}
@@ -67,7 +67,7 @@ func queryFinalizedHeader(ctx context.Context, fn getHeaderFn, height uint64, li
6767
}
6868
return append(ethHeaders, targetETHHeader, childETHHeader, grandChildETHHeader), nil
6969
}
70-
log.GetLogger().Debug("Insufficient verifying headers to finalize", "height", height, "limit", limitHeight)
70+
log.GetLogger().DebugContext(ctx, "Insufficient verifying headers to finalize", "height", height, "limit", limitHeight)
7171
return nil, nil
7272
}
7373

module/proof.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,19 @@ func withValidators(ctx context.Context, headerFn getHeaderFn, height uint64, et
188188
if err != nil {
189189
return nil, err
190190
}
191-
log.GetLogger().Debug("target fork spec", "currentForkSpec", currentForkSpec, "prevForkSpec", prevForkSpec)
191+
log.GetLogger().DebugContext(ctx, "target fork spec", "currentForkSpec", currentForkSpec, "prevForkSpec", prevForkSpec)
192192

193-
boundaryHeight, err := GetBoundaryHeight(headerFn, height, *currentForkSpec)
193+
boundaryHeight, err := GetBoundaryHeight(ctx, headerFn, height, *currentForkSpec)
194194
if err != nil {
195195
return nil, err
196196
}
197-
log.GetLogger().Debug("get boundary height by ", "height", height, "boundaryHeight", boundaryHeight)
197+
log.GetLogger().DebugContext(ctx, "get boundary height by ", "height", height, "boundaryHeight", boundaryHeight)
198198

199199
boundaryEpochs, err := boundaryHeight.GetBoundaryEpochs(*prevForkSpec)
200200
if err != nil {
201201
return nil, err
202202
}
203-
log.GetLogger().Debug("boundary epoch", "prevLast", boundaryEpochs.PrevLast, "currentFirst", boundaryEpochs.CurrentFirst, "intermediates", boundaryEpochs.Intermediates)
203+
log.GetLogger().DebugContext(ctx, "boundary epoch", "prevLast", boundaryEpochs.PrevLast, "currentFirst", boundaryEpochs.CurrentFirst, "intermediates", boundaryEpochs.Intermediates)
204204

205205
// Get validator set for verify headers
206206
currentEpoch := boundaryEpochs.CurrentEpochBlockNumber(height)

module/prover.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (pr *Prover) GetLatestFinalizedHeader(ctx context.Context) (out core.Header
8383
if err != nil {
8484
return nil, err
8585
}
86-
log.GetLogger().Debug("GetLatestFinalizedHeader", "finalized", header.GetHeight(), "latest", latestHeight)
86+
log.GetLogger().DebugContext(ctx, "GetLatestFinalizedHeader", "finalized", header.GetHeight(), "latest", latestHeight)
8787
return header, err
8888
}
8989

@@ -207,7 +207,7 @@ func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.Ch
207207
}
208208
threshold := durationMulByFraction(pr.config.GetTrustingPeriod(), pr.config.GetRefreshThresholdRate())
209209
if elapsedTime > threshold {
210-
log.GetLogger().Debug("needs refresh", "elapsedTime", elapsedTime, "threshold", threshold)
210+
log.GetLogger().DebugContext(ctx, "needs refresh", "elapsedTime", elapsedTime, "threshold", threshold)
211211
return true, nil
212212
}
213213

@@ -218,7 +218,7 @@ func (pr *Prover) CheckRefreshRequired(ctx context.Context, counterparty core.Ch
218218
}
219219
blockDiff := selfQueryHeight.GetRevisionHeight() - cs.GetLatestHeight().GetRevisionHeight()
220220
if blockDiff > blockDiffThreshold {
221-
log.GetLogger().Debug("needs refresh due to block diff",
221+
log.GetLogger().DebugContext(ctx, "needs refresh due to block diff",
222222
"chain", cpQueryHeight.GetRevisionHeight(),
223223
"cs", cs.GetLatestHeight().GetRevisionHeight(),
224224
"threshold", blockDiffThreshold)

module/setup.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ const skip = 100
1616
type queryVerifiableNeighboringEpochHeaderFn = func(context.Context, uint64, uint64) (core.Header, error)
1717

1818
func shouldSubmitBoundaryTimestampHeader(
19+
ctx context.Context,
1920
getHeader getHeaderFn,
2021
trustedBlockNumber uint64,
2122
latestFinalizedBlockNumber uint64,
2223
forkSpecs []*ForkSpec) (*uint64, uint64, error) {
2324

24-
trustedBlock, err := getHeader(context.Background(), trustedBlockNumber)
25+
trustedBlock, err := getHeader(ctx, trustedBlockNumber)
2526
if err != nil {
2627
return nil, 0, err
2728
}
28-
latestFinalizedBlock, err := getHeader(context.Background(), latestFinalizedBlockNumber)
29+
latestFinalizedBlock, err := getHeader(ctx, latestFinalizedBlockNumber)
2930
if err != nil {
3031
return nil, 0, err
3132
}
@@ -34,7 +35,7 @@ func shouldSubmitBoundaryTimestampHeader(
3435
latestCondition := latestForkSpec.GetHeightOrTimestamp()
3536
if x, ok := latestCondition.(*ForkSpec_Timestamp); ok {
3637
if MilliTimestamp(trustedBlock) < x.Timestamp && x.Timestamp < MilliTimestamp(latestFinalizedBlock) {
37-
boundaryHeight, err := GetBoundaryHeight(getHeader, latestFinalizedBlock.Number.Uint64(), *latestForkSpec)
38+
boundaryHeight, err := GetBoundaryHeight(ctx, getHeader, latestFinalizedBlock.Number.Uint64(), *latestForkSpec)
3839
if err != nil {
3940
return nil, 0, err
4041
}
@@ -44,7 +45,7 @@ func shouldSubmitBoundaryTimestampHeader(
4445
}
4546

4647
nextForkBoundaryHeightMinus1 := uint64(boundaryHeight.Height) - 1
47-
log.GetLogger().Info("ForkSpec height required", "ts", x.Timestamp, "height", boundaryHeight, "nextForkBoundaryHeightMinus1", nextForkBoundaryHeightMinus1)
48+
log.GetLogger().InfoContext(ctx, "ForkSpec height required", "ts", x.Timestamp, "height", boundaryHeight, "nextForkBoundaryHeightMinus1", nextForkBoundaryHeightMinus1)
4849
return &x.Timestamp, nextForkBoundaryHeightMinus1, nil
4950
}
5051
}
@@ -61,7 +62,7 @@ func setupHeadersForUpdate(
6162
forkSpecs []*ForkSpec,
6263
) ([]core.Header, error) {
6364
logger := log.GetLogger()
64-
logger.Debug("setupHeadersForUpdate start", "target", latestFinalizedHeader.GetHeight().GetRevisionHeight())
65+
logger.DebugContext(ctx, "setupHeadersForUpdate start", "target", latestFinalizedHeader.GetHeight().GetRevisionHeight())
6566
targetHeaders := make([]core.Header, 0)
6667

6768
// Needless to update already saved state
@@ -70,7 +71,7 @@ func setupHeadersForUpdate(
7071
}
7172
savedLatestHeight := clientStateLatestHeight.GetRevisionHeight()
7273

73-
trustedBlock, err := getHeader(context.Background(), savedLatestHeight)
74+
trustedBlock, err := getHeader(ctx, savedLatestHeight)
7475
if err != nil {
7576
return nil, err
7677
}
@@ -79,7 +80,7 @@ func setupHeadersForUpdate(
7980
if err != nil {
8081
return nil, err
8182
}
82-
trustedBoundaryHeight, err := GetBoundaryHeight(getHeader, savedLatestHeight, *trustedCurrentForkSpec)
83+
trustedBoundaryHeight, err := GetBoundaryHeight(ctx, getHeader, savedLatestHeight, *trustedCurrentForkSpec)
8384
if err != nil {
8485
return nil, err
8586
}
@@ -92,7 +93,7 @@ func setupHeadersForUpdate(
9293
latestFinalizedHeight := latestFinalizedHeader.GetHeight().GetRevisionHeight()
9394

9495
// If the condition is timestamp. we must submit the header with the timestamp
95-
nextForkBoundaryTs, nextForkBoundaryHeightMinus1, err := shouldSubmitBoundaryTimestampHeader(getHeader, savedLatestHeight, latestFinalizedHeader.GetHeight().GetRevisionHeight(), forkSpecs)
96+
nextForkBoundaryTs, nextForkBoundaryHeightMinus1, err := shouldSubmitBoundaryTimestampHeader(ctx, getHeader, savedLatestHeight, latestFinalizedHeader.GetHeight().GetRevisionHeight(), forkSpecs)
9697
if err != nil {
9798
return nil, err
9899
}
@@ -103,7 +104,7 @@ func setupHeadersForUpdate(
103104
}
104105

105106
submittingHeights := makeSubmittingHeights(latestFinalizedHeight, savedLatestHeight, firstUnsaved, nextForkBoundaryTs, nextForkBoundaryHeightMinus1)
106-
logger.Debug("submitting heights", "heights", submittingHeights, "trusted height", savedLatestHeight, "trusted epoch", trustedEpochHeight, "first unsaved", firstUnsaved)
107+
logger.DebugContext(ctx, "submitting heights", "heights", submittingHeights, "trusted height", savedLatestHeight, "trusted epoch", trustedEpochHeight, "first unsaved", firstUnsaved)
107108

108109
trustedHeight := clientStateLatestHeight.GetRevisionHeight()
109110
for _, submittingHeight := range submittingHeights {
@@ -112,14 +113,14 @@ func setupHeadersForUpdate(
112113
return nil, err
113114
}
114115
if verifiableHeader == nil {
115-
logger.Error("[FastFinalityError]", fmt.Errorf("insufficient vote attestation: submittingHeight=%d, trusted=%d", submittingHeight, trustedHeight))
116-
return withTrustedHeight(targetHeaders, clientStateLatestHeight), nil
116+
logger.ErrorContext(ctx, "[FastFinalityError]", fmt.Errorf("insufficient vote attestation: submittingHeight=%d, trusted=%d", submittingHeight, trustedHeight))
117+
return withTrustedHeight(ctx, targetHeaders, clientStateLatestHeight), nil
117118
}
118119
targetHeaders = append(targetHeaders, verifiableHeader)
119120
trustedHeight = submittingHeight
120-
logger.Debug("setup epoch header", "trusted", trustedHeight, "height", submittingHeight)
121+
logger.DebugContext(ctx, "setup epoch header", "trusted", trustedHeight, "height", submittingHeight)
121122
}
122-
return withTrustedHeight(append(targetHeaders, latestFinalizedHeader), clientStateLatestHeight), nil
123+
return withTrustedHeight(ctx, append(targetHeaders, latestFinalizedHeader), clientStateLatestHeight), nil
123124
}
124125

125126
// Get verifiable headers. This method must be executed at block intervals that do not miss any epochs.
@@ -132,7 +133,7 @@ func setupIntermediateHeader(
132133
return queryVerifiableHeader(ctx, submittingHeight, minUint64(submittingHeight+skip, latestHeight.GetRevisionHeight()))
133134
}
134135

135-
func withTrustedHeight(targetHeaders []core.Header, clientStateLatestHeight exported.Height) []core.Header {
136+
func withTrustedHeight(ctx context.Context, targetHeaders []core.Header, clientStateLatestHeight exported.Height) []core.Header {
136137
logger := log.GetLogger()
137138
for i, h := range targetHeaders {
138139
var trustedHeight clienttypes.Height
@@ -143,7 +144,7 @@ func withTrustedHeight(targetHeaders []core.Header, clientStateLatestHeight expo
143144
}
144145
h.(*Header).TrustedHeight = &trustedHeight
145146

146-
logger.Debug("setupHeadersForUpdate end", "target", h.GetHeight(), "trusted", trustedHeight, "headerSize", len(h.(*Header).Headers))
147+
logger.DebugContext(ctx, "setupHeadersForUpdate end", "target", h.GetHeight(), "trusted", trustedHeight, "headerSize", len(h.(*Header).Headers))
147148
}
148149
return targetHeaders
149150
}

tests/prover_network_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (ts *ProverNetworkTestSuite) TestSuccessCreateInitialLightClientState() {
9595
forkParams := module.GetForkParameters(module.Localnet)
9696
currentForkSpec, prevForkSpec, err := module.FindTargetForkSpec(forkParams, header.Number.Uint64(), module.MilliTimestamp(header))
9797
ts.Require().NoError(err)
98-
bs, err := module.GetBoundaryHeight(ts.chain.Header, header.Number.Uint64(), *currentForkSpec)
98+
bs, err := module.GetBoundaryHeight(ctx, ts.chain.Header, header.Number.Uint64(), *currentForkSpec)
9999
ts.Require().NoError(err)
100100
be, err := bs.GetBoundaryEpochs(*prevForkSpec)
101101
ts.Require().NoError(err)

0 commit comments

Comments
 (0)