block.go 12.2 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// 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.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
F
Felix Lange 已提交
16

17
// Package types contains data types related to Ethereum consensus.
18
package types
O
obscuren 已提交
19 20

import (
21
	"bytes"
O
obscuren 已提交
22
	"encoding/binary"
23
	"encoding/json"
O
obscuren 已提交
24
	"fmt"
25
	"io"
O
obscuren 已提交
26
	"math/big"
O
obscuren 已提交
27
	"sort"
28
	"sync/atomic"
O
obscuren 已提交
29
	"time"
O
obscuren 已提交
30

O
obscuren 已提交
31
	"github.com/ethereum/go-ethereum/common"
32
	"github.com/ethereum/go-ethereum/crypto/sha3"
O
obscuren 已提交
33
	"github.com/ethereum/go-ethereum/rlp"
O
obscuren 已提交
34 35
)

F
Felix Lange 已提交
36 37 38 39
// 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
O
obscuren 已提交
40

F
Felix Lange 已提交
41 42 43 44
func EncodeNonce(i uint64) BlockNonce {
	var n BlockNonce
	binary.BigEndian.PutUint64(n[:], i)
	return n
45 46
}

F
Felix Lange 已提交
47 48
func (n BlockNonce) Uint64() uint64 {
	return binary.BigEndian.Uint64(n[:])
49 50
}

F
Felix Lange 已提交
51 52 53 54 55 56 57 58 59 60 61 62
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
63
	Time        *big.Int       // Creation time
F
Felix Lange 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	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,
	})
O
obscuren 已提交
89 90
}

91 92 93 94 95 96
func (h *Header) UnmarshalJSON(data []byte) error {
	var ext struct {
		ParentHash string
		Coinbase   string
		Difficulty string
		GasLimit   string
97
		Time       *big.Int
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
		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
}

113 114 115 116 117
func rlpHash(x interface{}) (h common.Hash) {
	hw := sha3.NewKeccak256()
	rlp.Encode(hw, x)
	hw.Sum(h[:0])
	return h
O
obscuren 已提交
118 119
}

120 121 122 123 124 125 126
// 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
}

O
obscuren 已提交
127
type Block struct {
O
obscuren 已提交
128 129 130
	header       *Header
	uncles       []*Header
	transactions Transactions
F
Felix Lange 已提交
131
	receipts     Receipts
O
obscuren 已提交
132

133 134 135 136 137 138
	// 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.
139
	td *big.Int
140 141

	// ReceivedAt is used by package eth to track block propagation time.
142
	ReceivedAt time.Time
O
obscuren 已提交
143 144
}

145 146 147 148 149 150 151
// 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
}

152
// [deprecated by eth/63]
153 154 155 156 157 158 159 160 161 162 163 164
// 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
}

165
// [deprecated by eth/63]
166 167 168 169 170 171 172 173
// "storage" block encoding. used for database.
type storageblock struct {
	Header *Header
	Txs    []*Transaction
	Uncles []*Header
	TD     *big.Int
}

F
Felix Lange 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186
var (
	emptyRootHash  = DeriveSha(Transactions{})
	emptyUncleHash = CalcUncleHash(nil)
)

// 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 {
187
	b := &Block{header: CopyHeader(header), td: new(big.Int)}
F
Felix Lange 已提交
188 189 190 191 192 193 194 195

	// TODO: panic if len(txs) != len(receipts)
	if len(txs) == 0 {
		b.header.TxHash = emptyRootHash
	} else {
		b.header.TxHash = DeriveSha(Transactions(txs))
		b.transactions = make(Transactions, len(txs))
		copy(b.transactions, txs)
O
obscuren 已提交
196
	}
O
obscuren 已提交
197

F
Felix Lange 已提交
198 199 200 201 202 203 204 205
	if len(receipts) == 0 {
		b.header.ReceiptHash = emptyRootHash
	} else {
		b.header.ReceiptHash = DeriveSha(Receipts(receipts))
		b.header.Bloom = CreateBloom(receipts)
		b.receipts = make([]*Receipt, len(receipts))
		copy(b.receipts, receipts)
	}
O
obscuren 已提交
206

