/
blocks.go
55 lines (48 loc) · 2.08 KB
/
blocks.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
55
package types
import (
"math/big"
etypes "github.com/ethereum/go-ethereum/core/types"
"github.com/33cn/chain33/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
//BlockDetailToEthBlock chain33 blockdetails transfer to eth block format
func BlockDetailToEthBlock(details *types.BlockDetails, cfg *types.Chain33Config, full bool) (*Block, error) {
var block Block
var header Header
fullblock := details.GetItems()[0]
header.Time = hexutil.Uint64(fullblock.GetBlock().GetBlockTime())
header.Number = (*hexutil.Big)(big.NewInt(fullblock.GetBlock().Height))
header.TxHash = common.BytesToHash(fullblock.GetBlock().GetHeader(cfg).TxHash)
header.Difficulty = (*hexutil.Big)(big.NewInt(int64(fullblock.GetBlock().GetDifficulty())))
header.ParentHash = common.BytesToHash(fullblock.GetBlock().ParentHash)
header.Root = common.BytesToHash(fullblock.GetBlock().GetStateHash())
header.Coinbase = common.HexToAddress(fullblock.GetBlock().GetTxs()[0].From())
//header.GasUsed=
//暂不支持ReceiptHash,UncleHash
//header.ReceiptHash=
header.UncleHash = etypes.EmptyUncleHash
//处理交易
txs, fee, err := TxsToEthTxs(common.BytesToHash(fullblock.GetBlock().Hash(cfg)), fullblock.GetBlock().Height, fullblock.GetBlock().GetTxs(), cfg, full)
if err != nil {
return nil, err
}
header.GasUsed = hexutil.Uint64(fee)
header.GasLimit = header.GasUsed
block.Hash = common.BytesToHash(fullblock.GetBlock().Hash(cfg)).Hex()
block.Header = &header
block.Uncles = []*Header{}
block.Transactions = txs
return &block, nil
}
//BlockHeaderToEthHeader transfer chain33 header to eth header
func BlockHeaderToEthHeader(cHeader *types.Header) (*Header, error) {
var header Header
header.Time = hexutil.Uint64(cHeader.GetBlockTime())
header.Number = (*hexutil.Big)(big.NewInt(cHeader.Height))
header.TxHash = common.BytesToHash(cHeader.TxHash)
header.Difficulty = (*hexutil.Big)(big.NewInt(int64(cHeader.GetDifficulty())))
header.ParentHash = common.BytesToHash(cHeader.ParentHash)
header.Root = common.BytesToHash(cHeader.GetStateHash())
return &header, nil
}