mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-12 15:03:45 +00:00
Merge pull request #4 from maticnetwork/bor-consensus-fix
Bor consensus fix
This commit is contained in:
commit
810fd3adba
4 changed files with 163 additions and 207 deletions
|
|
@ -108,9 +108,10 @@ func (w *wizard) makeGenesis() {
|
||||||
genesis.Difficulty = big.NewInt(1)
|
genesis.Difficulty = big.NewInt(1)
|
||||||
genesis.GasLimit = 10000000
|
genesis.GasLimit = 10000000
|
||||||
genesis.Config.Bor = ¶ms.BorConfig{
|
genesis.Config.Bor = ¶ms.BorConfig{
|
||||||
Period: 1,
|
Period: 1,
|
||||||
ProducerDelay: 5,
|
ProducerDelay: 5,
|
||||||
Epoch: 60,
|
Sprint: 60,
|
||||||
|
ValidatorContract: "0x0000000000000000000000000000000000001000",
|
||||||
}
|
}
|
||||||
|
|
||||||
// We also need the initial list of signers
|
// We also need the initial list of signers
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -80,7 +80,6 @@ func newSnapshot(config *params.BorConfig, sigcache *lru.ARCCache, number uint64
|
||||||
Recents: make(map[uint64]common.Address),
|
Recents: make(map[uint64]common.Address),
|
||||||
// Tally: make(map[common.Address]Tally),
|
// Tally: make(map[common.Address]Tally),
|
||||||
}
|
}
|
||||||
fmt.Println("New validator set", "number", number, "proposer", snap.ValidatorSet.Proposer.Address.Hex())
|
|
||||||
return snap
|
return snap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,6 +97,9 @@ func loadSnapshot(config *params.BorConfig, sigcache *lru.ARCCache, db ethdb.Dat
|
||||||
snap.sigcache = sigcache
|
snap.sigcache = sigcache
|
||||||
snap.ethAPI = ethAPI
|
snap.ethAPI = ethAPI
|
||||||
|
|
||||||
|
// update total voting power
|
||||||
|
snap.ValidatorSet.updateTotalVotingPower()
|
||||||
|
|
||||||
return snap, nil
|
return snap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,10 +128,6 @@ func (s *Snapshot) copy() *Snapshot {
|
||||||
for block, signer := range s.Recents {
|
for block, signer := range s.Recents {
|
||||||
cpy.Recents[block] = signer
|
cpy.Recents[block] = signer
|
||||||
}
|
}
|
||||||
// for address, tally := range s.Tally {
|
|
||||||
// cpy.Tally[address] = tally
|
|
||||||
// }
|
|
||||||
// copy(cpy.Votes, s.Votes)
|
|
||||||
|
|
||||||
return cpy
|
return cpy
|
||||||
}
|
}
|
||||||
|
|
@ -198,10 +196,6 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
|
||||||
for _, header := range headers {
|
for _, header := range headers {
|
||||||
// Remove any votes on checkpoint blocks
|
// Remove any votes on checkpoint blocks
|
||||||
number := header.Number.Uint64()
|
number := header.Number.Uint64()
|
||||||
if (number+1)%s.config.Sprint == 0 {
|
|
||||||
// snap.Votes = nil
|
|
||||||
// snap.Tally = make(map[common.Address]Tally)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the oldest signer from the recent list to allow it signing again
|
// Delete the oldest signer from the recent list to allow it signing again
|
||||||
if number >= s.config.Sprint && number-s.config.Sprint >= 0 {
|
if number >= s.config.Sprint && number-s.config.Sprint >= 0 {
|
||||||
|
|
@ -214,6 +208,18 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// change validator set and change proposer
|
||||||
|
if number > 0 && (number+1)%s.config.Sprint == 0 {
|
||||||
|
validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal]
|
||||||
|
|
||||||
|
// newVals, _ := GetValidators(number, number+1, s.config.Sprint, s.config.ValidatorContract, snap.ethAPI)
|
||||||
|
newVals, _ := ParseValidators(validatorBytes)
|
||||||
|
v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals)
|
||||||
|
v.IncrementProposerPriority(1)
|
||||||
|
snap.ValidatorSet = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if signer is in validator set
|
||||||
if !snap.ValidatorSet.HasAddress(signer.Bytes()) {
|
if !snap.ValidatorSet.HasAddress(signer.Bytes()) {
|
||||||
return nil, errUnauthorizedSigner
|
return nil, errUnauthorizedSigner
|
||||||
}
|
}
|
||||||
|
|
@ -248,80 +254,12 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
|
||||||
snap.Recents[number] = signer
|
snap.Recents[number] = signer
|
||||||
// TODO remove
|
// TODO remove
|
||||||
fmt.Println("Recent signer", "number", number, "signer", signer.Hex())
|
fmt.Println("Recent signer", "number", number, "signer", signer.Hex())
|
||||||
// change proposer on epoch
|
|
||||||
if number > 0 && (number+1)%s.config.Sprint == 0 {
|
|
||||||
newVals, _ := GetValidators(number, s.config.Sprint, s.config.ValidatorContract, snap.ethAPI)
|
|
||||||
v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals)
|
|
||||||
v.IncrementProposerPriority(1)
|
|
||||||
snap.ValidatorSet = v
|
|
||||||
fmt.Println("New validator set", "number", number, "proposer", v.Proposer.Address.Hex())
|
|
||||||
}
|
|
||||||
// // Header authorized, discard any previous votes from the signer
|
|
||||||
// for i, vote := range snap.Votes {
|
|
||||||
// if vote.Signer == signer && vote.Address == header.Coinbase {
|
|
||||||
// // Uncast the vote from the cached tally
|
|
||||||
// snap.uncast(vote.Address, vote.Authorize)
|
|
||||||
|
|
||||||
// // Uncast the vote from the chronological list
|
|
||||||
// snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
|
|
||||||
// break // only one vote allowed
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// Tally up the new vote from the signer
|
|
||||||
// var authorize bool
|
|
||||||
// switch {
|
|
||||||
// case bytes.Equal(header.Nonce[:], nonceAuthVote):
|
|
||||||
// authorize = true
|
|
||||||
// case bytes.Equal(header.Nonce[:], nonceDropVote):
|
|
||||||
// authorize = false
|
|
||||||
// default:
|
|
||||||
// return nil, errInvalidVote
|
|
||||||
// }
|
|
||||||
// if snap.cast(header.Coinbase, authorize) {
|
|
||||||
// snap.Votes = append(snap.Votes, &Vote{
|
|
||||||
// Signer: signer,
|
|
||||||
// Block: number,
|
|
||||||
// Address: header.Coinbase,
|
|
||||||
// Authorize: authorize,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// If the vote passed, update the list of signers
|
|
||||||
// if tally := snap.Tally[header.Coinbase]; tally.Votes > len(snap.Signers)/2 {
|
|
||||||
// if tally.Authorize {
|
|
||||||
// snap.Signers[header.Coinbase] = struct{}{}
|
|
||||||
// } else {
|
|
||||||
// delete(snap.Signers, header.Coinbase)
|
|
||||||
|
|
||||||
// // Signer list shrunk, delete any leftover recent caches
|
|
||||||
// if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
|
|
||||||
// delete(snap.Recents, number-limit)
|
|
||||||
// }
|
|
||||||
// // Discard any previous votes the deauthorized signer cast
|
|
||||||
// for i := 0; i < len(snap.Votes); i++ {
|
|
||||||
// if snap.Votes[i].Signer == header.Coinbase {
|
|
||||||
// // Uncast the vote from the cached tally
|
|
||||||
// snap.uncast(snap.Votes[i].Address, snap.Votes[i].Authorize)
|
|
||||||
|
|
||||||
// // Uncast the vote from the chronological list
|
|
||||||
// snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
|
|
||||||
|
|
||||||
// i--
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// // Discard any previous votes around the just changed account
|
|
||||||
// for i := 0; i < len(snap.Votes); i++ {
|
|
||||||
// if snap.Votes[i].Address == header.Coinbase {
|
|
||||||
// snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
|
|
||||||
// i--
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// delete(snap.Tally, header.Coinbase)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
snap.Number += uint64(len(headers))
|
snap.Number += uint64(len(headers))
|
||||||
snap.Hash = headers[len(headers)-1].Hash()
|
snap.Hash = headers[len(headers)-1].Hash()
|
||||||
|
|
||||||
|
fmt.Println("Current validator set", "number", snap.Number, "validatorSet", snap.ValidatorSet)
|
||||||
|
|
||||||
return snap, nil
|
return snap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,9 @@ package bor
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
|
@ -86,3 +88,39 @@ func (v *Validator) Bytes() []byte {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HeaderBytes return header bytes
|
||||||
|
func (v *Validator) HeaderBytes() []byte {
|
||||||
|
result := make([]byte, 40)
|
||||||
|
copy(result[:20], v.Address.Bytes())
|
||||||
|
copy(result[20:], v.PowerBytes())
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// PowerBytes return power bytes
|
||||||
|
func (v *Validator) PowerBytes() []byte {
|
||||||
|
powerBytes := big.NewInt(0).SetInt64(v.VotingPower).Bytes()
|
||||||
|
result := make([]byte, 20)
|
||||||
|
copy(result[20-len(powerBytes):], powerBytes)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseValidators returns validator set bytes
|
||||||
|
func ParseValidators(validatorsBytes []byte) ([]*Validator, error) {
|
||||||
|
if len(validatorsBytes)%40 != 0 {
|
||||||
|
return nil, errors.New("Invalid validators bytes")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]*Validator, len(validatorsBytes)/40)
|
||||||
|
for i := 0; i < len(validatorsBytes); i += 40 {
|
||||||
|
address := make([]byte, 20)
|
||||||
|
power := make([]byte, 20)
|
||||||
|
|
||||||
|
copy(address, validatorsBytes[i:i+20])
|
||||||
|
copy(power, validatorsBytes[i+20:i+40])
|
||||||
|
|
||||||
|
result[i/40] = NewValidator(common.BytesToAddress(address), big.NewInt(0).SetBytes(power).Int64())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue