Getting started: App development with local network

How do any rust app developer get started on developing a simple app with local safe network?

  • How to properly setup a local node for development purposes?
  • How to properly connect to that local node?
  • How to interact with DHT, Register, wallet and more?
  • Are there any best practice to be considered?
2 Likes

Start with this topic and come back with questions: Can someone give me a guide on running a local network

4 Likes

Thanks, I have a local network run.

I tried connecting but getting error:

2024-07-11T01:54:57.402882Z ERROR sn_client::api: Timeout: Client failed to connect to the network within 30s

Codebase:

 
// SN CLIENT
use bls::SecretKey;
use sn_client::Client;
use sn_peers_acquisition::parse_peer_addr;

/// Autonomi network
struct Autonomi {
    client: Option<Client>,
    config: AutonomiConfig,
}

/// Autonomi Config
#[derive(Clone, Debug)]
struct AutonomiConfig {
    peers: Option<String>,
}

impl Autonomi {
    fn new() -> Self {
        Self {
            client: None,
            config: AutonomiConfig {
                peers: Some("127.0.0.1:8000".into()),
                // peers: Some("127.0.0.1:39275,127.0.0.1:37453,127.0.0.1:39019,127.0.0.1:34641".into()),
            },
        }
    }

    /// Create a new Client for testing
    pub async fn connect(&mut self, secret_key: SecretKey) -> Result<()> {
        let bootstrap_peers = match &self.config.peers {
            Some(str) => match parse_peer_addr(&str) {
                Ok(peer) => Some(vec![peer]),
                Err(err) => {
                    tracing::warn!("Can't parse SAFE_PEERS {str:?} with error {err:?}");
                    None
                }
            },
            None => {
                tracing::warn!("Can't get env var SAFE_PEERS with error");
                None
            }
        };

        tracing::info!("Client bootstrap with peer {bootstrap_peers:?}");
        self.client = Some(
            Client::new(secret_key, bootstrap_peers, None, None)
                .await
                .map_err(|e| {
                    tracing::error!("{e}");
                    Error::technical(Some(&e.to_string()), None)
                })?,
        );

        Ok(())
    }
}

I can’t debug this because there are too many things you don’t show that could be wrong.

Before you start trying with your own code I suggest you run through the examples and if they work, adapt that code.

You can also look at my code for a working app here: GitHub - happybeing/awe: A Website Publisher/Browser for Autonomi (demo) That can be used as a CLI app which shows how to connect and store data to the network which should get you started. Run it with --help to see the subcommands and subcommand --help to see the options for each subcommand. There is some information in the README but this is a demo under development, so don’t expect much support.

I find working code is a good way to learn, which is how I figured out the APIs I’m using: by looking at the code under safe_network`. There are some docs for those crates on docs.rs so look there too.

My code is AGPL so free to re-use if that helps. It may be a bit more complex than is helpful when starting from scratch, which is why I suggest you start with the examples. Can you use them with your local network to upload is a good first step.

Be aware though, you can, well I can, easily destroy your real wallet on the local machine when doing this. So I copy the wallet to a safe place every time I run a local testnet.

Good luck.

BTW this is wrong. I suspect you’re using ChatGPT or similar which is a bad approach IMO. You will just cause yourself pain and delay if you do that because it will create great looking code full of mistakes. To learn you need to figure things out yourself, and look at good working code.

2 Likes

Thank for your suggestion. I am not using ChatGPT, I don’t use ChatGPT for written my programs.

This is my own code and not ChatGPTs. Most of the code in the connect function was derived from the get_new_client in sn_client/src/test_utils.rs in the safe_network repo. Get new client code in sn_client repo.

All I am doing in my code is to wrap that function in my own Autonomi struct.

4 Likes

Glad to hear but your code does look ChatGPT, and my apologies for the insult.

You are hard coding invalid peers which is why I thought ChatGPT. You won’t find that in any examples or my code, so you should do that in the proper way before asking why it can’t connect.

It takes some work to get going - I know :laughing: - but I’m sure you can do it if you are willing to study the examples and in particular the sn_client API and how it is used in safe (sn_cli) and my own app.

Keep at it until you beat it into submission. It’s hard to do this rather than be spoon fed examples but well worth it IMO. You will also get ahead of others if that matters to you.

2 Likes

Hard coding peers was just a thing for test . All I am trying to achieve quickly get a node running, connect to it and validate how it fits my solution before committing to diving deep. I just needed a quick start guided that should be the focus of your replies rather than how another mans code looks similar to that of GPTs.

@happybeing I appreciate your suggestions but given the fact that you’ve being a long term member of this community I think you should be more useful in create start guides for devs interested in the network to increase adoption of the network. Not all experience devs will have the time to dig in to code just to try out a network.

Telling me what I should be doing is not going to get my help. I have apologised for my mistake and wish you luck.

Thanks for the help you’ve already provided it was helpful but you are an arrogant man. I believe I can be arrogant too and still appreciate your help.

Don’t worry I am going to commit to get this community a simple quick start guide to help devs easily try out the network.

1 Like

That is brilliant and I’ll help with that if and when I can. Perhaps we are both arrogant IDK. I suggest we focus on helping where we choose and accepting what help is offered.

I suspect we have different ways of working though, so that may mean we each go our own way.

Keep posting questions and let’s see who answers them and how. This isn’t my job, so I pick and choose how to spend my time according to what I enjoy, which includes helping others most of the time.

4 Likes