F
Felix Lange 已提交
207 208 209 210 211 212
	if len(uncles) == 0 {
		b.header.UncleHash = emptyUncleHash
	} else {
		b.header.UncleHash = CalcUncleHash(uncles)
		b.uncles = make([]*Header, len(uncles))
		for i := range uncles {
213
			b.uncles[i] = CopyHeader(uncles[i])
F
Felix Lange 已提交
214 215 216 217
		}
	}

	return b
O
obscuren 已提交
218 219
}

F
Felix Lange 已提交
220 221 222
// 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.
O
obscuren 已提交
223
func NewBlockWithHeader(header *Header) *Block {
224
	return &Block{header: CopyHeader(header)}
F
Felix Lange 已提交
225 226
}

227 228 229
// CopyHeader creates a deep copy of a block header to prevent side effects from
// modifying a header variable.
func CopyHeader(h *Header) *Header {
F
Felix Lange 已提交
230
	cpy := *h
231 232 233
	if cpy.Time = new(big.Int); h.Time != nil {
		cpy.Time.Set(h.Time)
	}
F
Felix Lange 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	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
O
obscuren 已提交
251 252
}

F
Felix Lange 已提交
253 254
func (b *Block) ValidateFields() error {
	if b.header == nil {
255 256
		return fmt.Errorf("header is nil")
	}
F
Felix Lange 已提交
257
	for i, transaction := range b.transactions {
258 259 260
		if transaction == nil {
			return fmt.Errorf("transaction %d is nil", i)
		}
261
	}
F
Felix Lange 已提交
262
	for i, uncle := range b.uncles {
263 264 265
		if uncle == nil {
			return fmt.Errorf("uncle %d is nil", i)
		}
266 267 268 269
	}
	return nil
}

F
Felix Lange 已提交
270
func (b *Block) DecodeRLP(s *rlp.Stream) error {
271
	var eb extblock
272
	_, size, _ := s.Kind()
273 274
	if err := s.Decode(&eb); err != nil {
		return err
O
obscuren 已提交
275
	}
F
Felix Lange 已提交
276
	b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs
277
	b.size.Store(common.StorageSize(rlp.ListSize(size)))
278 279 280
	return nil
}

281
func (b *Block) EncodeRLP(w io.Writer) error {
282
	return rlp.Encode(w, extblock{
F
Felix Lange 已提交
283 284 285
		Header: b.header,
		Txs:    b.transactions,
		Uncles: b.uncles,
286 287 288
	})
}

289
// [deprecated by eth/63]
F
Felix Lange 已提交
290
func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
291 292
	var sb storageblock
	if err := s.Decode(&sb); err != nil {
O
obscuren 已提交
293 294
		return err
	}
295
	b.header, b.uncles, b.transactions, b.td = sb.Header, sb.Uncles, sb.Txs, sb.TD
O
obscuren 已提交
296
	return nil
O
obscuren 已提交
297 298
}

F
Felix Lange 已提交
299 300 301 302
// TODO: copies
func (b *Block) Uncles() []*Header          { return b.uncles }
func (b *Block) Transactions() Transactions { return b.transactions }
func (b *Block) Receipts() Receipts         { return b.receipts }
303

F
Felix Lange 已提交
304 305
func (b *Block) Transaction(hash common.Hash) *Transaction {
	for _, transaction := range b.transactions {
306
		if transaction.Hash() == hash {
O
obscuren 已提交
307
			return transaction
O
obscuren 已提交
308 309
		}
	}
O
obscuren 已提交
310
	return nil
O
obscuren 已提交
311 312
}

F
Felix Lange 已提交
313 314 315 316
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) }
317
func (b *Block) Time() *big.Int       { return new(big.Int).Set(b.header.Time) }
O
obscuren 已提交
318

F
Felix Lange 已提交
319 320 321 322 323 324 325 326 327 328 329
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) }
O
obscuren 已提交
330

331
func (b *Block) Header() *Header { return CopyHeader(b.header) }
O
obscuren 已提交
332

F
Felix Lange 已提交
333 334
func (b *Block) HashNoNonce() common.Hash {
	return b.header.HashNoNonce()
335
}
O
obscuren 已提交
336

