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.
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.
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.