Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

[BUG - found cause] zero ada error mistakenly raised #16

Closed
KristianBalaj opened this issue May 5, 2022 · 3 comments
Closed

[BUG - found cause] zero ada error mistakenly raised #16

KristianBalaj opened this issue May 5, 2022 · 3 comments

Comments

@KristianBalaj
Copy link
Collaborator

KristianBalaj commented May 5, 2022

I've posted #15 issue some time ago and when I was digging deeper into the actual problem I've found out that there is probably a bug somewhere and created the minimal reproducible example.

The following code fails the test with the zero ada error even when there should be no such error.

I found out that this happens when I changed this package in the project from commit 64f37881c3c6a1ece104413e95b6a272d22dbfaa to 2077162655a62f9b6404e153eed3ef56c65cad67

EDIT:
The error occurs with both of the commits, so the bug is implemented somewhere further in the past.
Modifying this line:

void $ sendValue admin (Ada.toValue minAdaTxOut <> Value.assetClassValue (fakeCoin (FakeCoin "SMTH")) 10_000_000) user

to this:

void $ sendValue admin (Ada.toValue minAdaTxOut <> Ada.toValue minAdaTxOut  <> Value.assetClassValue (fakeCoin (FakeCoin "SMTH")) 10_000_000) user

solves the error.

However, it should work either way in my opinion.

Buggy code
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}

module Spec.MyTest where

import Control.Monad
import Ledger
import qualified Ledger.Value as Value
import Plutus.Test.Model
import qualified Plutus.V1.Ledger.Ada as Ada
import Test.Tasty
import Utils.Helpers (checkBool)

main :: IO ()
main = do
  cfg <- readDefaultBchConfig
  defaultMain $
    testGroup
      "benchmark test suite"
      [ tests cfg
      ]

tests :: BchConfig -> TestTree
tests cfg =
  testGroup
    "Foo"
    [ check True "foo" foo'
    ]
  where
    tokens = Value.assetClassValue (fakeCoin (FakeCoin "SMTH")) 10_000_000

    check :: Bool -> String -> Run Bool -> TestTree
    check = checkBool cfg (Ada.adaValueOf 100_000_000 <> tokens)

    foo' = do
      user <- newUser mempty
      admin <- getMainUser
      void $ sendValue admin (Ada.toValue minAdaTxOut <> Value.assetClassValue (fakeCoin (FakeCoin "SMTH")) 10_000_000) user
      foo user

foo :: PubKeyHash -> Run Bool
foo user = do
  admin <- getMainUser
  let spendVal = Ada.toValue minAdaTxOut
  sp <- sendValue admin spendVal user >> spend user spendVal
  void $ Plutus.Test.Model.signTx user (userSpend sp <> payToPubKey admin spendVal) >>= sendTx
  noErrors
@KristianBalaj
Copy link
Collaborator Author

Okay, just found out that this is a bad minimal reproducible example, there's no bug in there actually in this example.

Since it is logical that there can't be only the fakecoins in the user's wallet.

Will have to find time and check back at the problem where it was happening and try to create correct minimal example.

@KristianBalaj
Copy link
Collaborator Author

I've come across this issue once again and I've found the cause.

When I look at the user's UTXOs (utxoAt <$> getMainUser) I can find these fractured UTXOs there.

[ ( TxOutRef
        { txOutRefId = 157 a6dc880c001b691c6be2bc02f5d1b9dbfca6244a2fc2292a631cf38f560ac,
          txOutRefIdx = 3
        },
      TxOut
        { txOutAddress = Address {addressCredential = PubKeyCredential 918 f37f350507ca60236ef12a001f7405198df34d68e48178b75d98a, addressStakingCredential = Nothing},
          txOutValue = Value (Map [(,Map [("", 2000000)]), (3 d88123b74593927b403fb99108a5b7594fc03ad8677d95172df8778, Map [("asset", 1000000000)])]),
          txOutDatumHash = Nothing
        }
    ),
    ( TxOutRef
        { txOutRefId = 157 a6dc880c001b691c6be2bc02f5d1b9dbfca6244a2fc2292a631cf38f560ac,
          txOutRefIdx = 4
        },
      TxOut
        { txOutAddress = Address {addressCredential = PubKeyCredential 918 f37f350507ca60236ef12a001f7405198df34d68e48178b75d98a, addressStakingCredential = Nothing},
          txOutValue = Value (Map [(,Map [("", 29999988000000)]), (3 d88123b74593927b403fb99108a5b7594fc03ad8677d95172df8778, Map [("asset", 198000000000)])]),
          txOutDatumHash = Nothing
        }
    )
  ]

The problem is that when I want to send a transaction that does userSpend with minAda, the first user's UTXO gets consumed and it results in Transaction outputs should not contain zero Ada because there is still the asset in the UTXO that remains without the spent Ada there.

I don't think this is right behaviour since the user has enough Ada still at his address. Correct me if I'm wrong 🙌

@KristianBalaj KristianBalaj changed the title [BUG] zero ada error mistakenly raised [BUG - found cause] zero ada error mistakenly raised May 17, 2022
@anton-k
Copy link
Contributor

anton-k commented Jul 5, 2022

I guess it can be caused by plutus bug in lt it caused wrong implementation for spend.
It was recently fixed in the code by iusing correct lt implementation

Sorry for late reply

@anton-k anton-k closed this as completed Aug 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants