Skip to content

Commit

Permalink
got nix flake working; still need to take out fake package
Browse files Browse the repository at this point in the history
  • Loading branch information
giantimi committed Sep 26, 2023
1 parent 889ab77 commit 9616c5e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 116 deletions.
30 changes: 26 additions & 4 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
packages: txg.cabal

package aeson
flags: +cffi

source-repository-package
type: git
location: https://github.com/kadena-io/pact.git
tag: 0c51656d9cdad608c88d0c42e57c5f781dabe25b
tag: 75a3c9fbd79a5c748fe1ac4cd61f800d3164a628
--sha256: sha256-+XYLIHz7VV1jS0KTEI5gzgZYCI6pjstlP4AXGna2t5k=

source-repository-package
type: git
location: https://github.com/kadena-io/thyme.git
tag: 6ee9fcb026ebdb49b810802a981d166680d867c9
--sha256: sha256-DqGIoPGBg8py044Xa7l/Y09K+cKGFwlxONHoZRJyzCU=

source-repository-package
type: git
tag: e43073d0b8d89d9b300980913b842f4be339846d
location: https://github.com/kadena-io/pact-json
--sha256: sha256-ZWbAId0JBaxDsYhwcYUyw04sjYstXyosSCenzOvUxsQ=

-- Patch merged into master (upcoming verison 10.0). We are currently using 9.2
source-repository-package
type: git
tag: 3946a0e94470d7403a855dd60f8e54687ecc2b1d
location: https://github.com/larskuhtz/sbv
--sha256: 1msbz6525nmsywpm910jh23siil4qgn3rpsm52m8j6877r7v5zw3

package hostaddress
flags: +with-configuration-tools +with-aeson


package configuration-tools
flags: -remote-configs

constraints: sbv <=8.8
constraints: base64-bytestring <= 1.1.0.0
-- constraints: sbv <=8.8
constraints: sbv < 10

-- these upper bounds are required in order to not break payload validation in chainweb
constraints: base16-bytestring <1
constraints: base64-bytestring <1.1

allow-newer: base64-bytestring:*
allow-newer: base16-bytestring:*

136 changes: 42 additions & 94 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,96 +1,44 @@
{
description = "Chainweb transaction generator";

inputs = {
nixpkgs.follows = "haskellNix/nixpkgs-unstable";
haskellNix.url = "github:input-output-hk/haskell.nix";
flake-utils.url = "github:numtide/flake-utils";
};

nixConfig = {
# This sets the flake to use the IOG nix cache.
# Nix should ask for permission before using it.
# but remove it here if you do not want it.
extra-substituters = [ "https://cache.iog.io" "https://nixcache.chainweb.com" ];
extra-trusted-public-keys = ["hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" "nixcache.chainweb.com:FVN503ABX9F8x8K0ptnc99XEz5SaA4Sks6kNcZn2pBY="];
allow-import-from-derivation = "true";
};

outputs = { self, nixpkgs, flake-utils, haskellNix}:
flake-utils.lib.eachSystem
[ "x86_64-linux" "x86_64-darwin"
"aarch64-linux" "aarch64-darwin"] (system:
let
pkgs = import nixpkgs { inherit system;};
hsPkgs = pkgs.haskell.packages.ghc884;
nix-thunk = import ./deps/nix-thunk {};
gitignoreSrc = import (pkgs.fetchFromGitHub {
owner = "hercules-ci";
repo = "gitignore";
rev = "9e80c4d83026fa6548bc53b1a6fab8549a6991f6";
sha256 = "04n9chlpbifgc5pa3zx6ff3rji9am6msrbn1z3x1iinjz2xjfp4p";
}) {};
in { defaultPackage = hsPkgs.developPackage {
root = gitignoreSrc.gitignoreSource ./.;
name = builtins.baseNameOf ./.;
withHoogle = false;
source-overrides = {
chainweb-api = nix-thunk.thunkSource ./deps/chainweb-api;
pact = nix-thunk.thunkSource ./deps/pact;
};
overrides = self: super: with pkgs.haskell.lib; rec {
aeson = dontCheck (self.callHackageDirect {
pkg = "aeson";
ver = "1.5.6.0";
sha256 = "18yb8j0jvvzp275ylj16hskgxwdy55hljch9bjvpxl25vvslnk1n";
} {});
base64 = self.callHackageDirect {
pkg = "base64";
ver = "0.4.2.3";
sha256 = "1i4cf1xfbkxlxshwlsxgw2w5gi3hkkfm1n99vnzq7rixz8nxcw7r";
} {};
configuration-tools = dontBenchmark (dontCheck (self.callHackageDirect {
pkg = "configuration-tools";
ver = "0.6.0";
sha256 = "0ia2bhy35qv1xgbqrx0jalxznj8zgg97y0zkp8cnr1r3pq5adbcd";
} {}));
## ghc-byteorder = self.callHackageDirect {
## pkg = "ghc-byteorder";
## ver = "4.11.0.0.10";
## sha256 = "1inrpb74i811k0gh8iazfqayrqrl86pwsm3zqgxn0aivxjh2ygsz";
## } {};
hostaddress = overrideCabal (enableCabalFlag (enableCabalFlag (doJailbreak (self.callHackageDirect {
pkg = "hostaddress";
ver = "0.1.0.0";
sha256 = "13wqzkw32inc8v03vl94ibzxdy0hmiybch62c8rana38r4yn4fnl";
} {})) "with-configuration-tools") "with-aeson") (drv: {
librarySystemDepends = drv.librarySystemDepends or [] ++ [self.aeson self.configuration-tools optparse-applicative];
});
optparse-applicative = dontCheck (self.callHackageDirect {
pkg = "optparse-applicative";
ver = "0.15.1.0";
sha256 = "1mii408cscjvids2xqdcy2p18dvanb0qc0q1bi7234r23wz60ajk";
} {});
pact = overrideCabal (enableCabalFlag (dontCheck (doJailbreak super.pact)) "build-tool") (drv:
{ patches = drv.patches or [] ++ [./pact-base16-bytestring.patch]; });
pretty-simple = dontCheck (self.callHackageDirect {
pkg = "pretty-simple";
ver = "3.3.0.0";
sha256 = "19zwzzvjgadmzp9gw235bsr6wkljr8k0cqy75h5q8n0d5m60ip7m";
} {});
prettyprinter = dontCheck (self.callHackageDirect {
pkg = "prettyprinter";
ver = "1.6.0";
sha256 = "0f8wqaj3cv3yra938afqf62wrvq20yv9jd048miw5zrfavw824aa";
} {});
};
modifier = drv: pkgs.lib.overrideDerivation drv (attrs: {
buildTools = (attrs.buildTools or []) ++ [
hsPkgs.cabal-install
hsPkgs.ghcid
];
});
};
});

description = "TXG: Chainweb transaction generator";
inputs.haskellNix.url = "github:input-output-hk/haskell.nix";
inputs.nixpkgs.follows = "haskellNix/nixpkgs-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils, haskellNix }:
flake-utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ] (system:
let
overlays = [ haskellNix.overlay
(final: prev: {
# This overlay adds our project to pkgs
txgProject =
final.haskell-nix.project' {
src = ./.;
compiler-nix-name = "ghc962";
projectFileName = "cabal.project";
# This is used by `nix develop .` to open a shell for use with
# `cabal`, `hlint` and `haskell-language-server`
shell.tools = {
cabal = {};
hlint = {};
haskell-language-server = {};
};
# Non-Haskell shell tools go here
shell.buildInputs = with pkgs; [
nixpkgs-fmt
];
# This adds `js-unknown-ghcjs-cabal` to the shell.
# shell.crossPlatforms = p: [p.ghcjs];
};
})
];
pkgs = import nixpkgs { inherit system overlays; inherit (haskellNix) config; };
flake = pkgs.txgProject
.flake {
# This adds support for `nix build .#js-unknown-ghcjs:hello:exe:hello`
# crossPlatforms = p: [p.ghcjs];
};
in flake // {
# Built by `nix build .`
packages.default = flake.packages."txg:exe:txg";
});
}

38 changes: 27 additions & 11 deletions lib/TXG/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import Control.Monad.Catch

import Data.Bifunctor
import qualified Data.ByteString as B
import Data.ByteString.Base64.URL
import qualified Data.ByteString.Base64.URL as B64U
import qualified Data.ByteString.Lazy as BL
import qualified Data.List.NonEmpty as NEL
import Data.Maybe
Expand All @@ -88,6 +88,7 @@ import Pact.Types.API
import qualified Pact.Types.ChainMeta as CM
import Pact.Types.Command
import Pact.Types.Hash
import qualified Pact.JSON.Encode as J

import Text.Read (readEither)

Expand Down Expand Up @@ -130,6 +131,9 @@ newtype ChainId = ChainId Int
deriving (Eq, Ord, Num, Generic)
deriving newtype (ToJSON, FromJSON, Read, Show)

instance J.Encode ChainId where
build (ChainId i) = J.build $ J.Base10 i

cidToText :: ChainId -> T.Text
cidToText (ChainId i) = T.pack $ show i

Expand All @@ -153,21 +157,27 @@ instance Show TransactionHash where
show = T.unpack . T.decodeUtf8 . BL.toStrict . encode

