Chromia
Start Building

19 / 26Part 5: Blockchain state and transactions explainedBeginner

How blockchain state and transactions work

This lesson looks at how Chromia stores transactions and manages state. Start with the diagram from Lesson 1, which traced a create_book transaction through the system:

  • You sign the transaction create_book("ISBN1234", "1984", "George Orwell").
  • The Chromia Node receives the transaction, validates it, and passes it on to the blockchain.
  • The operation runs create_book(isbn = isbn, title = title, author = author);, and Rell applies the change to the DApp table state.
  • Once confirmed, the transaction goes into a block on the blockchain, and the DApp table state reflects the update.

The diagram shows two things: the transaction is recorded on the blockchain, and the operation inside it changes the state. The rest of this lesson examines that split so you can see how the blockchain and the state differ.

First, you fetch the state with the get_all_books query.

Querying state for all books

A query such as get_all_books reads only from the DApp table state. You want the current set of books, so the state is the right place to look. The query follows this path:

Step by step:

  • You send a request for all books to the Chromia Node.
  • The node hands the query to Rell.
  • Rell runs a query against the state to retrieve every book.
  • The state executes it and sends the result back to Rell.
  • Rell returns the full list of books from the state to you.

Next, you query the blockchain itself for transactions.

Querying the blockchain for transactions

Here you query the blockchain to retrieve every transaction that produced the current DApp table state.

The blockchain keeps each transaction in the system transaction entity. One of its fields, tx_data, holds the complete transaction as GTV-encoded binary: operations, arguments, signers, and signatures.

Rell's built-in gtx_transaction type lets you decode that binary and work with its contents.

Adding a Query

Put this query in src/main/queries.rell:

src/main/queries.rell
query get_transactions() {  return transaction @* { } ( gtx_transaction.from_bytes(.tx_data) );}

The query returns a list of structured transactions. Each one contains:

  • The RID of the blockchain that holds the transaction
  • Every operation along with its arguments
  • The transaction's signers
  • The matching signatures

For a full description of the decoded transaction format, see the system entities reference.

That completes the transaction query definitions. The next lesson shows you how to run these queries in practice.

Written for this site from Chromia’s public course. Code samples are the course’s own.