Is there time?

One of the items I will need is a wait function. I am used to having one in Arduino based C and Python 3.x, but this is new in C++ from what I can see from my searches of the web. So I thought I would cover it here for a short post.

There is a boost function for thread handling that can be used to do a wait. Apparently,​ the compile C++11 can also handle this in the build classes. As we are using AWS’s Cloud9 development enviroment, lets use the C++11 solution.

To code this you will need an Include of chrono and thread; this code will give you a 1/2 second sleep:

std::this_thread::sleep_for(std::chrono::milliseconds(500));

Again, we have to type std:: everywhere as that is the coding standard of the day. The definition of time is also enjoying extra typing with a whopper of stt::chrono::milliseconds. That is almost reaching silly. Almost.

I have not learned the thread safe coding stuff supplied now by C++11. More to learn one of these days.

Well I thought I would take the time to cover wait.

First Utility Program

Well,​ the spacing was better in my code. I have written a utility program to create the Master Control Program data, mcpmainbuild and all the other header and class files. The program just creates a new file, MCP.xml, with all of the text for the name for the program and then ten random newly minted UUID values. These will be shared with the other running copies. This allows for a cheap and easy proof of work (POW). Well here is the code, again the editor played havoc with the formatting.

int main()
{
    
	// Print a header of execution out
	auto result = std::time(nullptr);	
	std::cout << "Master Control Build Program " << std::endl;
	std::cout << "Local Time: " <<
	    std::asctime(std::localtime(&result)) << std::endl;
	    
	// Define our tree of data
	pt::ptree tree;
	    
    // Create program information
    tree.put("mcp", "mcpmain");
    tree.put("mcp.programtext", "Master Control Program");
    tree.put("mcp.version", "0.0.1");
    
	// Create ten UUID in the file 
	boost::uuids::random_generator generator;
    boost::uuids::uuid uuid1 = generator();
	tree.put("UUID.0", uuid1);
	uuid1 = generator();
	tree.put("UUID.1", uuid1);
    uuid1 = generator();
	tree.put("UUID.2", uuid1);
	uuid1 = generator();
	tree.put("UUID.3", uuid1);
	uuid1 = generator();
	tree.put("UUID.4", uuid1);
	uuid1 = generator();
	tree.put("UUID.5", uuid1);
    uuid1 = generator();
	tree.put("UUID.6", uuid1);
	uuid1 = generator();
	tree.put("UUID.7", uuid1);
	uuid1 = generator();
	tree.put("UUID.8", uuid1);
	uuid1 = generator();
	tree.put("UUID.9", uuid1);
	
	pt::write_xml("MCP.xml", tree);
    
    result = std::time(nullptr);
    std::cout << "Exit: Master Control Build Program " << std::endl;
	std::cout << "Local Time: " <<
	    std::asctime(std::localtime(&result)) << std::endl;
    
    
    // Leave politely
    return 0;

}

This program just declares a tree using the boost library cool code. It then builds some data in the tree. Lastly, it just writes out the UUID values. Nothing interesting really, but we need to start somewhere.

I will add another table creation for the base value of blockchain file and the run data that is different by instance. I have decided for now to allow up to ten instances of this new blockchain code. The first and master instance is zero and must always be running. It is the one that panic calls will be sent to. If this zero instance does not reply then the blockchain instances panic and shutdown. It is easier to have one instance, I think, play the primary while letting most functions be shared and passed around.

We are starting!

Once I get further we will start referring to GitHub code and not posting it here.

The run looks like this:

Running /home/ec2-user/environment/mcpbuildmain.cpp
Master Control Build Program
Local Time: Fri May 4 05:08:22 2018

Exit: Master Control Build Program
Local Time: Fri May 4 05:08:22 2018

Process exited with code: 0

Quick note on AWS

It is the beginning of May 2018. My bill of about a dollar came in from AWS. It appears that I am paying $0.011 an hour for compute and a tiny amount for storage. I am using the Cloud9 development servers, the “free” level. So yes free is about a buck a month. I figure if I was more full-time on this project I would pay about $70 a month. I have the system shutdown after 30 mins of inactivity to keep the costs down. The is recommended by AWS.

Just some facts for anyone out there looking at Cloud9, nice and not that expensive but not free.

More on UUID

Wrote a lot of code at work today so needed a bit of a break. Not much coding then tonight.

But–even tired, I was wondering about UUID and some code I did not understand. Please see the previous UUID post for more of the code. Here is the code that got my mind spinning:

boost::uuids::name_generator_sha1 mygen(boost::uuids::ns::dns());
boost::uuids::uuid udoc = mygen("To Be or not to be");
std::cout << "boost.org uuid in dns namespace, sha1 version: " << udoc << std::endl;
boost::uuids::name_generator_sha1 moregen(uuid2);
boost::uuids::uuid udoc2 = moregen("To Be or not to be");
std::cout << "boost.org uuid in random, sha1 version: " << udoc2 << std::endl;
boost::uuids::uuid udoc3 = moregen("To Be or not to be");
std::cout << "boost.org uuid in random, sha1 version: " << udoc3 << std::endl;

This code starts with defining the mygen function with a base value. It then uses a string to be the base to be turned into a UUID. A hashing of sorts. What I did not understand was that the definition defined a unique evaluation and this would create the same value for the same string. I also did not know that a different starting value would allow for a different value but always again the same value.

The execution of the code looks like this:

boost.org uuid in dns namespace, sha1 version: 49826646-175a-5fd8-9712-97f89d1e1ce3
boost.org uuid in random, sha1 version: 75255484-63d0-5332-addf-0bc4e551507c
boost.org uuid in random, sha1 version: 75255484-63d0-5332-addf-0bc4e551507c

This allows for me to create a cheap and easy proof-of-work (POW). I could pick a random UUID and share it between two implementations of the software. Each could then generate the same value, as long as they used sha1, for a given string like a random UUID. I could avoid cryptological code a bit longer. So UUIDs appear worthy of investing some code in.

I am thinking about storing some values in a table and also to compile in some values for UUID values. Specifically, I am thinking of an XML config table using the tree routines I covered in a previous post. I could including some shared UUID values. I was thinking of sharing ten values for a network​ of ten implementations with each one assigned a number. Number 0 would be a sort of master. Then I would also compile one value in for start-up and for crashes.

This was worth a second look.

​

Only time for a short note

No code tonight, I was writing code for work and cannot get my focus back.

Instead, lets me share my idea. I would like to create a software that contains all of the blockchains​ in a simple XML file. The file contains all the history of the one blockchain and is, in fact,​ the blockchain. The shared ledger is the population of the files. The shared update would be implemented by a request for control of the shared ledger and then followed by updating a blockchain single file. This file would be then supplied to all the other ledgers. Each ledger would accept the file and replace the local file. An acknowledgment​​ of the update would be recorded. The transfer of files would be by remote-mounted directories. Thus a network of five Raspberry Pi computers all would be connected through a normal network, ​and each would have a remote directory for the other four systems.

The blockchains will have some control information and some data that should be encrypted. That will follow as I get this working. The main part of the blockchain is a 4096-byte​ store of hex data. This can be a program or data. I intend to build a contract that is a program and data in the hex store. An update of the file adds a replacement of the store at the end of the file. I will design a hex assembly-like​ language for this in another iteration.

The request to perform an update will require a handshake​ and proof-of-work (POW) or a simulation of that (i.e., a very weak POW like requiring two hex values when added together equal twelve)​. The control will then pass to the controlling process which will add a new file to the shared store or will replace an existing file’s content. The changed to the shared ledger then must be sent, the local copies aligned with the new data, and a response sent back.

For this iteration,​ I will try to get the basic files and UUIDs built. I will build a store and begin with that. I will follow with more complexity and multi-system updates.

All this should be fun.