block.go 9.2 KB
Newer Older
1
package types
O
obscuren 已提交
2 3

import (
O
obscuren 已提交
4
	"encoding/binary"
O
obscuren 已提交
5
	"fmt"
6
	"io"
O
obscuren 已提交
7
	"math/big"
O
obscuren 已提交
8
	"sort"
O
obscuren 已提交
9
	"time"
O
obscuren 已提交
10

O
obscuren 已提交
11
	"github.com/ethereum/go-ethereum/common"
12
	"github.com/ethereum/go-ethereum/crypto/sha3"
O
obscuren 已提交
13
	"github.com/ethereum/go-ethereum/rlp"
O
obscuren 已提交
14 15
)

O
obscuren 已提交
16
type Header struct {
O
obscuren 已提交
17
	// Hash to the previous block
O
obscuren 已提交
18
	ParentHash common.Hash
O
obscuren 已提交
19
	// Uncles of this block
O
obscuren 已提交
20
	UncleHash common.Hash
O
obscuren 已提交
21
	// The coin base address
O
obscuren 已提交
22
	Coinbase common.Address
O
obscuren 已提交
23
	// Block Trie state
O
obscuren 已提交
24
	Root common.Hash
O
obscuren 已提交
25
	// Tx sha
O
obscuren 已提交
26
	TxHash common.Hash
O
obscuren 已提交
27
	// Receipt sha
O
obscuren 已提交
28
	ReceiptHash common.Hash
O
obscuren 已提交
29
	// Bloom
O
obscuren 已提交
30
	Bloom Bloom
O
obscuren 已提交
31 32
	// Difficulty for the current block
	Difficulty *big.Int
O
obscuren 已提交
33 34 35 36 37 38
	// The block number
	Number *big.Int
	// Gas limit
	GasLimit *big.Int
	// Gas used
	GasUsed *big.Int
O
obscuren 已提交
39 40
	// Creation time
	Time uint64
O
obscuren 已提交
41 42
	// Extra data
	Extra string
43
	// Mix digest for quick checking to prevent DOS
O
obscuren 已提交
44
	MixDigest common.Hash
45
	// Nonce
O
obscuren 已提交
46
	Nonce [8]byte
O
obscuren 已提交
47 48
}

49 50 51 52 53 54 55 56
func (self *Header) Hash() common.Hash {
	return rlpHash(self.rlpData(true))
}

func (self *Header) HashNoNonce() common.Hash {
	return rlpHash(self.rlpData(false))
}

O
obscuren 已提交
57
func (self *Header) rlpData(withNonce bool) []interface{} {
M
Matthew Wampler-Doty 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
	fields := []interface{}{
		self.ParentHash,
		self.UncleHash,
		self.Coinbase,
		self.Root,
		self.TxHash,
		self.ReceiptHash,
		self.Bloom,
		self.Difficulty,
		self.Number,
		self.GasLimit,
		self.GasUsed,
		self.Time,
71 72
		self.Extra,
	}
O
obscuren 已提交
73
	if withNonce {
74
		fields = append(fields, self.MixDigest, self.Nonce)
O
obscuren 已提交
75 76 77 78 79 80
	}
	return fields
}

func (self *Header) RlpData() interface{} {
	return self.rlpData(true)
O
obscuren 已提交
81 82
}

83 84 85 86 87
func rlpHash(x interface{}) (h common.Hash) {
	hw := sha3.NewKeccak256()
	rlp.Encode(hw, x)
	hw.Sum(h[:0])
	return h
O
obscuren 已提交
88 89
}

O
obscuren 已提交
90
type Block struct {
O
obscuren 已提交
91 92 93 94 95 96 97 98 99
	// Preset Hash for mock (Tests)
	HeaderHash       common.Hash
	ParentHeaderHash common.Hash
	// ^^^^ ignore ^^^^

	header       *Header
	uncles       []*Header
	transactions Transactions
	Td           *big.Int
O
obscuren 已提交
100 101 102 103

	receipts Receipts
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
// 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
}

// "storage" block encoding. used for database.
type storageblock struct {
	Header *Header
	Txs    []*Transaction
	Uncles []*Header
	TD     *big.Int
}

O
obscuren 已提交
124
func NewBlock(parentHash common.Hash, coinbase common.Address, root common.Hash, difficulty *big.Int, nonce uint64, extra string) *Block {
O
obscuren 已提交
125 126 127 128 129 130
	header := &Header{
		Root:       root,
		ParentHash: parentHash,
		Coinbase:   coinbase,
		Difficulty: difficulty,
		Time:       uint64(time.Now().Unix()),
O
obscuren 已提交
131 132 133
		Extra:      extra,
		GasUsed:    new(big.Int),
		GasLimit:   new(big.Int),
O
obscuren 已提交
134
	}
O
obscuren 已提交
135
	header.SetNonce(nonce)
O
obscuren 已提交
136
	block := &Block{header: header}
O
obscuren 已提交
137 138 139
	return block
}

