Quick problem I've been slamming my head against since last night,
My truffle (v5.2.3) test file performs two (2) tests, that serially call()
then actually send a transaction to make persistent changes on my smart contract's storage. These tests are as follow (sorry for the dumped file, I've minimized as much as I could):
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);
});
*/
});
The first test calls, then sends a transaction just fine! At every truffle test
that I run, I get nice, expected log results in my console:
Test1 on PassportManager address: 0x871bbABdAeA0721FEB5529A07119edC7f05aB508
✓ should initialize a new passport linked with the current user's address (71ms)
TxHash 0x0760cb2738da2a21cc404e0627e1008599fe81f2c3a6914a1b06ff712dc8adca
and now it continues to the 2nd test, where for some reason it doesn't send the transaction, even though the call succeeds!
Test2 on PassportManager address: 0x871bbABdAeA0721FEB5529A07119edC7f05aB508
what the hell
✓ should add an identity file sha256 hash to a controlled passport (58ms)
The assertions are succeeding, the returned results from my contract call are logged, we even reach the frustrated little what the hell
which is BELOW the sendTransaction
, but I never get a TxHash like on the sent transaction of the 1st it()
test!
What is even weirder is that if I add a 3rd it()
test, it works as expected:
Uncomment the it("return false", () => { assert(0==1); });
from my test block, and lo and behold:
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! */
---------------------------
I've been losing hair on this since yesterday, and I smell some sort of concurrency/timing problem. Is there any way to control the sequence of sent transactions, or wait for one to be submitted and mined before moving on with the tests?
The weird sequence of printed console.log()
s (for example, the third test failing before test 2 actually prints out the txHash and event data) is maybe pointing towards something like that, but I thought the result.on()
block would ensure we're waiting for the tx to be mined!