Parallel Counter
A Simple Concurrent Example
Counters are a common concept in programming. A counter is essentially a variable that is used to keep track of a numerical value, and it is often employed to count occurrences of events, iterations, or any other incrementing or decrementing process.
Concurrent Counter
The contract uses the
U256Cumulative
contract from Arcology's concurrent library to manage the cumulative count.The constructor initializes the
visitCounter
variable with an initial count of 0 and a maximum value of 1,000,000.The
visit
function is a public function that can be called by multiple transactions in parallel. When called, it increments thevisitCounter
by 1 using theadd
function provided by theU256Cumulative
contract.The
getCounter
function allows anyone to retrieve the current value of the visit counter. It calls theget
function provided by theU256Cumulative
contract and returns the count.
Create an Empty Truffle Project
Modify the truffle-config.js
file is a configuration file used to point Truffle to your Arcology devnet. It resides in the root directory of your Truffle project.
Download Concurrentlib
In the 'contract' directory, create a new contract named visitCounter.sol
with the following content.
Compile the Contract
Deploy the Contract
Before deploying the contract, you need to write a deployment script. Create a file named '1_deploy_contracts.js' in the 'migrations' directory with the following content:
Run the Deployment Script
Write Tests
Create a test script file named visit.js
in the test
directory with the following content:
Run the Test
Last updated