@@ -487,6 +487,33 @@ func (api *Server) ReadState(ctx context.Context, in *iotexapi.ReadStateRequest)
487
487
return & out , nil
488
488
}
489
489
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
+
490
517
// SuggestGasPrice suggests gas price
491
518
func (api * Server ) SuggestGasPrice (ctx context.Context , in * iotexapi.SuggestGasPriceRequest ) (* iotexapi.SuggestGasPriceResponse , error ) {
492
519
suggestPrice , err := api .gs .SuggestGasPrice ()
@@ -953,6 +980,41 @@ func (api *Server) readState(ctx context.Context, p protocol.Protocol, height st
953
980
return p .ReadState (ctx , api .sf , methodName , arguments ... )
954
981
}
955
982
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
+
956
1018
func (api * Server ) getActionsFromIndex (totalActions , start , count uint64 ) (* iotexapi.GetActionsResponse , error ) {
957
1019
var actionInfo []* iotexapi.ActionInfo
958
1020
hashes , err := api .indexer .GetActionHashFromIndex (start , count )
@@ -1599,7 +1661,7 @@ func (api *Server) getProtocolAccount(ctx context.Context, height uint64, addr s
1599
1661
MethodName : []byte ("TotalBalance" ),
1600
1662
Height : fmt .Sprintf ("%d" , height ),
1601
1663
}
1602
- out , err = api .ReadState (ctx , req )
1664
+ out , err = api .ReadState2 (ctx , req )
1603
1665
if err != nil {
1604
1666
return
1605
1667
}
@@ -1630,7 +1692,7 @@ func (api *Server) getProtocolAccount(ctx context.Context, height uint64, addr s
1630
1692
Arguments : [][]byte {arg },
1631
1693
Height : fmt .Sprintf ("%d" , height ),
1632
1694
}
1633
- out , err = api .ReadState (ctx , req )
1695
+ out , err = api .ReadState2 (ctx , req )
1634
1696
if err != nil {
1635
1697
return nil , err
1636
1698
}
0 commit comments