DECA's Carbon Credits OrbitDB Code and configs, everything you need for setting up a node.
Go to file
David E. Perez Negron Rocha ea45180523 Adding official DECA OrbitDB Database 2020-06-12 16:30:25 -05:00
images initial commit 2020-05-30 13:03:35 -05:00
services initial commit 2020-05-30 13:03:35 -05:00
.envSample Feature finished 2020-06-11 17:47:59 -05:00
.gitignore Fix for a diferent directories for Proyect Receipts, so that Receipts is for DECAs Distributed Database 2020-06-12 00:26:29 -05:00
LICENSE initial commit 2020-05-30 13:03:35 -05:00
README.md initial commit 2020-05-30 13:03:35 -05:00
connectNodes.sh initial commit 2020-05-30 13:03:35 -05:00
divisaTraceback.js Feature finished 2020-06-11 17:47:59 -05:00
index.js Adding official DECA OrbitDB Database 2020-06-12 16:30:25 -05:00
interfaz.js Adding official DECA OrbitDB Database 2020-06-12 16:30:25 -05:00
package.json initial commit 2020-05-30 13:03:35 -05:00
receipts.sh Fix for a diferent directories for Proyect Receipts, so that Receipts is for DECAs Distributed Database 2020-06-12 00:26:29 -05:00

README.md

DECASCarbonCreditsBacklog

DECA's Carbon Credits OrbitDB Code and configs, everything you need for setting up a node.

IMPORTANT: Please verify that the database address matches the smart contract address

OrbitDB4CC

Carbon Credit Structure

{
/*
  CC - Carbon credits
  Unix Time is date and time based in the UTC 0
  example for the first carbon credit that is 1365209
*/
  SerialNo: 'GS1-1-MX-GS2441-16-2018-17438-328-328',
  CCAddress: 'https://registry.goldstandard.org/credit-blocks/details/107995', //Address GS Registry
  issueDate: 1552953600, // CC creation UnixTime
  cancelDate: 1587214800, // CC cancelation cancelation UnixTime
  ccVintageStart: 1514764800, // CC what year it was created in UnixTime
  ccVintageEnd: 1514764800, // CC what year it was created in UnixTime
  ccCategory: 'Energy Efficiency - Domestic', // CC category agricultural
  ccMeasurement: 'VER(TCO2e)', //CC Measurement
  ccStandard: 'GS', // CC standard Woodland Carbon Credit  
  countryCode: 'MX' // Country code
  ccProjectID: 'GS2441', // CC project ID in the original backlog is it unique?
  ccID: 17438-328-328,
  CCAdquisitionRecipt: 'QmWWQSuPMS6aXCbZKpEjPHPUZN2NjB3YrhJTHsV4X3vb2t', //Recipt of CC in IPFS
  cancelPrice: {
    qty: 19.50, 
    divisa : 'USD'},
  conversionPrice:{
    USD: 19.50,
    EUR: 17.93,
    CNY: 137.93,
    BTC: 0.0027,
    LTC: 0.45,
    ETH: 0.11,
  },
  cancelRemaks: 'Cancel by account 1067262',
} 

Requirements

  • Node.js >= 12
  • IPFS Daemon

Instalation

Download and install Node.js v12.x and npm.

  • Node.js

Using Ubuntu

  $ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  $ sudo apt-get install -y nodejs

Using Debian, as root

 $ curl -sL https://deb.nodesource.com/setup_12.x | bash - 
 $ apt-get install -y nodejs
  • IPFS Daemon

Go to and download for your platform https://dist.ipfs.io/#go-ipfs

 $ tar xvfz go-ipfs.tar.gz
 $ cd go-ipfs
 $ ./install.sh

Test

 $ ipfs help

Init your node

 $ ipfs init

Configure ipfs as a service, set your self in the repository directory and do as follows:

 $ sudo cp services/ipfs.service /etc/systemd/system/

Modify user and path to ipfs so that it matches with your system and user that runs ipfs:

 $ sudo vim /etc/systemd/system/ipfs.service

[Unit]
Description=IPFS Daemon

[Service]
ExecStart=/usr/local/bin/ipfs daemon --enable-pubsub-experiment
User=nodemaster
Restart=always
LimitNOFILE=10240

[Install]
WantedBy=multi-user.target

NOTE: in this example user that runs ipfs and orbitdb instance is nodemaster, also the ipfs location is at /usr/local/bin/ipfs

Enable the service

 $ sudo systemctl daemon-reload
 $ sudo systemctl enable ipfs.service
 $ sudo systemctl start publicNode.service
 $ sudo systemctl status ipfs.service

NOTE: service must be set as active (running), if not please verify the preview steps

OrbitDB

