Skip to content

Commit a3fa452

Browse files
authored
update systemlog indexer and evm transfer api (iotexproject#2098)
1 parent 11a4802 commit a3fa452

File tree

4 files changed

+105
-18
lines changed

4 files changed

+105
-18
lines changed

api/api.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"math/big"
1414
"net"
1515
"strconv"
16+
"strings"
1617
"time"
1718

1819
"github.com/golang/protobuf/proto"
@@ -749,11 +750,14 @@ func (api *Server) GetEvmTransfersByActionHash(ctx context.Context, in *iotexapi
749750

750751
actHash, err := hash.HexStringToHash256(in.ActionHash)
751752
if err != nil {
752-
return nil, status.Error(codes.Internal, err.Error())
753+
return nil, status.Error(codes.InvalidArgument, err.Error())
753754
}
754755

755756
transfers, err := api.systemLogIndexer.GetEvmTransfersByActionHash(actHash)
756757
if err != nil {
758+
if errors.Cause(err) == db.ErrNotExist {
759+
return nil, status.Error(codes.NotFound, "no such action with evm transfer")
760+
}
757761
return nil, status.Error(codes.Internal, err.Error())
758762
}
759763

@@ -766,8 +770,18 @@ func (api *Server) GetEvmTransfersByBlockHeight(ctx context.Context, in *iotexap
766770
return nil, status.Error(codes.Unavailable, "evm transfer index not supported")
767771
}
768772

773+
if in.BlockHeight < 1 {
774+
return nil, status.Errorf(codes.InvalidArgument, "invalid block height = %d", in.BlockHeight)
775+
}
776+
769777
transfers, err := api.systemLogIndexer.GetEvmTransfersByBlockHeight(in.BlockHeight)
770778
if err != nil {
779+
if errors.Cause(err) == db.ErrNotExist {
780+
return nil, status.Error(codes.NotFound, "no such block with evm transfer")
781+
}
782+
if strings.Contains(err.Error(), systemlog.ErrHeightNotReached.Error()) {
783+
return nil, status.Errorf(codes.InvalidArgument, "height = %d is higher than current height", in.BlockHeight)
784+
}
771785
return nil, status.Error(codes.Internal, err.Error())
772786
}
773787

api/api_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/pkg/errors"
2020
"github.com/stretchr/testify/assert"
2121
"github.com/stretchr/testify/require"
22+
"google.golang.org/grpc/codes"
23+
"google.golang.org/grpc/status"
2224

2325
"github.com/iotexproject/go-pkgs/hash"
2426
"github.com/iotexproject/iotex-election/test/mock/mock_committee"
@@ -1722,6 +1724,15 @@ func TestServer_GetEvmTransfersByActionHash(t *testing.T) {
17221724
svr, err := createServer(cfg, false)
17231725
require.NoError(err)
17241726

1727+
request := &iotexapi.GetEvmTransfersByActionHashRequest{
1728+
ActionHash: hex.EncodeToString(hash.ZeroHash256[:]),
1729+
}
1730+
_, err = svr.GetEvmTransfersByActionHash(context.Background(), request)
1731+
require.Error(err)
1732+
sta, ok := status.FromError(err)
1733+
require.Equal(true, ok)
1734+
require.Equal(codes.NotFound, sta.Code())
1735+
17251736
for _, test := range getEvmTransfersByActionHashTest {
17261737
request := &iotexapi.GetEvmTransfersByActionHashRequest{
17271738
ActionHash: hex.EncodeToString(test.actHash[:]),
@@ -1747,6 +1758,22 @@ func TestServer_GetEvmTransfersByBlockHeight(t *testing.T) {
17471758
svr, err := createServer(cfg, false)
17481759
require.NoError(err)
17491760

1761+
request := &iotexapi.GetEvmTransfersByBlockHeightRequest{
1762+
BlockHeight: 101,
1763+
}
1764+
_, err = svr.GetEvmTransfersByBlockHeight(context.Background(), request)
1765+
require.Error(err)
1766+
sta, ok := status.FromError(err)
1767+
require.Equal(true, ok)
1768+
require.Equal(codes.InvalidArgument, sta.Code())
1769+
1770+
request.BlockHeight = 2
1771+
_, err = svr.GetEvmTransfersByBlockHeight(context.Background(), request)
1772+
require.Error(err)
1773+
sta, ok = status.FromError(err)
1774+
require.Equal(true, ok)
1775+
require.Equal(codes.NotFound, sta.Code())
1776+
17501777
for _, test := range getEvmTransfersByBlockHeightTest {
17511778
request := &iotexapi.GetEvmTransfersByBlockHeightRequest{
17521779
BlockHeight: test.height,
@@ -2009,6 +2036,12 @@ func setupSystemLogIndexer(indexer *systemlog.Indexer) error {
20092036
if err != nil {
20102037
return err
20112038
}
2039+
emptyBlock, err := block.NewTestingBuilder().
2040+
SetHeight(2).
2041+
SignAndBuild(identityset.PrivateKey(30))
2042+
if err != nil {
2043+
return err
2044+
}
20122045
blk.Receipts = []*action.Receipt{testReceiptWithSystemLog}
20132046

20142047
ctx := context.Background()
@@ -2019,6 +2052,9 @@ func setupSystemLogIndexer(indexer *systemlog.Indexer) error {
20192052
if err := indexer.PutBlock(ctx, &blk); err != nil {
20202053
return err
20212054
}
2055+
if err := indexer.PutBlock(ctx, &emptyBlock); err != nil {
2056+
return err
2057+
}
20222058

20232059
return nil
20242060
}

systemlog/systemlog.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ var (
3535
actionHashPrefix = []byte("ah.")
3636
tipBlockHeightKey = []byte("tipHeight")
3737
)
38+
var (
39+
// ErrHeightNotReached defines the error when query's block height is higher than tip height
40+
ErrHeightNotReached = errors.New("query's block height is higher than tip height")
41+
)
3842

3943
// Indexer is the indexer for system log
4044
type Indexer struct {
@@ -188,19 +192,24 @@ func (x *Indexer) DeleteTipBlock(blk *block.Block) error {
188192
blockKey := blockKey(blk.Height())
189193
data, err := x.kvStore.Get(evmTransferNS, blockKey)
190194
if err != nil {
191-
return errors.Wrapf(err, "failed to get ActionHashList of block %d", height)
192-
}
193-
batch.Delete(evmTransferNS, blockKey, "failed to delete blockHeight -> ActionHashList")
195+
// there may be no blockEvmTransfer data if the block contains no evm transfers
196+
if errors.Cause(err) != db.ErrNotExist {
197+
return errors.Wrapf(err, "failed to get ActionHashList of block %d", height)
198+
}
199+
} else {
200+
batch.Delete(evmTransferNS, blockKey, "failed to delete blockHeight -> ActionHashList")
194201

195-
actionHashList := &systemlogpb.ActionHashList{}
196-
if err := proto.Unmarshal(data, actionHashList); err != nil {
197-
return errors.Wrap(err, "failed to deserialize ActionHashList")
198-
}
202+
actionHashList := &systemlogpb.ActionHashList{}
203+
if err := proto.Unmarshal(data, actionHashList); err != nil {
204+
return errors.Wrap(err, "failed to deserialize ActionHashList")
205+
}
199206

200-
for _, actionHash := range actionHashList.ActionHashList {
201-
actionKey := actionKey(hash.BytesToHash256(actionHash))
202-
batch.Delete(evmTransferNS, actionKey, "failed to delete actionHash -> EvmTransferList")
207+
for _, actionHash := range actionHashList.ActionHashList {
208+
actionKey := actionKey(hash.BytesToHash256(actionHash))
209+
batch.Delete(evmTransferNS, actionKey, "failed to delete actionHash -> EvmTransferList")
210+
}
203211
}
212+
204213
batch.Put(
205214
indexerStatus,
206215
tipBlockHeightKey,
@@ -233,6 +242,13 @@ func (x *Indexer) GetEvmTransfersByActionHash(actionHash hash.Hash256) (*iotexty
233242

234243
// GetEvmTransfersByBlockHeight queries evm transfers by block height
235244
func (x *Indexer) GetEvmTransfersByBlockHeight(blockHeight uint64) (*iotextypes.BlockEvmTransfer, error) {
245+
tipHeight, err := x.tipHeight()
246+
if err != nil {
247+
return nil, err
248+
}
249+
if tipHeight < blockHeight {
250+
return nil, ErrHeightNotReached
251+
}
236252
data, err := x.kvStore.Get(evmTransferNS, blockKey(blockHeight))
237253
if err != nil {
238254
return nil, errors.Wrap(err, "failed to get evm transfers by block height")

systemlog/systemlog_test.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"math/big"
1313
"testing"
1414

15+
"github.com/pkg/errors"
1516
"github.com/stretchr/testify/require"
1617

1718
"github.com/iotexproject/go-pkgs/hash"
@@ -71,6 +72,13 @@ func getTestBlocksAndExpected(t *testing.T) ([]*block.Block, []*iotextypes.Block
7172
SignAndBuild(identityset.PrivateKey(27))
7273
require.NoError(t, err)
7374

75+
blk3, err := block.NewTestingBuilder().
76+
SetHeight(3).
77+
SetPrevBlockHash(blk2.HashBlock()).
78+
SetTimeStamp(testutil.TimestampNow()).
79+
SignAndBuild(identityset.PrivateKey(27))
80+
require.NoError(t, err)
81+
7482
receipt1 := &action.Receipt{
7583
Status: 1,
7684
BlockHeight: blk1.Height(),
@@ -207,9 +215,13 @@ func getTestBlocksAndExpected(t *testing.T) ([]*block.Block, []*iotextypes.Block
207215
},
208216
},
209217
},
218+
{
219+
BlockHeight: blk3.Height(),
220+
NumEvmTransfers: 0,
221+
},
210222
}
211223

212-
return []*block.Block{&blk1, &blk2}, expectedEvmTransfers
224+
return []*block.Block{&blk1, &blk2, &blk3}, expectedEvmTransfers
213225
}
214226

215227
func TestSystemLogIndexer(t *testing.T) {
@@ -230,19 +242,28 @@ func TestSystemLogIndexer(t *testing.T) {
230242
}
231243
tipHeight, err = indexer.Height()
232244
r.NoError(err)
233-
r.Equal(blocks[1].Height(), tipHeight)
245+
r.Equal(uint64(len(blocks)), tipHeight)
234246

235247
for _, expectedBlock := range expectedList {
236248
actualBlock, err := indexer.GetEvmTransfersByBlockHeight(expectedBlock.BlockHeight)
237-
r.NoError(err)
238-
checkBlockEvmTransfers(t, expectedBlock, actualBlock)
239-
for _, expectedAction := range expectedBlock.ActionEvmTransfers {
240-
actualAction, err := indexer.GetEvmTransfersByActionHash(hash.BytesToHash256(expectedAction.ActionHash))
249+
if expectedBlock.NumEvmTransfers != 0 {
241250
r.NoError(err)
242-
checkActionEvmTransfers(t, expectedAction, actualAction)
251+
checkBlockEvmTransfers(t, expectedBlock, actualBlock)
252+
for _, expectedAction := range expectedBlock.ActionEvmTransfers {
253+
actualAction, err := indexer.GetEvmTransfersByActionHash(hash.BytesToHash256(expectedAction.ActionHash))
254+
r.NoError(err)
255+
checkActionEvmTransfers(t, expectedAction, actualAction)
256+
}
257+
} else {
258+
r.Equal(db.ErrNotExist, errors.Cause(err))
243259
}
244260
}
261+
_, err = indexer.GetEvmTransfersByBlockHeight(uint64((len(blocks))) + 1)
262+
r.Equal(ErrHeightNotReached, err)
263+
_, err = indexer.GetEvmTransfersByActionHash(hash.ZeroHash256)
264+
r.Equal(db.ErrNotExist, errors.Cause(err))
245265

266+
r.NoError(indexer.DeleteTipBlock(blocks[2]))
246267
r.NoError(indexer.DeleteTipBlock(blocks[1]))
247268
tipHeight, err = indexer.Height()
248269
r.NoError(err)

0 commit comments

Comments
 (0)