Creating a custom private SQL blockchain with Daisy

in #blockchain6 years ago (edited)

Daisy, my PoA blockchain where blocks are SQLite databases, is a bit over a year old by now, and it's gaining features mostly on as-needed basis. Patches and contributors are always welcome, of course. Today, I've finished a milestone feature: the ability to run custom blockchains without modifying the platform code (written in Go, btw). This means that it's now easily used to create and run arbitrary blockchains in arbitrary, possibly private, environments, without touching the public blockchain.

What is it good for? 

My original purpose for creating Daisy was distribution of authenticated data. As in, a small set of nodes have the authority to create blocks of data (and those blocks are SQLite databases), while the whole world can replicate those blocks and read them - either through Daisy code or even by directly attaching to the databases themselves. Daisy doesn't mind, as long as the blocks are opened read-only.

Some use cases I think this model fits well are:

  • Distribution of authenticated sensor data
  • Distribution of municipal / governmental records
  • A world-wide database of common data, such as UPC codes
  • Distribution of logging / auditing data

Note that Daisy does away with the concept of "blockchain transactions". The blocks are the data. Of course, these blocks can contain arbitrary data and this includes the possibility to create a layer which could behave by certain rules, such as those forming a cryptocurrency. Transactions and smart contract processing can be introduced on top of Daisy's existing infrastructure.

Getting started

Assuming you have set up your Go development environment, first, clone the Daisy Git repository from https://github.com/ivoras/daisy , e.g. with:

git clone https://github.com/ivoras/daisy

Then get the sqlite3 dependancy with:

go get -u github.com/mattn/go-sqlite3

and build Daisy with:

go build

This will result in an executable file named daisy or daisy.exe, depending on your OS.

Next, you'll need to create a JSON file containing information about the new blockchain. There's a provided example file named chainparams.example.json which looks like this: 

{
"creator": "Ivan Voras <[email protected]>",
"genesis_block_timestamp": "2018-08-16T12:49:32+02:00",
"bootstrap_peers": [ "cosmos.ivoras.net:2017" ]
}

For now, that's all there is. Be sure to edit the bootstrap_peers list so you don't connect to my default node. While creating the new genesis block, Daisy will also create a new keypair, whose public key is recorded in the block itself, and sign the block with it, which is enough to guarantee that every new genesis block created in this way is unique.

To start a new blockchain, just issue the "daisy newchain" command and observe its output:

$ daisy newchain chainparams.json
2018/08/16 13:33:38 Starting up godaisy/0.1 ...
2018/08/16 13:33:38 Data directory /home/ivoras/.daisy doesn't exist, creating.
2018/08/16 13:33:38 Creating a new blockchain from chainparams.example.json
2018/08/16 13:33:38 Creating directory /home/ivoras/.daisy/blocks
2018/08/16 13:33:38 Creating the genesis block at
/home/ivoras/.daisy/blocks/block_00000000.db
2018/08/16 13:33:38 Generating the genesis keypair...
2018/08/16 13:33:38 Generated.
2018/08/16 13:33:38 Genesis public key: 1:4217553d9fddf70217ab03ee4d5dcfd1c04c109734df3699cf0739fc5c191a65
2018/08/16 13:33:38 Genesis block hash: 6b6d33a30699aa91f5deb467a9cc3fde8716e13a09e420123643b5705e8c7001
2018/08/16 13:33:38 Reloading to verify...
2018/08/16 13:33:38 Loading custom blockchain params from /home/ivoras/.daisy/chainparams.json
2018/08/16 13:33:38 All done.

... and that's it. To start Daisy, just run the daisy executable.

If you wish to specify a different data directory instead of the default one (which is ~/.daisy), you can specify an optional argument to all invocation of daisy: --dir /your/dir .

To run the blockchain on multiple nodes, the safest option is to make an archive of the data directory after the "daisy newchain" step (while Daisy is not running) and unpack it on another node. A useful thing to do at this step is to edit the chainparams.json file which is recorded in the data directory, and edit the bootstrap_peers list to include the address of other nodes. Any new blocks will synchronise when Daisy gets started.

Running the example custom chain

If you don't have a few spare machines to test Daisy, you could download and use my own test data directory.  Unpack this archive in a fresh, new directory and run daisy with the --dir argument to point to this directory. This will connect you to the test blockchain I currently have running (of course, there are no guarantees I will keep it running indefinitely).

The test chain currently has the 2000 US census survey data imported as the first block, and you'll receive this block as a part of the synchronisation process.

To query this data with Daisy's built-in query tool, just issue a command like:

daisy query "select name, count from surnames order by count desc limit 10"

This will attempt to run the query over all databases in the blockchain and return the results as JSON lines:

{"count":2376206,"name":"SMITH"}
{"count":1857160,"name":"JOHNSON"}
{"count":1534042,"name":"WILLIAMS"}
{"count":1380145,"name":"BROWN"}
{"count":1362755,"name":"JONES"}
{"count":1127803,"name":"MILLER"}
{"count":1072335,"name":"DAVIS"}
{"count":858289,"name":"GARCIA"}
{"count":804240,"name":"RODRIGUEZ"}
{"count":783051,"name":"WILSON"}

Adding data blocks

Daisy is created to be a PoA blockchain where new nodes with authority to publish blocks are added by the consensus of existing nodes. In other words, the owners of publishing nodes can accept a new publisher into their circle, and then the new publisher can add blocks to the blockchain as usual.

If you've started Daisy from my test data directory, you already have my private key and can publish new blocks with it by default. Of course, this is test data and it can disappear at any time.

To add a new database to the Daisy blockchain, simply create a new SQLite 3 database file, and use a command such as the following:

daisy signimportblock mydata.db

This will alter the database to include metadata and will import it into the blockchain, to be distributed to other nodes.

The future

Daisy is still experimental code, not a polished product. There are edge cases which are not implemented and will probably misbehave in production use. YMMV.

I'm working on Daisy in my spare time or when I need a particular feature for something. Collaborators, donations and investors are welcome ;)

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.029
BTC 69505.86
ETH 2493.71
USDT 1.00
SBD 2.54