OrbitDB is a serverless, distributed, peer-to-peer database. OrbitDB uses IPFS as its data storage and IPFS Pubsub to automatically sync databases with peers. It's an eventually consistent database that uses CRDTs for conflict-free database merges making OrbitDB an excellent choice for decentralized apps (dApps), blockchain applications and offline-first web applications.

Usage local

Clone the repo

 $ git clone https://gitlab.com/deca-currency/decascarboncreditsbacklog.git
 $ cd orbitdb4cc

Install the dependencies:

 $ npm install 

Start IPFS Daemon (Note: pubsub-experiment is necesary to work with OrbitDB)

 $ ipfs daemon --enable-pubsub-experiment

Start the DecaCC Interface

 $ node interfaz.js

Note: With this you can use OrbitDB but only in your local node

Usage with replication

You need to do all the past steps.

Run index.js with uncomment line 24, 25, the first log is your orbitdb address (See the Wiki for more information), the second is your orbitDB address database copy this address and pass in index.js in line 6, and in interfaz.js in the same line.

If you want to debug orbitdb in bash

 $ export LOG="orbit*" node

Start the DecaCC replicated

 $ node index.js

Later in the nodes that will work like peers only, install the requeriments and run index.js with the new address.

Start the DecaCC Interface

 $ node interfaz.js

In the node that can write run interfaz.js and choose an option.

 $ Show Carbon Credit records
 $ Insert Carbon credit      
 $ Delete Carbon credit      
 $ Exit 

Note: If the nodes will replicated you need connect before with IPFS Daemon (See Wiki).

Note 2: Remember dont lost your orbitdb folder because your identity.id change and cant write in a database another time

Connect Nodes

In order to connect nodes, we create a bash script which resolves the domain name of each node and ipfs swarms them, this script should be configured in a crontab to run every 5 minutes. The crontab should be configured as the user that holds this repository and that runs the orbitdb.

Open the crontab as the user that holds the script

 $ crontab -e

Append the following line at the end of the editor and write it

0,5,10,15,20,25,30,35,40,45,50,55 * * * * sleep 28 ; /home/nodemaster/orbitdb4cc/connectNodes.sh >> /tmp/NodeStatus.log  2>&1 &

NOTE: in this example the path is nodemaster user directory, you should change it with your username and the path to the script connectNodes.sh

Finally verify the logs at /tmp/NodeStatus.log to verify that it is working, this should look similar to this:

$ cat /tmp/NodeStatus.log

Error: connect Qmd4Cv2fNwixP6cabEnTVFkF57GUGD6VBEcDhUkqHPG4X9 failure: dial to self attempted
connect QmdcWqBmvAvdNY2gq14LTP6rAgSQAe1cBvnpwqWrJy8S2Y success
connect QmfBASmqe3Az9AUjCxx3dtomSmbZEiJCsXusPxznjNnjU5 success
connect QmPbDkCpSE9uQuiXTBnoWjnzRESpSe9p8P1de12nExA4fY success
connect QmQBAsbA49q7QrKhetJpbo5gKxQQiL6sxVXCep5skmuHsq success
connect QmZL1otpiCzWMEJTHXbQ5Hb4aFE7TKLjAuuBAAet1WAgtD success

NOTE: it is possible that you won't connect at the first try with most of the nodes, so you can wait for 10 or 15 minutes to verify again

For Public NODES updater as service

This is in case you want to have a node that is just for public access and constantly replicating the last data in orbit DB

Configure public nodes updater service, set your self in the repository directory and do as follows:

 $ sudo cp services/publicNode.service /etc/systemd/system/

Modify user and path to node and the repository so that it matches with your system and user that runs ipfs and node:

 $ sudo vim /etc/systemd/system/publicNode.service 
 
[Unit]
Description=Public Node Updater

[Service]
WorkingDirectory=/home/nodemaster/orbitdb4cc/
ExecStart=/usr/local/bin/node /home/nodemaster/orbitdb4cc/index.js
User=nodemaster
Restart=always
LimitNOFILE=10240

[Install]
WantedBy=multi-user.target

NOTE: in this example user that runs ipfs, node and orbitdb instance is nodemaster, also the node location is at /usr/local/bin/node

NOTE1: set WorkingDirectory where is the repository, for this example /home/nodemaster/orbitdb4cc/

NOTE2: set where is index.js mostly in the repository as follow path to node path, for this example /home/nodemaster/orbitdb4cc/index.js

Enable the service

 $ sudo systemctl daemon-reload
 $ sudo systemctl enable publicNode.service 
 $ sudo systemctl start publicNode.service 
 $ sudo systemctl status publicNode.service 

NOTE: service must be set as active (running), if not please verify the preview steps