Lab: Convert the Double Counter to an Aggregatable Double Counter

Complete the exercise in entry.rs in the folder Lab-AggDoubleCounter-Exercise/contracts/aggdoublecounter/src by adding code and values as necessary.

The DoubleCounter type script ensures that the two u64 values contained in the data area of the cell are incremented each time the cell is transferred or updated. The first value must be incremented by exactly one, and the second value must be incremented by exactly 2.

The AggDoubleCounter type script is the same as the DoubleCounter type script, except that it allows multiple AggDoubleCounter cells to be aggregated together and processed in a single transaction.

The entry.rs file contains the source code for the DoubleCounter type script. Your task is to modify the functionality of this script to match that of AggDoubleCounter.

  1. The AggDoubleCounter takes no script args.

  2. Both counter values stored in the cell's data area should be u64 LE values (8 bytes each).

  3. You will not need to modify error.rs. This already contains the errors required for the AggDoubleCounter type script.

Build your code by opening a terminal to the Lab-AggDoubleCounter-Exercise folder and running capsule build, then test your code using capsule test after the build is successful. If you get stuck you can find the solution in the Lab-AggDoubleCounter-Solution folder.

Note: Be sure to always build your code after modifying it before it is tested again so that changes are properly reflected.

Once your code successfully compiles and all tests are passed, you have completed this lab. The test output will contain the following to indicate a successful test.

running 16 tests
test tests::test_aggdoublecounter_burn ... ok
test tests::test_aggdoublecounter_burn_multiple ... ok
test tests::test_aggdoublecounter_create ... ok
test tests::test_aggdoublecounter_create_invalid_output_data_value ... ok
test tests::test_aggdoublecounter_create_no_output_data ... ok
test tests::test_aggdoublecounter_create_multiple ... ok
test tests::test_aggdoublecounter_transfer ... ok
test tests::test_aggdoublecounter_transfer_high_value ... ok
[contract debug] panic occurred: range end index 16 out of range for slice of length 8, in file contracts/aggdoublecounter/src/entry.rs:42
test tests::test_aggdoublecounter_transfer_invalid_input_data_panic_expected ... ok
[contract debug] panic occurred: range end index 16 out of range for slice of length 8, in file contracts/aggdoublecounter/src/entry.rs:49
test tests::test_aggdoublecounter_transfer_invalid_output_data_panic_expected ... ok
test tests::test_aggdoublecounter_transfer_multiple_mismatch_cell_count_too_few_outputs ... ok
test tests::test_aggdoublecounter_transfer_multiple ... ok
[contract debug] panic occurred: attempt to add with overflow, in file contracts/aggdoublecounter/src/entry.rs:53
test tests::test_aggdoublecounter_transfer_overflow_panic_expected ... ok
test tests::test_aggdoublecounter_transfer_multiple_mismatch_cell_count_too_many_outputs ... ok
test tests::test_aggdoublecounter_transfer_value_1_plus_2 ... ok
test tests::test_aggdoublecounter_transfer_value_2_plus_9000 ... ok

test result: ok. 16 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Note: Our tests expect certain output values and also test error cases. If your script contains different logic for error handling, it may be reported as a failed test. If you have included better error handling than was expected by our tests, disregard the error. Good job!

Last updated