How to create your own Cellframe consensus

How to create your own Cellframe consensus

Category: Guide

Title image, read title

Cellframe node is a product, that allows create quantum-safe blockchain network on Linux, Windows and MacOs.

Cellframe node scheme:

Cellframe node scheme

For creating your own consensus, using Cellframe node software, you need small changes of cellframe node code.

Clone latest source codes from gitlab:

git clone https://gitlab.demlabs.net/cellframe/cellframe-node.git — recursive

First, read guide “How to create your own Cellframe network” https://cellframe.net/blog/How-to-create-your-own-Cellframe-consensus/. We will use home21-network configs for examples. We called new consensus DaG-PoC (Proof-of-Concept)

For creating consensus, you need to do:

  1. Copy consensus config files from file cellframe-node\etc\network\home21-network\chain-poa.cfg to cellframe-node\etc\network\home21-network\chain-poc.cfg

2. Copy cellframe-sdk\modules\consensus\dag-poa (DaG PoA consensus code) to cellframe-sdk\modules\consensus\dag-poc

Rename files:

dap_chain_cs_dag_poc.hdap_chain_cs_dag_poc.c

We have structure dap_chain_cs_dag_poc_pvt

typedef struct dap_chain_cs_dag_poc_pvt
{
dap_enc_key_t *events_sign_key;
char ** tokens_hold;
uint64_t * tokens_hold_value;
size_t tokens_hold_size;
uint16_t confirmations_minimum;
} dap_chain_cs_dag_poc_pvt_t;

This structure is filled from config file cellframe-node\etc\network\home21-network\chain-poc.cfg.

Rename strings “dag_poa” to “dag_poc“.

Rename strings “poa” to “poc” in dap_chain_cs_dag_poc.h, dap_chain_cs_dag_poc.c and CMakeLists.txt.

3. Create new module cs-block-poc in cellframe-node (full source code you can see on github):

3.1. Add #include “dap_chain_cs_dag_poc.h” in main.c

3.2. Add cs-dag-poc to cellframe-node\cellframe-sdk\CMakeLists.txt
# PoC consensus for DAG
if (CELLFRAME_MODULES MATCHES “cs-dag-poc”)
message(“[+] Module ‘cs-dag-poc’”)
set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_cs_dag_poc)
endif()

and in CELLFRAME_MODULES variable for your platform, f.e.

if (WIN32)

set(“core chains mining network srv cs-dag-poa cs-block-poa cs-dag-pos cs-dag-poc cs-block-pos cs-none srv-app srv-app-db srv-datum srv-stake srv-xchange”)

3.3. Add cs-dag-poc to cellframe-node\CMakeLists.txt in CELLFRAME_MODULES variable

3.4. Change consensus name to dag_poc in dap_chain_cs_add function in dap_chain_cs_dag_poc_init function

3.5. Add consensus initialization function in main.c

if( dap_chain_cs_dag_poc_init() !=0){
log_it(L_CRITICAL,”Can’t init dap chain dag consensus PoC module”);
return -64;
}

3.6. Add cs-dag-poc to cellframe-node\cellframe-sdk\modules\CMakeLists.txt

# DAG PoC
if (CELLFRAME_MODULES MATCHES “cs-dag-poc”)
add_subdirectory(consensus/dag-poc)
endif()

Cellframe node with consensus code example you can download

git clone https://gitlab.demlabs.net/cellframe/cellframe-node.git — branch consensus-example — recursive

4. Modify consensus functions:

Consensus module contains two parts: functions with node integration and functions for consensus core.

Node integration part:

s_callback_new — registers consensus module using dap_chain_cs_dag_new function

s_callback_delete, s_callback_event_verify, s_callback_event_create, s_callback_created — registers callbacks

s_callback_created — load certificates for event signing

s_callback_delete — delete consensus objects

s_callback_event_create — create event

s_callback_event_verify — consensus verification code. For demo purposes we simply check file existents on hard drive.

Core part:

