Skip to content

Commit

Permalink
feat: hardfork proposal feature
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Sep 7, 2023
1 parent 691247b commit 5005294
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
42 changes: 40 additions & 2 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use protocol::types::{
Block, BlockVersion, Bytes, ExecResp, Hash, Hex, Metadata, Proof, Proposal, SignedTransaction,
ValidatorExtend, BASE_FEE_PER_GAS, MAX_BLOCK_GAS_LIMIT, RLP_NULL,
};
use protocol::{async_trait, tokio::sync::Mutex as AsyncMutex, ProtocolError, ProtocolResult};
use protocol::{
async_trait, codec::ProtocolCodec, tokio::sync::Mutex as AsyncMutex, types::HardforkInfoInner,
ProtocolError, ProtocolResult,
};

use crate::message::{
END_GOSSIP_AGGREGATED_VOTE, END_GOSSIP_SIGNED_CHOKE, END_GOSSIP_SIGNED_PROPOSAL,
Expand Down Expand Up @@ -88,7 +91,15 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
timestamp: time_now(),
number: next_number,
gas_limit: MAX_BLOCK_GAS_LIMIT.into(),
extra_data: Default::default(),
extra_data: {
self.node_info
.hardfork_proposals
.read()
.unwrap()
.as_ref()
.map(|v| v.encode().unwrap())
.unwrap_or_default()
},
base_fee_per_gas: BASE_FEE_PER_GAS.into(),
proof: status.proof,
chain_id: self.node_info.chain_id,
Expand Down Expand Up @@ -135,6 +146,20 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
.into());
}

if let Ok(data) = HardforkInfoInner::decode(&proposal.extra_data) {
if !self
.node_info
.hardfork_proposals
.read()
.unwrap()
.as_ref()
.map(|v| &data == v)
.unwrap_or_default()
{
return Err(ProtocolError::from(ConsensusError::HardforkDontMatch).into());
}
}

let tx_hashes = proposal.tx_hashes.clone();
let tx_hashes_len = tx_hashes.len();
let exemption = {
Expand Down Expand Up @@ -608,6 +633,19 @@ impl<Adapter: ConsensusAdapter + 'static> ConsensusEngine<Adapter> {
// Save the block
self.adapter.save_block(ctx.clone(), block.clone()).await?;

if let Ok(data) = HardforkInfoInner::decode(&block.header.extra_data) {
let mut self_proposal = self.node_info.hardfork_proposals.write().unwrap();

if self_proposal
.as_ref()
.map(|v| v == &data)
.unwrap_or_default()
{
// remove self hardfork proposal
self_proposal.take();
}
}

if self
.adapter
.is_last_block_in_current_epoch(block_number)
Expand Down
3 changes: 3 additions & 0 deletions core/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ pub enum ConsensusError {

#[display(fmt = "Build trie merkle tree error {}", _0)]
BuildMerkle(String),

#[display(fmt = "Proposal hardfork info don't match")]
HardforkDontMatch,
}

#[derive(Debug, Display)]
Expand Down
22 changes: 21 additions & 1 deletion core/executor/src/system_contract/metadata/handle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use protocol::types::{CkbRelatedInfo, HardforkInfo, Metadata, H160, H256};
use protocol::ProtocolResult;

use crate::system_contract::metadata::MetadataStore;
use std::sync::Arc;

use crate::system_contract::metadata::{MetadataStore, HARDFORK_INFO};

/// The MetadataHandle is used to expose apis that can be accessed from outside
/// of the system contract.
Expand Down Expand Up @@ -51,4 +53,22 @@ impl MetadataHandle {
pub fn hardfork_infos(&self) -> ProtocolResult<HardforkInfo> {
MetadataStore::new(self.root)?.hardfork_infos()
}

pub fn init_hardfork(&self, block_number: u64) -> ProtocolResult<()> {
let hardfork = MetadataStore::new(self.root)?
.hardfork_info(block_number)
.unwrap();

HARDFORK_INFO.swap(Arc::new(hardfork));
Ok(())
}

// this function use to init an axon chain with genesis block
pub fn set_current_hardfork_info(&self, info: H256) -> ProtocolResult<()> {
let mut store = MetadataStore::new(self.root)?;

store.set_hardfork_info(0, info)?;

Ok(())
}
}
6 changes: 4 additions & 2 deletions core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ async fn start<K: KeyProvider>(
Proposal::new_without_state_root(&current_block.header).into(),
)?
.get_metadata_root();
let metadata = MetadataHandle::new(metadata_root)
.get_metadata_by_block_number(current_block.header.number)?;

let metadata_handle = MetadataHandle::new(metadata_root);
metadata_handle.init_hardfork(current_block.header.number)?;
let metadata = metadata_handle.get_metadata_by_block_number(current_block.header.number)?;
let validators: Vec<Validator> = metadata
.verifier_list
.iter()
Expand Down
17 changes: 11 additions & 6 deletions protocol/src/traits/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::collections::HashMap;
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};

use common_crypto::Secp256k1PublicKey;

use crate::types::{
Address, Block, BlockNumber, Bytes, ExecResp, Hash, Header, Hex, MerkleRoot, Metadata,
PackedTxHashes, Proof, Proposal, Receipt, SignedTransaction, Validator, U256,
Address, Block, BlockNumber, Bytes, ExecResp, HardforkInfoInner, Hash, Header, Hex, MerkleRoot,
Metadata, PackedTxHashes, Proof, Proposal, Receipt, SignedTransaction, Validator, U256,
};
use crate::{async_trait, traits::Context, ProtocolResult};

Expand All @@ -16,9 +19,10 @@ pub enum MessageTarget {

#[derive(Debug, Clone)]
pub struct NodeInfo {
pub chain_id: u64,
pub self_pub_key: Secp256k1PublicKey,
pub self_address: Address,
pub chain_id: u64,
pub self_pub_key: Secp256k1PublicKey,
pub self_address: Address,
pub hardfork_proposals: Arc<RwLock<Option<HardforkInfoInner>>>,
}

impl NodeInfo {
Expand All @@ -28,6 +32,7 @@ impl NodeInfo {
chain_id,
self_pub_key: pubkey,
self_address: address,
hardfork_proposals: Default::default(),
}
}
}
Expand Down

0 comments on commit 5005294

Please sign in to comment.