aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-03-17 00:27:23 +0800
committerobscuren <geffobscura@gmail.com>2015-03-17 00:27:23 +0800
commitbfcd2cf132c2f1e5c1afe6d71e3d9d9dcee5017b (patch)
treef3eb1f9fabc0ffe9be1a781fbbc04407049d7b7a /core
parente620bde405161771ea7ecd1cee8641eb9265465e (diff)
downloaddexon-bfcd2cf132c2f1e5c1afe6d71e3d9d9dcee5017b.tar.gz
dexon-bfcd2cf132c2f1e5c1afe6d71e3d9d9dcee5017b.tar.zst
dexon-bfcd2cf132c2f1e5c1afe6d71e3d9d9dcee5017b.zip
block conversion
Diffstat (limited to 'core')
-rw-r--r--core/types/block.go61
-rw-r--r--core/types/block_test.go19
-rw-r--r--core/types/bloom9.go6
-rw-r--r--core/types/common.go19
-rw-r--r--core/types/derive_sha.go6
5 files changed, 75 insertions, 36 deletions
diff --git a/core/types/block.go b/core/types/block.go
index fadf67086..9a97061b7 100644
--- a/core/types/block.go
+++ b/core/types/block.go
@@ -27,7 +27,7 @@ type Header struct {
// Receipt sha
ReceiptHash common.Hash
// Bloom
- Bloom [256]byte
+ Bloom Bloom
// Difficulty for the current block
Difficulty *big.Int
// The block number
@@ -73,28 +73,30 @@ func (self *Header) RlpData() interface{} {
return self.rlpData(true)
}
-func (self *Header) Hash() []byte {
- return crypto.Sha3(common.Encode(self.rlpData(true)))
+func (self *Header) Hash() common.Hash {
+ return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(true))))
}
-func (self *Header) HashNoNonce() []byte {
- return crypto.Sha3(common.Encode(self.rlpData(false)))
+func (self *Header) HashNoNonce() common.Hash {
+ return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(false))))
}
type Block struct {
- // Preset Hash for mock
- HeaderHash []byte
- ParentHeaderHash []byte
- header *Header
- uncles []*Header
- transactions Transactions
- Td *big.Int
+ // Preset Hash for mock (Tests)
+ HeaderHash common.Hash
+ ParentHeaderHash common.Hash
+ // ^^^^ ignore ^^^^
+
+ header *Header
+ uncles []*Header
+ transactions Transactions
+ Td *big.Int
receipts Receipts
Reward *big.Int
}
-func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce uint64, extra string) *Block {
+func NewBlock(parentHash common.Hash, coinbase common.Address, root common.Hash, difficulty *big.Int, nonce uint64, extra string) *Block {
header := &Header{
Root: root,
ParentHash: parentHash,
@@ -113,8 +115,7 @@ func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.I
}
func (self *Header) setNonce(nonce uint64) {
- self.Nonce = make([]byte, 8)
- binary.BigEndian.PutUint64(self.Nonce, nonce)
+ binary.BigEndian.PutUint64(self.Nonce[:], nonce)
}
func NewBlockWithHeader(header *Header) *Block {
@@ -148,7 +149,7 @@ func (self *Block) Uncles() []*Header {
func (self *Block) SetUncles(uncleHeaders []*Header) {
self.uncles = uncleHeaders
- self.header.UncleHash = crypto.Sha3(common.Encode(uncleHeaders))
+ self.header.UncleHash = common.BytesToHash(crypto.Sha3(common.Encode(uncleHeaders)))
}
func (self *Block) Transactions() Transactions {
@@ -196,23 +197,23 @@ func (self *Block) RlpDataForStorage() interface{} {
}
// Header accessors (add as you need them)
-func (self *Block) Number() *big.Int { return self.header.Number }
-func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
-func (self *Block) MixDigest() []byte { return self.header.MixDigest }
+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 }
func (self *Block) Nonce() uint64 {
- return binary.BigEndian.Uint64(self.header.Nonce)
+ return binary.BigEndian.Uint64(self.header.Nonce[:])
}
func (self *Block) SetNonce(nonce uint64) {
self.header.setNonce(nonce)
}
-func (self *Block) Bloom() []byte { return self.header.Bloom }
-func (self *Block) Coinbase() []byte { return self.header.Coinbase }
+func (self *Block) Bloom() Bloom { return self.header.Bloom }
+func (self *Block) Coinbase() common.Address { return self.header.Coinbase }
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 }
-func (self *Block) Root() []byte { return self.header.Root }
-func (self *Block) SetRoot(root []byte) { self.header.Root = root }
+func (self *Block) Root() common.Hash { return self.header.Root }
+func (self *Block) SetRoot(root common.Hash) { self.header.Root = root }
func (self *Block) Size() common.StorageSize { return common.StorageSize(len(common.Encode(self))) }
func (self *Block) GetTransaction(i int) *Transaction {
if len(self.transactions) > i {
@@ -228,19 +229,19 @@ func (self *Block) GetUncle(i int) *Header {
}
// Implement pow.Block
-func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
-func (self *Block) HashNoNonce() []byte { return self.header.HashNoNonce() }
+func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
+func (self *Block) HashNoNonce() common.Hash { return self.header.HashNoNonce() }
-func (self *Block) Hash() []byte {
- if self.HeaderHash != nil {
+func (self *Block) Hash() common.Hash {
+ if (self.HeaderHash != common.Hash{}) {
return self.HeaderHash
} else {
return self.header.Hash()
}
}
-func (self *Block) ParentHash() []byte {
- if self.ParentHeaderHash != nil {
+func (self *Block) ParentHash() common.Hash {
+ if (self.ParentHeaderHash != common.Hash{}) {
return self.ParentHeaderHash
} else {
return self.header.ParentHash
diff --git a/core/types/block_test.go b/core/types/block_test.go
index ab1254f4c..e2fa2cad2 100644
--- a/core/types/block_test.go
+++ b/core/types/block_test.go
@@ -1 +1,20 @@
package types
+
+import (
+ "fmt"
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+func TestConversion(t *testing.T) {
+ var (
+ parent common.Hash
+ coinbase common.Address
+ hash common.Hash
+ )
+
+ block := NewBlock(parent, coinbase, hash, big.NewInt(0), 0, "")
+ fmt.Println(block)
+}
diff --git a/core/types/bloom9.go b/core/types/bloom9.go
index af76f226f..e5b5e395f 100644
--- a/core/types/bloom9.go
+++ b/core/types/bloom9.go
@@ -3,18 +3,18 @@ package types
import (
"math/big"
- "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/state"
)
-func CreateBloom(receipts Receipts) []byte {
+func CreateBloom(receipts Receipts) Bloom {
bin := new(big.Int)
for _, receipt := range receipts {
bin.Or(bin, LogsBloom(receipt.logs))
}
- return common.LeftPadBytes(bin.Bytes(), 256)
+ return BytesToBloom(bin.Bytes())
}
func LogsBloom(logs state.Logs) *big.Int {
diff --git a/core/types/common.go b/core/types/common.go
index 795374959..dd5dc00c8 100644
--- a/core/types/common.go
+++ b/core/types/common.go
@@ -5,3 +5,22 @@ import "math/big"
type BlockProcessor interface {
Process(*Block) (*big.Int, error)
}
+
+type Bloom [256]byte
+
+func BytesToBloom(b []byte) Bloom {
+ var bloom Bloom
+ bloom.SetBytes(b)
+ return bloom
+}
+
+func (b *Bloom) SetBytes(d []byte) {
+ if len(b) > len(d) {
+ panic("bloom bytes too big")
+ }
+
+ // reverse loop
+ for i := len(d) - 1; i >= 0; i-- {
+ b[i] = b[i]
+ }
+}
diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go
index 593a31f1c..0fc45a508 100644
--- a/core/types/derive_sha.go
+++ b/core/types/derive_sha.go
@@ -1,8 +1,8 @@
package types
import (
- "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie"
)
@@ -11,12 +11,12 @@ type DerivableList interface {
GetRlp(i int) []byte
}
-func DeriveSha(list DerivableList) []byte {
+func DeriveSha(list DerivableList) common.Hash {
db, _ := ethdb.NewMemDatabase()
trie := trie.New(nil, db)
for i := 0; i < list.Len(); i++ {
trie.Update(common.Encode(i), list.GetRlp(i))
}
- return trie.Root()
+ return common.BytesToHash(trie.Root())
}