* aura: genesis + init-script

* aura genesis

* core, params: wip on aura, still not working
This commit is contained in:
Martin Holst Swende 2018-09-09 02:27:45 +02:00 committed by Priom Chowdhury
parent e177f5294e
commit 1743af1dd3
6 changed files with 209 additions and 74 deletions

View file

@ -12,7 +12,8 @@
"authorities":[
"0x0082a7bf6aaadab094061747872243059c3c6a07",
"0x00faa37564140c1a5e96095f05466b9f73441e44"],
"difficulty" : 131072
"difficulty" : 131072,
"signatures": ["gA==","uEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACsHcD/7g=="]
}
},
"nonce": "0x0",

View file

@ -286,7 +286,7 @@ func (a *Aura) verifyHeader(chain consensus.ChainReader, header *types.Header, p
return errInvalidExtraData
}
// Ensure that the mix digest is zero as we don't have fork protection currently
if header.MixDigest != (common.Hash{}) {
if header.MixDig() != (common.Hash{}) {
return errInvalidMixDigest
}
// Ensure that the block doesn't contain any uncles which are meaningless in PoA

View file

@ -234,6 +234,8 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
}
}
root := statedb.IntermediateRoot(false)
head := &types.Header{
Number: new(big.Int).SetUint64(g.Number),
Nonce: types.EncodeNonce(g.Nonce),
@ -247,12 +249,21 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
Coinbase: g.Coinbase,
Root: root,
}
if g.Config.Aura != nil {
for _, signature := range g.Config.Aura.Signatures {
head.Signatures = append(head.Signatures, signature)
}
}
if g.GasLimit == 0 {
head.GasLimit = params.GenesisGasLimit
}
if g.Difficulty == nil {
head.Difficulty = params.GenesisDifficulty
}
data,_ := rlp.EncodeToBytes(head)
fmt.Printf("Genesis header rlp %v\n", common.Bytes2Hex(data))
statedb.Commit(false)
statedb.Database().TrieDB().Commit(root, true)
@ -332,6 +343,7 @@ func DefaultRinkebyGenesisBlock() *Genesis {
Alloc: decodePrealloc(rinkebyAllocData),
}
}
//Need alloc data
// DefaultGoerliGenesisBlock returns the Goerli network genesis block.
func DefaultGoerliGenesisBlock() *Genesis {

View file

@ -19,6 +19,7 @@ package types
import (
"encoding/binary"
"github.com/ethereum/go-ethereum/params"
"io"
"math/big"
"sort"
@ -65,6 +66,23 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
}
//go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
type AuraHeader struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *big.Int `json:"difficulty" gencodec:"required"`
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
Time *big.Int `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData" gencodec:"required"`
Signature1 []byte `json:"signature1,omitempty"`
Signature2 []byte `json:"signature2,omitempty"`
}
// Header represents a block header in the Ethereum blockchain.
type Header struct {
@ -83,6 +101,11 @@ type Header struct {
Extra []byte `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash" gencodec:"required"`
Nonce BlockNonce `json:"nonce" gencodec:"required"`
Signatures params.Signatures `json:"signature,omitempty"`
}
func (h *Header) MixDig() common.Hash {
return common.Hash{}
}
// field type overrides for gencodec
@ -96,10 +119,108 @@ type headerMarshaling struct {
Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
}
// DecodeRLP decodes the Ethereum
func (h *Header) DecodeRLP(s *rlp.Stream) error {
var aura AuraHeader
// Try decoding as aura header first
if err := s.Decode(&aura); err != nil {
return err
//return s.Decode(h)
}
//Aura decoded fine, now convert to header
h.ParentHash = aura.ParentHash
h.UncleHash = aura.UncleHash
h.Coinbase = aura.Coinbase
h.Root = aura.Root
h.TxHash = aura.TxHash
h.ReceiptHash = aura.ReceiptHash
h.Bloom = aura.Bloom
h.Difficulty = aura.Difficulty
h.Number = aura.Number
h.GasLimit = aura.GasLimit
h.GasUsed = aura.GasUsed
h.Time = aura.Time
h.Extra = aura.Extra
h.MixDigest = common.Hash{}
h.Nonce = BlockNonce{}
h.Signatures = append(h.Signatures, aura.Signature1)
h.Signatures = append(h.Signatures, aura.Signature2)
return nil
}
// EncodeRLP serializes b into the Ethereum RLP block format.
func (header *Header) EncodeRLP(w io.Writer) error {
if false && header.Signatures != nil {
return rlp.Encode(w, []interface{}{
header.ParentHash,
header.UncleHash,
header.Coinbase,
header.Root,
header.TxHash,
header.ReceiptHash,
header.Bloom,
header.Difficulty,
header.Number,
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra,
// Yes, this is butt-ugly
header.Signatures[0],
header.Signatures[1],
})
} else {
return rlp.Encode(w, []interface{}{
header.ParentHash,
header.UncleHash,
header.Coinbase,
header.Root,
header.TxHash,
header.ReceiptHash,
header.Bloom,
header.Difficulty,
header.Number,
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra, // Yes, this will panic if extra is too short
header.MixDigest,
header.Nonce,
})
}
}
// Hash returns the block hash of the header, which is simply the keccak256 hash of its
// RLP encoding.
func (h *Header) Hash() common.Hash {
return rlpHash(h)
func (header *Header) Hash() (h common.Hash) {
//if header.Signatures == nil{
// return rlpHash(h)
//}
hw := sha3.NewKeccak256()
rlp.Encode(hw, []interface{}{
header.ParentHash,
header.UncleHash,
header.Coinbase,
header.Root,
header.TxHash,
header.ReceiptHash,
header.Bloom,
header.Difficulty,
header.Number,
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra,
// Yes, this is butt-ugly
//header.Signatures[0],
//header.Signatures[1],
})
hw.Sum(h[:0])
return h
}
// Size returns the approximate memory used by all internal contents. It is used
@ -289,8 +410,10 @@ func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficu
func (b *Block) Time() *big.Int { return new(big.Int).Set(b.header.Time) }
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }
func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) }
func (b *Block) MixDigest() common.Hash {
return common.Hash{}
}
func (b *Block) Nonce() uint64 { return 0 }
func (b *Block) Bloom() Bloom { return b.header.Bloom }
func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
func (b *Block) Root() common.Hash { return b.header.Root }

