47 lines
1.0 KiB
Rust
47 lines
1.0 KiB
Rust
// NVM-L0 共识引擎
|
|
use crate::block::Block;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
pub trait ConsensusEngine {
|
|
fn validate_block(&self, block: &Block) -> bool;
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct DagConsensus {
|
|
difficulty: u64,
|
|
}
|
|
|
|
impl DagConsensus {
|
|
pub fn new(difficulty: u64) -> Self {
|
|
Self { difficulty }
|
|
}
|
|
}
|
|
|
|
impl ConsensusEngine for DagConsensus {
|
|
fn validate_block(&self, _block: &Block) -> bool {
|
|
true // 简化版本
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::block::{Block, BlockHeader};
|
|
use crate::types::{Address, Hash};
|
|
|
|
#[test]
|
|
fn test_consensus() {
|
|
let consensus = DagConsensus::new(1000);
|
|
let header = BlockHeader {
|
|
number: 1,
|
|
timestamp: 1000,
|
|
parent_hash: Hash::zero(),
|
|
state_root: Hash::zero(),
|
|
tx_root: Hash::zero(),
|
|
miner: Address::zero(),
|
|
};
|
|
let block = Block::new(header, Vec::new());
|
|
assert!(consensus.validate_block(&block));
|
|
}
|
|
}
|