Skip to content

Commit

Permalink
successfully ran running node
Browse files Browse the repository at this point in the history
  • Loading branch information
lla-dane committed Aug 22, 2024
1 parent 15e32fc commit da08f98
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 42 deletions.
7 changes: 7 additions & 0 deletions crates/floresta-wire/src/p2p_wire/sync_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ where

pub async fn run(&mut self, kill_signal: Arc<RwLock<bool>>, done_cb: impl FnOnce(&Chain)) {
info!("Starting sync node");

println!("SYNC NODE");
self.1.last_block_requested = self.chain.get_validation_index().unwrap();

loop {
Expand Down Expand Up @@ -151,6 +153,11 @@ where

self.blocks.insert(block.block.block_hash(), (peer, block));

println!(
"CHAIN INDEX VALID: {}",
self.chain.get_validation_index().unwrap()
);

let next_block = self.chain.get_validation_index()? + 1;
let mut next_block = self.chain.get_block_hash(next_block)?;

Expand Down
33 changes: 10 additions & 23 deletions crates/floresta-wire/src/p2p_wire/tests/running_node.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#[cfg(test)]
mod tests_utils {
use std::collections::HashMap;
use std::mem::ManuallyDrop;
use std::sync::Arc;
use std::time::Duration;

use async_std::sync::RwLock;
use async_std::task;
use bitcoin::block::Header;
use bitcoin::BlockHash;
use floresta_chain::AssumeValidArg;
use floresta_chain::ChainState;
use floresta_chain::KvChainStore;
use floresta_chain::UtreexoBlock;
use tokio::sync::RwLock;
use tokio::task;

use crate::mempool::Mempool;
use crate::node::UtreexoNode;
Expand Down Expand Up @@ -45,7 +44,7 @@ mod tests_utils {
);

for (i, peer) in peers.into_iter().enumerate() {
let (sender, receiver) = async_std::channel::bounded(10);
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
let peer = create_peer(
peer.0,
peer.1,
Expand All @@ -60,25 +59,13 @@ mod tests_utils {

node.peers.insert(i as u32, peer);
}
// let mut node = ManuallyDrop::new(Box::new(node));
let node = Box::new(node);

let kill_signal = Arc::new(RwLock::new(false));
// FIXME: This doesn't look very safe, but we need to coerce a &mut reference of the node
// to live for the static lifetime, or it can't be spawn-ed by async-std::task

// let _node: &'static mut UtreexoNode<RunningNode, Arc<ChainState<KvChainStore>>> =
// unsafe { std::mem::transmute(&mut **node) };

let (sender, _) = futures::channel::oneshot::channel();

// future::timeout(Duration::from_secs(10), _node.run(kill_signal, sender))
// .await
// .unwrap();

task::spawn(node.run(kill_signal.clone(), sender));

task::sleep(Duration::from_secs(4)).await;
tokio::time::sleep(Duration::from_secs(4)).await;

let mut kill_guard = kill_signal.write().await;
*kill_guard = true;
Expand All @@ -96,9 +83,9 @@ mod tests {
use super::tests_utils::setup_node;
use crate::p2p_wire::tests::utils::get_essentials;

#[async_std::test]
async fn test_one() {
let (mut headers, blocks, _, _, _) = get_essentials();
#[tokio::test]
async fn send_headers() {
let (_, mut headers, _, blocks, _, _, _) = get_essentials();

// GONNA WORK WITH ONLY 5 BLOCKS:
// CREATE A PEER WITH 5 BLOCK HEADERS AND 5 BLOCKS. POW_FRAUD_PROOFS GONNA BE OFF !!
Expand All @@ -111,12 +98,12 @@ mod tests {
headers.truncate(5);

let chain = setup_node(
vec![(headers, blocks.clone(), HashMap::new())],
vec![(headers.clone(), blocks.clone(), HashMap::new())],
false,
floresta_chain::Network::Signet,
floresta_chain::Network::Regtest,
)
.await;

println!("BEST HEIGTH: {:?}", chain.get_best_block().unwrap());
assert_eq!(chain.get_best_block().unwrap().1, headers[4].block_hash());
}
}
8 changes: 4 additions & 4 deletions crates/floresta-wire/src/p2p_wire/tests/sync_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests_utils {
let chain = Arc::new(chain);

// Adding 9 signet headers in the chain-state prior validation
let mut headers = get_test_headers();
let (mut headers, _) = get_test_headers().unwrap();
headers.remove(0);
headers.truncate(9);
for header in headers {
Expand Down Expand Up @@ -98,7 +98,7 @@ mod tests {

#[tokio::test]
async fn test_sync_valid_blocks() {
let (headers, blocks, _, _, _) = get_essentials();
let (headers, _, blocks, _, _, _, _) = get_essentials();
let chain = setup_node(
vec![(Vec::new(), blocks.clone(), HashMap::new())],
false,
Expand All @@ -119,7 +119,7 @@ mod tests {
// THIS SIMULATION WILL TEST:
// 1) SENDING BLOCK WITH A BADMERKLEROOT: 7TH BLOCK WILL BE INVALIDATED.

let (headers, mut blocks, _, _, invalid_block) = get_essentials();
let (headers, regtest_headers, mut blocks, _, _, _, invalid_block) = get_essentials();
blocks.insert(headers[7].block_hash(), invalid_block);

let peer = vec![(Vec::new(), blocks.clone(), HashMap::new())];
Expand All @@ -142,7 +142,7 @@ mod tests {
//
// SO FINALLY THE LAST VALIDATED BLOCK WILL BE 9.

let (headers, mut blocks, _, _, _) = get_essentials();
let (headers, _, mut blocks, _, _, _, _) = get_essentials();
let v_blocks = blocks.clone();

let u_block = blocks.get(&headers[3].block_hash().clone()).unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
1fb6bb73278d7163bc6df078e80f2b25604c58afe2eedb4a3e0712a7f661567a
427d19db2c5640b681f83561ae3ae8c80198291bd1e3c08ad673add1d7da049e
0d31e559c46a42b5a90a4115c1caa00f72c32539d55e54783fb9428a5b56311d
180c26fbf8f3e7ebebdc5e55d3d813b042a2a00d484d21955190abb6ec855433
078ddd529b050e90789708dd0a8d99360898753a714464b9bc794110076cc454
74055fdd1586be69d01bcf796dd52a39b161c6c3699850cd0aad6822d6768fb0
0ce52c450809912f3c64713725be2f75fde5524978dea9e9f63882ca01ba12b2
01efc3f6abcb04d1685d8366cdfa2cb916c95acc7435a99a32f4c3eab8905006
463475f9a491f3619895bfa1b59759064ab2fb323c47e44ce650641ea05da0ef
21b484bfec5981a2de2053bb8577fb9a33bc387d1d56f88e42684f5e067781cb
368839b7ea7dd640aa53a9b17ea1ff2fa89ed7550752db3701213952827fdd61
3d1cb21728f74f6ef0701193c4a895a5a00d7bbadeb4d644856cd23234a6dce6
43019d36eaf0ae02c00206e4c849788c4498afc13b06e77f20a37ef09164f80e
59036004648dfbcb615e4909ee6f52d059db9260c3612aab371ce2a85864c3a4
3fac199bd45466a53479b1abcd218a27428a7b88b6f186db6363209df5c64ced
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"block": "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f20020000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000",
"height": 0
},
{
"block": "0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f6f5f11f61208a2c479f620ebfe9c9ab668aed8c8a21f4b127c24c9cf174d26a3bde8c566ffff7f20000000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff165108f7184e96f3a2aad80b2f503253482f627463642fffffffff0100f2052a0100000016001487a87e0e17a80a2d2bd65c421a1090df8ed6cc9a00000000",
"height": 1
},
{
"block": "000000207a5661f6a712073e4adbeee2af584c60252b0fe878f06dbc63718d2773bbb61fb325d7c2914189d3d0c5f8b6db6efdf523cdba4af9358f944653b61e947643b7bee8c566ffff7f20000000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff1652088ae45abf085cbd7a0b2f503253482f627463642fffffffff0100f2052a0100000016001487a87e0e17a80a2d2bd65c421a1090df8ed6cc9a00000000",
"height": 2
},
{
"block": "000000209e04dad7d1ad73d68ac0e3d11b299801c8e83aae6135f881b640562cdb197d42e14a2f40a40c937377c587b9d97ad666a6554f0d97147e0acb98b2e714160870bee8c566ffff7f20030000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff16530820a1bcf9de4f1ff80b2f503253482f627463642fffffffff0100f2052a0100000016001487a87e0e17a80a2d2bd65c421a1090df8ed6cc9a00000000",
"height": 3
},
{
"block": "000000201d31565b8a42b93f78545ed53925c3720fa0cac115410aa9b5426ac459e5310d886e4c202f3dc738eaa46e0291e56f0b14fdc16a8c4ce2ad0a9111fbd7b417b8bfe8c566ffff7f20000000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff165408ffa7d9c9d143bd0d0b2f503253482f627463642fffffffff0100f2052a0100000016001487a87e0e17a80a2d2bd65c421a1090df8ed6cc9a00000000",
"height": 4
},
{
"block": "00000020335485ecb6ab905195214d480da0a242b013d8d3555edcebebe7f3f8fb260c183d3a29ceaeab888099bccd7c10aea91b00a7a1b7dc71354e1193d60a5d22152ebfe8c566ffff7f20000000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff165508c4e74e308227a4210b2f503253482f627463642fffffffff0100f2052a0100000016001487a87e0e17a80a2d2bd65c421a1090df8ed6cc9a00000000",
"height": 5
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"block": "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000",
"height": 0
},
{
"block": "0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f6f5f11f61208a2c479f620ebfe9c9ab668aed8c8a21f4b127c24c9cf174d26a3bde8c566ffff7f2000000000",
"height": 1
},
{
"block": "000000207a5661f6a712073e4adbeee2af584c60252b0fe878f06dbc63718d2773bbb61fb325d7c2914189d3d0c5f8b6db6efdf523cdba4af9358f944653b61e947643b7bee8c566ffff7f2000000000",
"height": 2
},
{
"block": "000000209e04dad7d1ad73d68ac0e3d11b299801c8e83aae6135f881b640562cdb197d42e14a2f40a40c937377c587b9d97ad666a6554f0d97147e0acb98b2e714160870bee8c566ffff7f2003000000",
"height": 3
},
{
"block": "000000201d31565b8a42b93f78545ed53925c3720fa0cac115410aa9b5426ac459e5310d886e4c202f3dc738eaa46e0291e56f0b14fdc16a8c4ce2ad0a9111fbd7b417b8bfe8c566ffff7f2000000000",
"height": 4
},
{
"block": "00000020335485ecb6ab905195214d480da0a242b013d8d3555edcebebe7f3f8fb260c183d3a29ceaeab888099bccd7c10aea91b00a7a1b7dc71354e1193d60a5d22152ebfe8c566ffff7f2000000000",
"height": 5
}
]
Empty file.
74 changes: 59 additions & 15 deletions crates/floresta-wire/src/p2p_wire/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ struct Block {
block: String,
}

#[derive(Deserialize, Debug)]
struct Regtest_headers {
block: String,
}

#[derive(Debug)]
pub struct TestPeer {
headers: Vec<Header>,
Expand Down Expand Up @@ -94,12 +99,10 @@ impl TestPeer {
.unwrap();

loop {
println!("RECEIVING");
let req = self.node_rx.recv().await.unwrap();

match req {
NodeRequest::GetHeaders(hashes) => {
println!("REQUESTED: HEADERS");
let pos = hashes.first().unwrap();
let pos = self.headers.iter().position(|h| h.block_hash() == *pos);
let headers = match pos {
Expand All @@ -112,7 +115,6 @@ impl TestPeer {
self.peer_id,
PeerMessages::Headers(headers),
))
.await
.unwrap();
}
NodeRequest::GetUtreexoState((hash, _)) => {
Expand All @@ -122,12 +124,10 @@ impl TestPeer {
self.peer_id,
PeerMessages::UtreexoState(filters),
))
.await
.unwrap();
}

NodeRequest::GetBlock((hashes, _)) => {
println!("REQUESTED BLOCKS: {}", hashes.len());
for hash in hashes {
let block = self.blocks.get(&hash).unwrap().clone();
self.node_tx
Expand Down Expand Up @@ -230,34 +230,68 @@ pub fn create_false_acc(tip: usize) -> Vec<u8> {
serialize(utreexo_root)
}

pub fn get_test_headers() -> Vec<Header> {
pub fn get_test_headers() -> io::Result<(Vec<Header>, Vec<Header>)> {
let file =
include_bytes!("../../../../floresta-chain/src/pruned_utreexo/testdata/signet_headers.zst");

let regtest_file = "./src/p2p_wire/tests/test_data/regtest_headers.json";

let mut signet_headers: Vec<Header> = Vec::new();

// EXTRACTING SIGNET HEADERS
let uncompressed: Vec<u8> = zstd::decode_all(std::io::Cursor::new(file)).unwrap();
let mut cursor = Cursor::new(uncompressed);
let mut headers: Vec<Header> = Vec::new();

while let Ok(header) = Header::consensus_decode(&mut cursor) {
headers.push(header);
signet_headers.push(header);
}
headers

// EXTRACTING REGTEST HEADERS
let mut contents = String::new();
File::open(regtest_file)?.read_to_string(&mut contents)?;
let r_headers: Vec<Regtest_headers> = serde_json::from_str(&contents).unwrap();
let mut regtest_headers: Vec<Header> = Vec::new();

for header in r_headers {
let header = Vec::from_hex(&header.block).unwrap();
let header: Header = deserialize(&header).unwrap();
regtest_headers.push(header);
}

Ok((signet_headers, regtest_headers))
}

pub fn get_test_blocks() -> io::Result<HashMap<BlockHash, UtreexoBlock>> {
pub fn get_test_blocks() -> io::Result<(
HashMap<BlockHash, UtreexoBlock>,
HashMap<BlockHash, UtreexoBlock>,
)> {
let dir = "./src/p2p_wire/tests/test_data/blocks.json";
let regtest_dir = "./src/p2p_wire/tests/test_data/regtest_blocks.json";
let mut contents = String::new();
let mut regtest_contents = String::new();

File::open(regtest_dir)?.read_to_string(&mut regtest_contents)?;
File::open(dir)?.read_to_string(&mut contents)?;

let blocks: Vec<Block> = serde_json::from_str(&contents).unwrap();
let regtest_blocks: Vec<Block> = serde_json::from_str(&regtest_contents).unwrap();

let mut u_blocks = HashMap::new();
let mut regtest_u_blocks = HashMap::new();

for block_str in blocks {
let block = Vec::from_hex(&block_str.block).unwrap();
let block: UtreexoBlock = deserialize(&block).unwrap();
u_blocks.insert(block.block.block_hash(), block);
}

Ok(u_blocks)
for block_str in regtest_blocks {
let block = Vec::from_hex(&block_str.block).unwrap();
let block: UtreexoBlock = deserialize(&block).unwrap();
regtest_u_blocks.insert(block.block.block_hash(), block);
}

Ok((u_blocks, regtest_u_blocks))
}

pub fn get_test_filters() -> io::Result<HashMap<BlockHash, Vec<u8>>> {
Expand All @@ -267,7 +301,7 @@ pub fn get_test_filters() -> io::Result<HashMap<BlockHash, Vec<u8>>> {
.read_to_string(&mut contents)
.unwrap();
let roots: Vec<UtreexoRoots> = serde_json::from_str(&contents).unwrap();
let headers = get_test_headers();
let (headers, _) = get_test_headers().unwrap();
let mut filters = HashMap::new();

for root in roots.into_iter() {
Expand All @@ -290,13 +324,15 @@ pub fn generate_invalid_block() -> UtreexoBlock {

pub fn get_essentials() -> (
Vec<Header>,
Vec<Header>,
HashMap<BlockHash, UtreexoBlock>,
HashMap<BlockHash, UtreexoBlock>,
HashMap<BlockHash, Vec<u8>>,
BlockHash,
UtreexoBlock,
) {
let headers = get_test_headers();
let blocks = get_test_blocks().unwrap();
let (signet_headers, regtest_headers) = get_test_headers().unwrap();
let (blocks, regtest_blocks) = get_test_blocks().unwrap();
let true_filters = get_test_filters().unwrap();
let invalid_block = generate_invalid_block();

Expand All @@ -305,5 +341,13 @@ pub fn get_essentials() -> (
BlockHash::from_str("0000035f0e5513b26bba7cead874fdf06241a934e4bc4cf7a0381c60e4cdd2bb")
.unwrap();

(headers, blocks, true_filters, tip_hash, invalid_block)
(
signet_headers,
regtest_headers,
blocks,
regtest_blocks,
true_filters,
tip_hash,
invalid_block,
)
}

0 comments on commit da08f98

Please sign in to comment.