Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for bsc parlia consensus #4

Merged
merged 4 commits into from
May 15, 2024

Conversation

pythonberg1997
Copy link
Contributor

TODO

@@ -40,6 +40,7 @@ tokio = { workspace = true, features = ["full"] }
secp256k1 = { workspace = true, features = ["alloc", "recovery", "rand"] }

[features]
bsc = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. [] is legal. This means this feature requires no additional crate.


/// Error type transparently wrapping ParliaConsensusError.
#[error(transparent)]
ParliaConsensusError(#[from] ParliaConsensusError),
Copy link
Collaborator

@unclezoro unclezoro May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be more concrete error, but not Parliaxxx error, for example InvalidDifficulty error is kind of HeaderValidationError, let us try to reuse current trait and add more Enum type to current errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. This is a transparent error. I do implement a ParliaConsensusError enum struct.

chain_spec: Arc<ChainSpec>,
stack: Option<InspectorStack>,
/// Type that defines how the produced EVM should be configured.
evm_config: EvmConfig,
/// Parlia consensus instance
parlia_consensus: Option<Arc<Parlia<P>>>,
Copy link
Collaborator

@unclezoro unclezoro May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not a good pattern, I did not see anywhere use this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -60,9 +72,11 @@ pub struct EVMProcessor<'a, EvmConfig> {
pub(crate) stats: BlockExecutorStats,
/// The type that is able to configure the EVM environment.
_evm_config: EvmConfig,

parlia_consensus: Option<Arc<Parlia<P>>>,
Copy link
Collaborator

@unclezoro unclezoro May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems not a good pattern.

If it is used as a flag, we can replace with feature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

#[cfg(feature = "bsc")]
pub fn finalize(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to move to consensus.

@pythonberg1997 pythonberg1997 changed the base branch from develop to bsc_feature May 6, 2024 12:04
&mut self,
block: &BlockWithSenders,
executor: &mut EVMProcessor<'_, EvmConfig>,
executor: &mut EVMProcessor<'_, EvmConfig, P>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think EVMProcessor is a very high level trait, prefer not to inject the Parlia engine into it.

/// algorithm by
/// DP Mitchell and JA Reeds

const RNG_LEN: usize = 607;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this file to a more common packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will only be used inside parlia.

@@ -0,0 +1,857 @@
/// Uniform distribution
/// algorithm by
/// DP Mitchell and JA Reeds
Copy link
Collaborator

@unclezoro unclezoro May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you comment the detailed go implementation lib?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does akula handle this random library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied from akula

}

/// BSC parlia consensus implementation
pub struct Parlia<P> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should't P have some trait?

))
}

pub fn get_snapshot_from_cache(&self, hash: &B256) -> Option<Snapshot> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used in BscEVMProcesser.finalize

}
}

impl<P> Parlia<P> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see many impl<P> Parlia<P>, can we structure them better in a more readable way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There're too many method of parlia, so I try to split them into different topics. Assemble system tx, abi encode/decode, check fork and others

(*STAKE_HUB_CONTRACT, Bytes::from(function.abi_encode_input(&[]).unwrap()))
}

pub fn unpack_data_into_max_elected_validators(&self, data: &[u8]) -> U256 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these unpack function still usable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

chain_spec: Arc<ChainSpec>,
stack: Option<InspectorStack>,
/// Type that defines how the produced EVM should be configured.
evm_config: EvmConfig,

_phantom: PhantomData<P>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_phantom seems useless?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

evm_config,
_phantom: PhantomData::default(),
#[cfg(feature = "bsc")]
parlia_cfg: None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a factory should not have parlia_cfg, but it can generate a Processor that have parlia_cfg?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to use trait instead of concreat parlia_cfg?

self.chain_spec.clone(),
database_state,
self.evm_config.clone(),
);
if let Some(stack) = &self.stack {
evm.set_stack(stack.clone());
}
#[cfg(feature = "bsc")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not good code pattern, if more chain support, it will be chaos,

}

// perf: do not execute empty blocks
if block.body.is_empty() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even an empty block, may need to consensus finalize (like breath block and so on)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact there should never be empty block on BSC. Anyway, I remove this line.

total_difficulty: U256,
) -> Result<Vec<Receipt>, BlockExecutionError> {
self.init_env(&block.header, total_difficulty);
let (mut system_tx, mut receipts, mut cumulative_gas_used) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==> system_txs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixd

let validator = header.beneficiary;
let parent = self.parlia_consensus.get_header_by_hash(header.number, header.parent_hash)?;

// The snapshot should be ready after the header stage
Copy link
Collaborator

@unclezoro unclezoro May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we wrap most of the following code into the conseneus parlia crate?

}

let nonce = self.db_mut().basic(validator).unwrap().unwrap().nonce;
self.parlia_consensus.distribute_to_validator(nonce, validator, block_reward)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need todo self.transact_system_tx?

@@ -79,6 +86,24 @@ where
consensus: Arc<dyn Consensus>,
etl_config: EtlConfig,
) -> Self {
#[cfg(feature = "bsc")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a good pattern to use the feature inside a function?

@forcodedancing forcodedancing marked this pull request as ready for review May 15, 2024 07:24
@forcodedancing forcodedancing merged commit fede7b9 into bsc_feature May 15, 2024
5 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants