From 11108046d04021efad1b8c9fbd603c9f9f5cd6f1 Mon Sep 17 00:00:00 2001 From: Chen Chen <34592639+envestcc@users.noreply.github.com> Date: Fri, 26 Apr 2024 08:48:13 +0800 Subject: [PATCH] [api] Fix wrong cache for api readState at tip height (#4253) --- api/coreservice.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/coreservice.go b/api/coreservice.go index 565e73747c..87451fb99c 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -910,8 +910,12 @@ 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) } @@ -919,7 +923,6 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei } // TODO: need to complete the context - tipHeight := core.bc.TipHeight() ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{ BlockHeight: tipHeight, }) @@ -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 @@ -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