instance ToJSON TransactionHash where
toJSON (TransactionHash x) = toJSON $! encodeBase64Unpadded x
toJSON (TransactionHash x) = toJSON $! B64U.encodeBase64Unpadded x

instance FromJSON TransactionHash where
parseJSON = withText "TransactionHash"
$! either (fail . T.unpack) return
. fmap TransactionHash
. decodeBase64Unpadded
. B64U.decodeBase64Unpadded
. T.encodeUtf8

instance J.Encode TransactionHash where
build (TransactionHash x) = J.build $ encodeB64UrlNoPaddingText x

encodeB64UrlNoPaddingText :: B.ByteString -> T.Text
encodeB64UrlNoPaddingText = T.dropWhileEnd (== '=') . B64U.encodeBase64Unpadded
-- -------------------------------------------------------------------------- --
-- Time

currentTxTime :: IO CM.TxCreationTime
currentTxTime = CM.TxCreationTime . ParsedInteger . round <$> getPOSIXTime


-- -------------------------------------------------------------------------- --
-- Misc Utils

Expand Down Expand Up @@ -199,7 +209,7 @@ newtype ClientError = ClientError T.Text
instance Exception ClientError

post
:: ToJSON body
:: J.Encode body
=> FromJSON result
=> Manager
-> HostAddress
Expand Down Expand Up @@ -228,12 +238,12 @@ post mgr hostAddr urlPath s body = do
, port = fromIntegral $ _hostAddressPort hostAddr
, path = T.encodeUtf8 urlPath
, requestHeaders = [("content-type", "application/json")]
, requestBody = RequestBodyLBS $ encode body
, requestBody = RequestBodyLBS $ J.encode body
, responseTimeout = responseTimeoutMicro $ 1000000 * 60 * 4
}

pactPost
:: ToJSON body
:: J.Encode body
=> FromJSON result
=> Manager
-> HostAddress
Expand All @@ -246,7 +256,7 @@ pactPost mgr hostAddr v cid pactPath
= post mgr hostAddr (pactBasePath v cid <> pactPath) False

mempoolPost
:: ToJSON body
:: J.Encode body
=> FromJSON result
=> Manager
-> HostAddress
Expand Down Expand Up @@ -306,12 +316,18 @@ listen
-> IO (Either ClientError ListenResponse)
listen m a v c rk = pactPost m a v c "/listen" (ListenerRequest rk)

newtype TransactionHashes = TransactionHashes [TransactionHash]
deriving (Show, Eq, Ord, Generic)

instance J.Encode TransactionHashes where
build (TransactionHashes xs) = J.build $ J.Array xs

mempoolMember
:: Manager
-> HostAddress
-> ChainwebVersion
-> ChainId
-> [TransactionHash]
-> TransactionHashes
-> IO (Either ClientError [Bool])
mempoolMember m a v c txh = mempoolPost m a v c "/member" txh

Expand All @@ -322,8 +338,8 @@ spv
-> ChainId
-> RequestKey
-> IO (Either ClientError T.Text)
spv m a v c rk = pactPost m a v c "/spv" $ object
spv m a v c rk = pactPost m a v c "/spv" $ J.object
[
"requestKey" .= rk
, "targetChainId" .= c
"requestKey" J..= rk
, "targetChainId" J..= c
]
14 changes: 7 additions & 7 deletions txg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ common commons
, containers
, Decimal >=0.5
, exceptions >=0.10
, fake >=0.1
, hostaddress
, http-client >=0.6
, http-client-tls >=0.3
, http-types >=0.12
, http-client
, http-client-tls
, http-types
, lens >=4.17
, mtl >=2.2
, optparse-applicative
, pact >=3.5
, pact-json
, random >=1.1
, text
, time >=1.8
Expand All @@ -54,7 +54,7 @@ common commons

library
import: commons
hs-source-dirs: lib
hs-source-dirs: lib pact
exposed-modules:
TXG.Repl
TXG.Simulate.Contracts.CoinContract
Expand All @@ -77,7 +77,7 @@ library
executable txg
import: commons
main-is: TXG.hs
hs-source-dirs: exec
hs-source-dirs: exec pact
ghc-options: -threaded -rtsopts -with-rtsopts=-N
other-modules: TXG.Types
build-depends:
Expand All @@ -99,7 +99,7 @@ executable txg
executable mempool-p2p-tester
import: commons
main-is: MPT.hs
hs-source-dirs: exec
hs-source-dirs: exec pact
ghc-options: -threaded -rtsopts -with-rtsopts=-N
other-modules:
TXG.Types
Expand Down

0 comments on commit 9616c5e

Please sign in to comment.