aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/block.go
diff options
context:
space:
mode:
Diffstat (limited to 'ethchain/block.go')
-rw-r--r--ethchain/block.go53
1 files changed, 47 insertions, 6 deletions
diff --git a/ethchain/block.go b/ethchain/block.go
index 5765abd51..157be2a52 100644
--- a/ethchain/block.go
+++ b/ethchain/block.go
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"math/big"
+ "sort"
_ "strconv"
"time"
@@ -31,11 +32,45 @@ func (bi *BlockInfo) RlpEncode() []byte {
return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent})
}
+type Blocks []*Block
+
+func (self Blocks) AsSet() ethutil.UniqueSet {
+ set := make(ethutil.UniqueSet)
+ for _, block := range self {
+ set.Insert(block.Hash())
+ }
+
+ return set
+}
+
+type BlockBy func(b1, b2 *Block) bool
+
+func (self BlockBy) Sort(blocks Blocks) {
+ bs := blockSorter{
+ blocks: blocks,
+ by: self,
+ }
+ sort.Sort(bs)
+}
+
+type blockSorter struct {
+ blocks Blocks
+ by func(b1, b2 *Block) bool
+}
+
+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]
+}
+func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
+
+func Number(b1, b2 *Block) bool { return b1.Number.Cmp(b2.Number) < 0 }
+
type Block struct {
// Hash to the previous block
- PrevHash []byte
+ PrevHash ethutil.Bytes
// Uncles of this block
- Uncles []*Block
+ Uncles Blocks
UncleSha []byte
// The coin base address
Coinbase []byte
@@ -57,7 +92,7 @@ type Block struct {
// Extra data
Extra string
// Block Nonce for verification
- Nonce []byte
+ Nonce ethutil.Bytes
// List of transactions and/or contracts
transactions []*Transaction
receipts []*Receipt
@@ -106,8 +141,9 @@ func CreateBlock(root interface{},
}
// Returns a hash of the block
-func (block *Block) Hash() []byte {
- return ethcrypto.Sha3Bin(block.Value().Encode())
+func (block *Block) Hash() ethutil.Bytes {
+ return ethcrypto.Sha3Bin(ethutil.NewValue(block.header()).Encode())
+ //return ethcrypto.Sha3Bin(block.Value().Encode())
}
func (block *Block) HashNoNonce() []byte {
@@ -351,7 +387,7 @@ func (block *Block) header() []interface{} {
func (block *Block) String() string {
return fmt.Sprintf(`
- BLOCK(%x):
+ BLOCK(%x): Size: %v
PrevHash: %x
UncleSha: %x
Coinbase: %x
@@ -368,6 +404,7 @@ func (block *Block) String() string {
NumTx: %v
`,
block.Hash(),
+ block.Size(),
block.PrevHash,
block.UncleSha,
block.Coinbase,
@@ -384,3 +421,7 @@ func (block *Block) String() string {
len(block.transactions),
)
}
+
+func (self *Block) Size() ethutil.StorageSize {
+ return ethutil.StorageSize(len(self.RlpEncode()))
+}