O
obscuren 已提交
140
func (self *Header) SetNonce(nonce uint64) {
O
obscuren 已提交
141
	binary.BigEndian.PutUint64(self.Nonce[:], nonce)
O
obscuren 已提交
142 143
}

O
obscuren 已提交
144 145
func NewBlockWithHeader(header *Header) *Block {
	return &Block{header: header}
O
obscuren 已提交
146 147
}

O
obscuren 已提交
148
func (self *Block) DecodeRLP(s *rlp.Stream) error {
149 150 151
	var eb extblock
	if err := s.Decode(&eb); err != nil {
		return err
O
obscuren 已提交
152
	}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	self.header, self.uncles, self.transactions = eb.Header, eb.Uncles, eb.Txs
	return nil
}

func (self Block) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, extblock{
		Header: self.header,
		Txs:    self.transactions,
		Uncles: self.uncles,
	})
}

func (self *StorageBlock) DecodeRLP(s *rlp.Stream) error {
	var sb storageblock
	if err := s.Decode(&sb); err != nil {
O
obscuren 已提交
168 169
		return err
	}
170
	self.header, self.uncles, self.transactions, self.Td = sb.Header, sb.Uncles, sb.Txs, sb.TD
O
obscuren 已提交
171
	return nil
O
obscuren 已提交
172 173
}

174 175 176 177 178 179 180 181 182
func (self StorageBlock) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, storageblock{
		Header: self.header,
		Txs:    self.transactions,
		Uncles: self.uncles,
		TD:     self.Td,
	})
}

O
obscuren 已提交
183 184
func (self *Block) Header() *Header {
	return self.header
O
obscuren 已提交
185 186
}

O
obscuren 已提交
187 188
func (self *Block) Uncles() []*Header {
	return self.uncles
O
obscuren 已提交
189 190
}

O
obscuren 已提交
191 192
func (self *Block) SetUncles(uncleHeaders []*Header) {
	self.uncles = uncleHeaders
193
	self.header.UncleHash = rlpHash(uncleHeaders)
O
obscuren 已提交
194 195
}

O
obscuren 已提交
196 197
func (self *Block) Transactions() Transactions {
	return self.transactions
O
obscuren 已提交
198 199
}

200
func (self *Block) Transaction(hash common.Hash) *Transaction {
O
obscuren 已提交
201
	for _, transaction := range self.transactions {
202
		if transaction.Hash() == hash {
O
obscuren 已提交
203
			return transaction
O
obscuren 已提交
204 205
		}
	}
O
obscuren 已提交
206
	return nil
O
obscuren 已提交
207 208
}

O
obscuren 已提交
209 210 211
func (self *Block) SetTransactions(transactions Transactions) {
	self.transactions = transactions
	self.header.TxHash = DeriveSha(transactions)
O
obscuren 已提交
212
}
O
obscuren 已提交
213 214 215 216
func (self *Block) AddTransaction(transaction *Transaction) {
	self.transactions = append(self.transactions, transaction)
	self.SetTransactions(self.transactions)
}
O
obscuren 已提交
217

O
obscuren 已提交
218 219
func (self *Block) Receipts() Receipts {
	return self.receipts
O
obscuren 已提交
220 221
}

O
obscuren 已提交
222 223 224 225
func (self *Block) SetReceipts(receipts Receipts) {
	self.receipts = receipts
	self.header.ReceiptHash = DeriveSha(receipts)
	self.header.Bloom = CreateBloom(receipts)
226
}
O
obscuren 已提交
227 228 229 230
func (self *Block) AddReceipt(receipt *Receipt) {
	self.receipts = append(self.receipts, receipt)
	self.SetReceipts(self.receipts)
}
231

O
obscuren 已提交
232 233 234 235 236 237 238 239 240
func (self *Block) RlpData() interface{} {
	return []interface{}{self.header, self.transactions, self.uncles}
}

func (self *Block) RlpDataForStorage() interface{} {
	return []interface{}{self.header, self.transactions, self.uncles, self.Td /* TODO receipts */}
}

// Header accessors (add as you need them)
O
obscuren 已提交
241 242 243
func (self *Block) Number() *big.Int       { return self.header.Number }
func (self *Block) NumberU64() uint64      { return self.header.Number.Uint64() }
func (self *Block) MixDigest() common.Hash { return self.header.MixDigest }
O
obscuren 已提交
244
func (self *Block) Nonce() uint64 {
O
obscuren 已提交
245
	return binary.BigEndian.Uint64(self.header.Nonce[:])
O
obscuren 已提交
246 247
}
func (self *Block) SetNonce(nonce uint64) {
O
obscuren 已提交
248
	self.header.SetNonce(nonce)
O
obscuren 已提交
249 250
}

