substrate icon indicating copy to clipboard operation
substrate copied to clipboard

Failed to Gossip messages using GossipEngine

Open revoltez opened this issue 3 years ago • 1 comments

Is there an existing issue?

  • [X] I have searched the existing issues

Experiencing problems? Have you tried our Stack Exchange first?

  • [X] This is not a support question.

Description of bug

I have written a very simple GossipEngine but it is failing to gossip messages to other peers. in the following example i have two loops, the first one (TestEngine::gossip) keeps gossiping messages that contains a one randomly generated number and the other one (TestEngine::listen) listening to them.

im gossiping a random number to know whether messages are gossiped or not, however the problem is that each validator is only getting the message that he generated but not from others.

This is the the Gossip engine i created:

use sp_runtime::traits::{Block as BlockT,Hash as HashT,Header as HeaderT};

pub struct TestEngine<B: BlockT> {
	test_engine: Arc<Mutex<GossipEngine<B>>>,
}

pub fn create_topic<B:BlockT>() -> B::Hash
{
        <<B::Header as HeaderT>::Hashing as HashT>::hash(format!("Test").as_bytes())
}

pub fn generate_random() -> u8{
        let mut rng = rand::thread_rng(); 
		rng.gen_range(1..=255)
}
impl<B: BlockT> TestEngine<B> {
    pub async fn run(
		network: Arc<NetworkService<B, B::Hash>>,
		validator: Arc<dyn Validator<B>>,
	){ 
        let rand_num = generate_random();   
        let test_engine = Arc::new(Mutex::new(GossipEngine::new(network, "TEST", validator, None)));
        TestEngine::gossip(test_engine.clone(),vec![rand_num]).await;
        let test_engine_clone = test_engine.clone();
        tokio::spawn(async{
            TestEngine::listen(test_engine_clone).await;
        });
    }
	pub async fn gossip(engine:Arc<Mutex<GossipEngine<B>>>, message: Vec<u8>) {
        tokio::spawn(async move{
            loop{
                    engine.lock().await.gossip_message(create_topic::<B>(), message.clone(), false);
                    tokio::time::sleep(Duration::from_millis(3000)).await;
                }
        });
	}
	pub async fn listen(engine:Arc<Mutex<GossipEngine<B>>>) {
		let mut receiver = engine.lock().await.messages_for(create_topic::<B>());
                while let Some(notification) = receiver.next().await
                {
                    println!("Received Gossiped Message: {:?}",notification);    
                }
    }
}

and this is the validator struct:

pub struct TestEventValidator {}
impl<B:BlockT> Validator<B> for TestEventValidator {
	fn validate(
		&self,
		context: &mut dyn sc_network_gossip::ValidatorContext<B>,
		sender: &PeerId,
		data: &[u8],
	) -> sc_network_gossip::ValidationResult<B::Hash> {
		sc_network_gossip::ValidationResult::ProcessAndKeep(create_topic::<B>())
	}
}

in node_template/node/service.rs im passing the NetworkService to the GossipEngine and starting it in a seperate Task in the new_full() method like this:

let test_event_validator = Arc::new(TestEventValidator{});
task_manager.spawn_handle().spawn_blocking("test Engine", None, TestEngine::run(network.clone(),test_event_validator));

is there a problem with the protocol name? are there any specific guidelines to create a protocol name or just pass any random string?

Steps to reproduce

No response

revoltez avatar Jan 20 '23 08:01 revoltez

We should add some docs around the gossip engine being deprecated. It is still being used by Grandpa but it also has bugs. It would be better if you implement your own gossip.

bkchr avatar Jan 20 '23 09:01 bkchr

I believe the issue is you're not polling the GossipEngine, which itself is a future. Use select to simultaneously poll the engine and receiver.

kayabaNerve avatar Jan 21 '23 21:01 kayabaNerve

We should really categorize and support the different gossip requirements one day.

burdges avatar Jan 22 '23 01:01 burdges

@bkchr okey thanks for the info. @kayabaNerve i dont think so because i managed to send a message to myself only, but anyways im closing this issue because it is a deprecated anyways and wont be useful.

revoltez avatar Jan 22 '23 06:01 revoltez