block conversion
This commit is contained in:
parent
e620bde405
commit
bfcd2cf132
@ -27,7 +27,7 @@ type Header struct {
|
|||||||
// Receipt sha
|
// Receipt sha
|
||||||
ReceiptHash common.Hash
|
ReceiptHash common.Hash
|
||||||
// Bloom
|
// Bloom
|
||||||
Bloom [256]byte
|
Bloom Bloom
|
||||||
// Difficulty for the current block
|
// Difficulty for the current block
|
||||||
Difficulty *big.Int
|
Difficulty *big.Int
|
||||||
// The block number
|
// The block number
|
||||||
@ -73,18 +73,20 @@ func (self *Header) RlpData() interface{} {
|
|||||||
return self.rlpData(true)
|
return self.rlpData(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Header) Hash() []byte {
|
func (self *Header) Hash() common.Hash {
|
||||||
return crypto.Sha3(common.Encode(self.rlpData(true)))
|
return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(true))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Header) HashNoNonce() []byte {
|
func (self *Header) HashNoNonce() common.Hash {
|
||||||
return crypto.Sha3(common.Encode(self.rlpData(false)))
|
return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(false))))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
// Preset Hash for mock
|
// Preset Hash for mock (Tests)
|
||||||
HeaderHash []byte
|
HeaderHash common.Hash
|
||||||
ParentHeaderHash []byte
|
ParentHeaderHash common.Hash
|
||||||
|
// ^^^^ ignore ^^^^
|
||||||
|
|
||||||
header *Header
|
header *Header
|
||||||
uncles []*Header
|
uncles []*Header
|
||||||
transactions Transactions
|
transactions Transactions
|
||||||
@ -94,7 +96,7 @@ type Block struct {
|
|||||||
Reward *big.Int
|
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{
|
header := &Header{
|
||||||
Root: root,
|
Root: root,
|
||||||
ParentHash: parentHash,
|
ParentHash: parentHash,
|
||||||
@ -113,8 +115,7 @@ func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.I
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Header) setNonce(nonce uint64) {
|
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 {
|
func NewBlockWithHeader(header *Header) *Block {
|
||||||
@ -148,7 +149,7 @@ func (self *Block) Uncles() []*Header {
|
|||||||
|
|
||||||
func (self *Block) SetUncles(uncleHeaders []*Header) {
|
func (self *Block) SetUncles(uncleHeaders []*Header) {
|
||||||
self.uncles = uncleHeaders
|
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 {
|
func (self *Block) Transactions() Transactions {
|
||||||
@ -198,21 +199,21 @@ func (self *Block) RlpDataForStorage() interface{} {
|
|||||||
// Header accessors (add as you need them)
|
// Header accessors (add as you need them)
|
||||||
func (self *Block) Number() *big.Int { return self.header.Number }
|
func (self *Block) Number() *big.Int { return self.header.Number }
|
||||||
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
|
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
|
||||||
func (self *Block) MixDigest() []byte { return self.header.MixDigest }
|
func (self *Block) MixDigest() common.Hash { return self.header.MixDigest }
|
||||||
func (self *Block) Nonce() uint64 {
|
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) {
|
func (self *Block) SetNonce(nonce uint64) {
|
||||||
self.header.setNonce(nonce)
|
self.header.setNonce(nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Block) Bloom() []byte { return self.header.Bloom }
|
func (self *Block) Bloom() Bloom { return self.header.Bloom }
|
||||||
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
|
func (self *Block) Coinbase() common.Address { return self.header.Coinbase }
|
||||||
func (self *Block) Time() int64 { return int64(self.header.Time) }
|
func (self *Block) Time() int64 { return int64(self.header.Time) }
|
||||||
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
|
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
|
||||||
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
|
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
|
||||||
func (self *Block) Root() []byte { return self.header.Root }
|
func (self *Block) Root() common.Hash { return self.header.Root }
|
||||||
func (self *Block) SetRoot(root []byte) { self.header.Root = 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) Size() common.StorageSize { return common.StorageSize(len(common.Encode(self))) }
|
||||||
func (self *Block) GetTransaction(i int) *Transaction {
|
func (self *Block) GetTransaction(i int) *Transaction {
|
||||||
if len(self.transactions) > i {
|
if len(self.transactions) > i {
|
||||||
@ -229,18 +230,18 @@ func (self *Block) GetUncle(i int) *Header {
|
|||||||
|
|
||||||
// Implement pow.Block
|
// Implement pow.Block
|
||||||
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
|
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
|
||||||
func (self *Block) HashNoNonce() []byte { return self.header.HashNoNonce() }
|
func (self *Block) HashNoNonce() common.Hash { return self.header.HashNoNonce() }
|
||||||
|
|
||||||
func (self *Block) Hash() []byte {
|
func (self *Block) Hash() common.Hash {
|
||||||
if self.HeaderHash != nil {
|
if (self.HeaderHash != common.Hash{}) {
|
||||||
return self.HeaderHash
|
return self.HeaderHash
|
||||||
} else {
|
} else {
|
||||||
return self.header.Hash()
|
return self.header.Hash()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Block) ParentHash() []byte {
|
func (self *Block) ParentHash() common.Hash {
|
||||||
if self.ParentHeaderHash != nil {
|
if (self.ParentHeaderHash != common.Hash{}) {
|
||||||
return self.ParentHeaderHash
|
return self.ParentHeaderHash
|
||||||
} else {
|
} else {
|
||||||
return self.header.ParentHash
|
return self.header.ParentHash
|
||||||
|
@ -1 +1,20 @@
|
|||||||
package types
|
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)
|
||||||
|
}
|
||||||
|
@ -3,18 +3,18 @@ package types
|
|||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/state"
|
"github.com/ethereum/go-ethereum/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateBloom(receipts Receipts) []byte {
|
func CreateBloom(receipts Receipts) Bloom {
|
||||||
bin := new(big.Int)
|
bin := new(big.Int)
|
||||||
for _, receipt := range receipts {
|
for _, receipt := range receipts {
|
||||||
bin.Or(bin, LogsBloom(receipt.logs))
|
bin.Or(bin, LogsBloom(receipt.logs))
|
||||||
}
|
}
|
||||||
|
|
||||||
return common.LeftPadBytes(bin.Bytes(), 256)
|
return BytesToBloom(bin.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func LogsBloom(logs state.Logs) *big.Int {
|
func LogsBloom(logs state.Logs) *big.Int {
|
||||||
|
@ -5,3 +5,22 @@ import "math/big"
|
|||||||
type BlockProcessor interface {
|
type BlockProcessor interface {
|
||||||
Process(*Block) (*big.Int, error)
|
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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,12 +11,12 @@ type DerivableList interface {
|
|||||||
GetRlp(i int) []byte
|
GetRlp(i int) []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeriveSha(list DerivableList) []byte {
|
func DeriveSha(list DerivableList) common.Hash {
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie := trie.New(nil, db)
|
trie := trie.New(nil, db)
|
||||||
for i := 0; i < list.Len(); i++ {
|
for i := 0; i < list.Len(); i++ {
|
||||||
trie.Update(common.Encode(i), list.GetRlp(i))
|
trie.Update(common.Encode(i), list.GetRlp(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
return trie.Root()
|
return common.BytesToHash(trie.Root())
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ func (self *StateDB) RawDump() World {
|
|||||||
|
|
||||||
it := self.trie.Iterator()
|
it := self.trie.Iterator()
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
fmt.Printf("%x\n", it.Key, len(it.Key))
|
|
||||||
stateObject := NewStateObjectFromBytes(common.BytesToAddress(it.Key), it.Value, self.db)
|
stateObject := NewStateObjectFromBytes(common.BytesToAddress(it.Key), it.Value, self.db)
|
||||||
|
|
||||||
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
|
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
|
||||||
|
Loading…
Reference in New Issue
Block a user