O
obscuren 已提交
251 252
func (self *Block) Bloom() Bloom             { return self.header.Bloom }
func (self *Block) Coinbase() common.Address { return self.header.Coinbase }
O
obscuren 已提交
253 254 255
func (self *Block) Time() int64              { return int64(self.header.Time) }
func (self *Block) GasLimit() *big.Int       { return self.header.GasLimit }
func (self *Block) GasUsed() *big.Int        { return self.header.GasUsed }
O
obscuren 已提交
256 257
func (self *Block) Root() common.Hash        { return self.header.Root }
func (self *Block) SetRoot(root common.Hash) { self.header.Root = root }
258 259 260 261 262 263 264 265 266 267 268 269
func (self *Block) GetTransaction(i int) *Transaction {
	if len(self.transactions) > i {
		return self.transactions[i]
	}
	return nil
}
func (self *Block) GetUncle(i int) *Header {
	if len(self.uncles) > i {
		return self.uncles[i]
	}
	return nil
}
O
obscuren 已提交
270

271 272 273 274 275 276 277 278 279 280 281 282 283
func (self *Block) Size() common.StorageSize {
	c := writeCounter(0)
	rlp.Encode(&c, self)
	return common.StorageSize(c)
}

type writeCounter common.StorageSize

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

O
Merge  
obscuren 已提交
284
// Implement pow.Block
O
obscuren 已提交
285 286
func (self *Block) Difficulty() *big.Int     { return self.header.Difficulty }
func (self *Block) HashNoNonce() common.Hash { return self.header.HashNoNonce() }
O
Merge  
obscuren 已提交
287

O
obscuren 已提交
288 289
func (self *Block) Hash() common.Hash {
	if (self.HeaderHash != common.Hash{}) {
O
Merge  
obscuren 已提交
290 291 292 293 294 295
		return self.HeaderHash
	} else {
		return self.header.Hash()
	}
}

O
obscuren 已提交
296 297
func (self *Block) ParentHash() common.Hash {
	if (self.ParentHeaderHash != common.Hash{}) {
O
Merge  
obscuren 已提交
298 299 300 301
		return self.ParentHeaderHash
	} else {
		return self.header.ParentHash
	}
O
obscuren 已提交
302 303
}

O
obscuren 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
func (self *Block) Copy() *Block {
	block := NewBlock(self.ParentHash(), self.Coinbase(), self.Root(), self.Difficulty(), self.Nonce(), self.header.Extra)
	block.header.Bloom = self.header.Bloom
	block.header.TxHash = self.header.TxHash
	block.transactions = self.transactions
	block.header.UncleHash = self.header.UncleHash
	block.uncles = self.uncles
	block.header.GasLimit.Set(self.header.GasLimit)
	block.header.GasUsed.Set(self.header.GasUsed)
	block.header.ReceiptHash = self.header.ReceiptHash
	block.header.Difficulty.Set(self.header.Difficulty)
	block.header.Number = self.header.Number
	block.header.Time = self.header.Time
	block.header.MixDigest = self.header.MixDigest
	return block
}

O
obscuren 已提交
321
func (self *Block) String() string {
322
	return fmt.Sprintf(`BLOCK(%x): Size: %v TD: %v {
O
obscuren 已提交
323
NoNonce: %x
O
obscuren 已提交
324 325
Header:
[
O
obscuren 已提交
326
%v
O
obscuren 已提交
327 328
]
Transactions:
O
obscuren 已提交
329
%v
O
obscuren 已提交
330
Uncles:
O
obscuren 已提交
331 332
%v
}
O
obscuren 已提交
333
`, self.header.Hash(), self.Size(), self.Td, self.header.HashNoNonce(), self.header, self.transactions, self.uncles)
O
obscuren 已提交
334 335 336
}

func (self *Header) String() string {
O
obscuren 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350
	return fmt.Sprintf(`
	ParentHash:	    %x
	UncleHash:	    %x
	Coinbase:	    %x
	Root:		    %x
	TxSha		    %x
	ReceiptSha:	    %x
	Bloom:		    %x
	Difficulty:	    %v
	Number:		    %v
	GasLimit:	    %v
	GasUsed:	    %v
	Time:		    %v
	Extra:		    %v
O
obscuren 已提交
351 352
	MixDigest:          %x
	Nonce:		    %x`,
O
obscuren 已提交
353
		self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.MixDigest, self.Nonce)
M
Maran 已提交
354
}
O
obscuren 已提交
355

O
obscuren 已提交
356
type Blocks []*Block
357

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

O
obscuren 已提交
360 361 362 363
func (self BlockBy) Sort(blocks Blocks) {
	bs := blockSorter{
		blocks: blocks,
		by:     self,
O
obscuren 已提交
364
	}
O
obscuren 已提交
365
	sort.Sort(bs)
O
obscuren 已提交
366
}
O
obscuren 已提交
367

O
obscuren 已提交
368 369 370
type blockSorter struct {
	blocks Blocks
	by     func(b1, b2 *Block) bool
O
obscuren 已提交
371
}
O
obscuren 已提交
372

O
obscuren 已提交
373 374 375
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 已提交
376
}
O
obscuren 已提交
377
func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
O
obscuren 已提交
378

O
obscuren 已提交
379
func Number(b1, b2 *Block) bool { return b1.Header().Number.Cmp(b2.Header().Number) < 0 }