warp-contracts / warp

An implementation of the Arweave SmartWeave smart contracts protocol.
MIT License
157 stars 44 forks source link

Issue with nested inner-contract calls #239

Closed marslab2022 closed 1 year ago

marslab2022 commented 1 year ago

Hi, team. I got different evaluated contract states when integrate contract to DApp - The result of two calls to the function readState is not the same. Could team please check this? Thanks.

Test environment preparation

  1. Clone code to local: https://github.com/marslab2022/ThetAR_exchange

  2. Cmd: npm install

  3. Cmd: npx arlocal 1984

  4. Head tocontract/ folder and run yarn install & npm run build:contracts. Then, to contract/deploy/ folder, and run command: ts-node deploy-test-contract.ts It will shows: image And generate a JWK file: key-file-for-test.json image

  5. Head to file: src/lib/api.js.Replace thetARContractAddress with txid. Replace feeWalletAdrress with wallet address generated below(step 4). image

  6. Cmd: npm run build

  7. Cmd: serve -s build

  8. Head to localhost:3000 image

  9. Connect wallet using key-file-for-test.json generated in step 4.

  10. Go to my page and click register; image

  11. Refresh page and click activate: image

    Test for badcase

  12. Click home botton and enter #0 pair details image

  13. Create 3 orders:

    • Limit, buy, price:1, amount: 1;
    • Limit, buy, price:2, amount: 1;
    • Limit, buy, price:3, amount: 1; Click refresh botton to see latest state: image
  14. Head to My page and cancel these 3 orders respectively: image

  15. Check the console log: After click cancel button, print the newest contract state to console. Check the last state, we can find userInfos->orders is Array(0), means no remaining orders. image image

  16. Go to Home page and click #0, you could find order: image And when click Refresh button, it will print contract state to console. Check it: image

The order that had been cancelled came back???

ppedziwiatr commented 1 year ago

Thanks! Should I run this: https://github.com/marslab2022/ThetARExchange/blob/main/contract/tests/test_thetar_badcase.ts testcase?

marslab2022 commented 1 year ago

Thanks! Should I run this: https://github.com/marslab2022/ThetARExchange/blob/main/contract/tests/test_thetar_badcase.ts testcase?

Yes. But maybe I need to check whether the latest contract code can reproduce this problem, please wait.

marslab2022 commented 1 year ago

So sorry to keep you waiting. I just pushed new code to the repository. test_thetar_badcase1.ts reproduces the issue. And at the same time, we also tested a scenario(test_thetar_badcase2.ts) - user tries to obtain the latest order book info(readState) before the operation of creating an order is completed(writeInteraction). In this case I got error like below. I am not sure if it is the same issue as badcase1, or just there's something wrong I called these functions.

