I am just getting started with libp2p, rust-libp2p specifically, and I am trying to figure out how to use the Kademlia distributed hash table for arbitrary key-value store, not for peer discovery. I am very closely following the example here and it is working very nicely for me. I understand how
Transports are built and how custom
Behaviours are implemented. I was also able to create a
Swarm just like in the example, and I understand that. However, there is one aspect of all of this that I am slightly confused about…
Starting on line 153 is where I start to get confused. My main point of confusion is: Why does this code need to be in a future, and what is the point of the second
Why does this code need to be run in a future?
So, I tried removing the future entirely and just making a loop that handles each line from stdin, but that didn’t work at all. It appears that the
handle_input_linewasn’t working at all, and no data was stored or retrieved from the swarm’s Kad DHT. Why didn’t that work? Isn’t it the same thing as reading a line from stdin and then handling that line? I don’t get why the same code magically works when it’s being run inside a future. Also,
I don’t see the point of the second
It doesn’t look like the second loop does anything besides just print out the listening address… am I correct? Does this second
loopblock actually do anything crucial?
I’ve done a lot of experimenting, like removing the second loop and changing around how data is read from stdin with a hope of trying to understand the last part of this (otherwise really helpful) example code. But I still don’t get why the code works in a future.
So, are there any resources that you can point me to to help me understand this problem of mine?
TL;DR: Why does reading from stdin need to be in a future (example code in link above)