1

I want to get/store the peerId or some identifier of all active nodes in the blockchain and use them in the logic of a custom pallet.

I think that I need to get this data from some API and call the data into the pallet.

1 Answer 1

3

Nodes don't store networking information beyond their own peers, and there's no reason for them to implement some rather complex network traversal algorithm to keep track of literally ALL nodes in the network. So there's no API that will give you that answer out-of-the-box.

That being said, sp_io::offchain::network_state allows you to set up an Offchain Worker that would, for every node, create an on-chain entry. So your pallet can set a StorageMap<PeerId, Vec<Multiaddr>> to keep track of all network nodes, along with the OCW that keeps updating such item for every node.

Check node-authorization pallet, along with this tutorial for some reference on how to get started on this. Its use case is slightly different, but there's some parallels with your question.

You also need to make sure that nodes that eventually go offline are cleaned up from this StorageMap. So your OCW should get the list of all nodes from on-chain Storage, sweep the list checking if each one is online, and if not, remove it.

One big caveat for this solution is that you will only be keeping track of nodes that have OCW enabled. Non-validator nodes will not enable OCW by default, so the CLI option --offchain-worker always needs to be used on every non-validator nodes. You could always maintain your own fork of sc-cli to make OCWs run by default on every node of your network.

1
  • Thank you Bernardo for the tips! I will dig into it.
    – viac92
    Commented Aug 19, 2022 at 9:06

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.