2015-07-06 17:54:22 -07:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 09:48:40 -07:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 17:54:22 -07:00
|
|
|
//
|
2015-07-23 09:35:11 -07:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 17:54:22 -07:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 09:48:40 -07:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 17:54:22 -07:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 09:48:40 -07:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 17:54:22 -07:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 09:48:40 -07:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 17:54:22 -07:00
|
|
|
|
2015-07-06 20:08:16 -07:00
|
|
|
// Package types contains data types related to Ethereum consensus.
|
2014-11-18 07:58:22 -08:00
|
|
|
package types
|
2014-02-14 14:56:09 -08:00
|
|
|
|
|
|
|
import (
|
2015-05-31 06:53:17 -07:00
|
|
|
"bytes"
|
2015-03-03 12:48:05 -08:00
|
|
|
"encoding/binary"
|
2015-05-31 06:53:17 -07:00
|
|
|
"encoding/json"
|
2014-02-14 14:56:09 -08:00
|
|
|
"fmt"
|
2015-03-18 05:24:34 -07:00
|
|
|
"io"
|
2014-07-26 02:24:44 -07:00
|
|
|
"math/big"
|
2014-09-15 06:42:12 -07:00
|
|
|
"sort"
|
2015-06-25 05:46:08 -07:00
|
|
|
"sync/atomic"
|
2014-07-26 02:24:44 -07:00
|
|
|
"time"
|
2015-02-13 09:15:23 -08:00
|
|
|
|
2015-03-16 03:27:38 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-18 05:24:34 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
2014-12-23 04:48:44 -08:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-02-14 14:56:09 -08:00
|
|
|
)
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
// A BlockNonce is a 64-bit hash which proves (combined with the
|
|
|
|
// mix-hash) that a suffcient amount of computation has been carried
|
|
|
|
// out on a block.
|
|
|
|
type BlockNonce [8]byte
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func EncodeNonce(i uint64) BlockNonce {
|
|
|
|
var n BlockNonce
|
|
|
|
binary.BigEndian.PutUint64(n[:], i)
|
|
|
|
return n
|
2015-03-18 05:24:34 -07:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (n BlockNonce) Uint64() uint64 {
|
|
|
|
return binary.BigEndian.Uint64(n[:])
|
2015-03-18 05:24:34 -07:00
|
|
|
}
|
|
|
|
|
2015-10-15 07:07:19 -07:00
|
|
|
func (n BlockNonce) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"0x%x"`, n)), nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
type Header struct {
|
|
|
|
ParentHash common.Hash // Hash to the previous block
|
|
|
|
UncleHash common.Hash // Uncles of this block
|
|
|
|
Coinbase common.Address // The coin base address
|
|
|
|
Root common.Hash // Block Trie state
|
|
|
|
TxHash common.Hash // Tx sha
|
|
|
|
ReceiptHash common.Hash // Receipt sha
|
|
|
|
Bloom Bloom // Bloom
|
|
|
|
Difficulty *big.Int // Difficulty for the current block
|
|
|
|
Number *big.Int // The block number
|
|
|
|
GasLimit *big.Int // Gas limit
|
|
|
|
GasUsed *big.Int // Gas used
|
2015-08-23 17:52:53 -07:00
|
|
|
Time *big.Int // Creation time
|
2015-06-16 03:41:50 -07:00
|
|
|
Extra []byte // Extra data
|
|
|
|
MixDigest common.Hash // for quick difficulty verification
|
|
|
|
Nonce BlockNonce
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Header) Hash() common.Hash {
|
|
|
|
return rlpHash(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Header) HashNoNonce() common.Hash {
|
|
|
|
return rlpHash([]interface{}{
|
|
|
|
h.ParentHash,
|
|
|
|
h.UncleHash,
|
|
|
|
h.Coinbase,
|
|
|
|
h.Root,
|
|
|
|
h.TxHash,
|
|
|
|
h.ReceiptHash,
|
|
|
|
h.Bloom,
|
|
|
|
h.Difficulty,
|
|
|
|
h.Number,
|
|
|
|
h.GasLimit,
|
|
|
|
h.GasUsed,
|
|
|
|
h.Time,
|
|
|
|
h.Extra,
|
|
|
|
})
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2015-05-31 06:53:17 -07:00
|
|
|
func (h *Header) UnmarshalJSON(data []byte) error {
|
|
|
|
var ext struct {
|
|
|
|
ParentHash string
|
|
|
|
Coinbase string
|
|
|
|
Difficulty string
|
|
|
|
GasLimit string
|
2015-08-23 17:52:53 -07:00
|
|
|
Time *big.Int
|
2015-05-31 06:53:17 -07:00
|
|
|
Extra string
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
|
|
if err := dec.Decode(&ext); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ParentHash = common.HexToHash(ext.ParentHash)
|
|
|
|
h.Coinbase = common.HexToAddress(ext.Coinbase)
|
|
|
|
h.Difficulty = common.String2Big(ext.Difficulty)
|
|
|
|
h.Time = ext.Time
|
|
|
|
h.Extra = []byte(ext.Extra)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-18 05:24:34 -07:00
|
|
|
func rlpHash(x interface{}) (h common.Hash) {
|
|
|
|
hw := sha3.NewKeccak256()
|
|
|
|
rlp.Encode(hw, x)
|
|
|
|
hw.Sum(h[:0])
|
|
|
|
return h
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2015-09-07 10:43:01 -07:00
|
|
|
// Body is a simple (mutable, non-safe) data container for storing and moving
|
|
|
|
// a block's data contents (transactions and uncles) together.
|
|
|
|
type Body struct {
|
|
|
|
Transactions []*Transaction
|
|
|
|
Uncles []*Header
|
|
|
|
}
|
|
|
|
|
2014-12-23 04:48:44 -08:00
|
|
|
type Block struct {
|
2015-03-16 09:27:23 -07:00
|
|
|
header *Header
|
|
|
|
uncles []*Header
|
|
|
|
transactions Transactions
|
2014-12-23 04:48:44 -08:00
|
|
|
|
2015-06-25 05:46:08 -07:00
|
|
|
// caches
|
|
|
|
hash atomic.Value
|
|
|
|
size atomic.Value
|
|
|
|
|
|
|
|
// Td is used by package core to store the total difficulty
|
|
|
|
// of the chain up to and including the block.
|
2015-09-07 10:43:01 -07:00
|
|
|
td *big.Int
|
2015-06-25 05:46:08 -07:00
|
|
|
|
|
|
|
// ReceivedAt is used by package eth to track block propagation time.
|
2015-04-29 10:55:30 -07:00
|
|
|
ReceivedAt time.Time
|
2014-12-23 04:48:44 -08:00
|
|
|
}
|
|
|
|
|
2015-09-07 10:43:01 -07:00
|
|
|
// DeprecatedTd is an old relic for extracting the TD of a block. It is in the
|
|
|
|
// code solely to facilitate upgrading the database from the old format to the
|
|
|
|
// new, after which it should be deleted. Do not use!
|
|
|
|
func (b *Block) DeprecatedTd() *big.Int {
|
|
|
|
return b.td
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:21:02 -07:00
|
|
|
// [deprecated by eth/63]
|
2015-03-18 05:24:34 -07:00
|
|
|
// StorageBlock defines the RLP encoding of a Block stored in the
|
|
|
|
// state database. The StorageBlock encoding contains fields that
|
|
|
|
// would otherwise need to be recomputed.
|
|
|
|
type StorageBlock Block
|
|
|
|
|
|
|
|
// "external" block encoding. used for eth protocol, etc.
|
|
|
|
type extblock struct {
|
|
|
|
Header *Header
|
|
|
|
Txs []*Transaction
|
|
|
|
Uncles []*Header
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:21:02 -07:00
|
|
|
// [deprecated by eth/63]
|
2015-03-18 05:24:34 -07:00
|
|
|
// "storage" block encoding. used for database.
|
|
|
|
type storageblock struct {
|
|
|
|
Header *Header
|
|
|
|
Txs []*Transaction
|
|
|
|
Uncles []*Header
|
|
|
|
TD *big.Int
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
var (
|
2015-09-28 09:27:31 -07:00
|
|
|
EmptyRootHash = DeriveSha(Transactions{})
|
|
|
|
EmptyUncleHash = CalcUncleHash(nil)
|
2015-06-16 03:41:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewBlock creates a new block. The input data is copied,
|
|
|
|
// changes to header and to the field values will not affect the
|
|
|
|
// block.
|
|
|
|
//
|
|
|
|
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
|
|
|
|
// are ignored and set to values derived from the given txs, uncles
|
|
|
|
// and receipts.
|
|
|
|
func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt) *Block {
|
2015-09-21 05:36:29 -07:00
|
|
|
b := &Block{header: CopyHeader(header), td: new(big.Int)}
|
2015-06-16 03:41:50 -07:00
|
|
|
|
|
|
|
// TODO: panic if len(txs) != len(receipts)
|
|
|
|
if len(txs) == 0 {
|
2015-09-28 09:27:31 -07:00
|
|
|
b.header.TxHash = EmptyRootHash
|
2015-06-16 03:41:50 -07:00
|
|
|
} else {
|
|
|
|
b.header.TxHash = DeriveSha(Transactions(txs))
|
|
|
|
b.transactions = make(Transactions, len(txs))
|
|
|
|
copy(b.transactions, txs)
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
2015-03-24 05:38:16 -07:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
if len(receipts) == 0 {
|
2015-09-28 09:27:31 -07:00
|
|
|
b.header.ReceiptHash = EmptyRootHash
|
2015-06-16 03:41:50 -07:00
|
|
|
} else {
|
|
|
|
b.header.ReceiptHash = DeriveSha(Receipts(receipts))
|
|
|
|
b.header.Bloom = CreateBloom(receipts)
|
|
|
|
}
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
if len(uncles) == 0 {
|
2015-09-28 09:27:31 -07:00
|
|
|
b.header.UncleHash = EmptyUncleHash
|
2015-06-16 03:41:50 -07:00
|
|
|
} else {
|
|
|
|
b.header.UncleHash = CalcUncleHash(uncles)
|
|
|
|
b.uncles = make([]*Header, len(uncles))
|
|
|
|
for i := range uncles {
|
2015-09-21 05:36:29 -07:00
|
|
|
b.uncles[i] = CopyHeader(uncles[i])
|
2015-06-16 03:41:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
2015-03-03 12:48:05 -08:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
// NewBlockWithHeader creates a block with the given header data. The
|
|
|
|
// header data is copied, changes to header and to the field values
|
|
|
|
// will not affect the block.
|
2014-12-23 04:48:44 -08:00
|
|
|
func NewBlockWithHeader(header *Header) *Block {
|
2015-09-21 05:36:29 -07:00
|
|
|
return &Block{header: CopyHeader(header)}
|
2015-06-16 03:41:50 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 05:36:29 -07:00
|
|
|
// CopyHeader creates a deep copy of a block header to prevent side effects from
|
|
|
|
// modifying a header variable.
|
|
|
|
func CopyHeader(h *Header) *Header {
|
2015-06-16 03:41:50 -07:00
|
|
|
cpy := *h
|
2015-08-23 17:52:53 -07:00
|
|
|
if cpy.Time = new(big.Int); h.Time != nil {
|
|
|
|
cpy.Time.Set(h.Time)
|
|
|
|
}
|
2015-06-16 03:41:50 -07:00
|
|
|
if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
|
|
|
|
cpy.Difficulty.Set(h.Difficulty)
|
|
|
|
}
|
|
|
|
if cpy.Number = new(big.Int); h.Number != nil {
|
|
|
|
cpy.Number.Set(h.Number)
|
|
|
|
}
|
|
|
|
if cpy.GasLimit = new(big.Int); h.GasLimit != nil {
|
|
|
|
cpy.GasLimit.Set(h.GasLimit)
|
|
|
|
}
|
|
|
|
if cpy.GasUsed = new(big.Int); h.GasUsed != nil {
|
|
|
|
cpy.GasUsed.Set(h.GasUsed)
|
|
|
|
}
|
|
|
|
if len(h.Extra) > 0 {
|
|
|
|
cpy.Extra = make([]byte, len(h.Extra))
|
|
|
|
copy(cpy.Extra, h.Extra)
|
|
|
|
}
|
|
|
|
return &cpy
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) ValidateFields() error {
|
|
|
|
if b.header == nil {
|
2015-03-27 17:48:37 -07:00
|
|
|
return fmt.Errorf("header is nil")
|
|
|
|
}
|
2015-06-16 03:41:50 -07:00
|
|
|
for i, transaction := range b.transactions {
|
2015-03-30 05:48:07 -07:00
|
|
|
if transaction == nil {
|
|
|
|
return fmt.Errorf("transaction %d is nil", i)
|
|
|
|
}
|
2015-03-27 17:48:37 -07:00
|
|
|
}
|
2015-06-16 03:41:50 -07:00
|
|
|
for i, uncle := range b.uncles {
|
2015-03-30 05:48:07 -07:00
|
|
|
if uncle == nil {
|
|
|
|
return fmt.Errorf("uncle %d is nil", i)
|
|
|
|
}
|
2015-03-27 17:48:37 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) DecodeRLP(s *rlp.Stream) error {
|
2015-03-18 05:24:34 -07:00
|
|
|
var eb extblock
|
2015-06-25 05:46:08 -07:00
|
|
|
_, size, _ := s.Kind()
|
2015-03-18 05:24:34 -07:00
|
|
|
if err := s.Decode(&eb); err != nil {
|
|
|
|
return err
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
2015-06-16 03:41:50 -07:00
|
|
|
b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs
|
2015-06-25 05:46:08 -07:00
|
|
|
b.size.Store(common.StorageSize(rlp.ListSize(size)))
|
2015-03-18 05:24:34 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 10:54:53 -07:00
|
|
|
func (b *Block) EncodeRLP(w io.Writer) error {
|
2015-03-18 05:24:34 -07:00
|
|
|
return rlp.Encode(w, extblock{
|
2015-06-16 03:41:50 -07:00
|
|
|
Header: b.header,
|
|
|
|
Txs: b.transactions,
|
|
|
|
Uncles: b.uncles,
|
2015-03-18 05:24:34 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:21:02 -07:00
|
|
|
// [deprecated by eth/63]
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
|
2015-03-18 05:24:34 -07:00
|
|
|
var sb storageblock
|
|
|
|
if err := s.Decode(&sb); err != nil {
|
2014-12-23 04:48:44 -08:00
|
|
|
return err
|
|
|
|
}
|
2015-09-07 10:43:01 -07:00
|
|
|
b.header, b.uncles, b.transactions, b.td = sb.Header, sb.Uncles, sb.Txs, sb.TD
|
2014-12-23 04:48:44 -08:00
|
|
|
return nil
|
2014-10-29 06:20:42 -07:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
// TODO: copies
|
|
|
|
func (b *Block) Uncles() []*Header { return b.uncles }
|
|
|
|
func (b *Block) Transactions() Transactions { return b.transactions }
|
2015-04-22 06:29:19 -07:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) Transaction(hash common.Hash) *Transaction {
|
|
|
|
for _, transaction := range b.transactions {
|
2015-03-16 09:43:42 -07:00
|
|
|
if transaction.Hash() == hash {
|
2014-12-23 04:48:44 -08:00
|
|
|
return transaction
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
}
|
2014-12-23 04:48:44 -08:00
|
|
|
return nil
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) }
|
|
|
|
func (b *Block) GasLimit() *big.Int { return new(big.Int).Set(b.header.GasLimit) }
|
|
|
|
func (b *Block) GasUsed() *big.Int { return new(big.Int).Set(b.header.GasUsed) }
|
|
|
|
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
|
2015-08-23 17:52:53 -07:00
|
|
|
func (b *Block) Time() *big.Int { return new(big.Int).Set(b.header.Time) }
|
2014-12-23 04:48:44 -08:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
|
|
|
|
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }
|
|
|
|
func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) }
|
|
|
|
func (b *Block) Bloom() Bloom { return b.header.Bloom }
|
|
|
|
func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
|
|
|
|
func (b *Block) Root() common.Hash { return b.header.Root }
|
|
|
|
func (b *Block) ParentHash() common.Hash { return b.header.ParentHash }
|
|
|
|
func (b *Block) TxHash() common.Hash { return b.header.TxHash }
|
|
|
|
func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash }
|
|
|
|
func (b *Block) UncleHash() common.Hash { return b.header.UncleHash }
|
|
|
|
func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) }
|
2014-12-23 04:48:44 -08:00
|
|
|
|
2015-09-21 05:36:29 -07:00
|
|
|
func (b *Block) Header() *Header { return CopyHeader(b.header) }
|
2015-03-03 12:48:05 -08:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) HashNoNonce() common.Hash {
|
|
|
|
return b.header.HashNoNonce()
|
2015-02-18 04:14:21 -08:00
|
|
|
}
|
2014-12-23 04:48:44 -08:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) Size() common.StorageSize {
|
2015-06-25 05:46:08 -07:00
|
|
|
if size := b.size.Load(); size != nil {
|
|
|
|
return size.(common.StorageSize)
|
|
|
|
}
|
2015-03-18 05:24:34 -07:00
|
|
|
c := writeCounter(0)
|
2015-06-16 03:41:50 -07:00
|
|
|
rlp.Encode(&c, b)
|
2015-06-25 05:46:08 -07:00
|
|
|
b.size.Store(common.StorageSize(c))
|
2015-03-18 05:24:34 -07:00
|
|
|
return common.StorageSize(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeCounter common.StorageSize
|
|
|
|
|
|
|
|
func (c *writeCounter) Write(b []byte) (int, error) {
|
|
|
|
*c += writeCounter(len(b))
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func CalcUncleHash(uncles []*Header) common.Hash {
|
|
|
|
return rlpHash(uncles)
|
2015-01-05 08:10:42 -08:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
// WithMiningResult returns a new block with the data from b
|
|
|
|
// where nonce and mix digest are set to the provided values.
|
|
|
|
func (b *Block) WithMiningResult(nonce uint64, mixDigest common.Hash) *Block {
|
|
|
|
cpy := *b.header
|
|
|
|
binary.BigEndian.PutUint64(cpy.Nonce[:], nonce)
|
|
|
|
cpy.MixDigest = mixDigest
|
|
|
|
return &Block{
|
|
|
|
header: &cpy,
|
|
|
|
transactions: b.transactions,
|
|
|
|
uncles: b.uncles,
|
2015-01-05 08:10:42 -08:00
|
|
|
}
|
2014-12-23 04:48:44 -08:00
|
|
|
}
|
|
|
|
|
2015-08-14 11:25:41 -07:00
|
|
|
// WithBody returns a new block with the given transaction and uncle contents.
|
|
|
|
func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
|
|
|
|
block := &Block{
|
2015-09-21 05:36:29 -07:00
|
|
|
header: CopyHeader(b.header),
|
2015-08-14 11:25:41 -07:00
|
|
|
transactions: make([]*Transaction, len(transactions)),
|
|
|
|
uncles: make([]*Header, len(uncles)),
|
|
|
|
}
|
|
|
|
copy(block.transactions, transactions)
|
|
|
|
for i := range uncles {
|
2015-09-21 05:36:29 -07:00
|
|
|
block.uncles[i] = CopyHeader(uncles[i])
|
2015-08-14 11:25:41 -07:00
|
|
|
}
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
// Implement pow.Block
|
2015-03-24 05:38:16 -07:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) Hash() common.Hash {
|
2015-06-25 05:46:08 -07:00
|
|
|
if hash := b.hash.Load(); hash != nil {
|
|
|
|
return hash.(common.Hash)
|
|
|
|
}
|
|
|
|
v := rlpHash(b.header)
|
|
|
|
b.hash.Store(v)
|
|
|
|
return v
|
2015-03-24 02:33:00 -07:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (b *Block) String() string {
|
2015-09-07 10:43:01 -07:00
|
|
|
str := fmt.Sprintf(`Block(#%v): Size: %v {
|
2015-04-20 03:01:20 -07:00
|
|
|
MinerHash: %x
|
2014-12-23 04:48:44 -08:00
|
|
|
%v
|
2014-12-23 05:33:15 -08:00
|
|
|
Transactions:
|
2014-12-23 04:48:44 -08:00
|
|
|
%v
|
2014-12-23 05:33:15 -08:00
|
|
|
Uncles:
|
2014-12-23 04:48:44 -08:00
|
|
|
%v
|
|
|
|
}
|
2015-09-07 10:43:01 -07:00
|
|
|
`, b.Number(), b.Size(), b.header.HashNoNonce(), b.header, b.transactions, b.uncles)
|
2015-04-29 03:40:05 -07:00
|
|
|
return str
|
2014-12-23 04:48:44 -08:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func (h *Header) String() string {
|
2015-04-20 03:01:20 -07:00
|
|
|
return fmt.Sprintf(`Header(%x):
|
|
|
|
[
|
2014-12-23 05:33:15 -08:00
|
|
|
ParentHash: %x
|
|
|
|
UncleHash: %x
|
|
|
|
Coinbase: %x
|
|
|
|
Root: %x
|
|
|
|
TxSha %x
|
|
|
|
ReceiptSha: %x
|
|
|
|
Bloom: %x
|
|
|
|
Difficulty: %v
|
|
|
|
Number: %v
|
|
|
|
GasLimit: %v
|
|
|
|
GasUsed: %v
|
|
|
|
Time: %v
|
2015-04-05 09:57:03 -07:00
|
|
|
Extra: %s
|
2015-06-16 03:41:50 -07:00
|
|
|
MixDigest: %x
|
2015-04-20 03:01:20 -07:00
|
|
|
Nonce: %x
|
2015-06-16 03:41:50 -07:00
|
|
|
]`, h.Hash(), h.ParentHash, h.UncleHash, h.Coinbase, h.Root, h.TxHash, h.ReceiptHash, h.Bloom, h.Difficulty, h.Number, h.GasLimit, h.GasUsed, h.Time, h.Extra, h.MixDigest, h.Nonce)
|
2014-03-20 03:20:29 -07:00
|
|
|
}
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2014-12-23 04:48:44 -08:00
|
|
|
type Blocks []*Block
|
2014-10-10 13:42:37 -07:00
|
|
|
|
2014-12-23 04:48:44 -08:00
|
|
|
type BlockBy func(b1, b2 *Block) bool
|
2014-05-22 08:35:26 -07:00
|
|
|
|
2014-12-23 04:48:44 -08:00
|
|
|
func (self BlockBy) Sort(blocks Blocks) {
|
|
|
|
bs := blockSorter{
|
|
|
|
blocks: blocks,
|
|
|
|
by: self,
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
2014-12-23 04:48:44 -08:00
|
|
|
sort.Sort(bs)
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
2014-05-20 06:02:46 -07:00
|
|
|
|
2014-12-23 04:48:44 -08:00
|
|
|
type blockSorter struct {
|
|
|
|
blocks Blocks
|
|
|
|
by func(b1, b2 *Block) bool
|
2014-08-25 03:53:06 -07:00
|
|
|
}
|
2014-10-29 02:34:40 -07:00
|
|
|
|
2014-12-23 04:48:44 -08:00
|
|
|
func (self blockSorter) Len() int { return len(self.blocks) }
|
|
|
|
func (self blockSorter) Swap(i, j int) {
|
|
|
|
self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i]
|
2014-10-29 02:34:40 -07:00
|
|
|
}
|
2014-12-23 04:48:44 -08:00
|
|
|
func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
|
2014-12-10 07:45:16 -08:00
|
|
|
|
2015-06-16 03:41:50 -07:00
|
|
|
func Number(b1, b2 *Block) bool { return b1.header.Number.Cmp(b2.header.Number) < 0 }
|