forked from protolambda/eth2-testnet-genesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethblock.go
54 lines (44 loc) · 1.42 KB
/
ethblock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type rpcBlock struct {
Hash common.Hash `json:"hash"`
Transactions []rpcTransaction `json:"transactions"`
UncleHashes []common.Hash `json:"uncles"`
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
}
type rpcTransaction struct {
tx *types.Transaction
txExtraInfo
}
type txExtraInfo struct {
BlockNumber *string `json:"blockNumber,omitempty"`
BlockHash *common.Hash `json:"blockHash,omitempty"`
From *common.Address `json:"from,omitempty"`
}
func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
if err := json.Unmarshal(msg, &tx.tx); err != nil {
return err
}
return json.Unmarshal(msg, &tx.txExtraInfo)
}
func ParseEthBlock(blockData json.RawMessage) (*types.Block, error) {
var resultHeader types.Header
if err := json.Unmarshal(blockData, &resultHeader); err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON header: %w", err)
}
var body rpcBlock
if err := json.Unmarshal(blockData, &body); err != nil {
return nil, err
}
// get transactions
txs := make([]*types.Transaction, len(body.Transactions))
for idx := range body.Transactions {
txs[idx] = body.Transactions[idx].tx
}
return types.NewBlockWithHeader(&resultHeader).WithBody(txs, []*types.Header{}).WithWithdrawals(body.Withdrawals), nil
}