diff --git a/api/coreservice.go b/api/coreservice.go index 24527e0d8b..6a451e9ff4 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -1157,7 +1157,6 @@ func (core *coreService) actionsInBlock(blk *block.Block, start, count uint64) [ h := blk.HashBlock() blkHash := hex.EncodeToString(h[:]) blkHeight := blk.Height() - ts := blk.Header.BlockHeaderCoreProto().Timestamp lastAction := start + count if count == math.MaxUint64 { @@ -1175,14 +1174,21 @@ func (core *coreService) actionsInBlock(blk *block.Block, start, count uint64) [ log.L().Debug("Skipping action due to hash error", zap.Error(err)) continue } + receipt, err := core.dao.GetReceiptByActionHash(actHash, blkHeight) + if err != nil { + log.L().Debug("Skipping action due to failing to get receipt", zap.Error(err)) + continue + } + gas := new(big.Int).Mul(selp.GasPrice(), big.NewInt(int64(receipt.GasConsumed))) sender := selp.SrcPubkey().Address() res = append(res, &iotexapi.ActionInfo{ Action: selp.Proto(), ActHash: hex.EncodeToString(actHash[:]), BlkHash: blkHash, - Timestamp: ts, + Timestamp: blk.Header.BlockHeaderCoreProto().Timestamp, BlkHeight: blkHeight, Sender: sender.String(), + GasFee: gas.String(), Index: uint32(i), }) } @@ -1193,7 +1199,6 @@ func (core *coreService) reverseActionsInBlock(blk *block.Block, reverseStart, c h := blk.HashBlock() blkHash := hex.EncodeToString(h[:]) blkHeight := blk.Height() - ts := blk.Header.BlockHeaderCoreProto().Timestamp var res []*iotexapi.ActionInfo for i := reverseStart; i < uint64(len(blk.Actions)) && i < reverseStart+count; i++ { @@ -1204,14 +1209,21 @@ func (core *coreService) reverseActionsInBlock(blk *block.Block, reverseStart, c log.L().Debug("Skipping action due to hash error", zap.Error(err)) continue } + receipt, err := core.dao.GetReceiptByActionHash(actHash, blkHeight) + if err != nil { + log.L().Debug("Skipping action due to failing to get receipt", zap.Error(err)) + continue + } + gas := new(big.Int).Mul(selp.GasPrice(), big.NewInt(int64(receipt.GasConsumed))) sender := selp.SrcPubkey().Address() res = append([]*iotexapi.ActionInfo{{ Action: selp.Proto(), ActHash: hex.EncodeToString(actHash[:]), BlkHash: blkHash, - Timestamp: ts, + Timestamp: blk.Header.BlockHeaderCoreProto().Timestamp, BlkHeight: blkHeight, Sender: sender.String(), + GasFee: gas.String(), Index: uint32(ri), }}, res...) } diff --git a/api/grpcserver_test.go b/api/grpcserver_test.go index de4c4df5cc..12b32345bf 100644 --- a/api/grpcserver_test.go +++ b/api/grpcserver_test.go @@ -290,30 +290,35 @@ var ( start uint64 count uint64 numActions int + firstTxGas string }{ { 2, 0, 7, 7, + "0", }, { 4, 2, 5, 3, + "0", }, { 3, 0, 0, 0, + "", }, { 1, 0, math.MaxUint64, 2, + "0", }, } @@ -1137,6 +1142,9 @@ func TestGrpcServer_GetActionsByBlock(t *testing.T) { } require.NoError(err) require.Equal(test.numActions, len(res.ActionInfo)) + if test.numActions > 0 { + require.Equal(test.firstTxGas, res.ActionInfo[0].GasFee) + } for _, v := range res.ActionInfo { require.Equal(test.blkHeight, v.BlkHeight) require.Equal(blkHash[test.blkHeight], v.BlkHash)