F
Felix Lange 已提交
337
func (b *Block) Size() common.StorageSize {
338 339 340
	if size := b.size.Load(); size != nil {
		return size.(common.StorageSize)
	}
341
	c := writeCounter(0)
F
Felix Lange 已提交
342
	rlp.Encode(&c, b)
343
	b.size.Store(common.StorageSize(c))
344 345 346 347 348 349 350 351 352 353
	return common.StorageSize(c)
}

type writeCounter common.StorageSize

func (c *writeCounter) Write(b []byte) (int, error) {
	*c += writeCounter(len(b))
	return len(b), nil
}

F
Felix Lange 已提交
354 355
func CalcUncleHash(uncles []*Header) common.Hash {
	return rlpHash(uncles)
O
Merge  
obscuren 已提交
356 357
}

F
Felix Lange 已提交
358 359 360 361 362 363 364 365 366 367 368
// 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,
		receipts:     b.receipts,
		uncles:       b.uncles,
O
Merge  
obscuren 已提交
369
	}
O
obscuren 已提交
370 371
}

372 373 374
// WithBody returns a new block with the given transaction and uncle contents.
func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
	block := &Block{
375
		header:       CopyHeader(b.header),
376 377 378 379 380
		transactions: make([]*Transaction, len(transactions)),
		uncles:       make([]*Header, len(uncles)),
	}
	copy(block.transactions, transactions)
	for i := range uncles {
381
		block.uncles[i] = CopyHeader(uncles[i])
382 383 384 385
	}
	return block
}

F
Felix Lange 已提交
386
// Implement pow.Block
O
obscuren 已提交
387

F
Felix Lange 已提交
388
func (b *Block) Hash() common.Hash {
389 390 391 392 393 394
	if hash := b.hash.Load(); hash != nil {
		return hash.(common.Hash)
	}
	v := rlpHash(b.header)
	b.hash.Store(v)
	return v
O
obscuren 已提交
395 396
}

F
Felix Lange 已提交
397
func (b *Block) String() string {
398
	str := fmt.Sprintf(`Block(#%v): Size: %v {
399
MinerHash: %x
O
obscuren 已提交
400
%v
O
obscuren 已提交
401
Transactions:
O
obscuren 已提交
402
%v
O
obscuren 已提交
403
Uncles:
O
obscuren 已提交
404 405
%v
}
406
`, b.Number(), b.Size(), b.header.HashNoNonce(), b.header, b.transactions, b.uncles)
407
	return str
O
obscuren 已提交
408 409
}

F
Felix Lange 已提交
410
func (h *Header) String() string {
411 412
	return fmt.Sprintf(`Header(%x):
[
O
obscuren 已提交
413 414 415 416 417 418 419 420 421 422 423 424
	ParentHash:	    %x
	UncleHash:	    %x
	Coinbase:	    %x
	Root:		    %x
	TxSha		    %x
	ReceiptSha:	    %x
	Bloom:		    %x
	Difficulty:	    %v
	Number:		    %v
	GasLimit:	    %v
	GasUsed:	    %v
	Time:		    %v
425
	Extra:		    %s
F
Felix Lange 已提交
426
	MixDigest:      %x
427
	Nonce:		    %x
F
Felix Lange 已提交
428
]`, 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)
M
Maran 已提交
429
}
O
obscuren 已提交
430

O
obscuren 已提交
431
type Blocks []*Block
432

O
obscuren 已提交
433
type BlockBy func(b1, b2 *Block) bool
O
obscuren 已提交
434

O
obscuren 已提交
435 436 437 438
func (self BlockBy) Sort(blocks Blocks) {
	bs := blockSorter{
		blocks: blocks,
		by:     self,
O
obscuren 已提交
439
	}
O
obscuren 已提交
440
	sort.Sort(bs)
O
obscuren 已提交
441
}
O
obscuren 已提交
442

O
obscuren 已提交
443 444 445
type blockSorter struct {
	blocks Blocks
	by     func(b1, b2 *Block) bool
O
obscuren 已提交
446
}
O
obscuren 已提交
447

O
obscuren 已提交
448 449 450
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]
O
obscuren 已提交
451
}
O
obscuren 已提交
452
func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
O
obscuren 已提交
453

F
Felix Lange 已提交
454
func Number(b1, b2 *Block) bool { return b1.header.Number.Cmp(b2.header.Number) < 0 }