1

I have an Off Chain Worker that validates some data in IPFS and then calls a function on chain to set the status to valid or invalid. That's all working fine.

However, I do not need all nodes to validate all of the data. To avoid wasting node resources I would like to rotate the work of validation between nodes. For example, imagine my network has 5 nodes, on block 1 node 1 runs the validation, on block 2 node 2, on block 3 node 3 etc.

Given that it doesn't matter if there are occasional blocks when 2 nodes run validation or blocks when no node does, my idea has been to use a random number generator in the off chain worker for each node to decide whether to do validation or not. The offchain worker would do something like this:

let peer_count = no. of peers
let random_int = random integer between 0 and peer_count
if random_int == 0 {Carry out the valdiation}
else { quit }

So in my example with 5 nodes, each node will do the validation, on average, once every 5 blocks.

Here's my question - how do I get the no. of peers?

1 Answer 1

1

There is no way to get the number of peers of your node. Doing that would also only work as long as all nodes are connected to each other, which isn't working in a really decentralized network. So, you will need to do something different. You will not be able to prevent that multiple nodes are validating the same data. From your short description it actually sounds like you want/need multiple nodes that validate the data. How do you want to ensure that when only one node is validating the data, it isn't lying? That it actually verified the data correctly.

Now to a possible solution. You can use the random_seed function to give you a random seed.

let random_seed = sp_io::offchain::random_seed();

let number = u32::decode(&mut &random_seed[..]).expect("The seed is 32 byte and `u32` can be decoded from any 4 bytes; qed");

if number % 100 < 50 {
    do_the_validation();
} 

Be aware that for you real implementation you should ensure that always multiple nodes are validating the data and sending the result to the runtime.

6
  • Thanks @bkchr. I don't need multiple nodes validating the data in this instance. There is no benefit to any node for lying and only a very minor inconvenience to the network if they do, so I see it as low risk. Yes I could implement crowd validation but it would really be over engineering for the sake of it in this case. If I can't get the node count I'll design some form of algorithm similar to networked resource management.
    – jpataylor
    Commented Apr 27, 2022 at 15:11
  • Okay, but even with knowing the number of peers, you will not know how many of them run the offchain worker. You will need to have some kind of backup.
    – bkchr
    Commented Apr 28, 2022 at 9:05
  • Okay, so let's ignore the number of peers part and go with your idea to have multiple validators. If say, I wanted to require 3 validators to either approve or reject, what's the best way to implement that in such a way that a single node cannot approve/reject 3 times and therefore validate alone?
    – jpataylor
    Commented Apr 28, 2022 at 11:13
  • In that case, each node should have a signing key, like they do for BABE/GRANDPA, which allows you to tell them apart from each other.
    – Shawn Tabrizi
    Commented Apr 28, 2022 at 15:26
  • @ShawnTabrizi, is consensus from offchain workers necessary? Or if my validate_unsigned function checks the payload has been signed with an AuthorityID and that the source is local and doesn't propagate the validated transaction, can I assume that the call is not coming from some malicious node running a customised version of my offchain worker function?
    – jpataylor
    Commented May 6, 2022 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.