* 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":[ "authorities":[
"0x0082a7bf6aaadab094061747872243059c3c6a07", "0x0082a7bf6aaadab094061747872243059c3c6a07",
"0x00faa37564140c1a5e96095f05466b9f73441e44"], "0x00faa37564140c1a5e96095f05466b9f73441e44"],
"difficulty" : 131072 "difficulty" : 131072,
"signatures": ["gA==","uEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACsHcD/7g=="]
} }
}, },
"nonce": "0x0", "nonce": "0x0",

View file

@ -286,7 +286,7 @@ func (a *Aura) verifyHeader(chain consensus.ChainReader, header *types.Header, p
return errInvalidExtraData return errInvalidExtraData
} }
// Ensure that the mix digest is zero as we don't have fork protection currently // 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 return errInvalidMixDigest
} }
// Ensure that the block doesn't contain any uncles which are meaningless in PoA // 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) root := statedb.IntermediateRoot(false)
head := &types.Header{ head := &types.Header{
Number: new(big.Int).SetUint64(g.Number), Number: new(big.Int).SetUint64(g.Number),
Nonce: types.EncodeNonce(g.Nonce), Nonce: types.EncodeNonce(g.Nonce),
@ -247,12 +249,21 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
Coinbase: g.Coinbase, Coinbase: g.Coinbase,
Root: root, Root: root,
} }
if g.Config.Aura != nil {
for _, signature := range g.Config.Aura.Signatures {
head.Signatures = append(head.Signatures, signature)
}
}
if g.GasLimit == 0 { if g.GasLimit == 0 {
head.GasLimit = params.GenesisGasLimit head.GasLimit = params.GenesisGasLimit
} }
if g.Difficulty == nil { if g.Difficulty == nil {
head.Difficulty = params.GenesisDifficulty head.Difficulty = params.GenesisDifficulty
} }
data,_ := rlp.EncodeToBytes(head)
fmt.Printf("Genesis header rlp %v\n", common.Bytes2Hex(data))
statedb.Commit(false) statedb.Commit(false)
statedb.Database().TrieDB().Commit(root, true) statedb.Database().TrieDB().Commit(root, true)
@ -332,6 +343,7 @@ func DefaultRinkebyGenesisBlock() *Genesis {
Alloc: decodePrealloc(rinkebyAllocData), Alloc: decodePrealloc(rinkebyAllocData),
} }
} }
//Need alloc data //Need alloc data
// DefaultGoerliGenesisBlock returns the Goerli network genesis block. // DefaultGoerliGenesisBlock returns the Goerli network genesis block.
func DefaultGoerliGenesisBlock() *Genesis { func DefaultGoerliGenesisBlock() *Genesis {

View file

@ -19,6 +19,7 @@ package types
import ( import (
"encoding/binary" "encoding/binary"
"github.com/ethereum/go-ethereum/params"
"io" "io"
"math/big" "math/big"
"sort" "sort"
@ -65,24 +66,46 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
} }
//go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go //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. // Header represents a block header in the Ethereum blockchain.
type Header struct { type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"` ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"` UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"` Coinbase common.Address `json:"miner" gencodec:"required"`
Root common.Hash `json:"stateRoot" gencodec:"required"` Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"` TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"` ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom Bloom `json:"logsBloom" gencodec:"required"` Bloom Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *big.Int `json:"difficulty" gencodec:"required"` Difficulty *big.Int `json:"difficulty" gencodec:"required"`
Number *big.Int `json:"number" gencodec:"required"` Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"` GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"` GasUsed uint64 `json:"gasUsed" gencodec:"required"`
Time *big.Int `json:"timestamp" gencodec:"required"` Time *big.Int `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData" gencodec:"required"` Extra []byte `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash" gencodec:"required"` MixDigest common.Hash `json:"mixHash" gencodec:"required"`
Nonce BlockNonce `json:"nonce" 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 // field type overrides for gencodec
@ -96,10 +119,108 @@ type headerMarshaling struct {
Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON 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 // Hash returns the block hash of the header, which is simply the keccak256 hash of its
// RLP encoding. // RLP encoding.
func (h *Header) Hash() common.Hash { func (header *Header) Hash() (h common.Hash) {
return rlpHash(h) //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 // Size returns the approximate memory used by all internal contents. It is used
@ -288,9 +409,11 @@ func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) } func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
func (b *Block) Time() *big.Int { return new(big.Int).Set(b.header.Time) } 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) NumberU64() uint64 { return b.header.Number.Uint64() }
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest } func (b *Block) MixDigest() common.Hash {
func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) } return common.Hash{}
}
func (b *Block) Nonce() uint64 { return 0 }
func (b *Block) Bloom() Bloom { return b.header.Bloom } func (b *Block) Bloom() Bloom { return b.header.Bloom }
func (b *Block) Coinbase() common.Address { return b.header.Coinbase } func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
func (b *Block) Root() common.Hash { return b.header.Root } func (b *Block) Root() common.Hash { return b.header.Root }

View file

@ -9,28 +9,29 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/params"
) )
var _ = (*headerMarshaling)(nil) var _ = (*headerMarshaling)(nil)
// MarshalJSON marshals as JSON.
func (h Header) MarshalJSON() ([]byte, error) { func (h Header) MarshalJSON() ([]byte, error) {
type Header struct { type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"` ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"` UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"` Coinbase common.Address `json:"miner" gencodec:"required"`
Root common.Hash `json:"stateRoot" gencodec:"required"` Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"` TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"` ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom Bloom `json:"logsBloom" gencodec:"required"` Bloom Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
Number *hexutil.Big `json:"number" gencodec:"required"` Number *hexutil.Big `json:"number" gencodec:"required"`
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"` GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"` GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time *hexutil.Big `json:"timestamp" gencodec:"required"` Time *hexutil.Big `json:"timestamp" gencodec:"required"`
Extra hexutil.Bytes `json:"extraData" gencodec:"required"` Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash" gencodec:"required"` Signatures params.Signatures `json:"signature,omitempty"`
Nonce BlockNonce `json:"nonce" gencodec:"required"` Hash common.Hash `json:"hash"`
Hash common.Hash `json:"hash"`
} }
var enc Header var enc Header
enc.ParentHash = h.ParentHash enc.ParentHash = h.ParentHash
@ -46,29 +47,28 @@ func (h Header) MarshalJSON() ([]byte, error) {
enc.GasUsed = hexutil.Uint64(h.GasUsed) enc.GasUsed = hexutil.Uint64(h.GasUsed)
enc.Time = (*hexutil.Big)(h.Time) enc.Time = (*hexutil.Big)(h.Time)
enc.Extra = h.Extra enc.Extra = h.Extra
enc.MixDigest = h.MixDigest enc.Signatures = h.Signatures
enc.Nonce = h.Nonce
enc.Hash = h.Hash() enc.Hash = h.Hash()
return json.Marshal(&enc) return json.Marshal(&enc)
} }
// UnmarshalJSON unmarshals from JSON.
func (h *Header) UnmarshalJSON(input []byte) error { func (h *Header) UnmarshalJSON(input []byte) error {
type Header struct { type Header struct {
ParentHash *common.Hash `json:"parentHash" gencodec:"required"` ParentHash *common.Hash `json:"parentHash" gencodec:"required"`
UncleHash *common.Hash `json:"sha3Uncles" gencodec:"required"` UncleHash *common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase *common.Address `json:"miner" gencodec:"required"` Coinbase *common.Address `json:"miner" gencodec:"required"`
Root *common.Hash `json:"stateRoot" gencodec:"required"` Root *common.Hash `json:"stateRoot" gencodec:"required"`
TxHash *common.Hash `json:"transactionsRoot" gencodec:"required"` TxHash *common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash *common.Hash `json:"receiptsRoot" gencodec:"required"` ReceiptHash *common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom *Bloom `json:"logsBloom" gencodec:"required"` Bloom *Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
Number *hexutil.Big `json:"number" gencodec:"required"` Number *hexutil.Big `json:"number" gencodec:"required"`
GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"` GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"` GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time *hexutil.Big `json:"timestamp" gencodec:"required"` Time *hexutil.Big `json:"timestamp" gencodec:"required"`
Extra *hexutil.Bytes `json:"extraData" gencodec:"required"` Extra *hexutil.Bytes `json:"extraData" gencodec:"required"`
MixDigest *common.Hash `json:"mixHash" gencodec:"required"` Signatures *params.Signatures `json:"signature,omitempty"`
Nonce *BlockNonce `json:"nonce" gencodec:"required"`
} }
var dec Header var dec Header
if err := json.Unmarshal(input, &dec); err != nil { 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") return errors.New("missing required field 'extraData' for Header")
} }
h.Extra = *dec.Extra h.Extra = *dec.Extra
if dec.MixDigest == nil { if dec.Signatures != nil {
return errors.New("missing required field 'mixHash' for Header") 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 return nil
} }

View file

@ -18,8 +18,8 @@ package params
import ( import (
"fmt" "fmt"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"math/big"
) )
// Genesis hashes to enforce below configs on. // Genesis hashes to enforce below configs on.
@ -81,12 +81,12 @@ var (
// GoerliChainConfig contains the chain parameters to run a node on the Goerli test network. // GoerliChainConfig contains the chain parameters to run a node on the Goerli test network.
GoerliChainConfig = &ChainConfig{ GoerliChainConfig = &ChainConfig{
ChainID: big.NewInt(6382), ChainID: big.NewInt(6382),
HomesteadBlock: big.NewInt(0), HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil, DAOForkBlock: nil,
DAOForkSupport: false, DAOForkSupport: false,
// EIP150Block: big.NewInt(0), // EIP150Block: big.NewInt(0),
// EIP150Hash: common.HexToHash("0X0000000000000000000000000000000000000000000000000000000000000000"), // EIP150Hash: common.HexToHash("0X0000000000000000000000000000000000000000000000000000000000000000"),
EIP155Block: big.NewInt(0), EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0), EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0), ByzantiumBlock: big.NewInt(0),
@ -96,7 +96,7 @@ var (
common.HexToAddress("0x0082a7bf6aaadab094061747872243059c3c6a07"), common.HexToAddress("0x0082a7bf6aaadab094061747872243059c3c6a07"),
common.HexToAddress("0x00faa37564140c1a5e96095f05466b9f73441e44"), common.HexToAddress("0x00faa37564140c1a5e96095f05466b9f73441e44"),
}, },
Difficulty: big.NewInt(131072), Difficulty: big.NewInt(131072),
}, },
} }
@ -146,7 +146,7 @@ type ChainConfig struct {
// Various consensus engines // Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"` Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"` Clique *CliqueConfig `json:"clique,omitempty"`
Aura *AuraConfig `json:"aura,omitempty"` Aura *AuraConfig `json:"aura,omitempty"`
} }
// EthashConfig is the consensus engine configs for proof-of-work based sealing. // EthashConfig is the consensus engine configs for proof-of-work based sealing.
@ -163,12 +163,16 @@ type CliqueConfig struct {
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint 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. // AuraConfig is the consensus engine configs for proof-of-authority based sealing.
type AuraConfig struct { type AuraConfig struct {
Period uint64 `json:"period"` // Number of seconds between blocks to enforce Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Authorities []common.Address `json:"authorities"` // list of addresses of authorities Authorities []common.Address `json:"authorities"` // list of addresses of authorities
Difficulty *big.Int `json:"difficulty"` // Constant block difficulty Difficulty *big.Int `json:"difficulty"` // Constant block difficulty
Signatures Signatures `json:"signatures"`
} }
// String implements the stringer interface, returning the consensus engine details. // String implements the stringer interface, returning the consensus engine details.