Skip to content

Commit

Permalink
Add cardano-node-capi
Browse files Browse the repository at this point in the history
This adds a basic foreign export library for the node. Providing
the `void runNode(int argc, char ** argv)` as the c entry point.

Arguments are parsed the same as the command line cardano-node.
  • Loading branch information
angerman committed Jan 18, 2022
1 parent 814df2c commit 17ff2c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions cabal.project
Expand Up @@ -5,6 +5,7 @@ packages:
cardano-cli
cardano-client-demo
cardano-node
cardano-node-capi
cardano-node-chairman
cardano-submit-api
cardano-testnet
Expand Down
5 changes: 5 additions & 0 deletions cardano-node-capi/CHANGELOG.md
@@ -0,0 +1,5 @@
# Revision history for cardano-node-c

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
19 changes: 19 additions & 0 deletions cardano-node-capi/cardano-node-capi.cabal
@@ -0,0 +1,19 @@
cabal-version: 3.0

name: cardano-node-capi
version: 0.1.0.0
description: ffi c library around the full node
author: IOHK
maintainer: operations@iohk.io

extra-source-files: CHANGELOG.md

library
exposed-modules: Node
build-depends: base
, aeson
, bytestring
, cardano-node
, optparse-applicative-fork
hs-source-dirs: src
default-language: Haskell2010
35 changes: 35 additions & 0 deletions cardano-node-capi/src/Node.hs
@@ -0,0 +1,35 @@
module Node where

import Data.Aeson (eitherDecodeStrict)
import Cardano.Node.Run (runNode)

import Foreign.Ptr (Ptr)
import Foreign.C (CString, peekCString)
import Foreign.Marshal.Array (peekArray)
import Data.ByteString.Char8 (pack)

import System.IO (hSetBuffering, BufferMode (LineBuffering), stdout)

import Options.Applicative
import Cardano.Node.Parsers (nodeCLIParser, parserHelpHeader, parserHelpOptions,
renderHelpDoc)

-- allow setting stdout to linebuffering if needed
foreign export ccall setLineBuffering :: IO ()
setLineBuffering :: IO ()
setLineBuffering = hSetBuffering stdout LineBuffering

-- | @crunNode@ is an exported C entry point to start a node.
-- We parese the same arguments as the node CLI, but allow to
-- pass the argumnets as @char *argv[]@ from C.
foreign export ccall "runNode" crunNode :: Int -> Ptr CString -> IO ()
crunNode :: Int -> Ptr CString -> IO ()
crunNode argc argv = peekArray argc argv >>= mapM peekCString >>= \args ->
case execParserPure pref opts args of
Success pnc -> runNode pnc
Failure f -> print f
CompletionInvoked _ -> putStrLn "Completion Invoked?"
where
pref = prefs showHelpOnEmpty
opts = info nodeCLIParser
( fullDesc <> progDesc "Start node of the Cardano blockchain." )

0 comments on commit 17ff2c7

Please sign in to comment.