Skip to content

Commit

Permalink
fix(clique utils): make use of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Mar 13, 2019
1 parent df6fd20 commit 3a999a6
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions ethcore/src/engines/clique/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

use engines::EngineError;
use engines::clique::{ADDRESS_LENGTH, SIGNATURE_LENGTH, VANITY_LENGTH, NULL_NONCE, NULL_MIXHASH};
use error::Error;
use ethereum_types::{Address, H256};
use ethkey::{public_to_address, recover as ec_recover, Signature};
use lru_cache::LruCache;
use parking_lot::RwLock;
use rlp::encode;

use engines::clique::{ADDRESS_LENGTH, SIGNATURE_LENGTH, VANITY_LENGTH, NULL_NONCE, NULL_MIXHASH};
use error::Error;
use ethkey::{public_to_address, recover as ec_recover, Signature};
use types::header::Header;

/// How many recovered signature to cache in the memory.
Expand All @@ -42,13 +42,16 @@ pub fn recover_creator(header: &Header) -> Result<Address, Error> {
}

let data = header.extra_data();
if data.len() < VANITY_LENGTH {
Err(EngineError::CliqueMissingVanity)?
}

if data.len() < VANITY_LENGTH + SIGNATURE_LENGTH {
return Err(From::from("extra_data length is not enough!"));
Err(EngineError::CliqueMissingSignature)?
}

// Get vanity and signature data from `header.extra_data`
// Note, this shouldn't have a list of signers because this is only called on `non-checkpoints`
let (vanity_slice, signature_slice) = data.split_at(data.len() - SIGNATURE_LENGTH);
// Split `signed_extra data` and `signature`
let (signed_data_slice, signature_slice) = data.split_at(data.len() - SIGNATURE_LENGTH);

// convert `&[u8]` to `[u8; 65]`
let signature = {
Expand All @@ -59,7 +62,7 @@ pub fn recover_creator(header: &Header) -> Result<Address, Error> {

// modify header and hash it
let unsigned_header = &mut header.clone();
unsigned_header.set_extra_data(vanity_slice.to_vec());
unsigned_header.set_extra_data(signed_data_slice.to_vec());
let msg = unsigned_header.hash();

let pubkey = ec_recover(&Signature::from(signature), &msg)?;
Expand All @@ -81,14 +84,14 @@ pub fn extract_signers(header: &Header) -> Result<Vec<Address>, Error> {
let data = header.extra_data();

if data.len() <= VANITY_LENGTH + SIGNATURE_LENGTH {
return Err(Box::new("Invalid extra_data size.").into());
Err(EngineError::CliqueCheckpointNoSigner)?
}

// extract only the portion of extra_data which includes the signer list
let signers_raw = &data[(VANITY_LENGTH)..data.len() - (SIGNATURE_LENGTH)];

if signers_raw.len() % ADDRESS_LENGTH != 0 {
return Err(Box::new("bad signer list.").into());
Err(EngineError::CliqueCheckpointInvalidSigners(signers_raw.len()))?
}

let num_signers = signers_raw.len() / 20;
Expand Down

0 comments on commit 3a999a6

Please sign in to comment.