Skip to content

Commit 21e2ccb

Browse files
committed
x
1 parent d474676 commit 21e2ccb

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

api/api.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"context"
1212
"encoding/hex"
13+
"fmt"
1314
"math"
1415
"math/big"
1516
"net"
@@ -162,11 +163,24 @@ func NewServer(
162163

163164
// GetAccount returns the metadata of an account
164165
func (api *Server) GetAccount(ctx context.Context, in *iotexapi.GetAccountRequest) (*iotexapi.GetAccountResponse, error) {
166+
height := uint64(0)
167+
if len(in.Address) > 41 {
168+
heightInt, _ := strconv.Atoi(in.Address[41:])
169+
height = uint64(heightInt)
170+
in.Address = in.Address[:41]
171+
}
172+
165173
if in.Address == address.RewardingPoolAddr || in.Address == address.StakingBucketPoolAddr {
166-
return api.getProtocolAccount(ctx, in.Address)
174+
return api.getProtocolAccount(ctx, height, in.Address)
167175
}
168176

169-
state, tipHeight, err := accountutil.AccountStateWithHeight(api.sf, in.Address)
177+
var sr protocol.StateReader = api.sf
178+
if height != 0 {
179+
sr = factory.NewHistoryStateReader(api.sf, height)
180+
}
181+
fmt.Println(height, in.Address)
182+
//factory.NewHistoryStateReader(api.sf, rp.GetEpochHeight(inputEpochNum)
183+
state, tipHeight, err := accountutil.AccountStateWithHeight(sr, in.Address)
170184
if err != nil {
171185
return nil, status.Error(codes.NotFound, err.Error())
172186
}
@@ -1574,7 +1588,7 @@ func (api *Server) getProductivityByEpoch(
15741588
return num, produce, nil
15751589
}
15761590

1577-
func (api *Server) getProtocolAccount(ctx context.Context, addr string) (ret *iotexapi.GetAccountResponse, err error) {
1591+
func (api *Server) getProtocolAccount(ctx context.Context, height uint64, addr string) (ret *iotexapi.GetAccountResponse, err error) {
15781592
var req *iotexapi.ReadStateRequest
15791593
var balance string
15801594
var out *iotexapi.ReadStateResponse
@@ -1583,6 +1597,7 @@ func (api *Server) getProtocolAccount(ctx context.Context, addr string) (ret *io
15831597
req = &iotexapi.ReadStateRequest{
15841598
ProtocolID: []byte("rewarding"),
15851599
MethodName: []byte("TotalBalance"),
1600+
Height: fmt.Sprintf("%d", height),
15861601
}
15871602
out, err = api.ReadState(ctx, req)
15881603
if err != nil {
@@ -1613,6 +1628,7 @@ func (api *Server) getProtocolAccount(ctx context.Context, addr string) (ret *io
16131628
ProtocolID: []byte("staking"),
16141629
MethodName: methodName,
16151630
Arguments: [][]byte{arg},
1631+
Height: fmt.Sprintf("%d", height),
16161632
}
16171633
out, err = api.ReadState(ctx, req)
16181634
if err != nil {
@@ -1625,12 +1641,24 @@ func (api *Server) getProtocolAccount(ctx context.Context, addr string) (ret *io
16251641
balance = acc.GetBalance()
16261642
}
16271643

1644+
header, err := api.bc.BlockHeaderByHeight(height)
1645+
if err != nil {
1646+
return nil, status.Error(codes.NotFound, err.Error())
1647+
}
1648+
hash := header.HashBlock()
1649+
//return &iotexapi.GetAccountResponse{AccountMeta: accountMeta, BlockIdentifier: &iotextypes.BlockIdentifier{
1650+
// Hash: hex.EncodeToString(hash[:]),
1651+
// Height: tipHeight,
1652+
//}}
16281653
ret = &iotexapi.GetAccountResponse{
16291654
AccountMeta: &iotextypes.AccountMeta{
16301655
Address: addr,
16311656
Balance: balance,
16321657
},
1633-
BlockIdentifier: out.GetBlockIdentifier(),
1658+
BlockIdentifier: &iotextypes.BlockIdentifier{
1659+
Hash: hex.EncodeToString(hash[:]),
1660+
Height: height,
1661+
},
16341662
}
16351663
return
16361664
}

0 commit comments

Comments
 (0)