20 / 26Part 5: Blockchain state and transactions explainedBeginner
Walking through an example
With the setup complete, you can now submit a few transactions and query both the state and the transaction history to see how the two relate.
Start from a clean node. If the Chromia Node is running, stop it with Ctrl+C first, then wipe its data with this command:
chr node start --wipeThe command deletes every transaction on the blockchain and resets the current state. To confirm, run:
chr query get_all_booksYou should get an empty array, which means the current state contains no books.
Now submit a transaction that creates a book. Misspell "Chromia" on purpose so you have something to fix later with an update:
chr tx create_book "ISBN001" "Crhomia 101" "Alice" --key-id "book_admin" --awaitExpected output:
transaction with rid DF94A07EACBA673CC4ECA4A43D70CDA4FD30E4335E124B7153FB12A78FDD8CE9 was posted CONFIRMEDOnce the transaction is in, list every book with the get_all_books command:
chr query get_all_booksYou should see:
[ { "author": "Alice", "isbn": "ISBN001", "title": "Crhomia 101" }]The new book now appears in the state/table. Next, query the transactions stored on the blockchain:
chr query get_transactionsThe output is:
[ [ "body": [ "blockchain_rid": x"B52F0A078993374201853649F807FDEDD6516A424557F311E2AF769761F89D40", "operations": [ [ "args": [ "ISBN001", "Crhomia 101", "Alice" ], "name": "create_book" ] ], "signers": [ x"035A25AB32B85E6A5B58A31A71CC709CED159096194984243159492CBA0FADF82F" ] ], "signatures": [ x"91EFC02C2958698E71C41D2B020F9E4EF81F6C427FAA98667D6FF4B87857809E1ECE4C9C923B1E5659177B8D0635711E47A4EBB502217A437D3FFC89741E74DD" ] ]]This shows the transaction was recorded on the blockchain and the state changed to match.
To fix the misspelled title, you need an operation that updates a book's title. First, make the title field mutable in the book entity:
entity book { key isbn: text; mutable title: text; author: text;}Then 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 );}The running node does not yet know about this operation. Update the Chromia Node with the following command and wait for the next block before testing:
chr node updateWhen the update is done, send a transaction that changes the book's title:
chr tx update_book "ISBN001" "Chromia 101" --key-id "book_admin" --awaitQuery the state and the blockchain transactions once more:
chr query get_all_booksThe state now returns the corrected title:
[ { "author": "Alice", "isbn": "ISBN001", "title": "Chromia 101" }]Query the blockchain transactions the same way:
chr query get_transactionsThe output is:
[ [ "body": [ "blockchain_rid": x"B52F0A078993374201853649F807FDEDD6516A424557F311E2AF769761F89D40", "operations": [ [ "args": [ "ISBN001", "Crhomia 101", "Alice" ], "name": "create_book" ] ], "signers": [ x"035A25AB32B85E6A5B58A31A71CC709CED159096194984243159492CBA0FADF82F" ] ], "signatures": [ x"91EFC02C2958698E71C41D2B020F9E4EF81F6C427FAA98667D6FF4B87857809E1ECE4C9C923B1E5659177B8D0635711E47A4EBB502217A437D3FFC89741E74DD" ] ], [ "body": [ "blockchain_rid": x"B52F0A078993374201853649F807FDEDD6516A424557F311E2AF769761F89D40", "operations": [ [ "args": [ "ISBN001", "Chromia 101" ], "name": "update_book" ] ], "signers": [ x"035A25AB32B85E6A5B58A31A71CC709CED159096194984243159492CBA0FADF82F" ] ], "signatures": [ x"FD9402B45ED52A238B0C756AFA1ABA5BD2F89C9AE44343E58FE76E207F4672CF0AF5C37F7209E5D9A474491671000FAFA9753A4BF2B5D16AE7C5C48CFA18A8E1" ] ]]The blockchain now holds two transactions: the one that created the book and the one that changed its title.
The state is the combined result of both transactions. Because every transaction affecting this book is recorded, you can trace back through the history and see exactly how the current state came to be.
To build and maintain decentralized applications well, you need a clear picture of how Rell, Chromia, the blockchain, and the state work together.