From 6163173ea07fe5a8e96d6c84bfff141b5dd1d470 Mon Sep 17 00:00:00 2001 From: David Ansermino Date: Sat, 8 Sep 2018 04:38:57 -0600 Subject: [PATCH] Changed signaturs to match aura namespace (#7) --- consensus/aura/api.go | 29 +++++----- consensus/aura/aura.go | 122 ++++++++++++++++++++--------------------- 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/consensus/aura/api.go b/consensus/aura/api.go index fa40a80cf034..3a65a9e5360d 100644 --- a/consensus/aura/api.go +++ b/consensus/aura/api.go @@ -27,8 +27,7 @@ import ( // mechanisms of the proof-of-authority scheme. type API struct { chain consensus.ChainReader - //clique *Clique - //aura *Aura + aura *Aura } // GetSnapshot retrieves the state snapshot at a given block. @@ -44,7 +43,7 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) { if header == nil { return nil, errUnknownBlock } - return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + return api.aura.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) } // GetSnapshotAtHash retrieves the state snapshot at a given block. @@ -53,7 +52,7 @@ func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) { if header == nil { return nil, errUnknownBlock } - return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + return api.aura.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) } // GetSigners retrieves the list of authorized signers at the specified block. @@ -69,7 +68,7 @@ func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) { if header == nil { return nil, errUnknownBlock } - snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + snap, err := api.aura.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) if err != nil { return nil, err } @@ -82,7 +81,7 @@ func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) { if header == nil { return nil, errUnknownBlock } - snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + snap, err := api.aura.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) if err != nil { return nil, err } @@ -91,11 +90,11 @@ func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) { // Proposals returns the current proposals the node tries to uphold and vote on. func (api *API) Proposals() map[common.Address]bool { - api.clique.lock.RLock() - defer api.clique.lock.RUnlock() + api.aura.lock.RLock() + defer api.aura.lock.RUnlock() proposals := make(map[common.Address]bool) - for address, auth := range api.clique.proposals { + for address, auth := range api.aura.proposals { proposals[address] = auth } return proposals @@ -104,17 +103,17 @@ func (api *API) Proposals() map[common.Address]bool { // Propose injects a new authorization proposal that the signer will attempt to // push through. func (api *API) Propose(address common.Address, auth bool) { - api.clique.lock.Lock() - defer api.clique.lock.Unlock() + api.aura.lock.Lock() + defer api.aura.lock.Unlock() - api.clique.proposals[address] = auth + api.aura.proposals[address] = auth } // Discard drops a currently running proposal, stopping the signer from casting // further votes (either for or against). func (api *API) Discard(address common.Address) { - api.clique.lock.Lock() - defer api.clique.lock.Unlock() + api.aura.lock.Lock() + defer api.aura.lock.Unlock() - delete(api.clique.proposals, address) + delete(api.aura.proposals, address) } diff --git a/consensus/aura/aura.go b/consensus/aura/aura.go index 584f2723de9d..78c5f54b7ecb 100644 --- a/consensus/aura/aura.go +++ b/consensus/aura/aura.go @@ -208,7 +208,7 @@ type Aura struct { // New creates a Clique proof-of-authority consensus engine with the initial // signers set to the ones provided by the user. -func New(config *params.CliqueConfig, db ethdb.Database) *Clique { +func New(config *params.AuraConfig, db ethdb.Database) *Aura { // Set any missing consensus parameters to their defaults conf := *config if conf.Epoch == 0 { @@ -218,7 +218,7 @@ func New(config *params.CliqueConfig, db ethdb.Database) *Clique { recents, _ := lru.NewARC(inmemorySnapshots) signatures, _ := lru.NewARC(inmemorySignatures) - return &Clique{ + return &Aura{ config: &conf, db: db, recents: recents, @@ -229,25 +229,25 @@ func New(config *params.CliqueConfig, db ethdb.Database) *Clique { // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. -func (c *Aura) Author(header *types.Header) (common.Address, error) { - return ecrecover(header, c.signatures) +func (a *Aura) Author(header *types.Header) (common.Address, error) { + return ecrecover(header, a.signatures) } // VerifyHeader checks whether a header conforms to the consensus rules. -func (c *Aura) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { - return c.verifyHeader(chain, header, nil) +func (a *Aura) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { + return a.verifyHeader(chain, header, nil) } // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The // method returns a quit channel to abort the operations and a results channel to // retrieve the async verifications (the order is that of the input slice). -func (c *Aura) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { +func (a *Aura) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { abort := make(chan struct{}) results := make(chan error, len(headers)) go func() { for i, header := range headers { - err := c.verifyHeader(chain, header, headers[:i]) + err := a.verifyHeader(chain, header, headers[:i]) select { case <-abort: @@ -263,7 +263,7 @@ func (c *Aura) VerifyHeaders(chain consensus.ChainReader, headers []*types.Heade // caller may optionally pass in a batch of parents (ascending order) to avoid // looking those up from the database. This is useful for concurrently verifying // a batch of new headers. -func (c *Aura) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { +func (a *Aura) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { if header.Number == nil { return errUnknownBlock } @@ -274,7 +274,7 @@ func (c *Aura) verifyHeader(chain consensus.ChainReader, header *types.Header, p return consensus.ErrFutureBlock } // Checkpoint blocks need to enforce zero beneficiary - checkpoint := (number % c.config.Epoch) == 0 + checkpoint := (number % a.config.Epoch) == 0 if checkpoint && header.Coinbase != (common.Address{}) { return errInvalidCheckpointBeneficiary } @@ -319,14 +319,14 @@ func (c *Aura) verifyHeader(chain consensus.ChainReader, header *types.Header, p return err } // All basic checks passed, verify cascading fields - return c.verifyCascadingFields(chain, header, parents) + return a.verifyCascadingFields(chain, header, parents) } // verifyCascadingFields verifies all the header fields that are not standalone, // rather depend on a batch of previous headers. The caller may optionally pass // in a batch of parents (ascending order) to avoid looking those up from the // database. This is useful for concurrently verifying a batch of new headers. -func (c *Aura) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { +func (a *Aura) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { // The genesis block is the always valid dead-end number := header.Number.Uint64() if number == 0 { @@ -342,16 +342,16 @@ func (c *Aura) verifyCascadingFields(chain consensus.ChainReader, header *types. if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { return consensus.ErrUnknownAncestor } - if parent.Time.Uint64()+c.config.Period > header.Time.Uint64() { + if parent.Time.Uint64()+a.config.Period > header.Time.Uint64() { return ErrInvalidTimestamp } // Retrieve the snapshot needed to verify this header and cache it - snap, err := c.snapshot(chain, number-1, header.ParentHash, parents) + snap, err := a.snapshot(chain, number-1, header.ParentHash, parents) if err != nil { return err } // If the block is a checkpoint block, verify the signer list - if number%c.config.Epoch == 0 { + if number%a.config.Epoch == 0 { signers := make([]byte, len(snap.Signers)*common.AddressLength) for i, signer := range snap.signers() { copy(signers[i*common.AddressLength:], signer[:]) @@ -362,11 +362,11 @@ func (c *Aura) verifyCascadingFields(chain consensus.ChainReader, header *types. } } // All basic checks passed, verify the seal and return - return c.verifySeal(chain, header, parents) + return a.verifySeal(chain, header, parents) } // snapshot retrieves the authorization snapshot at a given point in time. -func (c *Aura) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { +func (a *Aura) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { // Search for a snapshot in memory or on disk for checkpoints var ( headers []*types.Header @@ -374,20 +374,20 @@ func (c *Aura) snapshot(chain consensus.ChainReader, number uint64, hash common. ) for snap == nil { // If an in-memory snapshot was found, use that - if s, ok := c.recents.Get(hash); ok { + if s, ok := a.recents.Get(hash); ok { snap = s.(*Snapshot) break } // If an on-disk checkpoint snapshot can be found, use that if number%checkpointInterval == 0 { - if s, err := loadSnapshot(c.config, c.signatures, c.db, hash); err == nil { + if s, err := loadSnapshot(a.config, a.signatures, a.db, hash); err == nil { log.Trace("Loaded voting snapshot from disk", "number", number, "hash", hash) snap = s break } } // If we're at an checkpoint block, make a snapshot if it's known - if number%c.config.Epoch == 0 { + if number%a.config.Epoch == 0 { checkpoint := chain.GetHeaderByNumber(number) if checkpoint != nil { hash := checkpoint.Hash() @@ -396,8 +396,8 @@ func (c *Aura) snapshot(chain consensus.ChainReader, number uint64, hash common. for i := 0; i < len(signers); i++ { copy(signers[i][:], checkpoint.Extra[extraVanity+i*common.AddressLength:]) } - snap = newSnapshot(c.config, c.signatures, number, hash, signers) - if err := snap.store(c.db); err != nil { + snap = newSnapshot(a.config, a.signatures, number, hash, signers) + if err := snap.store(a.db); err != nil { return nil, err } log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash) @@ -431,11 +431,11 @@ func (c *Aura) snapshot(chain consensus.ChainReader, number uint64, hash common. if err != nil { return nil, err } - c.recents.Add(snap.Hash, snap) + a.recents.Add(snap.Hash, snap) // If we've generated a new checkpoint snapshot, save to disk if snap.Number%checkpointInterval == 0 && len(headers) > 0 { - if err = snap.store(c.db); err != nil { + if err = snap.store(a.db); err != nil { return nil, err } log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash) @@ -445,7 +445,7 @@ func (c *Aura) snapshot(chain consensus.ChainReader, number uint64, hash common. // VerifyUncles implements consensus.Engine, always returning an error for any // uncles as this consensus mechanism doesn't permit uncles. -func (c *Aura) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { +func (a *Aura) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { if len(block.Uncles()) > 0 { return errors.New("uncles not allowed") } @@ -454,28 +454,28 @@ func (c *Aura) VerifyUncles(chain consensus.ChainReader, block *types.Block) err // VerifySeal implements consensus.Engine, checking whether the signature contained // in the header satisfies the consensus protocol requirements. -func (c *Aura) VerifySeal(chain consensus.ChainReader, header *types.Header) error { - return c.verifySeal(chain, header, nil) +func (a *Aura) VerifySeal(chain consensus.ChainReader, header *types.Header) error { + return a.verifySeal(chain, header, nil) } // verifySeal checks whether the signature contained in the header satisfies the // consensus protocol requirements. The method accepts an optional list of parent // headers that aren't yet part of the local blockchain to generate the snapshots // from. -func (c *Aura) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { +func (a *Aura) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { // Verifying the genesis block is not supported number := header.Number.Uint64() if number == 0 { return errUnknownBlock } // Retrieve the snapshot needed to verify this header and cache it - snap, err := c.snapshot(chain, number-1, header.ParentHash, parents) + snap, err := a.snapshot(chain, number-1, header.ParentHash, parents) if err != nil { return err } // Resolve the authorization key and check against signers - signer, err := ecrecover(header, c.signatures) + signer, err := ecrecover(header, a.signatures) if err != nil { return err } @@ -503,23 +503,23 @@ func (c *Aura) verifySeal(chain consensus.ChainReader, header *types.Header, par // Prepare implements consensus.Engine, preparing all the consensus fields of the // header for running the transactions on top. -func (c *Aura) Prepare(chain consensus.ChainReader, header *types.Header) error { +func (a *Aura) Prepare(chain consensus.ChainReader, header *types.Header) error { // If the block isn't a checkpoint, cast a random vote (good enough for now) header.Coinbase = common.Address{} header.Nonce = types.BlockNonce{} number := header.Number.Uint64() // Assemble the voting snapshot to check which votes make sense - snap, err := c.snapshot(chain, number-1, header.ParentHash, nil) + snap, err := a.snapshot(chain, number-1, header.ParentHash, nil) if err != nil { return err } - if number%c.config.Epoch != 0 { - c.lock.RLock() + if number%a.config.Epoch != 0 { + a.lock.RLock() // Gather all the proposals that make sense voting on - addresses := make([]common.Address, 0, len(c.proposals)) - for address, authorize := range c.proposals { + addresses := make([]common.Address, 0, len(a.proposals)) + for address, authorize := range a.proposals { if snap.validVote(address, authorize) { addresses = append(addresses, address) } @@ -527,16 +527,16 @@ func (c *Aura) Prepare(chain consensus.ChainReader, header *types.Header) error // If there's pending proposals, cast a vote on them if len(addresses) > 0 { header.Coinbase = addresses[rand.Intn(len(addresses))] - if c.proposals[header.Coinbase] { + if a.proposals[header.Coinbase] { copy(header.Nonce[:], nonceAuthVote) } else { copy(header.Nonce[:], nonceDropVote) } } - c.lock.RUnlock() + a.lock.RUnlock() } // Set the correct difficulty - header.Difficulty = CalcDifficulty(snap, c.signer) + header.Difficulty = CalcDifficulty(snap, a.signer) // Ensure the extra data has all it's components if len(header.Extra) < extraVanity { @@ -544,7 +544,7 @@ func (c *Aura) Prepare(chain consensus.ChainReader, header *types.Header) error } header.Extra = header.Extra[:extraVanity] - if number%c.config.Epoch == 0 { + if number%a.config.Epoch == 0 { for _, signer := range snap.signers() { header.Extra = append(header.Extra, signer[:]...) } @@ -559,7 +559,7 @@ func (c *Aura) Prepare(chain consensus.ChainReader, header *types.Header) error if parent == nil { return consensus.ErrUnknownAncestor } - header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(c.config.Period)) + header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(a.config.Period)) if header.Time.Int64() < time.Now().Unix() { header.Time = big.NewInt(time.Now().Unix()) } @@ -568,7 +568,7 @@ func (c *Aura) Prepare(chain consensus.ChainReader, header *types.Header) error // Finalize implements consensus.Engine, ensuring no uncles are set, nor block // rewards given, and returns the final block. -func (c *Aura) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { +func (a *Aura) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { // No block rewards in PoA, so the state remains as is and uncles are dropped header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) header.UncleHash = types.CalcUncleHash(nil) @@ -579,17 +579,17 @@ func (c *Aura) Finalize(chain consensus.ChainReader, header *types.Header, state // Authorize injects a private key into the consensus engine to mint new blocks // with. -func (c *Aura) Authorize(signer common.Address, signFn SignerFn) { - c.lock.Lock() - defer c.lock.Unlock() +func (a *Aura) Authorize(signer common.Address, signFn SignerFn) { + a.lock.Lock() + defer a.lock.Unlock() - c.signer = signer - c.signFn = signFn + a.signer = signer + a.signFn = signFn } // Seal implements consensus.Engine, attempting to create a sealed block using // the local signing credentials. -func (c *Aura) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { +func (a *Aura) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { header := block.Header() // Sealing the genesis block is not supported @@ -598,16 +598,16 @@ func (c *Aura) Seal(chain consensus.ChainReader, block *types.Block, results cha return errUnknownBlock } // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing) - if c.config.Period == 0 && len(block.Transactions()) == 0 { + if a.config.Period == 0 && len(block.Transactions()) == 0 { return errWaitTransactions } // Don't hold the signer fields for the entire sealing procedure - c.lock.RLock() - signer, signFn := c.signer, c.signFn - c.lock.RUnlock() + a.lock.RLock() + signer, signFn := a.signer, a.signFn + a.lock.RUnlock() // Bail out if we're unauthorized to sign a block - snap, err := c.snapshot(chain, number-1, header.ParentHash, nil) + snap, err := a.snapshot(chain, number-1, header.ParentHash, nil) if err != nil { return err } @@ -651,7 +651,7 @@ func (c *Aura) Seal(chain consensus.ChainReader, block *types.Block, results cha select { case results <- block.WithSeal(header): default: - log.Warn("Sealing result is not read by miner", "sealhash", c.SealHash(header)) + log.Warn("Sealing result is not read by miner", "sealhash", a.SealHash(header)) } }() @@ -661,12 +661,12 @@ func (c *Aura) Seal(chain consensus.ChainReader, block *types.Block, results cha // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // that a new block should have based on the previous blocks in the chain and the // current signer. -func (c *Aura) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { - snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil) +func (a *Aura) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { + snap, err := a.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil) if err != nil { return nil } - return CalcDifficulty(snap, c.signer) + return CalcDifficulty(snap, a.signer) } // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty @@ -680,22 +680,22 @@ func CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int { } // SealHash returns the hash of a block prior to it being sealed. -func (c *Aura) SealHash(header *types.Header) common.Hash { +func (a *Aura) SealHash(header *types.Header) common.Hash { return sigHash(header) } // Close implements consensus.Engine. It's a noop for clique as there is are no background threads. -func (c *Aura) Close() error { +func (a *Aura) Close() error { return nil } // APIs implements consensus.Engine, returning the user facing RPC API to allow // controlling the signer voting. -func (c *Aura) APIs(chain consensus.ChainReader) []rpc.API { +func (a *Aura) APIs(chain consensus.ChainReader) []rpc.API { return []rpc.API{{ Namespace: "clique", Version: "1.0", - Service: &API{chain: chain, clique: c}, + Service: &API{chain: chain, aura: a}, Public: false, }} } \ No newline at end of file