Skip to content

Commit

Permalink
[api] Move generateBlockMeta to grpcserver.go (#3303)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyPigeon authored Jun 27, 2022
1 parent fd6561a commit 40667e5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
44 changes: 4 additions & 40 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,10 @@ func (core *coreService) BlockByHash(blkHash string) (*block.Store, error) {
if err != nil {
return nil, errors.Wrap(ErrNotFound, err.Error())
}
return &block.Store{blk, receipts}, nil
return &block.Store{
Block: blk,
Receipts: receipts,
}, nil
}

// BlockMetas returns blockmetas response within the height range
Expand Down Expand Up @@ -1101,45 +1104,6 @@ func (core *coreService) getBlockMetaByHeight(height uint64) (*iotextypes.BlockM
return generateBlockMeta(blk), nil
}

// generateBlockMeta generates BlockMeta from block
func generateBlockMeta(blk *block.Block) *iotextypes.BlockMeta {
header := blk.Header
height := header.Height()
ts := timestamppb.New(header.Timestamp())
var (
producerAddress string
h hash.Hash256
)
if blk.Height() > 0 {
producerAddress = header.ProducerAddress()
h = header.HashBlock()
} else {
h = block.GenesisHash()
}
txRoot := header.TxRoot()
receiptRoot := header.ReceiptRoot()
deltaStateDigest := header.DeltaStateDigest()
prevHash := header.PrevHash()

blockMeta := iotextypes.BlockMeta{
Hash: hex.EncodeToString(h[:]),
Height: height,
Timestamp: ts,
ProducerAddress: producerAddress,
TxRoot: hex.EncodeToString(txRoot[:]),
ReceiptRoot: hex.EncodeToString(receiptRoot[:]),
DeltaStateDigest: hex.EncodeToString(deltaStateDigest[:]),
PreviousBlockHash: hex.EncodeToString(prevHash[:]),
}
if logsBloom := header.LogsBloomfilter(); logsBloom != nil {
blockMeta.LogsBloom = hex.EncodeToString(logsBloom.Bytes())
}
blockMeta.NumActions = int64(len(blk.Actions))
blockMeta.TransferAmount = blk.CalculateTransferAmount().String()
blockMeta.GasLimit, blockMeta.GasUsed = gasLimitAndUsed(blk)
return &blockMeta
}

// GasLimitAndUsed returns the gas limit and used in a block
func gasLimitAndUsed(b *block.Block) (uint64, uint64) {
var gasLimit, gasUsed uint64
Expand Down
40 changes: 40 additions & 0 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"google.golang.org/grpc/peer"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
Expand Down Expand Up @@ -721,3 +722,42 @@ func (svr *GRPCServer) TraceTransactionStructLogs(ctx context.Context, in *iotex
StructLogs: structLogs,
}, nil
}

// generateBlockMeta generates BlockMeta from block
func generateBlockMeta(blk *block.Block) *iotextypes.BlockMeta {
header := blk.Header
height := header.Height()
ts := timestamppb.New(header.Timestamp())
var (
producerAddress string
h hash.Hash256
)
if blk.Height() > 0 {
producerAddress = header.ProducerAddress()
h = header.HashBlock()
} else {
h = block.GenesisHash()
}
txRoot := header.TxRoot()
receiptRoot := header.ReceiptRoot()
deltaStateDigest := header.DeltaStateDigest()
prevHash := header.PrevHash()

blockMeta := iotextypes.BlockMeta{
Hash: hex.EncodeToString(h[:]),
Height: height,
Timestamp: ts,
ProducerAddress: producerAddress,
TxRoot: hex.EncodeToString(txRoot[:]),
ReceiptRoot: hex.EncodeToString(receiptRoot[:]),
DeltaStateDigest: hex.EncodeToString(deltaStateDigest[:]),
PreviousBlockHash: hex.EncodeToString(prevHash[:]),
}
if logsBloom := header.LogsBloomfilter(); logsBloom != nil {
blockMeta.LogsBloom = hex.EncodeToString(logsBloom.Bytes())
}
blockMeta.NumActions = int64(len(blk.Actions))
blockMeta.TransferAmount = blk.CalculateTransferAmount().String()
blockMeta.GasLimit, blockMeta.GasUsed = gasLimitAndUsed(blk)
return &blockMeta
}

0 comments on commit 40667e5

Please sign in to comment.