javascript - How to advance block number when I’m developing on RSK Regtest? - Stack Overflow

I have a smart contract that checks if the actual block number is higher than a fixed one to execute ce

I have a smart contract that checks if the actual block number is higher than a fixed one to execute certain functionality and I need to write a unit test to validate that behavior. I’m using RSK in Regtest mode to execute tests and I would need to increment the block number without actually waiting for time to pass.

The smart contract uses a block number, and I need to increment the block number without actually waiting for time to pass.

context('once deployed', function () {
   it('can only be released after cliff', async function () {
     // TODO here I need to increment time or block number
     await this.lockup.release();
   });
)};

How can I do this in a truffle (mocha) test like the one above?

I have a smart contract that checks if the actual block number is higher than a fixed one to execute certain functionality and I need to write a unit test to validate that behavior. I’m using RSK in Regtest mode to execute tests and I would need to increment the block number without actually waiting for time to pass.

The smart contract uses a block number, and I need to increment the block number without actually waiting for time to pass.

context('once deployed', function () {
   it('can only be released after cliff', async function () {
     // TODO here I need to increment time or block number
     await this.lockup.release();
   });
)};

How can I do this in a truffle (mocha) test like the one above?

Share Improve this question asked Jan 25, 2021 at 13:01 OwansOwans 1,0376 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Quick note, to stress this is not possible in "actual" RSK blockchains (Mainnet and Testnet), as it involves "fake" mining.

However, in Regtest, this is indeed possible:

(1)

Use the evm_mine JSON-RPC method to mine blocks.

function evmMine () {
    return new Promise((resolve, reject) => {
        web3.currentProvider.send({
            jsonrpc: "2.0",
            method: "evm_mine",
            id: new Date().getTime()
            }, (error, result) => {
                if (error) {
                    return reject(error);
                }
                return resolve(result);
            });
    });
};

await evmMine(); // Force a single block to be mined.

This is consistent with the approach used in Ethereum developer tools, e.g. Ganache.

(2)

Use the evm_increaseTime JSON-RPC method to increase time of the block:

function evmIncreaseTime(seconds) {
    return new Promise((resolve, reject) => {
        web3.currentProvider.send({
            method: "evm_increaseTime",
            params: [seconds],
            jsonrpc: "2.0",
            id: new Date().getTime()
          }, (error, result) => {
            if (error) {
                return reject(error);
            }
            return asyncMine().then( ()=> resolve(result));
          });
    }); 
}

await evmIncreaseTime(600); // Force block to be mined such that ~10 minutes has passed

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745567649a4633492.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信