use std::sync::{Weak, Arc};
use client_traits::EngineClient;
use common_types::{
BlockNumber,
header::Header,
errors::{EngineError, VapcoreError, BlockError},
ids::BlockId,
log_entry::LogEntry,
engines::machine::{Call, AuxiliaryData, AuxiliaryRequest},
receipt::Receipt,
};
use vapabi::FunctionOutputDecoder;
use vapabi_contract::use_contract;
use vapory_types::{H256, U256, Address, Bloom};
use tetsy_keccak_hash::keccak;
use tetsy_kvdb::DBValue;
use lazy_static::lazy_static;
use log::{debug, info, trace};
use mashina::Machine;
use memory_cache::MemoryLruCache;
use tetsy_bytes::Bytes;
use parking_lot::RwLock;
use tetsy_rlp::{Rlp, RlpStream};
use tetsy_unexpected::Mismatch;
use super::{SystemCall, ValidatorSet};
use super::simple_list::SimpleList;
use_contract!(validator_set, "res/validator_set.json");
const MEMOIZE_CAPACITY: usize = 500;
const EVENT_NAME: &'static [u8] = &*b"InitiateChange(bytes32,address[])";
lazy_static! {
static ref EVENT_NAME_HASH: H256 = keccak(EVENT_NAME);
}
struct StateProof {
contract_address: Address,
header: Header,
}
impl enjen::StateDependentProof for StateProof {
fn generate_proof(&self, caller: &Call) -> Result<Vec<u8>, String> {
prove_initial(self.contract_address, &self.header, caller)
}
fn check_proof(&self, mashina: &Machine, proof: &[u8]) -> Result<(), String> {
let (header, state_items) = decode_first_proof(&Rlp::new(proof))
.map_err(|e| format!("proof incorrectly encoded: {}", e))?;
if &header != &self.header {
return Err("wrong header in proof".into());
}
check_first_proof(mashina, self.contract_address, header, &state_items).map(|_| ())
}
}
pub struct ValidatorSafeContract {
contract_address: Address,
validators: RwLock<MemoryLruCache<H256, SimpleList>>,
client: RwLock<Option<Weak<dyn EngineClient>>>, }
fn encode_first_proof(header: &Header, state_items: &[Vec<u8>]) -> Bytes {
let mut stream = RlpStream::new_list(2);
stream.append(header).begin_list(state_items.len());
for item in state_items {
stream.append(item);
}
stream.out()
}
fn check_first_proof(mashina: &Machine, contract_address: Address, old_header: Header, state_items: &[DBValue])
-> Result<Vec<Address>, String>
{
use common_types::transaction::{Action, Transaction};
const PROVIDED_GAS: u64 = 50_000_000;
let env_info = tetsy_vm::EnvInfo {
number: old_header.number(),
author: *old_header.author(),
difficulty: *old_header.difficulty(),
gas_limit: PROVIDED_GAS.into(),
timestamp: old_header.timestamp(),
last_hashes: {
let mut last_hashes: Vec<_> = (0..256).map(|_| H256::zero()).collect();
last_hashes[255] = *old_header.parent_hash();
Arc::new(last_hashes)
},
gas_used: 0.into(),
};
let number = old_header.number();
let (data, decoder) = validator_set::functions::get_validators::call();
let from = Address::zero();
let tx = Transaction {
nonce: mashina.account_start_nonce(number),
action: Action::Call(contract_address),
gas: PROVIDED_GAS.into(),
gas_price: U256::default(),
value: U256::default(),
data,
}.fake_sign(from);
let res = executive_state::check_proof(
state_items,
*old_header.state_root(),
&tx,
mashina,
&env_info,
);
match res {
executive_state::ProvedExecution::BadProof => Err("Bad proof".into()),
executive_state::ProvedExecution::Failed(e) => Err(format!("Failed call: {}", e)),
executive_state::ProvedExecution::Complete(e) => decoder.decode(&e.output).map_err(|e| e.to_string()),
}
}
fn decode_first_proof(rlp: &Rlp) -> Result<(Header, Vec<DBValue>), VapcoreError> {
let header = rlp.val_at(0)?;
let state_items = rlp.at(1)?
.iter()
.map(|x| Ok(x.data()?.to_vec()) )
.collect::<Result<_, VapcoreError>>()?;
Ok((header, state_items))
}
fn encode_proof(header: &Header, receipts: &[Receipt]) -> Bytes {
let mut stream = RlpStream::new_list(2);
stream.append(header).append_list(receipts);
stream.drain()
}
fn decode_proof(rlp: &Rlp) -> Result<(Header, Vec<Receipt>), VapcoreError> {
Ok((rlp.val_at(0)?, rlp.list_at(1)?))
}
fn prove_initial(contract_address: Address, header: &Header, caller: &Call) -> Result<Vec<u8>, String> {
use std::cell::RefCell;
let epoch_proof = RefCell::new(None);
let validators = {
let (data, decoder) = validator_set::functions::get_validators::call();
let (value, proof) = caller(contract_address, data)?;
*epoch_proof.borrow_mut() = Some(encode_first_proof(header, &proof));
decoder.decode(&value).map_err(|e| e.to_string())?
};
let proof = epoch_proof.into_inner().expect("epoch_proof always set after call; qed");
trace!(target: "engine", "obtained proof for initial set: {} validators, {} bytes",
validators.len(), proof.len());
info!(target: "engine", "Signal for switch to contract-based validator set.");
info!(target: "engine", "Initial contract validators: {:?}", validators);
Ok(proof)
}
impl ValidatorSafeContract {
pub fn new(contract_address: Address) -> Self {
ValidatorSafeContract {
contract_address,
validators: RwLock::new(MemoryLruCache::new(MEMOIZE_CAPACITY)),
client: RwLock::new(None),
}
}
fn get_list(&self, caller: &Call) -> Option<SimpleList> {
let contract_address = self.contract_address;
let (data, decoder) = validator_set::functions::get_validators::call();
let value = caller(contract_address, data).and_then(|x| decoder.decode(&x.0).map_err(|e| e.to_string()));
match value {
Ok(new) => {
debug!(target: "engine", "Got validator set from contract: {:?}", new);
Some(SimpleList::new(new))
},
Err(s) => {
debug!(target: "engine", "Could not get validator set from contract: {}", s);
None
},
}
}
fn expected_bloom(&self, header: &Header) -> Bloom {
let topics = vec![*EVENT_NAME_HASH, *header.parent_hash()];
debug!(target: "engine", "Expected topics for header {}: {:?}",
header.hash(), topics);
LogEntry {
address: self.contract_address,
topics,
data: Vec::new(), }.bloom()
}
fn extract_from_event(&self, bloom: Bloom, header: &Header, receipts: &[Receipt]) -> Option<SimpleList> {
let check_log = |log: &LogEntry| {
log.address == self.contract_address &&
log.topics.len() == 2 &&
log.topics[0] == *EVENT_NAME_HASH &&
log.topics[1] == *header.parent_hash()
};
let mut decoded_events = receipts.iter()
.rev()
.filter(|r| r.log_bloom.contains_bloom(&bloom))
.flat_map(|r| r.logs.iter())
.filter(move |l| check_log(l))
.filter_map(|log| {
validator_set::events::initiate_change::parse_log((log.topics.clone(), log.data.clone()).into()).ok()
});
match decoded_events.next() {
None => None,
Some(matched_event) => Some(SimpleList::new(matched_event.new_set))
}
}
}
impl ValidatorSet for ValidatorSafeContract {
fn default_caller(&self, id: BlockId) -> Box<Call> {
let client = self.client.read().clone();
Box::new(move |addr, data| client.as_ref()
.and_then(Weak::upgrade)
.ok_or_else(|| "No client!".into())
.and_then(|c| {
match c.as_full_client() {
Some(c) => c.call_contract(id, addr, data),
None => Err("No full client!".into()),
}
})
.map(|out| (out, Vec::new()))) }
fn on_epoch_begin(&self, _first: bool, _header: &Header, caller: &mut SystemCall) -> Result<(), VapcoreError> {
let data = validator_set::functions::finalize_change::encode_input();
caller(self.contract_address, data)
.map(|_| ())
.map_err(EngineError::FailedSystemCall)
.map_err(Into::into)
}
fn genesis_epoch_data(&self, header: &Header, call: &Call) -> Result<Vec<u8>, String> {
prove_initial(self.contract_address, header, call)
}
fn is_epoch_end(&self, _first: bool, _chain_head: &Header) -> Option<Vec<u8>> {
None }
fn signals_epoch_end(&self, first: bool, header: &Header, aux: AuxiliaryData)
-> enjen::EpochChange
{
let receipts = aux.receipts;
if first {
debug!(target: "engine", "signalling transition to fresh contract.");
let state_proof = Arc::new(StateProof {
contract_address: self.contract_address,
header: header.clone(),
});
return enjen::EpochChange::Yes(enjen::Proof::WithState(state_proof as Arc<_>));
}
let bloom = self.expected_bloom(header);
let header_bloom = header.log_bloom();
if &bloom & header_bloom != bloom { return enjen::EpochChange::No }
trace!(target: "engine", "detected epoch change event bloom");
match receipts {
None => enjen::EpochChange::Unsure(AuxiliaryRequest::Receipts),
Some(receipts) => match self.extract_from_event(bloom, header, receipts) {
None => enjen::EpochChange::No,
Some(list) => {
info!(target: "engine", "Signal for transition within contract. New validator list: {:?}",
&*list);
let proof = encode_proof(&header, receipts);
enjen::EpochChange::Yes(enjen::Proof::Known(proof))
}
},
}
}
fn epoch_set(&self, first: bool, mashina: &Machine, _number: BlockNumber, proof: &[u8])
-> Result<(SimpleList, Option<H256>), VapcoreError>
{
let rlp = Rlp::new(proof);
if first {
trace!(target: "engine", "Recovering initial epoch set");
let (old_header, state_items) = decode_first_proof(&rlp)?;
let number = old_header.number();
let old_hash = old_header.hash();
let addresses = check_first_proof(mashina, self.contract_address, old_header, &state_items)
.map_err(EngineError::InsufficientProof)?;
trace!(target: "engine", "Extracted epoch validator set at block #{}: {} addresses",
number, addresses.len());
Ok((SimpleList::new(addresses), Some(old_hash)))
} else {
let (old_header, receipts) = decode_proof(&rlp)?;
let found_root = triehash::ordered_trie_root(
receipts.iter().map(::tetsy_rlp::encode)
);
if found_root != *old_header.receipts_root() {
return Err(BlockError::InvalidReceiptsRoot(
Mismatch { expected: *old_header.receipts_root(), found: found_root }
).into());
}
let bloom = self.expected_bloom(&old_header);
match self.extract_from_event(bloom, &old_header, &receipts) {
Some(list) => {
trace!(target: "engine", "Extracted epoch validator set at block #{}: {} addresses", old_header.number(), list.len());
Ok((list, Some(old_header.hash())))
},
None => Err(EngineError::InsufficientProof("No log event in proof.".into()).into()),
}
}
}
fn contains_with_caller(&self, block_hash: &H256, address: &Address, caller: &Call) -> bool {
let mut guard = self.validators.write();
let maybe_existing = guard
.get_mut(block_hash)
.map(|list| list.contains(block_hash, address));
maybe_existing
.unwrap_or_else(|| self
.get_list(caller)
.map_or(false, |list| {
let contains = list.contains(block_hash, address);
guard.insert(block_hash.clone(), list);
contains
}))
}
fn get_with_caller(&self, block_hash: &H256, nonce: usize, caller: &Call) -> Address {
let mut guard = self.validators.write();
let maybe_existing = guard
.get_mut(block_hash)
.map(|list| list.get(block_hash, nonce));
maybe_existing
.unwrap_or_else(|| self
.get_list(caller)
.map_or_else(Default::default, |list| {
let address = list.get(block_hash, nonce);
guard.insert(block_hash.clone(), list);
address
}))
}
fn count_with_caller(&self, block_hash: &H256, caller: &Call) -> usize {
let mut guard = self.validators.write();
let maybe_existing = guard
.get_mut(block_hash)
.map(|list| list.count(block_hash));
maybe_existing
.unwrap_or_else(|| self
.get_list(caller)
.map_or_else(usize::max_value, |list| {
let address = list.count(block_hash);
guard.insert(block_hash.clone(), list);
address
}))
}
fn register_client(&self, client: Weak<dyn EngineClient>) {
trace!(target: "engine", "Setting up contract caller.");
*self.client.write() = Some(client);
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use accounts::AccountProvider;
use common_types::{
ids::BlockId,
engines::mashina::AuxiliaryRequest,
header::Header,
log_entry::LogEntry,
transaction::{Transaction, Action},
verification::Unverified,
};
use client_traits::{BlockInfo, ChainInfo, ImportBlock, EngineClient, ForceUpdateSealing};
use engine::{EpochChange, Proof};
use vapcore::{
miner::{self, MinerService},
test_helpers::generate_dummy_client_with_spec
};
use tetsy_crypto::publickey::Secret;
use vapory_types::Address;
use tetsy_keccak_hash::keccak;
use rustc_hex::FromHex;
use spec;
use super::super::ValidatorSet;
use super::{ValidatorSafeContract, EVENT_NAME_HASH};
#[test]
fn fetches_validators() {
let client = generate_dummy_client_with_spec(spec::new_validator_safe_contract);
let vc = Arc::new(ValidatorSafeContract::new("0000000000000000000000000000000000000005".parse::<Address>().unwrap()));
vc.register_client(Arc::downgrade(&client) as _);
let last_hash = client.best_block_header().hash();
assert!(vc.contains(&last_hash, &"7d577a597b2742b498cb5cf0c26cdcd726d39e6e".parse::<Address>().unwrap()));
assert!(vc.contains(&last_hash, &"82a978b3f5962a5b0957d9ee9eef472ee55b42f1".parse::<Address>().unwrap()));
}
#[test]
fn knows_validators() {
let _ = env_logger::try_init();
let tap = Arc::new(AccountProvider::transient_provider());
let s0: Secret = keccak("1").into();
let v0 = tap.insert_account(s0.clone(), &"".into()).unwrap();
let v1 = tap.insert_account(keccak("0").into(), &"".into()).unwrap();
let chain_id = spec::new_validator_safe_contract().chain_id();
let client = generate_dummy_client_with_spec(spec::new_validator_safe_contract);
client.engine().register_client(Arc::downgrade(&client) as _);
let validator_contract = "0000000000000000000000000000000000000005".parse::<Address>().unwrap();
let signer = Box::new((tap.clone(), v1, "".into()));
client.miner().set_author(miner::Author::Sealer(signer));
let tx = Transaction {
nonce: 0.into(),
gas_price: 0.into(),
gas: 500_000.into(),
action: Action::Call(validator_contract),
value: 0.into(),
data: "bfc708a000000000000000000000000082a978b3f5962a5b0957d9ee9eef472ee55b42f1".from_hex().unwrap(),
}.sign(&s0, Some(chain_id));
client.miner().import_own_transaction(client.as_ref(), tx.into()).unwrap();
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
assert_eq!(client.chain_info().best_block_number, 1);
let tx = Transaction {
nonce: 1.into(),
gas_price: 0.into(),
gas: 500_000.into(),
action: Action::Call(validator_contract),
value: 0.into(),
data: "4d238c8e00000000000000000000000082a978b3f5962a5b0957d9ee9eef472ee55b42f1".from_hex().unwrap(),
}.sign(&s0, Some(chain_id));
client.miner().import_own_transaction(client.as_ref(), tx.into()).unwrap();
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
assert_eq!(client.chain_info().best_block_number, 1);
let signer = Box::new((tap.clone(), v0, "".into()));
client.miner().set_author(miner::Author::Sealer(signer));
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
assert_eq!(client.chain_info().best_block_number, 2);
let signer = Box::new((tap.clone(), v1, "".into()));
client.miner().set_author(miner::Author::Sealer(signer));
let tx = Transaction {
nonce: 2.into(),
gas_price: 0.into(),
gas: 21000.into(),
action: Action::Call(Address::zero()),
value: 0.into(),
data: Vec::new(),
}.sign(&s0, Some(chain_id));
client.miner().import_own_transaction(client.as_ref(), tx.into()).unwrap();
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
assert_eq!(client.chain_info().best_block_number, 3);
let sync_client = generate_dummy_client_with_spec(spec::new_validator_safe_contract);
sync_client.engine().register_client(Arc::downgrade(&sync_client) as _);
for i in 1..4 {
sync_client.import_block(Unverified::from_rlp(client.block(BlockId::Number(i)).unwrap().into_inner()).unwrap()).unwrap();
}
sync_client.flush_queue();
assert_eq!(sync_client.chain_info().best_block_number, 3);
}
#[test]
fn detects_bloom() {
let client = generate_dummy_client_with_spec(spec::new_validator_safe_contract);
let engine = client.engine().clone();
let validator_contract = "0000000000000000000000000000000000000005".parse::<Address>().unwrap();
let last_hash = client.best_block_header().hash();
let mut new_header = Header::default();
new_header.set_parent_hash(last_hash);
new_header.set_number(1);
let mut event = LogEntry {
address: validator_contract,
topics: vec![*EVENT_NAME_HASH],
data: Vec::new(),
};
new_header.set_log_bloom(event.bloom());
match engine.signals_epoch_end(&new_header, Default::default()) {
EpochChange::No => {},
_ => panic!("Expected bloom to be unrecognized."),
};
event.topics.push(last_hash);
new_header.set_log_bloom(event.bloom());
match engine.signals_epoch_end(&new_header, Default::default()) {
EpochChange::Unsure(AuxiliaryRequest::Receipts) => {},
_ => panic!("Expected bloom to be recognized."),
};
}
#[test]
fn initial_contract_is_signal() {
let client = generate_dummy_client_with_spec(spec::new_validator_safe_contract);
let engine = client.engine().clone();
let mut new_header = Header::default();
new_header.set_number(0);
match engine.signals_epoch_end(&new_header, Default::default()) {
EpochChange::Yes(Proof::WithState(_)) => {},
_ => panic!("Expected state to be required to prove initial signal"),
};
}
}