A Concurrent Example
An example of concurrent API
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 a Directory
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
Initialize Truffle
In the 'contract' directory, create a new contract named visitCounter.sol
with the following content.
Create an Empty Truffle Project
Compile the Contract
Write the Deployment Script
Deploy the Contract
Write the Test
All transactions calling "VisitCounter" are initiated without waiting for the previous one to complete.
Run the Test
These transactions will be included in the same block and are executed in parallel by multiple EVM instances.
Last updated