IntersectMBO / cardano-cli

This repository contains sources for the command-line interface (CLI) tool for interacting with the Cardano blockchain.
Apache License 2.0
39 stars 14 forks source link

Negative value overflows into positive in `create-protocol-parameters-update` #860

Closed mkoura closed 2 weeks ago

mkoura commented 1 month ago

Description

When creating pparam update proposal, negative value overflows into positive value.

Steps to Reproduce

> cabal run cardano-cli -- conway stake-address key-gen --verification-key-file stake.vkey --signing-key-file stake.skey
> cardano-cli conway governance action create-protocol-parameters-update --testnet --governance-action-deposit 100000000 --deposit-return-stake-verification-key-file stake.vkey --anchor-url "http://www.pparam-action-wrtz.com/" --anchor-data-hash fb013f4b7ddb08fa20dd2974b8a4447598477886f544d71033da53390325cd37 --max-block-header-size -3161913232 --out-file pparams_update.action
> cardano-cli conway governance action view --action-file pparams_update.action
{
    "anchor": {
        "dataHash": "fb013f4b7ddb08fa20dd2974b8a4447598477886f544d71033da53390325cd37",
        "url": "http://www.pparam-action-wrtz.com"
    },
    "deposit": 100000000,
    "governance action": {
        "contents": [
            null,
            {
                "maxBlockHeaderSize": 2160
            },
            null
        ],
        "tag": "ParameterChange"
    },
    "return address": {
        "credential": {
            "keyHash": "7499af8367c8ae3ae72210e5e54525acd3b21c1560f97106446a674c"
        },
        "network": "Testnet"
    }
}

ie. -3161913232 becomes 2160.

Additional Context

cardano-node 9.1.0 - linux-x86_64 - ghc-8.10
git rev 88293215da71e7284c0cd499d9fc1b9b1f9ce9a1

cardano-cli 9.2.1.0 - linux-x86_64 - ghc-8.10
git rev 88293215da71e7284c0cd499d9fc1b9b1f9ce9a1
smelc commented 1 month ago

I can reproduce :+1:

smelc commented 1 month ago

Investigation:

ghci> import Data.Word
ghci> read "-3161913232" :: Word16
2160

This is a known issue:

Trying possible fixes:

ghci> ((read "-31619132" :: Natural) & naturalToWordMaybe <&> fromIntegral) :: Maybe Word16
*** Exception: Prelude.read: no parse

Good :heavy_check_mark:

ghci> ((read "65537" :: Natural) & naturalToWordMaybe <&> fromIntegral) :: Maybe Word16
Just 1

Not good: 65537 doesn't fit in a Word16 An overflow is happening here (fromIntegral is the crux of the problem again) :x:

toIntegralSized seems to do the job:

ghci> ((65536 :: Word) & toIntegralSized) :: Maybe Word16
Nothing
ghci> ((65535 :: Word) & toIntegralSized) :: Maybe Word16
Just 65535