Skip to content

Commit

Permalink
[WIP] feat: Secondary miner issurance, split DAO as a separate contract
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Jun 22, 2019
1 parent 3648f80 commit 29bcc70
Show file tree
Hide file tree
Showing 42 changed files with 759 additions and 540 deletions.
95 changes: 63 additions & 32 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ckb_core::cell::{
resolve_transaction, BlockCellProvider, BlockHeadersProvider, OverlayCellProvider,
OverlayHeaderProvider, ResolvedTransaction,
};
use ckb_core::extras::{BlockExt, DaoStats};
use ckb_core::extras::BlockExt;
use ckb_core::service::{Request, DEFAULT_CHANNEL_SIZE, SIGNAL_CHANNEL_SIZE};
use ckb_core::transaction::{CellOutput, ProposalShortId};
use ckb_core::{BlockNumber, Cycle};
Expand All @@ -17,7 +17,6 @@ use ckb_store::{ChainStore, StoreBatch};
use ckb_traits::ChainProvider;
use ckb_verification::{BlockVerifier, ContextualBlockVerifier, ForkContext, Verifier};
use crossbeam_channel::{self, select, Receiver, Sender};
use dao::calculate_dao_data;
use failure::Error as FailureError;
use faketime::unix_time_as_millis;
use fnv::{FnvHashMap, FnvHashSet};
Expand Down Expand Up @@ -276,23 +275,12 @@ impl<CS: ChainStore + 'static> ChainService<CS> {

let epoch = next_epoch_ext.unwrap_or_else(|| parent_header_epoch.to_owned());

let (ar, c) = calculate_dao_data(
parent_header.number(),
&parent_header_epoch,
&parent_ext.dao_stats,
self.shared.consensus().secondary_epoch_reward(),
)?;

let ext = BlockExt {
received_at: unix_time_as_millis(),
total_difficulty: cannon_total_difficulty.clone(),
total_uncles_count: parent_ext.total_uncles_count + block.uncles().len() as u64,
verified: None,
txs_fees: vec![],
dao_stats: DaoStats {
accumulated_rate: ar,
accumulated_capacity: c.as_u64(),
},
};

batch.insert_block_epoch_index(
Expand Down
11 changes: 0 additions & 11 deletions core/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,9 @@ pub struct BlockExt {
pub total_difficulty: U256,
pub total_uncles_count: u64,
pub verified: Option<bool>,
pub dao_stats: DaoStats,
pub txs_fees: Vec<Capacity>,
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Default, Debug)]
pub struct DaoStats {
// DAO related fields
// accmulated rate is multiplied by 10**16 to keep as much decimals
// as we can. In this case, we can still represent a maximum value
// of around 1844.6744073709551, which is good enough for our case.
pub accumulated_rate: u64,
pub accumulated_capacity: u64,
}

#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct TransactionAddress {
// Block hash
Expand Down
11 changes: 11 additions & 0 deletions core/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct RawHeader {
uncles_count: u32,
/// Epoch sequence number
epoch: EpochNumber,
/// Statistic data used in NervosDAO calculation
dao: Bytes,
}

impl RawHeader {
Expand Down Expand Up @@ -275,6 +277,10 @@ impl Header {
&self.seal.proof
}

pub fn dao(&self) -> &Bytes {
&self.raw.dao
}

pub fn nonce(&self) -> u64 {
self.seal.nonce
}
Expand Down Expand Up @@ -388,6 +394,11 @@ impl HeaderBuilder {
self
}

pub fn dao(mut self, dao: Bytes) -> Self {
self.raw.dao = dao;
self
}

pub fn nonce(mut self, nonce: u64) -> Self {
self.seal.nonce = nonce;
self
Expand Down
6 changes: 1 addition & 5 deletions core/src/script.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use bytes::Bytes;
use faster_hex::hex_encode;
use hash::new_blake2b;
use numext_fixed_hash::{h256, H256};
use numext_fixed_hash::H256;
use occupied_capacity::{Capacity, Result as CapacityResult};
use serde_derive::{Deserialize, Serialize};
use std::fmt;

// This is the code hash for locking funds in NervosDAO. The hex used here
// is actually "NERVOSDAOCODE0001" in hex mode.
pub const DAO_CODE_HASH: H256 = h256!("0x4e4552564f5344414f434f444530303031");

// TODO: when flatbuffer work is done, remove Serialize/Deserialize here and
// implement proper From trait
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion db/src/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;
// - If the data can be migrated manually: update "x.y1.z" to "x.y2.0".
// - If the data can not be migrated: update "x1.y.z" to "x2.0.0".
pub(crate) const VERSION_KEY: &str = "db-version";
pub(crate) const VERSION_VALUE: &str = "0.1400.0";
pub(crate) const VERSION_VALUE: &str = "0.1500.0";

pub struct RocksDB {
inner: Arc<DB>,
Expand Down
22 changes: 16 additions & 6 deletions docs/hashes.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
# Generated by: ckb cli hashes -b
[ckb_dev]
genesis = "0x7d789ed1c7641670dfb411ff0d220b77b7e95a864d8b7284088660b353122345"
cellbase = "0x606242ae42a472e8d8bc542cc64f8ae0430d948e3efff4e44121f8ae67709671"
genesis = "0xd65a017201803f37d87130a94f9e19abd23369f77ee76a369b4e5040c9a8f50d"
cellbase = "0x54c5775eae546c599c9d9fba797710fc6745feae39742ec7f6b8e939844693be"

[[ckb_dev.system_cells]]
path = "Bundled(specs/cells/secp256k1_blake160_sighash_all)"
index = 1
code_hash = "0x94334bdda40b69bae067d84937aa6bbccf8acd0df6626d4b9ac70d4612a11933"
code_hash = "0x12ddb0d38e1ccd9f773d5f0f2fd063602205815e7bd80b19661e56874f9cb6a0"

[[ckb_dev.system_cells]]
path = "Bundled(specs/cells/dao)"
index = 2
code_hash = "0x0fe8ddd199ba79bddfe59af415b656844b8642a1fb0e6ae839e08445973e037b"

[ckb_testnet]
genesis = "0x742dcb9baa4d31a5440c00849da0ca0b429e0df4061b1d8ad11847160ba7d6c5"
cellbase = "0xfae4d4b63827e1cad8f4279f0b8032f4869725c223e287a8fd8164a80e5f5e86"
genesis = "0x35b42bbe6ec7dfc45fbb6bfd003e9f3b810093bd380787cdb2cfc7f4b6487fd4"
cellbase = "0xcc914ee3eef6648ba6050f3fd32aba6bbdb0c197ce82ddbed015141bba899a9d"

[[ckb_testnet.system_cells]]
path = "Bundled(specs/cells/secp256k1_blake160_sighash_all)"
index = 1
code_hash = "0x94334bdda40b69bae067d84937aa6bbccf8acd0df6626d4b9ac70d4612a11933"
code_hash = "0x12ddb0d38e1ccd9f773d5f0f2fd063602205815e7bd80b19661e56874f9cb6a0"

[[ckb_testnet.system_cells]]
path = "Bundled(specs/cells/dao)"
index = 2
code_hash = "0x0fe8ddd199ba79bddfe59af415b656844b8642a1fb0e6ae839e08445973e037b"
1 change: 1 addition & 0 deletions indexer/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ impl<CS: ChainStore + 'static> DefaultIndexerStore<CS> {
.collect();
let block = self
.shared
.store()
.get_ancestor(&tip_hash, block_number)
.and_then(|header| self.shared.store().get_block(&header.hash()))
.expect("block exists");
Expand Down
1 change: 1 addition & 0 deletions miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ckb-traits = { path = "../traits" }
failure = "0.1.5"
ckb-verification = { path = "../verification" }
indicatif = "0.11"
dao = { path = "../util/dao" }

[dev-dependencies]
proptest = "0.9"
Expand Down
32 changes: 32 additions & 0 deletions miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::BlockAssemblerConfig;
use crate::error::Error;
use ckb_core::block::Block;
use ckb_core::cell::resolve_transaction;
use ckb_core::extras::EpochExt;
use ckb_core::header::Header;
use ckb_core::script::Script;
Expand All @@ -17,6 +18,7 @@ use ckb_store::ChainStore;
use ckb_traits::ChainProvider;
use ckb_verification::TransactionError;
use crossbeam_channel::{self, select, Receiver, Sender};
use dao::DaoCalculator;
use failure::Error as FailureError;
use faketime::unix_time_as_millis;
use fnv::FnvHashSet;
Expand Down Expand Up @@ -335,6 +337,35 @@ impl<CS: ChainStore + 'static> BlockAssembler<CS> {
);
}

// Generate DAO fields here
let resolved_cellbase = resolve_transaction(
&cellbase,
&mut Default::default(),
&*chain_state,
&*chain_state,
)
.map_err(|_| Error::InvalidInput)?;
let rtxs = entries
.iter()
.try_fold(
vec![resolved_cellbase],
|mut rtxs, entry| match resolve_transaction(
&entry.transaction,
&mut Default::default(),
&*chain_state,
&*chain_state,
) {
Ok(rtx) => {
rtxs.push(rtx);
Ok(rtxs)
}
Err(e) => Err(e),
},
)
.map_err(|_| Error::InvalidInput)?;
let dao = DaoCalculator::new(&chain_state.consensus(), Arc::clone(chain_state.store()))
.dao_field(&rtxs, &tip_header)?;

// Release the lock as soon as possible, let other services do their work
drop(chain_state);

Expand All @@ -358,6 +389,7 @@ impl<CS: ChainStore + 'static> BlockAssembler<CS> {
proposals: proposals.into_iter().map(Into::into).collect(),
cellbase: Self::transform_cellbase(&cellbase, None),
work_id: Unsigned(self.work_id.fetch_add(1, Ordering::SeqCst) as u64),
dao: JsonBytes::from_bytes(dao),
};

self.template_caches.insert(
Expand Down
2 changes: 2 additions & 0 deletions protocol/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl<'a> FbsHeader<'a> {
let proposals_hash = header.proposals_hash().into();
let difficulty = FbsBytes::build(fbb, &uint_to_bytes(header.difficulty()));
let proof = FbsBytes::build(fbb, &header.proof());
let dao = FbsBytes::build(fbb, &header.dao());
let uncles_hash = header.uncles_hash().into();
let mut builder = HeaderBuilder::new(fbb);
builder.add_version(header.version());
Expand All @@ -69,6 +70,7 @@ impl<'a> FbsHeader<'a> {
builder.add_difficulty(difficulty);
builder.add_nonce(header.nonce());
builder.add_proof(proof);
builder.add_dao(dao);
builder.add_uncles_hash(&uncles_hash);
builder.add_uncles_count(header.uncles_count());
builder.finish()
Expand Down
4 changes: 4 additions & 0 deletions protocol/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ impl<'a> TryFrom<ckb_protocol::Header<'a>> for ckb_core::header::Header {
.proof()
.and_then(|p| p.seq())
.map(ckb_core::Bytes::from))?)
.dao(cast!(header
.dao()
.and_then(|d| d.seq())
.map(ckb_core::Bytes::from))?)
.uncles_count(header.uncles_count())
.build())
}
Expand Down
1 change: 1 addition & 0 deletions protocol/src/protocol.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ table Header {
uncles_hash: H256;
uncles_count: uint32;
epoch: uint64;
dao: Bytes;
}

table Block {
Expand Down
Loading

0 comments on commit 29bcc70

Please sign in to comment.