NKN Mining Dashboard

Fri May 21, 2021

In the last post I started an experiment: I set up four NKN nodes in different regions around the world, to see which (if any) would earn mining rewards in the following two weeks. During that time I wanted to keep half an eye on the experiment, to make sure the nodes were up and to find out if any of them earned rewards.

Keeping an eye on things

nknx has a dashboard that shows which nodes are online and what they’re up to, but due to a high volume of traffic they’ve switched off live updates. I decided to make my own dashboard and to make it an opportunity to learn SwiftUI. Here’s what I came up with:

It’s an iPhone app with two tabs. The first shows a list of nodes with an icon indicating whether each node is mining. Each row includes the IP address of the node, a name specified by the user and the state icon. ⛏ means the node is mining, ❌ means it isn’t and ? means waiting to finding out. The states are updated every time you open the app, switch back to the nodes tab or add a new node. The + button shows a sheet where the user can enter the IP address and a name for a new node. The app then fetches the node’s public key and stores it with the name and IP address in CoreData.

The second tab shows a list of mining rewards that have been sent to the beneficiary wallet. You specify the wallet address by tapping the settings cog. Each reward is shown with the date it was earned, the amount earned and the name of the node that earned it. The rewards are updated every time you open the app, switch back to the rewards tab or change the wallet address.

You can get the source here.

Implementation

The app is written in Swift, using SwiftUI for the view layer. I used Swift Package Manager to manage dependencies, Dip for dependency injection (once the app got big enough to warrant it), Alamofire for the networking layer and CoreData for persistence. It’s architected according to the MVVM pattern.

By default NKN nodes run a json-rpc server on port 30003. You can use that to query the node’s state by sending a getnodestate request, like this:

curl --location --request POST 'http://[ip-address]:30003/jsonrpc' \
--header 'Content-Type: application/json' \
--data-raw '{
        "jsonrpc": "2.0",
        "method":  "getnodestate",
        "params": {},
        "id": 1
}'

The response includes a string representation of the node’s state. A state of PERSIST_FINISHED indicates the node is mining.

Whenever a node receives a reward it sends it to the beneficiary wallet. We can use the NKN API’s Get Address Transactions endpoint to get all the transactions associated with our beneficiary wallet. Transactions for mining rewards have a txType of COINBASE_TYPE.

Now we just need to establish which node earned the reward. The transactions we’ve received from the Get Address Transactions endpoint include a payload object, which includes a signerPk field. This is the public key of the node that earned the reward. By comparing the public key of the signer against the keys of our nodes we can establish which node earned the reward. The public key of a node is included in its response to the getnodestate message used above.

Thoughts on SwiftUI

SwiftUI is frighteningly productive. This was my second time using it and even so it took less than two hours to bang out the proof of concept, using hard-coded IP addresses and a hard-coded beneficiary wallet address. Then probably another two hours making it all configurable and mucking around with the presentation. Even the tab icons are images that are included in the runtime. You can browse all of the bundled images by installing the SF Symols 2 app on your development machine.

Next steps

Now the experiment is up and running I have an app I can check compulsively for the next two weeks. Which node will be the golden goose? Will my Azure credit last long enough to complete the experiment? I’ll share my findings in the next post.


<< Mining NKN For Fun And No Profit Toy Boats >>

Long Fetch

[home] [rss] [all]

Latest posts

Beavertail Canoe Paddle I made a beavertail canoe paddle out of a plank of ash and paddled it down a river.

Springs and Neaps Trying to visualise the next 28 days of tides.

Home Tide v2 Getting even more in tune with the tides, by seeing them on the iOS lock screen.

Paulownia On paulownia wood, daughters of marriagable age, projects that last decades and water craft.

Home Tide Calm technology to show the tide and twilight on the iOS home screen.

Frequent topics

apps ash azure beavertail beeswax bio-resin bitcoin blockchain boats calm-technology canoe cedar coinbase compound dai daughters erc20 ether ethereum handplane home-screen home-tide homescreen instagram instrument ios journal kong lock-screen lockdrop lockscreen making metamask mining moleskine music neap-tides nkn p2p paddle paulownia pool-together projects sapele sea seeds smart-contracts spring-tides stand-up-paddle stories sup surf swift swiftui tides tom-wegener toys uniswap web3 widget woodwork xylophone