Add your first operation
In this section, we’ll create the create_book
operation. This operation allows us to add new book entries to the
blockchain by updating the dapp’s state.
Define the create_book
operation
In Rell, an operation is a signed public function that modifies the state of the blockchain or, more specifically, updates the dapp state. Operations serve as the primary means to interact with and alter the state of your dapp.
Open the file src/main/operations.rell
and add the following Rell code:
operation create_book(isbn: text, title: text, author: text) {
create book(isbn = isbn, title = title, author = author);
}
Breakdown of the code
operation
keyword: Defines a new operation. This functions like a public function that can be called to perform actions that change the state of your dapp.- Parameters: The
create_book
operation takes three parameters:isbn
,title
, andauthor
. These parameters correspond to the attributes of thebook
entity. create
command: Inside the operation, thecreate
command inserts a new book entry into the dapp’s state using the provided parameters.
Understanding the process
Here’s an overview of what happens when you execute the create_book
operation:
And a detailed view of the transaction process:
- User signs a transaction: The user initiates a transaction to execute the
create_book
operation. - Transaction validation: The Chromia node validates the transaction and forwards it to the blockchain.
- State update: The operation
create_book
updates the dapp’s state by creating a new book entry. - Blockchain confirmation: If the transaction is confirmed, it is added to the blockchain, and the dapp’s state is updated accordingly.
Add unit tests
Testing ensures that your operations work as expected. We’ll add a test for the create_book
operation.
Insert the following test code into src/test/book_review_test.rell
, so the file looks like this:
@test module;
import main.{ book, create_book };
function test_add_book() {
rell.test.tx()
.op(create_book("123", "Book1", "Author1"))
.op(create_book("124", "Book2", "Author2"))
.run();
val all_books = book @* { };
assert_equals(all_books.size(), 2);
assert_equals(all_books[0].title, "Book1");
assert_equals(all_books[0].author, "Author1");
}
Breakdown of the test
- Transaction creation:
rell.test.tx()
creates a test transaction. - Adding operations: Two
create_book
operations are added to the transaction. - Execution:
run()
executes the transaction. - Assertions: The test queries all books and verifies that there are two entries with the correct attributes.
Got it! Here’s the revised version:
Running the test
To run the test, ensure you're in your project folder and enter the following command in your terminal:
chr test
Testing on a local node
To test the create_book
operation on a local Chromia node, follow these steps:
-
Start a local Chromia node: Open your terminal and run the following command:
chr node start
-
Add a book: In a new terminal window or tab, run the following command to add a book:
chr tx --await create_book "ISBN1234" "'1984'" "George Orwell"
The result should be:
transaction with rid TxRid(rid=<RID>) was posted CONFIRMED
This confirms that the transaction was added to the blockchain, and the book
table now contains a new row with the
book data.