From 63282baf3bb470a0aa68df5bd10a0352d212b015 Mon Sep 17 00:00:00 2001 From: envestcc Date: Tue, 23 Apr 2024 08:28:45 +0800 Subject: [PATCH 1/3] readState adapter to non-rolldpos --- api/coreservice.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/api/coreservice.go b/api/coreservice.go index 565e73747c..de884e3b1e 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -930,24 +930,22 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx)) rp := rolldpos.FindProtocol(core.registry) - if rp == nil { - return nil, uint64(0), errors.New("rolldpos is not registered") - } - - tipEpochNum := rp.GetEpochNum(tipHeight) - if height != "" { - inputHeight, err := strconv.ParseUint(height, 0, 64) - if err != nil { - return nil, uint64(0), err - } - inputEpochNum := rp.GetEpochNum(inputHeight) - if inputEpochNum < tipEpochNum { - // 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 { - core.readCache.Put(key.Hash(), d) + if rp != nil { + tipEpochNum := rp.GetEpochNum(tipHeight) + if height != "" { + inputHeight, err := strconv.ParseUint(height, 0, 64) + if err != nil { + return nil, uint64(0), err + } + inputEpochNum := rp.GetEpochNum(inputHeight) + if inputEpochNum < tipEpochNum { + // 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 { + core.readCache.Put(key.Hash(), d) + } + return d, h, err } - return d, h, err } } From 86f20e8b0631e24adb1ebaf2533c1d697821764e Mon Sep 17 00:00:00 2001 From: envestcc Date: Tue, 23 Apr 2024 08:51:49 +0800 Subject: [PATCH 2/3] refactor --- api/coreservice.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/api/coreservice.go b/api/coreservice.go index de884e3b1e..6db7abb897 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -929,26 +929,30 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei ) ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx)) - rp := rolldpos.FindProtocol(core.registry) - if rp != nil { - tipEpochNum := rp.GetEpochNum(tipHeight) - if height != "" { - inputHeight, err := strconv.ParseUint(height, 0, 64) - if err != nil { - return nil, uint64(0), err - } - inputEpochNum := rp.GetEpochNum(inputHeight) - if inputEpochNum < tipEpochNum { - // 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 { - core.readCache.Put(key.Hash(), d) + if height != "" { + inputHeight, err := strconv.ParseUint(height, 0, 64) + if err != nil { + return nil, uint64(0), err + } + rp := rolldpos.FindProtocol(core.registry) + if rp != nil { + tipEpochNum := rp.GetEpochNum(tipHeight) + if height != "" { + inputEpochNum := rp.GetEpochNum(inputHeight) + if inputEpochNum < tipEpochNum { + inputHeight = rp.GetEpochHeight(inputEpochNum) } - return d, h, err } } + if inputHeight < tipHeight { + // old data, wrap to history state reader + d, h, err := p.ReadState(ctx, factory.NewHistoryStateReader(core.sf, inputHeight), methodName, arguments...) + if err == nil { + core.readCache.Put(key.Hash(), d) + } + return d, h, err + } } - // TODO: need to distinguish user error and system error d, h, err := p.ReadState(ctx, core.sf, methodName, arguments...) if err == nil { From 30d27cce8ffab505787048ccfe75d57ea2ffdcd4 Mon Sep 17 00:00:00 2001 From: envestcc Date: Wed, 24 Apr 2024 20:05:33 +0800 Subject: [PATCH 3/3] address comments --- api/coreservice.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/api/coreservice.go b/api/coreservice.go index 6db7abb897..6eecc8fad8 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -937,11 +937,9 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei rp := rolldpos.FindProtocol(core.registry) if rp != nil { tipEpochNum := rp.GetEpochNum(tipHeight) - if height != "" { - inputEpochNum := rp.GetEpochNum(inputHeight) - if inputEpochNum < tipEpochNum { - inputHeight = rp.GetEpochHeight(inputEpochNum) - } + inputEpochNum := rp.GetEpochNum(inputHeight) + if inputEpochNum < tipEpochNum { + inputHeight = rp.GetEpochHeight(inputEpochNum) } } if inputHeight < tipHeight {