Skip to content

Commit

Permalink
dccs: re-apply dccs consensus to master branch (ethereum#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadv authored Jul 8, 2019
1 parent 0241bfd commit 0122acd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions consensus/dccs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
}

// GetSigners retrieves the list of authorized signers at the specified block.
func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
func (api *API) GetSigners(number *rpc.BlockNumber) ([]Signer, error) {
// Retrieve the requested block number (or current if none requested)
var header *types.Header
if number == nil || *number == rpc.LatestBlockNumber {
Expand All @@ -76,7 +76,7 @@ func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
}

// GetSignersAtHash retrieves the list of authorized signers at the specified block.
func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
func (api *API) GetSignersAtHash(hash common.Hash) ([]Signer, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
Expand Down
6 changes: 3 additions & 3 deletions consensus/dccs/dccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ func (d *Dccs) verifyCascadingFields2(chain consensus.ChainReader, header *types
if d.config.IsCheckpoint(number) {
signers := make([]byte, len(snap.Signers)*common.AddressLength)
for i, signer := range snap.signers2() {
copy(signers[i*common.AddressLength:], signer[:])
copy(signers[i*common.AddressLength:], signer.Address[:])
}
extraSuffix := len(header.Extra) - extraSeal
if !bytes.Equal(header.Extra[extraVanity:extraSuffix], signers) {
Expand Down Expand Up @@ -842,7 +842,7 @@ func (d *Dccs) prepare2(chain consensus.ChainReader, header *types.Header) error

if d.config.IsCheckpoint(number) {
for _, signer := range snap.signers2() {
header.Extra = append(header.Extra, signer[:]...)
header.Extra = append(header.Extra, signer.Address[:]...)
}
}
header.Extra = append(header.Extra, make([]byte, extraSeal)...)
Expand Down Expand Up @@ -1205,7 +1205,7 @@ func (d *Dccs) calcDelayTime(snap *Snapshot, block *types.Block, signer common.A
sigs := snap.signers2()
pos := 0
for seen, sig := range sigs {
if sig == signer {
if sig.Address == signer {
pos = seen
}
}
Expand Down
45 changes: 40 additions & 5 deletions consensus/dccs/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)

// Vote represents a single vote that an authorized signer made to modify the
Expand Down Expand Up @@ -66,6 +68,23 @@ func (s signersAscending) Len() int { return len(s) }
func (s signersAscending) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
func (s signersAscending) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// Signer contain data of signer's address and checkpoint block hash
type Signer struct {
Hash common.Hash
Address common.Address
}

// signersAscendingByHash implements the sort interface to allow sorting a list of signers by hash
type signersAscendingByHash []Signer

func (sn signersAscendingByHash) Len() int { return len(sn) }
func (sn signersAscendingByHash) Less(i, j int) bool {
h1 := rlpHash(sn[i])
h2 := rlpHash(sn[j])
return bytes.Compare(h1[:], h2[:]) < 0
}
func (sn signersAscendingByHash) Swap(i, j int) { sn[i], sn[j] = sn[j], sn[i] }

// newSnapshot creates a new snapshot with the specified startup parameters. This
// method does not initialize the set of recent signers, so only ever use if for
// the genesis block.
Expand Down Expand Up @@ -316,9 +335,14 @@ func (s *Snapshot) signers() []common.Address {
return sigs
}

// signers retrieves the list of authorized signers in ascending order.
func (s *Snapshot) signers2() []common.Address {
return s.signers()
// signers2 retrieves the list of authorized signers in hash ascending order.
func (s *Snapshot) signers2() []Signer {
sigs := make([]Signer, 0, len(s.Signers))
for sig := range s.Signers {
sigs = append(sigs, Signer{Hash: s.Hash, Address: sig})
}
sort.Sort(signersAscendingByHash(sigs))
return sigs
}

// inturn returns if a signer at a given block height is in-turn or not.
Expand All @@ -341,9 +365,9 @@ func (s *Snapshot) inturn2(signer common.Address, parent *types.Header) bool {
return offset == 0
}

func signerPosition(signer common.Address, signers []common.Address) (int, bool) {
func signerPosition(signer common.Address, signers []Signer) (int, bool) {
for i, sig := range signers {
if sig == signer {
if sig.Address == signer {
return i, true
}
}
Expand Down Expand Up @@ -408,3 +432,14 @@ func (s *Snapshot) difficulty(signer common.Address, parent *types.Header) uint6

return uint64(n - offset)
}

// rlpHash return hash of an input
func rlpHash(s Signer) (h common.Hash) {
hasher := sha3.NewLegacyKeccak256()
rlp.Encode(hasher, []interface{}{
s.Address,
s.Hash,
})
hasher.Sum(h[:0])
return h
}

0 comments on commit 0122acd

Please sign in to comment.