running...
(node:46034) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{"owner":"9-vYItKDZ1bE9r1GBaSOmrCgeCkt8o-wRR3O0vSG7VI","tokenSrcTxs":[],"thetarTokenAddress":"UDl0d1SjeJXa3_S8WGEOk9j8qtRJV6JBA1mouLtM16o","maxPairId":0,"pairInfos":[{"pairId":0,"tokenAddress":"mygwOSptyIFeoPmARQcFGHU0Huv9qq6KWlW8aFg82oE","logo":"TEST_00000lQgApM_a3Z6bGFHYE7SXnBI6C5_2_24MQ","description":"test token","name":"TEST token","symbol":"TEST","decimals":2}],"userOrders":{},"orderInfos":{"0":{"orders":[]}},"logs":[],"tokenSrcTemplateHashs":[0]}
txid:  LPf1vMkJwJ_MblxyC_pSdqpTdAdCiFz0abmDXMUhyrI
wallet address:  9-vYItKDZ1bE9r1GBaSOmrCgeCkt8o-wRR3O0vSG7VI
create order...
BEFORE:  {"sortKey":"000000000230,0000000000000,2fd8f01bfe56ab20f54ba24d84cdbc2cd8e0a3e90e6c7df0aa6b31cd64fa3332","cachedValue":{"state":{"owner":"9-vYItKDZ1bE9r1GBaSOmrCgeCkt8o-wRR3O0vSG7VI","tokenSrcTxs":[],"thetarTokenAddress":"UDl0d1SjeJXa3_S8WGEOk9j8qtRJV6JBA1mouLtM16o","maxPairId":0,"pairInfos":[{"pairId":0,"tokenAddress":"mygwOSptyIFeoPmARQcFGHU0Huv9qq6KWlW8aFg82oE","logo":"TEST_00000lQgApM_a3Z6bGFHYE7SXnBI6C5_2_24MQ","description":"test token","name":"TEST token","symbol":"TEST","decimals":2}],"userOrders":{},"orderInfos":{"0":{"orders":[]}},"logs":[],"tokenSrcTemplateHashs":[0]},"validity":{"gRFJ70bY-IlbM3Dsk_oGMuWpX7D97xVjU85q8gkPfPI":true},"errorMessages":{}}}
Get order book:  {"sortKey":"000000000230,0000000000000,2fd8f01bfe56ab20f54ba24d84cdbc2cd8e0a3e90e6c7df0aa6b31cd64fa3332","cachedValue":{"state":{"owner":"9-vYItKDZ1bE9r1GBaSOmrCgeCkt8o-wRR3O0vSG7VI","tokenSrcTxs":[],"thetarTokenAddress":"UDl0d1SjeJXa3_S8WGEOk9j8qtRJV6JBA1mouLtM16o","maxPairId":0,"pairInfos":[{"pairId":0,"tokenAddress":"mygwOSptyIFeoPmARQcFGHU0Huv9qq6KWlW8aFg82oE","logo":"TEST_00000lQgApM_a3Z6bGFHYE7SXnBI6C5_2_24MQ","description":"test token","name":"TEST token","symbol":"TEST","decimals":2}],"userOrders":{},"orderInfos":{"0":{"orders":[]}},"logs":[],"tokenSrcTemplateHashs":[0]},"validity":{"gRFJ70bY-IlbM3Dsk_oGMuWpX7D97xVjU85q8gkPfPI":true},"errorMessages":{}}}
Get order book:  {"sortKey":"000000000230,0000000000000,2fd8f01bfe56ab20f54ba24d84cdbc2cd8e0a3e90e6c7df0aa6b31cd64fa3332","cachedValue":{"state":{"owner":"9-vYItKDZ1bE9r1GBaSOmrCgeCkt8o-wRR3O0vSG7VI","tokenSrcTxs":[],"thetarTokenAddress":"UDl0d1SjeJXa3_S8WGEOk9j8qtRJV6JBA1mouLtM16o","maxPairId":0,"pairInfos":[{"pairId":0,"tokenAddress":"mygwOSptyIFeoPmARQcFGHU0Huv9qq6KWlW8aFg82oE","logo":"TEST_00000lQgApM_a3Z6bGFHYE7SXnBI6C5_2_24MQ","description":"test token","name":"TEST token","symbol":"TEST","decimals":2}],"userOrders":{},"orderInfos":{"0":{"orders":[]}},"logs":[],"tokenSrcTemplateHashs":[0]},"validity":{"gRFJ70bY-IlbM3Dsk_oGMuWpX7D97xVjU85q8gkPfPI":true},"errorMessages":{}}}
Get order book:  {"sortKey":"000000000230,0000000000000,2fd8f01bfe56ab20f54ba24d84cdbc2cd8e0a3e90e6c7df0aa6b31cd64fa3332","cachedValue":{"state":{"owner":"9-vYItKDZ1bE9r1GBaSOmrCgeCkt8o-wRR3O0vSG7VI","tokenSrcTxs":[],"thetarTokenAddress":"UDl0d1SjeJXa3_S8WGEOk9j8qtRJV6JBA1mouLtM16o","maxPairId":0,"pairInfos":[{"pairId":0,"tokenAddress":"mygwOSptyIFeoPmARQcFGHU0Huv9qq6KWlW8aFg82oE","logo":"TEST_00000lQgApM_a3Z6bGFHYE7SXnBI6C5_2_24MQ","description":"test token","name":"TEST token","symbol":"TEST","decimals":2}],"userOrders":{},"orderInfos":{"0":{"orders":[]}},"logs":[],"tokenSrcTemplateHashs":[0]},"validity":{"gRFJ70bY-IlbM3Dsk_oGMuWpX7D97xVjU85q8gkPfPI":true},"errorMessages":{}}}
2023-01-14T14:56:10.903Z FATAL [HandlerBasedContract] Error while interacting with contract [
  {
    type: 'exception',
    error: "TypeError: Cannot read properties of undefined (reading 'interactionInput')\n" +
      '    at new HandlerBasedContract (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/contract/HandlerBasedContract.ts:84:19)\n' +
      '    at Warp.contract (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/core/Warp.ts:66:12)\n' +
      '    at Object.swGlobal.contracts.write (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/core/modules/impl/handler/AbstractContractHandler.ts:58:52)\n' +
      '    at checkOrderQuantity (eval at create (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/core/modules/impl/HandlerExecutorFactory.ts:194:34), <anonymous>:116:32)\n' +
      '    at async createOrder (eval at create (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/core/modules/impl/HandlerExecutorFactory.ts:194:34), <anonymous>:94:17)\n' +
      '    at async JsHandlerApi.handle [as contractFunction] (eval at create (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/core/modules/impl/HandlerExecutorFactory.ts:194:34), <anonymous>:302:16)\n' +
      '    at async JsHandlerApi.handle (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/core/modules/impl/handler/JsHandlerApi.ts:39:29)\n' +
      '    at async HandlerBasedContract.evalInteraction (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/contract/HandlerBasedContract.ts:645:20)\n' +
      '    at async HandlerBasedContract.callContract (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/contract/HandlerBasedContract.ts:580:26)\n' +
      '    at async HandlerBasedContract.createInteraction (/Users/lab/mARsLab/thetar_exchange/contract/node_modules/warp-contracts/src/contract/HandlerBasedContract.ts:313:29)'
  }
]
ppedziwiatr commented 1 year ago

Ok, this looks like an issue in the test itself, I will take a look later this week.

In the meantime - I've released a 1.2.48-beta.1 with refactored internal writes (https://github.com/warp-contracts/warp/pull/311) - feel free to test :-)

The updateCacheForEachInteraction should no longer be necessary.

marslab2022 commented 1 year ago

This works fine with refactored internalWrite, Thank you guys :)