Skip to content

Commit

Permalink
[api] add gasFee info (#3063)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Feb 10, 2022
1 parent 8cce51c commit 5eddd20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 16 additions & 4 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
})
}
Expand All @@ -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++ {
Expand All @@ -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...)
}
Expand Down
8 changes: 8 additions & 0 deletions api/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5eddd20

Please sign in to comment.