TransWikia.com

Synchronizing test and web3 enabled nodejs service

Ethereum Asked by Zach_is_my_name on November 26, 2020

I have a test suite running in truffle, and an external web3 connected node service running – both connected to a ganache-cli network.

My problem is: my tests are supposed to pick up an event triggered by the external service, BUT, the service only calls the blockchain (generating the event that tests are watching for) AFTER the tests complete.

(Notes: I obviously am looking for a solution altering the test not the service.
Please disregard the specific timing of the tests/events, I know the OZ test helper package I’m using does things under to the hood to advance blocks and time which I can drill down on to pass the test once I can grok the following)

only looking to solve: tests run, tests complete, service calls/transacts, service completes; want: tests run, service calls/transacts, service completes, tests complete)

Here is code for the service:

var currentBlock = 0;
web3.eth.getBlockNumber()
    .then((number)=>{
        currentBlock = number;
        console.log(currentBlock);
    });

setInterval(function(){
    web3.eth.getBlock('latest',async (err,block)=>{
        if(err){
            return;
        }
        if(currentBlock <= block.number){
            console.log(`New block received, Number: ${block.number}`);            
             
            // Get scheduleCallEvent events and save
            var events = await aionContract.getPastEvents('ScheduleCallEvent', {fromBlock: currentBlock-reqConfirmations, toBlock: block.number-reqConfirmations}) 

            for(var i = 0; i < events.length; i++){
                console.log('Registering new request to the database...');
                await saveRequestedTxs(events[i],web3);
            }                                     
            
            var events = await aionContract.getPastEvents('ExecutedCallEvent', {fromBlock: currentBlock-reqConfirmations,toBlock: block.number-reqConfirmations})
            for(var i = 0; i < events.length; i++){
                console.log('Registering successfully executed Tx...');
                await saveExecutedTxs(events[i],web3);
            }           

            //Execute pending transactions if any, Block and time based schedules
            await executeRequestedTxs(block.number,false,web3,account,aionContract);
            await executeRequestedTxs(block.timestamp,true,web3,account,aionContract);    
            
            // Save last processed block
            currentBlock = block.number+1;
        }
    })
},4000);

And the test:

  it('Aion contract calls returnBondsOnTimeOut()', async function() {

    await time.increase((await this.Escrow.suggestionDuration()).add(new BN ("1"))); 
        
    const executedCallEvents = await aionContract.getPastEvents("ExecutedCallEvent", 
        {fromBlock: "latest", toBlock:"pending"})
    
    await expectEvent.inLogs(executedCallEvents, 'ExecutedCallEvent', {})             
  })

One Answer

Your test package should be fully functional standalone.

Embed this event-generation within the test, and detach it from your production service.

Truffle suite is designated for testing your onchain code (contracts) not your offchain code.

Correct answer by goodvibration on November 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP