Skip to main content

Local Testnet

Run a fully local Amadeus testnet with 10 validators for contract and integration testing, send transactions and deploy RISC-V contracts to it.

The quickest way to build and debug, without deploying to the public testnet over and over, is a testnet on your own machine. A single amadeusd started with TESTNET runs a private chain with 10 validators and real block production and finality. It signs transactions for testnet and runs RISC-V smart contracts, like the public testnet.

What you get​

  • 10 validator keys, stored in $WORKFOLDER/seeds.
  • Each validator starts with 10,300,300.123456789 AMA (about 10.3 million) and a 1,000,000 AMA vault delegated to itself.
  • The HTTP API on http://127.0.0.1 (port 80) and the same API over HTTPS on port 443, with a self-signed certificate.

Requirements​

  • The amadeusd binary: download it as described in Using the REPL, or build it from source.
  • Ports 80 and 443. If you don't run as root, allow unprivileged processes to bind ports from 80 upwards (Linux):
sudo sysctl -w net.ipv4.ip_unprivileged_port_start=80

Start the testnet​

TESTNET=true WORKFOLDER=/tmp/testnet HTTP_IPV4=127.0.0.1 HTTP_PORT=80 ./amadeusd

Always set WORKFOLDER to a folder of its own. Without it the node uses ~/.cache/amadeusd, the folder that holds a node's own key in seeds.

On the first start in an empty WORKFOLDER, the node prints making testnet.., writes 10 new validator seeds to /tmp/testnet/seeds (replacing any seed file there), prints seeded testnet please restart node and exits. Run the same command again. The node then creates the genesis block and starts producing blocks. It also prints started https-api on 127.0.0.1:443 and started http-api on 127.0.0.1:80.

Keep HTTP_PORT=80: the HTTPS listener on 443 forwards to port 80. The HTTPS certificate is generated at startup, with the host names listed in /etc/hosts as its names.

Check that the API answers:

curl -s http://127.0.0.1/api/chain/stats

The response has the same shape as on the public RPC. To start over with a fresh chain, stop the node and delete the work folder (rm -rf /tmp/testnet).

Send a transaction from the REPL​

Submit a transfer between two validators in the node shell (iex(1)>):

key0 = Application.fetch_env!(:ama, :keys) |> Enum.at(0)
key1 = Application.fetch_env!(:ama, :keys) |> Enum.at(1)
Testnet.call(key0.seed, "Coin", "transfer", [key1.pk, "1", "AMA"])

The amount is a string in base units, so "1" is 0.000000001 AMA. The call waits until the transaction is in a block and returns:

%{
error: :ok,
hash: "...",
metadata: %{entry_hash: "...", entry_height: ..., status: :committed},
receipt: %{exec_used: "...", logs: [], result: "ok", success: true}
}

status is :committed once the transaction is in a block and :finalized once that block is rooted.

Testnet.transfer is a shortcut that sends from the first validator and converts floats to base units: Testnet.transfer(key1.pk, 1.0) sends 1 AMA.

Connect a wallet or the SDK​

To browse balances on the local chain, point the web wallet or the extension on the same machine at it: in the network settings choose Custom RPC and enter http://127.0.0.1 (the wallet adds /api). Transactions a wallet sends under Custom RPC are signed for mainnet, so the local testnet node rejects them. Send transactions from the REPL or with the SDK instead.

With the TypeScript SDK, point baseUrl at the local node and sign with NetworkType.TESTNET:

import { AmadeusSDK, NetworkType, buildCoinTransfer, signContractCall } from '@amadeus-protocol/sdk'

const sdk = new AmadeusSDK({ baseUrl: 'http://127.0.0.1/api' })

const call = buildCoinTransfer({ recipient: 'RECIPIENT_PUBLIC_KEY', amount: 1, symbol: 'AMA' })
// A local TESTNET node only accepts testnet-signed transactions
const { txPacked } = signContractCall('LOCAL_VALIDATOR_SEED', call, NetworkType.TESTNET)
const result = await sdk.transaction.submitAndWait(txPacked)

console.log(result.hash, result.receipt)

To get a funded seed for LOCAL_VALIDATOR_SEED, print one of the validator seeds in the REPL with Base58.encode(key0.seed), and its address with Base58.encode(key0.pk).

Deploy a contract​

Contracts are RISC-V ELF binaries. Build the samples from the node repository; this needs nightly Rust with the standard library sources:

git clone https://github.com/amadeusprotocol/node.git
rustup toolchain install nightly --component rust-src
cd node/contract_samples/riscv
./build.sh

The binaries are written to out/. Deploy the counter sample from the first validator, then call and view it. A contract lives at the address of the key that deployed it:

key0 = Application.fetch_env!(:ama, :keys) |> Enum.at(0)
Testnet.deploy key0, "/path/to/node/contract_samples/riscv/out/counter.elf"
Testnet.call key0.seed, key0.pk, "inc", ["1"]
Testnet.view key0.pk, "get", []

Testnet.deploy takes an optional third argument, the name of an init function to run on deploy (for example "init" for the coin and NFT samples). The contract samples README has more examples, and Build, test and deploy covers the toolchain, host calls and gas.

Multi-node localnet (source build)​

To test consensus and syncing across several nodes, the node repository has ex/localnet.sh. It needs the Elixir build toolchain (it starts the nodes with mix run). It wipes /tmp/ama-localnet (or $NET), creates a genesis with 10 validators and starts 5 nodes with 2 validator keys each, on 127.0.0.11 to 127.0.0.15, with simulated outbound latencies of 0, 15, 30, 100 and 200 ms:

cd node/ex
./localnet.sh

Logs go to $NET/n0.log to $NET/n4.log; stop the nodes with pkill -f 'mix run --no-halt'. The script doesn't set HTTP_IPV4, so these nodes don't serve the HTTP API, and they run with TESTNET off. For app and contract development, use the single-node testnet above.