Skip to content

Commit

Permalink
[api] Fix wrong cache for api readState at tip height (#4253)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Apr 26, 2024
1 parent 763969a commit 1110804
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions api/coreservice.go
Expand Up @@ -910,16 +910,19 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei
Method: methodName,
Args: arguments,
}
tipHeight := core.bc.TipHeight()
if height == "" {
key.Height = strconv.FormatUint(tipHeight, 10)
}
if d, ok := core.readCache.Get(key.Hash()); ok {
var h uint64
h := tipHeight
if height != "" {
h, _ = strconv.ParseUint(height, 0, 64)
}
return d, h, nil
}

// TODO: need to complete the context
tipHeight := core.bc.TipHeight()
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: tipHeight,
})
Expand All @@ -945,6 +948,7 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei
// old data, wrap to history state reader
d, h, err := p.ReadState(ctx, factory.NewHistoryStateReader(core.sf, rp.GetEpochHeight(inputEpochNum)), methodName, arguments...)
if err == nil {
key.Height = strconv.FormatUint(h, 10)
core.readCache.Put(key.Hash(), d)
}
return d, h, err
Expand All @@ -954,6 +958,7 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei
// TODO: need to distinguish user error and system error
d, h, err := p.ReadState(ctx, core.sf, methodName, arguments...)
if err == nil {
key.Height = strconv.FormatUint(h, 10)
core.readCache.Put(key.Hash(), d)
}
return d, h, err
Expand Down

0 comments on commit 1110804

Please sign in to comment.