MCPcopy Create free account
hub / github.com/CPChain/chain / getBlock

Method getBlock

api/cpclient/cpclient.go:90–124  ·  view source on GitHub ↗
(ctx context.Context, method string, args ...interface{})

Source from the content-addressed store, hash-verified

88}
89
90func (c *Client) getBlock(ctx context.Context, method string, args ...interface{}) (*types.Block, error) {
91 var raw json.RawMessage
92 err := c.c.CallContext(ctx, &raw, method, args...)
93 if err != nil {
94 return nil, err
95 } else if len(raw) == 0 {
96 return nil, cpchain.NotFound
97 }
98 // Decode header and transactions.
99 var head *types.Header
100 var body rpcBlock
101 if err := json.Unmarshal(raw, &head); err != nil {
102 return nil, err
103 }
104 if err := json.Unmarshal(raw, &body); err != nil {
105 return nil, err
106 }
107 // Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
108 if head.TxsRoot == types.EmptyRootHash && len(body.Transactions) > 0 {
109 return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
110 }
111 if head.TxsRoot != types.EmptyRootHash && len(body.Transactions) == 0 {
112 return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
113 }
114
115 // Fill the sender cache of transactions in the block.
116 txs := make([]*types.Transaction, len(body.Transactions))
117 for i, tx := range body.Transactions {
118 if tx.From != nil {
119 setSenderFromServer(tx.tx, *tx.From, body.Hash)
120 }
121 txs[i] = tx.tx
122 }
123 return types.NewBlockWithHeader(head).WithBody(txs), nil
124}
125
126// HeaderByHash returns the block header with the given hash.
127func (c *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {

Callers 4

BlockByHashMethod · 0.95
BlockByNumberMethod · 0.95
mainFunction · 0.45
mainFunction · 0.45

Calls 3

setSenderFromServerFunction · 0.85
CallContextMethod · 0.80
WithBodyMethod · 0.80

Tested by

no test coverage detected