Skip to content

Remove To/From JSON (Maybe PoolMetadata)#71

Closed
ch1bo wants to merge 1 commit intoblockfrost:masterfrom
cardano-scaling:remove-overlap-instance
Closed

Remove To/From JSON (Maybe PoolMetadata)#71
ch1bo wants to merge 1 commit intoblockfrost:masterfrom
cardano-scaling:remove-overlap-instance

Conversation

@ch1bo
Copy link
Copy Markdown
Contributor

@ch1bo ch1bo commented Apr 1, 2025

Credit @v0d1ch

We had problems compiling ToJSON instances with this ToJSON (Maybe PoolMetadata) in scope. As we neither control the instance from aeson, nor the one in your package, we cannot make instances like this one to compile.

FYI compilation errors coming from this:

[12 of 45] Compiling Hydra.Chain.Blockfrost.Client ( src/Hydra/Chain/Blockfrost/Client.hs, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/Chain/Blockfrost/Client.o, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/Chain/Blockfrost/Client.dyn_o )
[18 of 45] Compiling Hydra.API.ServerOutput ( src/Hydra/API/ServerOutput.hs, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/API/ServerOutput.o, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/API/ServerOutput.dyn_o )

src/Hydra/API/ServerOutput.hs:112:5: error: [GHC-43085]
    • Overlapping instances for ToJSON (Maybe (UTxOType tx))
        arising from a use of ‘genericToJSON’
      Matching instance:
        instance ToJSON a => ToJSON (Maybe a)
          -- Defined in ‘aeson-2.2.3.0:Data.Aeson.Types.ToJSON’
        ...plus one instance involving out-of-scope types
          Potentially matching instance:
            instance [overlap ok] ToJSON
                                    (Maybe
                                       blockfrost-api-0.12.1.0:Blockfrost.Types.Cardano.Pools.PoolMetadata)
      (The choice depends on the instantiation of ‘tx’
       and the result of evaluating ‘UTxOType’
       To pick the first instance above, use IncoherentInstances
       when compiling the other instance declarations)
    • In the expression:
        genericToJSON defaultOptions {omitNothingFields = True}
      In an equation for ‘toJSON’:
          toJSON = genericToJSON defaultOptions {omitNothingFields = True}
      In the instance declaration for ‘ToJSON (Greetings tx)’
    |
112 |     genericToJSON
    |     ^^^^^^^^^^^^^

We found that it is important what modules are compiled in one invocation of cabal build. For example the above error does not appear if we try another build of cabal build which had the module pulling in your instance already compiled (Hydra.Chain.Blockfrost.Client).

@sorki
Copy link
Copy Markdown
Contributor

sorki commented Apr 2, 2025

Interesting! Almost looks like a compiler or genericToJSON bug.

Do you mind trying to change OVERLAPS to OVERLAPPING, made a branch for you called srk/poolMetaOverlapping. If I understand https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/instances.html#overlapping-instances correctly, that should change the instance from both OVERLAPPABLE and OVERLAPPING to just the latter and possibly fix your issue.

I wish there was a mechanism to control the instance propagation..

@v0d1ch
Copy link
Copy Markdown

v0d1ch commented Apr 2, 2025

@sorki thanks for looking at this but unfortunately using both OVERLAPS or OVERLAPPABLE still fail to compile.
I can tatke a look once I have more time and see if I can make it work if you are unable to drop this instance completely. Currently we have to maintain a fork where this instance is removed.

@ch1bo
Copy link
Copy Markdown
Contributor Author

ch1bo commented Apr 2, 2025

@sorki @v0d1ch I just tried to verify this too. Using your branch and this diff:

diff --git a/cabal.project b/cabal.project
index 6245e60982..7c3e826f2f 100644
--- a/cabal.project
+++ b/cabal.project
@@ -17,9 +17,9 @@ index-state:

 source-repository-package
   type: git
-  location: https://github.com/cardano-scaling/blockfrost-haskell.git
-  tag: 042179583f439d09c271b11bac9e9289133c0a66
-  --sha256: j2kRxDxtehHB0mQ4lHE6PqjOVca2dHgQgJaxtCPDeM8=
+  location: https://github.com/blockfrost/blockfrost-haskell.git
+  tag: cc7ee38aeb63ea01e9198edee62ea669e6c406da
+  --sha256: 1YCtviZL9/X62OVpqM3AFZDM1wboocupQ7pUFvnCD+I=
   subdir:
     blockfrost-client-core
     blockfrost-api
diff --git a/hydra-node/src/Hydra/API/ServerOutput.hs b/hydra-node/src/Hydra/API/ServerOutput.hs
index 3015e2ce42..c20405a541 100644
--- a/hydra-node/src/Hydra/API/ServerOutput.hs
+++ b/hydra-node/src/Hydra/API/ServerOutput.hs
@@ -106,7 +106,7 @@ data Greetings tx = Greetings
 deriving instance IsChainState tx => Eq (Greetings tx)
 deriving instance IsChainState tx => Show (Greetings tx)

-instance IsChainState tx => ToJSON (Greetings tx) where
+instance {-# OVERLAPPABLE #-} IsChainState tx => ToJSON (Greetings tx) where
   toJSON =
     genericToJSON
       defaultOptions

I still get this (and more similar errors):

Building library for hydra-node-0.21.0..
[12 of 45] Compiling Hydra.Chain.Blockfrost.Client ( src/Hydra/Chain/Blockfrost/Client.hs, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/Chain/Blockfrost/Client.o, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/Chain/Blockfrost/Client.dyn_o )
[18 of 45] Compiling Hydra.API.ServerOutput ( src/Hydra/API/ServerOutput.hs, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/API/ServerOutput.o, /home/ch1bo/code/iog/hydra/dist-newstyle/build/x86_64-linux/ghc-9.6.6/hydra-node-0.21.0/build/Hydra/API/ServerOutput.dyn_o )

src/Hydra/API/ServerOutput.hs:111:5: error: [GHC-43085]
    • Overlapping instances for ToJSON (Maybe (UTxOType tx))
        arising from a use of ‘genericToJSON’
      Matching instance:
        instance ToJSON a => ToJSON (Maybe a)
          -- Defined in ‘aeson-2.2.3.0:Data.Aeson.Types.ToJSON’
        ...plus one instance involving out-of-scope types
          Potentially matching instance:
            instance [overlapping] ToJSON
                                     (Maybe
                                        blockfrost-api-0.12.1.0:Blockfrost.Types.Cardano.Pools.PoolMetadata)
      (The choice depends on the instantiation of ‘tx’
       and the result of evaluating ‘UTxOType’
       To pick the first instance above, use IncoherentInstances
       when compiling the other instance declarations)
    • In the expression:
        genericToJSON defaultOptions {omitNothingFields = True}
      In an equation for ‘toJSON’:
          toJSON = genericToJSON defaultOptions {omitNothingFields = True}
      In the instance declaration for ‘ToJSON (Greetings tx)’
    |
111 |     genericToJSON
    |     ^^^^^^^^^^^^^

@sorki
Copy link
Copy Markdown
Contributor

sorki commented Apr 2, 2025

Thanks! I mean OVERLAPS -> OVERLAPPING change in blockfrost-api like here master...srk/poolMetaOverlapping

My theory is that by using OVERLAPS (no idea why I've used it in the first place lol) my instance is both OVERLAPPABLE and OVERLAPPING and making it only the latter might fix your issue.

If not I'll remove the instance and find another solution.

@noonio
Copy link
Copy Markdown

noonio commented Apr 3, 2025

Thanks @sorki

Interestingly, the error is different:

src/Hydra/Network/Message.hs:48:1: error: [GHC-43085]
    • Overlapping instances for ToJSON (Maybe (UTxOType tx))
        arising from a use of ‘aeson-2.2.3.0:Data.Aeson.Types.ToJSON.$dmtoJSON’
      Matching instance:
        instance ToJSON a => ToJSON (Maybe a)
          -- Defined in ‘aeson-2.2.3.0:Data.Aeson.Types.ToJSON’
        ...plus one instance involving out-of-scope types
          Potentially matching instance:
            instance [overlapping] ToJSON
                                     (Maybe
                                        blockfrost-api-0.12.1.0:Blockfrost.Types.Cardano.Pools.PoolMetadata)
      (The choice depends on the instantiation of ‘tx’
       and the result of evaluating ‘UTxOType’
       To pick the first instance above, use IncoherentInstances
       when compiling the other instance declarations)
    • In the expression:
        aeson-2.2.3.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(Message tx)
      In an equation for ‘toJSON’:
          toJSON
            = aeson-2.2.3.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(Message tx)
      When typechecking the code for ‘toJSON’
        in a derived instance for ‘ToJSON (Message tx)’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘ToJSON (Message tx)’
   |
48 | deriving anyclass instance IsTx tx => ToJSON (Message tx)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Hydra/Network/Message.hs:49:1: error: [GHC-43085]
    • Overlapping instances for FromJSON (Maybe (UTxOType tx))
        arising from a use of ‘aeson-2.2.3.0:Data.Aeson.Types.FromJSON.$dmparseJSON’
      Matching instance:
        instance FromJSON a => FromJSON (Maybe a)
          -- Defined in ‘aeson-2.2.3.0:Data.Aeson.Types.FromJSON’
        ...plus one instance involving out-of-scope types
          Potentially matching instance:
            instance [overlapping] FromJSON
                                     (Maybe
                                        blockfrost-api-0.12.1.0:Blockfrost.Types.Cardano.Pools.PoolMetadata)
      (The choice depends on the instantiation of ‘tx’
       and the result of evaluating ‘UTxOType’
       To pick the first instance above, use IncoherentInstances
       when compiling the other instance declarations)
    • In the expression:
        aeson-2.2.3.0:Data.Aeson.Types.FromJSON.$dmparseJSON @(Message tx)
      In an equation for ‘parseJSON’:
          parseJSON
            = aeson-2.2.3.0:Data.Aeson.Types.FromJSON.$dmparseJSON
                @(Message tx)
      When typechecking the code for ‘parseJSON’
        in a derived instance for ‘FromJSON (Message tx)’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘FromJSON (Message tx)’
   |
49 | deriving anyclass instance IsTx tx => FromJSON (Message tx)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@ch1bo
Copy link
Copy Markdown
Contributor Author

ch1bo commented Apr 3, 2025

@sorki

If not I'll remove the instance and find another solution.

I started a fix using a dedicated type for the pool metadata response to avoid instances on Maybe altogether. I'll clean it up and send a PR tomorrow

@sorki
Copy link
Copy Markdown
Contributor

sorki commented Apr 6, 2025

Fixed via #72

@sorki sorki closed this Apr 6, 2025
@github-project-automation github-project-automation bot moved this from In progress 🕐 to Done ✔ in ☕ Hydra Team Work Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants