Blog

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.

UUID

Universal identifier is something I think I will need for my next step.

Boost supplies a couple of useful libraries​ for UUID.

So to get this done one needs to include the libraries (you will need to put brackets around the names):

#include boost/uuid/uuid.hpp
#include boost/uuid/uuid_generators.hpp // generators
#include boost/uuid/uuid_io.hpp // streaming operators etc.​

Then I had to head to stack overflow to find a simple enough example.

boost::uuids::random_generator generator;

boost::uuids::uuid uuid1 = generator();
std::cout << "First UUID: " << uuid1 << std::endl;

boost::uuids::uuid uuid2 = generator();
std::cout << "Second UUID: " << uuid2 << std::endl;

std::cout << "DNS UUID: " << boost::uuids::ns::dns() << std::endl;

This uses a randomly generated seed from the entropy routines from the underlying LINUX. These routines are not necessary cryptological​ secure. But for my purposes, I think I am ok.

The output of this code is this:

​First UUID: 20684e89-013a-49af-a733-c0e3bc9ef199
Second UUID: a43c20ac-4aa0-456e-b810-7cc9b19779f3

So, we can use this to create random created keys that are also unique as they are associated to some degree to the computer and should not repeat. I am still doing some homework in this area, but I thought a short post of UUIDs would be welcomed. I expect to use these as key for each block chain.

Code Start

As we covered in the previous posts, I plan to build a blockchain like software because I want something that runs on Raspberry Pi and I also want a bit more control. I have selected C++ and Cloud9 as my tools. Now to use them.

I first created a new user for myself and potentially others who I might use my Cloud9 account. AWS has tutorials for all of this, that is how I learned. So with a new user and IDE in the cloud, I was ready to start.

I first wanted some standards. I wanted a comment standard to follow. So I read various coding standards for C++ and selected the ones that looked the most standard and new–Google got my attention. I will try to fit close to their standard. I noticed that they restricted the Boost library functions to use to only those approved to use. Most of the ones I was looking at were not approved.

After some tries and thought, I wrote this and like it (sorry it is not in the best font–Don’t know how to change that):

/**

Copyright 2018 Michael R. Wild

Permission is hereby granted, free of charge, to any person obtaining a copy of
 this software and associated documentation files (the "Software"), to deal in
 the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

@file mcpmain.cpp
 @author Michael R. Wild
 @date 3/6/2018
 @version 0.0.1

@brief Master Control Program

@section DESCRIPTION

This is the description

@section CHANGES

20180306 Creation

*/

I decided to release the code and to use @-notation to capture relevant information. I liked the idea of having the information available to an editor. The MCP is a reference to the old TRON movie. I have to admit it took me days to decide on this and I read a lot of standards.

The main (not easy to show as I had to embed some characters) is just a group of demos. Still learning and guessing how to do all of this. I have not used the convention of std being defined before. I have not written formal C++ and was taken aback by some of the changes in standards I read. Now define the minimal and key a bit more. I also like the auto to define the obvious.

#include <iostream>
#include <ctime>
#include "mcp.cpp"

int main()
{
    
    MCPParms ourparms("some.xml"); // parms for program
	
	// Print a header of execution out
	auto result = std::time(nullptr);	
	std::cout << "Master Control Program " << std::endl;
	std::cout << "Local Time: " <<
	    std::asctime(std::localtime(&result)) << std::endl;
	    
	// Try to get the parms for the MCP
	std::cout << "Read in MCP controls " << std::endl;
	try
    {
        ourparms.load();
        std::string name = ourparms.get("mcp.programtext");
        std::string version = ourparms.get("mcp.version");
        std::cout << "Success: " << name << " " << version << std::endl;
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << std::endl;
    }
    
    // update something
    ourparms.put("mcp.more", "This is a test");
    
    // get update
    try
    {
        std::string name = ourparms.get("mcp.more");
        std::cout << "Success: " << name << " " << std::endl;
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << std::endl;
    }
    
    // save change
    try
    {
        ourparms.save();
        std::cout << "Saved!" << std::endl;
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << std::endl;
    }
    
    // Leave politely
    return 0;
}

The code read in an XML file into a tree structure. I really like this as I can just load data into a tree from a file and look it up and update it and even write it back. That gives me all sorts of ideas.

Next time I will expand the information into the includes. Once that happens this Blog will be caught up and I will have to code and Blog at the same time. I am thinking of blogging on Tuesdays and Thursday and code the other nights.

 

Boost

You can find at Boost Org the fantastic library of free and handy code. I remember writing my first tree structures and data structure code back in the 1980s (during the pre-Internet times) in Pascal and wondering why we had to write this code–it should be available. I remember having to recopy in all of my code into the compiler I wrote to do a tree search. Boost covers much of this and many things that were just ideas–dreams–in the 1980s (like XML parsing).

I first learned of Boost when I was trying to compile some blockchain code. The Boost version of the Raspberry Pi OS, Raspbian was one version too old. Annoyingly, the code for the blockchain solution contained legacy code that would not compile at a higher level. I learned how to build Boost on a slow Raspberry Pi 3. I still could not get the code to run. I moved on but liked the Boost library.

The Boost library is meant to extend C++. I have written C++ for years for so many sketches for the Arduino. I have written some pretty impressive code for robots and other projects in Arduino’s version of C and C++. I was feeling confident I could code C++.

I saw that advertisements on Amazon for Cloud9, a development platform. It was cloud-based and free. This being Amazon Cloud it would likely cost something, usually about the cost of coffee in Starbucks (this is the PNW). I tried it out and create my first server. The IDE was a weak Eclipse version, but it could do all the languages I might need and some I did not need. Go was missing–Google’s language, but as Google is a competitor to Amazon it was not a surprise. It has C++ and version 9 and Python 3.6+, so I was happy.

The editor worked with some coloring for reserved words and some standard completion logic. It has the requirement that you save your work to a file for it to work well. I also noticed that the first compile failed and then worked after that. Again, the file needs to be there for the compile options to work I think.

I had trouble with remembering my C++ and made a few basic mistakes. The Arduino code I worked with over the last four years is not as formal as the includes of GNU C++, but once I finally took a quick online class from YouTube, I got my C++ to run. I do not remember “Hello, World” as this hard!

I used the Linux terminal to update my software and load Boost library 61, newest version. I followed the directions and built a library. It was in my workspace and not in the compile path. I created a link to the director in the correct place, and then it worked.

With Boost now working and an IDE and me remembering my C++, we are ready to start our project. Next post we’ll look at that.