View file

@ -9,10 +9,12 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/params"
)
var _ = (*headerMarshaling)(nil)
// MarshalJSON marshals as JSON.
func (h Header) MarshalJSON() ([]byte, error) {
type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
@ -28,8 +30,7 @@ func (h Header) MarshalJSON() ([]byte, error) {
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time *hexutil.Big `json:"timestamp" gencodec:"required"`
Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash" gencodec:"required"`
Nonce BlockNonce `json:"nonce" gencodec:"required"`
Signatures params.Signatures `json:"signature,omitempty"`
Hash common.Hash `json:"hash"`
}
var enc Header
@ -46,12 +47,12 @@ func (h Header) MarshalJSON() ([]byte, error) {
enc.GasUsed = hexutil.Uint64(h.GasUsed)
enc.Time = (*hexutil.Big)(h.Time)
enc.Extra = h.Extra
enc.MixDigest = h.MixDigest
enc.Nonce = h.Nonce
enc.Signatures = h.Signatures
enc.Hash = h.Hash()
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (h *Header) UnmarshalJSON(input []byte) error {
type Header struct {
ParentHash *common.Hash `json:"parentHash" gencodec:"required"`
@ -67,8 +68,7 @@ func (h *Header) UnmarshalJSON(input []byte) error {
GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time *hexutil.Big `json:"timestamp" gencodec:"required"`
Extra *hexutil.Bytes `json:"extraData" gencodec:"required"`
MixDigest *common.Hash `json:"mixHash" gencodec:"required"`
Nonce *BlockNonce `json:"nonce" gencodec:"required"`
Signatures *params.Signatures `json:"signature,omitempty"`
}
var dec Header
if err := json.Unmarshal(input, &dec); err != nil {
@ -126,13 +126,8 @@ func (h *Header) UnmarshalJSON(input []byte) error {
return errors.New("missing required field 'extraData' for Header")
}
h.Extra = *dec.Extra
if dec.MixDigest == nil {
return errors.New("missing required field 'mixHash' for Header")
if dec.Signatures != nil {
h.Signatures = *dec.Signatures
}
h.MixDigest = *dec.MixDigest
if dec.Nonce == nil {
return errors.New("missing required field 'nonce' for Header")
}
h.Nonce = *dec.Nonce
return nil
}

View file

@ -18,8 +18,8 @@ package params
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"math/big"
)
// Genesis hashes to enforce below configs on.
@ -163,12 +163,16 @@ type CliqueConfig struct {
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
}
type Signature []byte
type Signatures []Signature
// AuraConfig is the consensus engine configs for proof-of-authority based sealing.
type AuraConfig struct {
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Authorities []common.Address `json:"authorities"` // list of addresses of authorities
Difficulty *big.Int `json:"difficulty"` // Constant block difficulty
Signatures Signatures `json:"signatures"`
}
// String implements the stringer interface, returning the consensus engine details.