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

  1. The contract uses the U256Cumulative contract from Arcology's concurrent library to manage the cumulative count.

  2. The constructor initializes the visitCounter variable with an initial count of 0 and a maximum value of 1,000,000.

  3. The visit function is a public function that can be called by multiple transactions in parallel. When called, it increments the visitCounter by 1 using the add function provided by the U256Cumulative contract.

  4. The getCounter function allows anyone to retrieve the current value of the visit counter. It calls the get function provided by the U256Cumulative contract and returns the count.

Create a Directory

>mkdir visitCounter
>cd visitCounter

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

visitCounter> npm install arcologynetwork

Initialize Truffle

visitCounter>truffle init

In the 'contract' directory, create a new contract named visitCounter.sol with the following content.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;

import "arcologynetwork/contracts/concurrentlib/commutative/U256Cum.sol";

contract VisitCounter {    
    U256Cumulative counter;  
    constructor()  {
        visitCounter = new U256Cumulative(0, 1000000) ;  

    function visit() public {

    function getCounter() public returns(uint256){
        return counter.get();

Create an Empty Truffle Project

>mkdir visitCounter
>cd visitCounter
visitCounter>truffle init

Compile the Contract

Write the Deployment Script

Deploy the Contract

const VisitCounter = artifacts.require("VisitCounter");

module.exports = function(deployer) {

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