3

从昨晚开始我就一直在努力解决一个快速的问题,

我的 truffle (v5.2.3) 测试文件执行两 (2) 个测试,call()然后依次实际发送交易以对我的智能合约的存储进行持久更改。这些测试如下(抱歉转储文件,我已尽可能地最小化):

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

contract("PassportManager", accounts => {
    it("should initialize a new passport linked with the current user's address", () => {
        let this_address = accounts[0];
        let this_nickname = "John Doe";
        let meta;
        
    return PassportManager.deployed()
        .then(instance => {
            meta = instance;
            console.log("Test1 on PassportManager address: " + meta.address);
            return meta.initPassport.call(this_nickname);
        })
        .then(returned_tuple => {
            assert.equal(returned_tuple[0], this_nickname, "Nickname, passed and returned by PassportManager.initPassport(), should match!");
            assert.equal(returned_tuple[1], this_address, "Controller address, passed and returned by PassportManager.initPassport(), should match!");
            //If we're here, it means the previous call() has succeeded. Now,
            //let's take things up a notch and let's actually send a transaction that changes the state of the blockchain.
            //Remember: We can't check for return values with a transaction, we have to debug the tx id manually.
            //#NOTE: We passed an extra parameter here. For more info on this special parameter object, check out:
            //https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts#making-a-transaction
            const result = meta.initPassport.sendTransaction(this_nickname, {from: accounts[0]});
            result.on('transactionHash', (hash) => {
                console.log('TxHash', hash);
            });
        });
    });
    
    it("should add an identity file sha256 hash to a controlled passport", () => {
        let this_address = accounts[0];
        let doc_hash = "0x21f3a9de43f07d855f49b946a10c30df432e8af95311435f77daf894216dcd41";
        let meta;
        
    return PassportManager.deployed()
        .then(instance => {
            meta = instance;
            console.log("Test2 on PassportManager address: " + meta.address);
            return meta.addIDFileToPassport.call(this_address, doc_hash);
        })
        .then(returned_tuple => {
            assert.equal(returned_tuple[0], this_address, "Passport controller, passed and returned by PassportManager.addIDFileToPassport(), should match!");
            assert.equal(returned_tuple[1], doc_hash, "Document hash (bytes32), passed and returned by PassportManager.addIDFileToPassport(), should match!");
            assert.equal(returned_tuple[2], 1, "Trust score of newly added doc_hash should be 1!");
            //Now let's actually pass a concrete, actuallly persistent transaction instead of a call.
            const result = meta.addIDFileToPassport.sendTransaction(this_address, doc_hash, {from: accounts[0]});
            result.on('transactionHash', (hash) => {
                console.log('TxHash', hash);
            });
            console.log("what the hell");
        });
    });
    
  /*it("return false", () => {
    assert(0==1);
  });
  */
});

第一个测试调用,然后发送一个事务就好了!在我每次truffle test运行时,我都会在控制台中得到很好的预期日志结果:

Test1 on PassportManager address: 0x871bbABdAeA0721FEB5529A07119edC7f05aB508
    ✓ should initialize a new passport linked with the current user's address (71ms)
TxHash 0x0760cb2738da2a21cc404e0627e1008599fe81f2c3a6914a1b06ff712dc8adca

现在它继续进行第二次测试,由于某种原因,即使调用成功,它也不会发送交易

Test2 on PassportManager address: 0x871bbABdAeA0721FEB5529A07119edC7f05aB508
what the hell
    ✓ should add an identity file sha256 hash to a controlled passport (58ms)

断言成功了,我的合约调用返回的结果被记录下来,我们甚至达到了BELOW的沮丧点what the hell,但我从来没有像第一次测试的发送交易那样得到 TxHash !sendTransactionit()

更奇怪的是,如果我添加第三个it()测试,它会按预期工作:it("return false", () => { assert(0==1); });从我的测试块中取消注释,你瞧:

Test2 on PassportManager address: 0x87465190eCBa7C287143f07653462a87464b1AbA
what the hell
    ✓ should add an identity file sha256 hash to a controlled passport (119ms)
    1) return false
TxHash 0x9226311347487d294b0bcf5bc1f535636fe886f08dfa327f15de43318aad37d7

    Events emitted during test:
    ---------------------------

    /* I even get proper emitted event data from my contract! */

    ---------------------------

从昨天开始,我一直在为此脱发,我闻到了某种并发/时间问题。有什么方法可以控制发送交易的顺序,或者在继续测试之前等待提交和挖掘交易?
奇怪的打印序列console.log()例如,在测试 2 实际打印出 txHash 和事件数据之前第三次测试失败)可能指向类似的东西,但我认为该result.on()块将确保我们正在等待 tx开采!

4

0 回答 0