Skip to content

Commit c625332

Browse files
committed
x
1 parent 194c665 commit c625332

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

api/api.go

+64-2
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,33 @@ func (api *Server) ReadState(ctx context.Context, in *iotexapi.ReadStateRequest)
487487
return &out, nil
488488
}
489489

490+
// ReadState reads state on blockchain
491+
func (api *Server) ReadState2(ctx context.Context, in *iotexapi.ReadStateRequest) (*iotexapi.ReadStateResponse, error) {
492+
p, ok := api.registry.Find(string(in.ProtocolID))
493+
if !ok {
494+
return nil, status.Errorf(codes.Internal, "protocol %s isn't registered", string(in.ProtocolID))
495+
}
496+
data, readStateHeight, err := api.readState2(ctx, p, in.GetHeight(), in.MethodName, in.Arguments...)
497+
if err != nil {
498+
return nil, status.Error(codes.NotFound, err.Error())
499+
}
500+
blkHash, err := api.dao.GetBlockHash(readStateHeight)
501+
if err != nil {
502+
if errors.Cause(err) == db.ErrNotExist {
503+
return nil, status.Error(codes.NotFound, err.Error())
504+
}
505+
return nil, status.Error(codes.Internal, err.Error())
506+
}
507+
out := iotexapi.ReadStateResponse{
508+
Data: data,
509+
BlockIdentifier: &iotextypes.BlockIdentifier{
510+
Height: readStateHeight,
511+
Hash: hex.EncodeToString(blkHash[:]),
512+
},
513+
}
514+
return &out, nil
515+
}
516+
490517
// SuggestGasPrice suggests gas price
491518
func (api *Server) SuggestGasPrice(ctx context.Context, in *iotexapi.SuggestGasPriceRequest) (*iotexapi.SuggestGasPriceResponse, error) {
492519
suggestPrice, err := api.gs.SuggestGasPrice()
@@ -953,6 +980,41 @@ func (api *Server) readState(ctx context.Context, p protocol.Protocol, height st
953980
return p.ReadState(ctx, api.sf, methodName, arguments...)
954981
}
955982

983+
func (api *Server) readState2(ctx context.Context, p protocol.Protocol, height string, methodName []byte, arguments ...[]byte) ([]byte, uint64, error) {
984+
// TODO: need to complete the context
985+
tipHeight := api.bc.TipHeight()
986+
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
987+
BlockHeight: tipHeight,
988+
})
989+
ctx = protocol.WithBlockchainCtx(
990+
protocol.WithRegistry(ctx, api.registry),
991+
protocol.BlockchainCtx{
992+
Genesis: api.cfg.Genesis,
993+
},
994+
)
995+
996+
rp := rolldpos.FindProtocol(api.registry)
997+
if rp == nil {
998+
return nil, uint64(0), errors.New("rolldpos is not registered")
999+
}
1000+
1001+
tipEpochNum := rp.GetEpochNum(tipHeight)
1002+
if height != "" {
1003+
inputHeight, err := strconv.ParseUint(height, 0, 64)
1004+
if err != nil {
1005+
return nil, uint64(0), err
1006+
}
1007+
inputEpochNum := rp.GetEpochNum(inputHeight)
1008+
if inputEpochNum < tipEpochNum {
1009+
// old data, wrap to history state reader
1010+
return p.ReadState(ctx, factory.NewHistoryStateReader(api.sf, inputHeight), methodName, arguments...)
1011+
}
1012+
}
1013+
1014+
// TODO: need to distinguish user error and system error
1015+
return p.ReadState(ctx, api.sf, methodName, arguments...)
1016+
}
1017+
9561018
func (api *Server) getActionsFromIndex(totalActions, start, count uint64) (*iotexapi.GetActionsResponse, error) {
9571019
var actionInfo []*iotexapi.ActionInfo
9581020
hashes, err := api.indexer.GetActionHashFromIndex(start, count)
@@ -1599,7 +1661,7 @@ func (api *Server) getProtocolAccount(ctx context.Context, height uint64, addr s
15991661
MethodName: []byte("TotalBalance"),
16001662
Height: fmt.Sprintf("%d", height),
16011663
}
1602-
out, err = api.ReadState(ctx, req)
1664+
out, err = api.ReadState2(ctx, req)
16031665
if err != nil {
16041666
return
16051667
}
@@ -1630,7 +1692,7 @@ func (api *Server) getProtocolAccount(ctx context.Context, height uint64, addr s
16301692
Arguments: [][]byte{arg},
16311693
Height: fmt.Sprintf("%d", height),
16321694
}
1633-
out, err = api.ReadState(ctx, req)
1695+
out, err = api.ReadState2(ctx, req)
16341696
if err != nil {
16351697
return nil, err
16361698
}

0 commit comments

Comments
 (0)