Let's look at an example
Now that we're all set up let's add a couple of transactions and then query both the state and transactions to see how they interact.
First, let's clear all data from our node to start with a fresh slate. Before running the command, if the Chromia Node
is running, stop it with Ctrl+C
. Then, you can clear the data by running the following command:
chr node start --wipe
This command will clear all transactions from the blockchain and reset the current table/state. You can verify this by running:
chr query get_all_books
This should return an empty array, indicating no books in the current state.
Now, let's add a new transaction to create a book, intentionally misspelling "Chromia" to later correct this with an update:
chr tx create_book --secret .chromia/config "ISBN001" "Crhomia 101" "Alice" --await
The output should look like this:
transaction with rid DF94A07EACBA673CC4ECA4A43D70CDA4FD30E4335E124B7153FB12A78FDD8CE9 was posted CONFIRMED
After running this transaction, you can query for all books using the get_all_books
query:
chr query get_all_books
The result should look like this:
[
{
"author": "Alice",
"isbn": "ISBN001",
"title": "Crhomia 101"
}
]
This confirms that the state/table has been correctly updated with the new book information. Now, let's query the blockchain transactions:
chr query get_transactions
which will give us the following result:
[
{
"signatures": [x"DB12DCDC62F8FB231C3CC7545AB5738C02E0CE29D912D07AAB865565B94324311D24BC1855FDB82446DCE92B9BC9F457DB2B471EDD71EBB2BE3A4A611C3E77EC"],
"tx_body": {
"ops": [
{
"op_args": ["ISBN001", "Crhomia 101", "Alice"],
"op_name": "create_book"
}
],
"signers": [x"02465E21B4297E2F665E39E44F5575DFDF03F542CE9252E22972CFF957E7ACFC1C"],
"tx_rid": x"B9BC06E8B8AAA65BDC4EE0A42EC01BAF92937E1FA8AF43C81031F3FCDFA8DF4B"
}
}
]
This indicates that the transaction has been successfully added to the blockchain, and the state has been updated accordingly.
This diagram illustrates what happens when we run query get_transactions
:
Let's continue with the next step by adding a new operation to update a book's title. This will allow us to correct the misspelt title for our book. We first need to make the title mutable so that it can be updated. This is simple; we just change the attribute of the title to mutable in our book entity:
entity book {
key isbn: text;
mutable title: text;
author: text;
}
Then we can add the update operation:
operation update_book(isbn: text, title: text){
val adminPubkey = chain_context.args.admin_pubkey;
require(op_context.is_signer(adminPubkey), "Only admin can update books");
update book @ { .isbn == isbn }( .title = title );
}
Before testing this new operation, update the Chromia Node to make sure it includes the new operation. Run the following command and wait for the next block to ensure the operation is available:
chr node update
After updating, let's test the new operation with a transaction where we update a book's title:
chr tx update_book --secret .chromia/config "ISBN001" "Chromia 101" --await
After this transaction, we can query both the blockchain transactions and the state again:
chr query get_all_books
The result will now show the updated title:
[
{
"author": "Alice",
"isbn": "ISBN001",
"title": "Chromia 101"
}
]
Similarly, when querying the blockchain transactions:
chr query get_transactions
The result will be as follows:
[
{
"signatures": [x"DB12DCDC62F8FB231C3CC7545AB5738C02E0CE29D912D07AAB865565B94324311D24BC1855FDB82446DCE92B9BC9F457DB2B471EDD71EBB2BE3A4A611C3E77EC"],
"tx_body": {
"ops": [
{
"op_args": ["ISBN001", "Crhomia 101", "Alice"],
"op_name": "create_book"
}
],
"signers": [x"02465E21B4297E2F665E39E44F5575DFDF03F542CE9252E22972CFF957E7ACFC1C"],
"tx_rid": x"B9BC06E8B8AAA65BDC4EE0A42EC01BAF92937E1FA8AF43C81031F3FCDFA8DF4B"
}
},
{
"signatures": [x"16EE4BA637E5D49FAAF1E3B6A78FA8AF63793AC92784914FC1890A54FC3365294F3FD5EC73AB9159EA61F9F8F1AE2650E0326C3B7A94739CDC0362212B731909"],
"tx_body": {
"ops": [
{
"op_args": ["ISBN001", "Chromia 101"],
"op_name": "update_book"
}
],
"signers": [x"02465E21B4297E2F665E39E44F5575DFDF03F542CE9252E22972CFF957E7ACFC1C"],
"tx_rid": x"B9BC06E8B8AAA65BDC4EE0A42EC01BAF92937E1FA8AF43C81031F3FCDFA8DF4B"
}
}
]
You'll see that there are two transactions on the blockchain — one to create the book and another to update its title.
If we look at the state, it now represents the two transactions aggregated. So, this state is all the transactions for this book combined, meaning that we can always go back in history and track exactly how the current state was built.
Understanding this interaction between Rell, Chromia, the blockchain, and the state is crucial for effectively building and managing decentralized applications.