//
// consensus emulation
//
if (dap_file_test(“D://Distr//concensus.txt”))
{
log_it(L_NOTICE, “Consensus file is found”);
return 0;
}
else
{
log_it(L_WARNING, “Consensus file is not found”);
return -2;
}

If file is presented, we see messages:

If file is not presented, you see message (consensus file is not found)

Manual consensus operations (2 nodes)

More interesting part is consensus negotiation on different nodes. You need 2 nodes for this.We make 2/2 consensus — it means for token emission all events/transactions must be signed on both nodes.We will use same config, but we set

auth_certs_number=2
auth_certs_number_verify=2

in chain-poc.cfg file in [dag-poc] section

When you load node, you see message about 2/2 minimum consensus

We will made token declaration and emission in manual mode for consensus working demonstration.

We setup 2 nodes on 2 computers\virtual machines. You need make some node configuration. Set auto_online=true in cellframe-node.cfg on every node, run nodes and execute command.

cellframe-node-cli net -net home21-network go online

then:

cellframe-node-cli token_decl -net home21-network -chain poc -token TKN_DIL_M -total_supply 5000 -signs_total 1 -signs_emission 1 -certs cert_sig_dil

datum 0x58C8BBEBD12CE7987FB9A15DCE0C185D4AC1F042F599447471DF0B6F4A44AA12 with token TKN_DIL_M is placed in datum pool.

cellframe-node-cli mempool_proc -net home21-network -chain poc -datum 0x58C8BBEBD12CE7987FB9A15DCE0C185D4AC1F042F599447471DF0B6F4A44AA12

hash 0x58C8BBEBD12CE7987FB9A15DCE0C185D4AC1F042F599447471DF0B6F4A44AA12: type_id=DATUM_TOKEN_DECL ts_create=Thu Jan 13 10:14:59 2022
data_size=3411
Datum processed well. Removed datum from mempool.

We see the message, that event was placed in round.

You see that event in DAG:

cellframe-node-cli dag -net home21-network -chain poc event list -from round.new

home21-network.poc: Found 1 records:0xD2FD03289E101C920166F0E6565A16FE5A29EA28F61974E8CC7147D568819947: ts_create=Thu Jan 13 10:15:12 2022

You can see event details:

cellframe-node-cli dag -net home21-network -chain poc event dump -event

0xD2FD03289E101C920166F0E6565A16FE5A29EA28F61974E8CC7147D568819947 -from round.new

Execute command

cellframe-node-cli net sync all -net home21-network

SYNC_ALL state requested to state machine. Current state: NET_STATE_OFFLINE

See messages about nodes syncing.

See event on 2nd node:

cellframe-node-cli dag -net home21-network -chain poc event list -from round.new

home21-network.poc: Found 1 records:

0xD2FD03289E101C920166F0E6565A16FE5A29EA28F61974E8CC7147D568819947: ts_create=Thu Jan 13 10:15:12 2022

It has same hash as on 1st node.

Sign this event:

cellframe-node-cli dag_poc -net home21-network -chain poc event sign -event 0xD2FD03289E101C920166F0E6565A16FE5A29EA28F61974E8CC7147D568819947

Added new sign with cert “cert_event01”, event 0x77F9F5B6C790418F339D4CA6FAC3BC69AA4A9B10B4EEF3026C76C40B92EC5FB9 placed back in round.new

Event changes its own hash.

Signs count 2.

Make synchronization with 1st node (cellframe-node-cli net sync all -net home21-network) and see event. It changes this hash also:

cellframe-node-cli dag -net home21-network -chain poc event list -from round.new

home21-network.poc: Found 1 records:

0x77F9F5B6C790418F339D4CA6FAC3BC69AA4A9B10B4EEF3026C76C40B92EC5FB9: ts_create=Thu Jan 13 10:15:12 2022

You see 2 signs on this event.

Now you can compete round

cellframe-node-cli dag -net home21-network -chain poc round complete

Completing round:

Event 0x77F9F5B6C790418F339D4CA6FAC3BC69AA4A9B10B4EEF3026C76C40B92EC5FB9 verification passed

Event 0x77F9F5B6C790418F339D4CA6FAC3BC69AA4A9B10B4EEF3026C76C40B92EC5FB9 added in chain successfully

You can see message, that token ticker was added to chain.

Set this event in static_genesis_event parameter of chain-poc.cfg, set seed_mode=false in cellframe-node.cfg and restart nodes. Make sure, that 2nd is syncing with 1st node.

Next, we made emission. Execute command:

cellframe-node-cli token_emit -net home21-network -chain_emission poc -chain_base_tx poc -token TKN_DIL_M -certs cert_sig_dil -emission_value 15000000000000 -addr rMcg8v2uM2b71q9sVJbmwvqZ6UHwwJTSNyW17LZEQPjt1uX6wcB2cnXXkrn9AWxBRBc1UxKBQTXhtZSm4sXhzUurp6ka69aSvArKEqrw

Datum emission 0x7EDBA32ABB53348FD967E83743FC02C3A29D3846C2ADAAD5216D37A1ED6E0D0D is placed in datum pool

datum tx 0x6D8CD78A17B70262E0F6E68AB72613C0ACA8C8DB1AE4999ED67DE2DB83E646F0 is placed in datum pool

We have 2 datums, that we will process one by one. First we processed emission datum

cellframe-node-cli mempool_proc -net home21-network -chain poc -datum 0x7EDBA32ABB53348FD967E83743FC02C3A29D3846C2ADAAD5216D37A1ED6E0D0D

hash 0x7EDBA32ABB53348FD967E83743FC02C3A29D3846C2ADAAD5216D37A1ED6E0D0D: type_id=DATUM_TOKEN_EMISSION ts_create=Thu Jan 13 11:11:38 2022

data_size=3646

Datum processed well. Removed datum from mempool.

Event placed in round. We sign it as previous event, generating during token declaration.

2nd node:

cellframe-node-cli dag -net home21-network -chain poc event list -from round.new

home21-network.poc: Found 1 records :

0x4DF8700CEFAD6EB542877537FCE7AB161C8036E81443FDD00120C5FB7F0FF994: ts_create=Thu Jan 13 11:22:31 2022

cellframe-node-cli dag -net home21-network -chain poc event dump -event 0x4DF8700CEFAD6EB542877537FCE7AB161C8036E81443FDD00120C5FB7F0FF994 -from round.new

Event 0x4DF8700CEFAD6EB542877537FCE7AB161C8036E81443FDD00120C5FB7F0FF994:Round cfg:confirmations_minimum:2confirmations_timeout:300

Header:version: 0x00cell_id: 0x0000000000000000chain_id: 0x0000000000000001ts_created: Thu Jan 13 11:22:31 2022hashes: count: 1hash: 0x0000000000000000000000000000000000000000000000000000000000000000

datum: datum_size: 3661version:=0x00type_id:=DATUM_TOKEN_EMISSIONts_create=Thu Jan 13 11:11:38 2022data_size=3646

signs: count: 2type: sig_dil addr: rMcg8v2uM2b71q9sUcybUPJSmQLzcwaR51KFk7N73Ss76iN5VuiiT8KsjmfB3T6BCqeYsvrNkGSqLCxqp5TULbeddVmyUXVS78tgpviRtype: sig_dil addr: rMcg8v2uM2b71q9sWBNVTmBgiMjXLPVXdSttoPiGPdMTcd4J7Kr1SQv95rhSyKYgWEapVcdCj9xAaRAn1cMK4HLShBXBPkZ7WBEBPxDV

1st node:

cellframe-node-cli dag -net home21-network -chain poc round complete

Completing round:

Event 0x4DF8700CEFAD6EB542877537FCE7AB161C8036E81443FDD00120C5FB7F0FF994 verification passedEvent 0x4DF8700CEFAD6EB542877537FCE7AB161C8036E81443FDD00120C5FB7F0FF994 added in chain successfully

Next we doing same things for tx datum:

1st node:

cellframe-node-cli mempool_proc -net home21-network -chain poc -datum 0x5F1BFBC74325D217CE2DAB43D25EE774F02A2DFE56B2844934344B0ABAC9F994

hash 0x5F1BFBC74325D217CE2DAB43D25EE774F02A2DFE56B2844934344B0ABAC9F994: type_id=DATUM_TX ts_create=Thu Jan 13 12:22:40 2022data_size=3664
Datum processed well. Removed datum from mempool.

2nd node

cellframe-node-cli dag_poc -net home21-network -chain poc event sign -event 0x8C44846A12F34C65A1125D2D5C6870DFC46FD71AC2EB001B3593E4AA72717A08

Added new sign with cert “cert_event01”, event 0xB3E14980EB8282A78C3786F801C297D97D5CCD7F5CF555F9CF7B339DA2F56FE1 placed back in round.new

cellframe-node-cli dag -net home21-network -chain poc event dump -event 0xB3E14980EB8282A78C3786F801C297D97D5CCD7F5CF555F9CF7B339DA2F56FE1 -from round.new

Event 0xB3E14980EB8282A78C3786F801C297D97D5CCD7F5CF555F9CF7B339DA2F56FE1:

Round cfg:confirmations_minimum:2confirmations_timeout:300

Header:version: 0x00cell_id: 0x0000000000000000chain_id: 0x0000000000000001ts_created: Thu Jan 13 12:32:52 2022hashes: count: 1hash: 0xEE0C5C2FD0A4BC1C3F4C3F6E7214B72D8DF2FD773915409D8FAD6EB5E8BA8E9C

datum: datum_size: 3679version:=0x00type_id:=DATUM_TXts_create=Thu Jan 13 12:22:40 2022data_size=3664

signs: count: 2type: sig_dil addr: rMcg8v2uM2b71q9sUcybUPJSmQLzcwaR51KFk7N73Ss76iN5VuiiT8KsjmfB3T6BCqeYsvrNkGSqLCxqp5TULbeddVmyUXVS78tgpviRtype: sig_dil addr: rMcg8v2uM2b71q9sWBNVTmBgiMjXLPVXdSttoPiGPdMTcd4J7Kr1SQv95rhSyKYgWEapVcdCj9xAaRAn1cMK4HLShBXBPkZ7WBEBPxDV

1st node:

cellframe-node-cli dag -net home21-network -chain poc round complete

Completing round:

Event 0xB3E14980EB8282A78C3786F801C297D97D5CCD7F5CF555F9CF7B339DA2F56FE1 verification passed

Event 0xB3E14980EB8282A78C3786F801C297D97D5CCD7F5CF555F9CF7B339DA2F56FE1 added in chain successfully

Finally, we got token emission and can syncing it with other node:

Set is_add_directly=false and restart node.

Consensus with autoround complete (3 nodes)

Set in dag_poc consensus config:

[dag_poc]
auth_certs_number=3
auth_certs_number_verify=3
confirmations_minimum=1
confirmations_timeout=60
auto_confirmation=true
auto_round_complete=true
wait_sync_before_complete=40

additionally

[dag]
is_add_directly=false
auth_certs_number=3
auth_certs_number_verify=3

This option allows to make event matching roundsm before events will be placed into DAG automatically. You can use mempool autoproc only for operation, which uses only one event (token declaration).

confirmations_timeout — sign waiting timeout. Its period of waiting signing events on all nodes.

auto_confirmation — enable auto signing. Function s_callback_event_round_sync is executed without manual action

auto_round_complete — allows execute s_round_event_cs_done in s_callback_event_round_sync of consensus.

wait_sync_before_complete — waiting for rounds syncing

After that you can make emission with your consensus confirmation on different nodes.

Run on one node:

cellframe-node-cli token_decl -net home21-network -chain poc -token TKN_DIL_M -total_supply 5000 -signs_total 1 -signs_emission 1 -certs cert_sig_dil

datum 0xB6CD352BABF82D82534155E7E338B5564C2562F27407B594C24130A4380DA691 with token TKN_DIL_M is placed in datum pool

cellframe-node-cli mempool_proc -net home21-network -chain poc -datum 0xB6CD352BABF82D82534155E7E338B5564C2562F27407B594C24130A4380DA691

hash 0xB6CD352BABF82D82534155E7E338B5564C2562F27407B594C24130A4380DA691: type_id=DATUM_TOKEN_DECL ts_create=Tue Jan 25 10:31:36 2022

data_size=3411

see event in round.new

cellframe-node-cli dag -net home21-network -chain poc event list -from round.new

home21-network.poc: Found 1 records:

0x26198C159802A08D87320B39CE653CEA7360F70B3B6C98F3A2B601A528FCF252: ts_create=Tue Jan 25 10:31:53 2022

Waiting for that event was moved to events partition of dag, after consensus will be confirmate on all nodes

cellframe-node-cli dag -net home21-network -chain poc event list -from events

home21-network.poc: Have 1 events:

0x6C66446FA86C745A5C8613AB1C50DF74BB2161B8A4B7AFD1043B3931F745DBE0: ts_create=Tue Jan 25 10:31:53 2022

After that set seed_mode to false in cellframe-node.cfg, add 0x6C66446FA86C745A5C8613AB1C50DF74BB2161B8A4B7AFD1043B3931F745DBE0 to static_genesis_event parameter of dag_poc.cfg, and restart all nodes.

After that execute command for token emission

cellframe-node-cli token_emit -net home21-network -chain_emission poc -chain_base_tx poc -token TKN_DIL_M -certs cert_sig_dil -emission_value 15000000000000 -addr rMcg8v2uM2b71q9sVJbmwvqZ6UHwwJTSNyW17LZEQPjt1uX6wcB2cnXXkrn9AWxBRBc1UxKBQTXhtZSm4sXhzUurp6ka69aSvArKEqrw

Datum emission 0xBA773C40646B3C6A0036262DC490959113F5CAC9695C44ED0389DF31A9572A10 is placed in datum pool

datum tx 0xA788F87BAA6FFDCD8097F8A826A9B03C0C7DF9394BA0477A9A0DA09112728597 is placed in datum pool

Process datum one-by-one and waiting for consensus confirmation is completed

cellframe-node-cli mempool_proc -net home21-network -chain poc -datum 0xBA773C40646B3C6A0036262DC490959113F5CAC9695C44ED0389DF31A9572A10

hash 0xBA773C40646B3C6A0036262DC490959113F5CAC9695C44ED0389DF31A9572A10: type_id=DATUM_TOKEN_EMISSION ts_create=Tue Jan 25 10:43:57 2022data_size=3646

Datum processed well. Removed datum from mempool.

You can see the intermediate result using the command:

cellframe-node-cli dag -net home21-network -chain poc event dump -event 0xA56E3B8842D5025762F3A1B9D75983C24450F1C2D139A6195BC4A4AF56823600 -from round.new

Second event will be added

cellframe-node-cli dag -net home21-network -chain poc event list -from events

home21-network.poc: Have 2 events :

0x6C66446FA86C745A5C8613AB1C50DF74BB2161B8A4B7AFD1043B3931F745DBE0: ts_create=Tue Jan 25 10:31:53 2022

0xD5EF9215DE43415CA67F4494072333A32EE352ED927ECB3D6660F0713FDCFF34: ts_create=Tue Jan 25 10:56:40 2022

cellframe-node-cli mempool_proc -net home21-network -chain poc -datum 0x156AB4C86C5C01DBF52F3ED36FFB04B7EDC6C3ED5867FCFC4644F22046423B15

hash 0x156AB4C86C5C01DBF52F3ED36FFB04B7EDC6C3ED5867FCFC4644F22046423B15: type_id=DATUM_TX ts_create=Tue Jan 25 10:56:28 2022

data_size=3664

Datum processed well. Removed datum from mempool.

You can see events in

cellframe-node-cli dag -net home21-network -chain poc event list -from events

home21-network.poc: Have 3 events:

0x6C66446FA86C745A5C8613AB1C50DF74BB2161B8A4B7AFD1043B3931F745DBE0: ts_create=Tue Jan 25 10:31:53 20220xD5EF9215DE43415CA67F4494072333A32EE352ED927ECB3D6660F0713FDCFF34: ts_create=Tue Jan 25 10:56:40 20220x10E2BE804FEE0A20D4FA7F2E32DC184FFDDAA18D636E7320CF7480FA7E1D208B: ts_create=Tue Jan 25 11:30:12 2022

Finally, after 2nd event from token emission operation (transaction event) was confirmed, we get token emission: