mirror of https://github.com/status-im/op-geth.git
block conversion
This commit is contained in:
parent
e620bde405
commit
bfcd2cf132
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ func (self *StateDB) RawDump() World {
|
|||
|
||||
it := self.trie.Iterator()
|
||||
for it.Next() {
|
||||
fmt.Printf("%x\n", it.Key, len(it.Key))
|
||||
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)}
|
||||
|
|
Loading…
Reference in New Issue