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

  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 an Empty Truffle Project

>mkdir visitCounter
>cd visitCounter
visitCounter>truffle init

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

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 {
        counter.add(1);
    }

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

Compile the Contract

visitCounter> truffle compile

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:

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

module.exports = function(deployer) {
  deployer.deploy(VisitCounter);
};

Run the Deployment Script

visitCounter> truffle migrate --network arcology_devnet

Write Tests

Create a test script file named visit.js in the test directory with the following content:

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

async function sendTx(contract,addr){
  await contract.giveRightVisit(addr);
  return new Promise((resolve, reject) => {  
    resolve('success1')
  })
}

contract('visit counter', (accounts) => {
  it('give right for visit contract', async () => {
    const VisitCounterInstance = await VisitCounter.at('0xB1e0e9e68297aAE01347F6Ce0ff21d5f72D3fa0F'); // The contract address
    
    var txs=new Array();

    accounts.forEach((acc,idx) => {
      txs.push(sendTx(VisitCounterInstance,acc));
    })

    await Promise.all(txs).then((values) => {
      values.forEach((item,idx) => {
        //console.log(item)
      })
    }).catch((error)=>{
      console.log(error)
    })
	//-------------------------------------------------------------------
  });

});

Run the Test

visitCounter> truffle test --network arcology_devnet

Last updated