Skip to content

Commit

Permalink
make config.json optional
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed May 8, 2024
1 parent a65026f commit d8381da
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 142 deletions.
86 changes: 34 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

# NKN Full Node

### *Official Go implementation of NKN full node.*
### _Official Go implementation of NKN full node._

<br/>

[![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/nknorg/nkn)](https://goreportcard.com/report/github.com/nknorg/nkn) [![Build Status](https://github.com/nknorg/nkn/actions/workflows/build-ubuntu.yml/badge.svg)](https://github.com/nknorg/nkn/actions/workflows/build-ubuntu.yml) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing)

### Dev Status: V2 line, Production (Stable and Feature-Complete)

---

<br/>

> NKN, short for New Kind of Network, is a project aiming to rebuild the
Expand All @@ -21,9 +24,9 @@ Note: This is the official **full node** implementation of the NKN protocol,
which relays data for clients and earn mining rewards. For **client**
implementation which can send and receive data, please refer to:

* [nkn-sdk-go](https://github.com/nknorg/nkn-sdk-go)
* [nkn-sdk-js](https://github.com/nknorg/nkn-sdk-js)
* [nkn-java-sdk](https://github.com/nknorg/nkn-java-sdk)
- [nkn-sdk-go](https://github.com/nknorg/nkn-sdk-go)
- [nkn-sdk-js](https://github.com/nknorg/nkn-sdk-js)
- [nkn-java-sdk](https://github.com/nknorg/nkn-java-sdk)

## Introduction

Expand All @@ -50,13 +53,13 @@ More details can be found in [our wiki](https://github.com/nknorg/nkn/wiki).

## Technical Highlights

* Transmit any data to any node/client without any centralized server.
* Proof-of-Relay, a useful proof of work: mining is relaying data.
* Extremely scalable consensus algorithm (billions of nodes within seconds).
* Strong consistency rather than eventual consistency.
* Dynamic, large-scale network.
* Verifiable topology and routes.
* Secure address scheme with public key embedded.
- Transmit any data to any node/client without any centralized server.
- Proof-of-Relay, a useful proof of work: mining is relaying data.
- Extremely scalable consensus algorithm (billions of nodes within seconds).
- Strong consistency rather than eventual consistency.
- Dynamic, large-scale network.
- Verifiable topology and routes.
- Secure address scheme with public key embedded.

### Use pre-built binaries

Expand All @@ -68,7 +71,7 @@ a node.

### Use pre-built Docker image

*Prerequirement*: Have working docker software installed. For help with that
_Prerequirement_: Have working docker software installed. For help with that
visit [official docker
docs](https://docs.docker.com/install/#supported-platforms)

Expand All @@ -85,7 +88,7 @@ a node.

### Building using Docker

*Prerequirement*: Have working docker software installed. For help with that
_Prerequirement_: Have working docker software installed. For help with that
visit [official docker
docs](https://docs.docker.com/install/#supported-platforms)

Expand All @@ -111,6 +114,7 @@ $ make
```

Run Unit Tests

```sh
# run all tests
$ go test -v ./...
Expand All @@ -121,8 +125,8 @@ go test -v ./chain/store

After building is successful, you should see two executables:

* `nknd`: the nkn node program
* `nknc`: command line tool for nkn node control
- `nknd`: the nkn node program
- `nknc`: command line tool for nkn node control

Now you can see [configuration](#configuration) for how to configure and run a
node.
Expand Down Expand Up @@ -152,24 +156,25 @@ be specified by passing arguments to `nknd` or in `config.json`, run `nknd

We provide a few sample `config.json`:

* `config.mainnet.json`: join the mainnet
* `config.testnet.json`: join the testnet
* `config.local.json`: create and join a private chain on your localhost
- `config.mainnet.json`: join the mainnet
- `config.local.json`: create and join a private chain on your localhost

You can copy the one you want to `config.json` or write your own.

For convenience, we ship a copy of `config.mainnet.json` in release version (as
`default.json`) and in docker image (under `/nkn/`). The docker container will
copy this default one to `/nkn/data/config.json` if not exists on nknd launch.

If config file is not provided, node will join the mainnet by default.

#### `wallet.json`:

Before starting the node, you need to create a new wallet first. Wallet
information will be saved at `wallet.json` and it's encrypted with the password
you provided when creating the wallet. So please make sure you pick a strong
password and remember it!

``` shell
```shell
$ ./nknc wallet -c
Password:
Re-enter Password:
Expand All @@ -191,11 +196,7 @@ random password saved to `/nkn/data/wallet.pswd` if not exists on nknd launch.

#### `certs/`

`nknd` uses Let's Encrypt to apply and renew TLS certificate and put in into
`cert/` directory.

By default `nknd` will generate certificate with `x-x-x-x.ipv4.nknlabs.io` domain name which `x-x-x-x` is your ipv4
address, replace dot with dash.
`nknd` uses Let's Encrypt to apply and renew TLS certificate and put in into `cert/` directory. By default `nknd` will generate certificate with `x-x-x-x.ipv4.staticdns{1,2,3}.io` domain name which `x-x-x-x` is your ipv4 address, replace dot with dash. **The default cert domain is deprecated and will be removed in the future version. Please provide your own domain or cert if you need to use tls.**

If you would like to use your own domain name, simply set `CertDomainName` with your domain name in `config.json`, `nknd` will
automatically apply or renew certificate from Let's Encrypt and deploy it.
Expand All @@ -204,17 +205,14 @@ If you already have certificate and want to use it in `nknd`, you can put it in
`HttpsJsonDomain` `HttpWssDomain` with your domain name, `HttpsJsonCert` `HttpsJsonKey` `HttpWssCert` `HttpWssKey`
with your certificate full chain file and private key file.


#### Data and Logs

After `nknd` starts, it will creates two directories: `ChainDB` to store
blockchain data, and `Log` to store logs. By default `nknd` will creates these
directories in the current working directory, but it can be changed by passing
`--chaindb` and `--log` arguments to `nknd` or specify in config.json.

Now you can [join the mainnet](#join-the-mainnet), [join the
testnet](#join-the-testnet) or [create a private
chain](https://github.com/nknorg/nkn/wiki/Create-a-Private-Chain).
Now you can [join the mainnet](#join-the-mainnet) or [create a private chain](https://github.com/nknorg/nkn/wiki/Create-a-Private-Chain).

### Join the MainNet

Expand Down Expand Up @@ -261,12 +259,12 @@ wrong. If the problem still persists, [create an
issue](https://github.com/nknorg/nkn/issues/new) or ask us in our [Discord
group](#community).

### [Recommended] Using BeneficiaryAddr
### [Recommended] Using Beneficiary Address

By default, token mined by your node will be sent to the wallet your node is
using, which is NOT as safe as you might think. The recommended way is to use
another cold wallet (that is saved and backed up well) to store your token. You
can use your code wallet address as `BeneficiaryAddr` in `config.json` such that
can use your code wallet address as `beneficiaryAddr` in `config.json` such that
token mined by your node will be sent directly to that beneficiary address. This
is safer and more convenient because: 1. even if your node is hacked, or your
node wallet is leaked, you will not lose any token; 2. if you run multiple
Expand All @@ -275,16 +273,7 @@ address.

### NAT traversal and port forwarding

Most likely your node is behind a router and does not have a public IP address.
By default, `nknd` will try to detect if your router supports UPnP or NAT-PMP
protocol, and if success, it will try to set up port forwarding automatically.
You can add `--no-nat` flag when starting nknd OR add `"NAT": false` in
`config.json` to disable automatic port forwarding. If your router does not
support such protocol, you **have to** setup port forwarding on your router for
port 30001 as well as **all** other ports specified in `config.json`
(30001-30005 by default), otherwise other nodes cannot establish connections to
you and you will **NOT** be able to mine token even though your node can still
run and sync blocks.
By default, `nknd` will try to detect if your node is behind a router and if your router supports UPnP or NAT-PMP protocol, and if success, it will try to set up port forwarding automatically. You can add `--no-nat` flag when starting nknd OR add `"NAT": false` in `config.json` to disable automatic port forwarding. If your router does not support such protocol, you **have to** setup port forwarding on your router for port 30001 as well as **all** other ports specified in `config.json` (30001-30005 by default), otherwise other nodes cannot establish connections to you and you will **NOT** be able to earn mining rewards even though your node can still run and sync blocks.

When setting up port forwarding, public port needs to be the same as private
port mapped to your node. For example, you should map port 30001 on your
Expand All @@ -297,13 +286,6 @@ mappings, one for each port. One of the easiest way to find out how to setup
port forwarding on your router is to search "how to setup port forwarding" +
your router model or name online.

### Join the TestNet

Joining the TestNet is the same as joining MainNet, except for using
`config.testnet.json` as your config file instead of `config.mainnet.json`. Note
that TestNet token is for testing purpose only (thus do not have value), and may
be cleared at any time when TestNet upgrades.

## Contributing

**Can I submit a bug, suggestion or feature request?**
Expand All @@ -330,8 +312,8 @@ git commit -s

## Community

* [Forum](https://forum.nkn.org/)
* [Discord](https://discord.gg/c7mTynX)
* [Telegram](https://t.me/nknorg)
* [Reddit](https://www.reddit.com/r/nknblockchain/)
* [Twitter](https://twitter.com/NKN_ORG)
- [Forum](https://forum.nkn.org/)
- [Discord](https://discord.gg/c7mTynX)
- [Telegram](https://t.me/nknorg)
- [Reddit](https://www.reddit.com/r/nknblockchain/)
- [Twitter](https://twitter.com/NKN_ORG)
3 changes: 2 additions & 1 deletion api/httpjson/RPCserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ func (s *RPCServer) Handle(w http.ResponseWriter, r *http.Request) {
err := recover()
if err != nil {
var code errcode.ErrCode
if _, err = chain.GetDefaultLedger(); err != nil {
if _, e := chain.GetDefaultLedger(); e != nil {
err = e
code = errcode.ErrNullDB
} else if s.GetNetNode() == nil {
code = errcode.ErrNullID
Expand Down
7 changes: 4 additions & 3 deletions api/websocket/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -159,7 +160,7 @@ func (ms *MsgServer) registryMethod() {
if okSig && okSalt { // if client send ClientSalt and Signature, then check it
strSignature, typeOk := signature.(string) // interface type assertion
if !typeOk {
return api.RespPacking(err.Error(), errcode.INVALID_PARAMS)
return api.RespPacking(errors.New("invalid signature"), errcode.INVALID_PARAMS)
}
byteSignature, err := hex.DecodeString(strSignature)
if err != nil {
Expand All @@ -168,7 +169,7 @@ func (ms *MsgServer) registryMethod() {

strClientSalt, typeOk := clientSalt.(string) // interface type assertion
if !typeOk {
return api.RespPacking(err.Error(), errcode.INVALID_PARAMS)
return api.RespPacking(errors.New("invalid salt"), errcode.INVALID_PARAMS)
}
byteClientSalt, err := hex.DecodeString(strClientSalt)
if err != nil {
Expand Down Expand Up @@ -562,7 +563,7 @@ func (ms *MsgServer) newConnection(conn session.Conn, r *http.Request) {
for {
messageType, bysMsg, err := conn.ReadMessage()
if err != nil {
log.Errorf("websocket read message error: %v", err)
log.Debugf("websocket read message error: %v", err)
break
}
conn.SetReadDeadline(time.Now().Add(pongTimeout))
Expand Down
19 changes: 5 additions & 14 deletions config.local.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"NAT": false,
"HttpWsPort": 30002,
"HttpJsonPort": 30003,
"Hostname": "127.0.0.1",
"SeedList": [
"http://127.0.0.1:30003"
],
"StunList": [
"stun:stun.l.google.com:19302",
"stun:stun.cloudflare.com:3478",
"stun:stunserver.stunprotocol.org:3478"
],
"GenesisBlockProposer": ""
{
"nat": false,
"hostname": "127.0.0.1",
"seedList": ["http://127.0.0.1:30003"],
"genesisBlockProposer": ""
}
56 changes: 2 additions & 54 deletions config.mainnet.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,3 @@
{
"BeneficiaryAddr": "",
"SeedList": [
"http://mainnet-seed-0001.nkn.org:30003",
"http://mainnet-seed-0002.nkn.org:30003",
"http://mainnet-seed-0003.nkn.org:30003",
"http://mainnet-seed-0004.nkn.org:30003",
"http://mainnet-seed-0005.nkn.org:30003",
"http://mainnet-seed-0006.nkn.org:30003",
"http://mainnet-seed-0007.nkn.org:30003",
"http://mainnet-seed-0008.nkn.org:30003",
"http://mainnet-seed-0009.nkn.org:30003",
"http://mainnet-seed-0010.nkn.org:30003",
"http://mainnet-seed-0011.nkn.org:30003",
"http://mainnet-seed-0012.nkn.org:30003",
"http://mainnet-seed-0013.nkn.org:30003",
"http://mainnet-seed-0014.nkn.org:30003",
"http://mainnet-seed-0015.nkn.org:30003",
"http://mainnet-seed-0016.nkn.org:30003",
"http://mainnet-seed-0017.nkn.org:30003",
"http://mainnet-seed-0018.nkn.org:30003",
"http://mainnet-seed-0019.nkn.org:30003",
"http://mainnet-seed-0020.nkn.org:30003",
"http://mainnet-seed-0021.nkn.org:30003",
"http://mainnet-seed-0022.nkn.org:30003",
"http://mainnet-seed-0023.nkn.org:30003",
"http://mainnet-seed-0024.nkn.org:30003",
"http://mainnet-seed-0025.nkn.org:30003",
"http://mainnet-seed-0026.nkn.org:30003",
"http://mainnet-seed-0027.nkn.org:30003",
"http://mainnet-seed-0028.nkn.org:30003",
"http://mainnet-seed-0029.nkn.org:30003",
"http://mainnet-seed-0030.nkn.org:30003",
"http://mainnet-seed-0031.nkn.org:30003",
"http://mainnet-seed-0032.nkn.org:30003",
"http://mainnet-seed-0033.nkn.org:30003",
"http://mainnet-seed-0034.nkn.org:30003",
"http://mainnet-seed-0035.nkn.org:30003",
"http://mainnet-seed-0036.nkn.org:30003",
"http://mainnet-seed-0037.nkn.org:30003",
"http://mainnet-seed-0038.nkn.org:30003",
"http://mainnet-seed-0039.nkn.org:30003",
"http://mainnet-seed-0040.nkn.org:30003",
"http://mainnet-seed-0041.nkn.org:30003",
"http://mainnet-seed-0042.nkn.org:30003",
"http://mainnet-seed-0043.nkn.org:30003",
"http://mainnet-seed-0044.nkn.org:30003"
],
"StunList": [
"stun:stun.l.google.com:19302",
"stun:stun.cloudflare.com:3478",
"stun:stunserver.stunprotocol.org:3478"
],
"GenesisBlockProposer": "a0309f8280ca86687a30ca86556113a253762e40eb884fc6063cad2b1ebd7de5"
{
"beneficiaryAddr": ""
}
17 changes: 0 additions & 17 deletions config.testnet.json

This file was deleted.

18 changes: 17 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,23 @@ var (
SyncBlockRateBurst: 1024,
SyncMode: "full",
MaxRollbackBlocks: 180,
ClientMsgCacheSize: 0, // client relay messag cache mega bytes
ClientMsgCacheSize: 0,
GenesisBlockProposer: "a0309f8280ca86687a30ca86556113a253762e40eb884fc6063cad2b1ebd7de5",
SeedList: []string{
"http://mainnet-seed-0001.nkn.org:30003",
"http://mainnet-seed-0002.nkn.org:30003",
"http://mainnet-seed-0003.nkn.org:30003",
"http://mainnet-seed-0004.nkn.org:30003",
"http://mainnet-seed-0005.nkn.org:30003",
"http://mainnet-seed-0006.nkn.org:30003",
"http://mainnet-seed-0007.nkn.org:30003",
"http://mainnet-seed-0008.nkn.org:30003",
},
StunList: []string{
"stun:stun.l.google.com:19302",
"stun:stun.cloudflare.com:3478",
"stun:stunserver.stunprotocol.org:3478",
},
}
)

Expand Down

0 comments on commit d8381da

Please sign in to comment.