2.2.2 Cycle 2 - Changing Node Language to V Lang
Design
Objectives
If I want to let users contribute using the node software and continue down the javascript & node.js route they will have to download the software, download node, then all of the libraries I used for the project, which is far from ideal.
Therefore I'm going to be switching to a different programming language and a different distribution method. The conditions I need this programming language to meet are the following:
Can be compiled with libraries to limit the amount of setup end users have to do.
Can be compiled to arm and x86 on macOS, Linux and Windows.
Ideally has type safety
Runs Fast
Has networking capabilities
Has modern features
The best language I could find for those specifications was V (aka Vlang) which is a type based language based upon C.
There are both upsides and downsides to using Vlang instead of typescript, the largest upsides are how much faster than typescript it is and that it can be compiled to binary so that end users won't have to worry about downloading additional packages/files. The downsides come more in the form of general development, where some functionality is likely to contain more bugs and there will likely be a lot less documentation because Vlang is such a new programming language.
Rewriting what I've done so far.
Now that I've decided to switch from typescript to Vlang I will first need to rewrite everything I've done in this new language. However since this isn't actually that much, I will also be setting up the api server in this cycle which will allow you to go to the ip address that the node is running on and using a http get request (what your browser sends when you open a webpage) to receive the message "Hello world".
Summary of the objectives for this cycle
Development
Setting up Vlang
Setting up the V compiler is actually pretty basic, all that is needed is to clone the git repo (https://github.com/vlang/v), run the make file and then follow the instructions given. I then also setup the symlink, which is what allows the compiler to run just by typing "v" in the terminal rather than having to run the path to the executable.
This can be verified to have being setup properly using "v --version" in the terminal, see tests.
Writing the Vlang program
In order to keep the code clean and easy to understand, I'm going to split the different parts of the node software into different files and then different modules to group those files together. In this dev cycle I will be setting up the first two modules, main and server.
The Main module
This will be the part of the node software which handles and operates all of the other modules, all the major data in this program should flow through the main module. Initially it will need to:
This will look something like:
// Pseudocode
module main
import server
procedure main():
output "A Welcome Message"
port = (an integer value between 0 and 25565)
server.start(port)
end procedure
After converting this Pseudocode to V code we get the following:
// Vlang Code - packages/node/src/main.v
module main // declares which module this part of the code is
import server // imports the server module referenced above
fn main() {
// output to the terminal to let the user know it's running,
println('***** MonoChain Mining Software *****')
port := 8001
// then start the webserver
server.start(port)
}
The Server module
Now that I have built out a basic main file, it needs the server module to allow the server.start(port) function to actually work, this is the point of that module.
// Pseudocode
module server // declare which part of the project this is
import webServer // import a http server module
function start(port): // starts running the webserver
webServer.run()
function index_fn(): // returns 'Hello World!' to the domain it runs on
return "Hello World!"
webServer.page("/", index_fn) // assign the index_fn to "/" on the server.
This looks slightly differently in V due to how the default api server works but it does the same thing.
// Vlang Code - packages/node/src/server/main.v
module server // declare which part of the project this is
import vweb // import the http api server library.
struct App { // generate key constants that represent the server
vweb.Context
}
pub fn start(port int) { // this launches the server
vweb.run(&App{}, port)
}
pub fn (mut app App) index() vweb.Result {
return app.text('Hello World!')
} // this is how a webpage is represented in V
The New Build Configuration
Because the previous version of this code was setup in a different language, it means that all the build configuration files are now incorrect, correcting this is fairly easy, as yarn - the configuration and package manager I use - allows support for any terminal commands to be executed through it's scripts function.
This means I just have to figure out what terminal commands will need to be run differently to the typescript version and adjust them accordingly. Then finish off the config files by removing any unnecessary junk left over by the old config.
Compiling
I've changed quite a lot in the config file, but most of it is fairly basic so I've chosen to only highlight the biggest change, which is how the program is compiled.
// Old Configuration
"compile": "tsc && cp \"./package.json\" ./dist/code/ && echo \"compiled\"",
// New Configuration
"compile": "v ./src/ && mv ./src/src ./dist/code && echo \"compiled\"",
As you can see there isn't a massive difference between the two, with the only actual difference being that the program is compiled using "v ./src/" rather than "tsc", and that the resulting file is now moved using "mv ./src/src ./dist/code" rather than being compiled in that location and partially copied using "cp \"./package.json\" ./dist/code/" although they both result in a compiled version of the program being in "./dist/code".
It's important to remember here that unlike typescript, which just compiles to javascript, V compiles to binary, this means that this compiled version of the project can only be run on an arm based macOS device, or whatever else it is compiled on.
Users who don't use a device of that exact type (most of the expected user pool), can however clone the git repository and compile the program on their own device and it should work without any issues!
Challenges
The main challenges for this cycle have been learning the syntax and flow of a completely new programming language, since I have never actually used V for a project before this one I spent a lot of time in the documentation and searching the internet for solutions to obscure and unusual compiling issues (which I didn't mention in the testing as it turned out to be an issue with how I downloaded the compiler and not the code I wrote.).
This wasn't a huge issue but it did certainly add some friction to the development process, and I'm hopeful that by the end of this project I should've proficiently learnt how to use Vlang to the extent at which I'd say I can code well in it.
Setup and Configuration Testing
1
run "v --version" in the terminal
Output of "V" then some version number and a hash of that update.
"V 0.3.0 426421b"
Passed
2
run "yarn start" in the node package's source.
The node software to be compiled and ran.
As expected
Passed
3
run "yarn build" in the node package's source
The node software to be compiled, zipped and put into the webportal's public folder.
As expected
Passed
Test 1 - Checking that the V compiler is installed properly

Test 2 - Seeing if the re-written software runs as expected

Test 3 - Building the node software

Program Testing
4
start the program using "yarn start"
Outputs a welcome message
"***** MonoChain Mining Software *****"
Pass
5
Web api should launch on port 8001
"Hello World" on "http://localhost:8001"
As expected
Pass
Evidence for Program Tests
Test 4 - See Evidence for Test 2
Test 5 - Web api launched at "http://localhost:8001"

Download Testing
6
Navigate to "https://monochain.network/download"
See updated download instructions
As expected
Pass
7
Download and unzip the node software
Should download and unzip to an executable file
As expected
Pass
8
Run the node software on an arm based macOS device
Should run as tested in program tests
As expected
Pass
Evidence for Download testing
Test 6 - Updated download instructions

Last updated