Skip to content

Commit

Permalink
upload genesis utxo to db
Browse files Browse the repository at this point in the history
  • Loading branch information
sadMaxim committed Nov 24, 2023
1 parent e6929d7 commit c53343e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 33 deletions.
26 changes: 4 additions & 22 deletions spammer/spammer/exe/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,14 @@ import Effect.Exception (throw)
import Node.Encoding (Encoding(..))
import Node.FS.Sync (readFile, readTextFile)
import Spammer.Db (executeQuery)

type KeyFile = {
type :: String,
description :: String,
cborHex :: String
}
import Spammer.Types (KeyFile)
import Spammer.Start (startSpammer)



main :: Effect Unit
main = do
-- log "hi"
utxo1_skey <- readTextFile UTF8 "/home/maxim/work/projects/congested-testnet/cardano-conf/utxo-keys/utxo1.skey"
utxo1_pkey <- readTextFile UTF8 "/home/maxim/work/projects/congested-testnet/cardano-conf/utxo-keys/utxo1.vkey"
let
json :: Either JsonDecodeError KeyFile
json = parseJson utxo1_skey >>= decodeJson
key <- case json of
Left e -> throw "decode error"
Right key -> pure key

log $ key.description
launchAff_ do
res <- executeQuery "select * from pkeys;"
-- h <- liftMaybe (error "no array") (head res)
log $ show $ Data.Array.null res
-- log "hi"
startSpammer



1 change: 1 addition & 0 deletions spammer/spammer/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You can edit this file as you like.
, "arrays"
, "aeson"
, "argonaut"
, "strings"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "exe/**/*.purs", "test/**/*.purs" ]
Expand Down
7 changes: 0 additions & 7 deletions spammer/spammer/src/Db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ exports._executeQuery = function (queryString) {
})
.then(result => {
console.log(result.rows);
// console.log(result.rows.length);
// onSuccess([{"one" : 1}])
onSuccess(result.rows)
})
.catch(err => {
Expand All @@ -30,9 +28,4 @@ exports._executeQuery = function (queryString) {
}
};

// exports._executeQuery = function (queryString) {
// return function (onError, onSuccess) {
// onSuccess([{"one":1}]);
// };
// };

9 changes: 5 additions & 4 deletions spammer/spammer/src/Db.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Spammer.Db (executeQuery, KeysRow) where
module Spammer.Db (executeQuery) where

import Contract.Prelude

import Control.Promise (Promise, toAffE)
import Data.Argonaut (Json)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)

type KeysRow = {pkey :: String, pubkey :: String}
type Query = String

foreign import _executeQuery :: String -> EffectFnAff (Array KeysRow)
foreign import _executeQuery :: Query -> EffectFnAff Json

executeQuery :: String -> Aff (Array KeysRow)
executeQuery :: Query -> Aff Json
executeQuery = fromEffectFnAff <<< _executeQuery
67 changes: 67 additions & 0 deletions spammer/spammer/src/Start.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Spammer.Start (startSpammer) where

import Contract.Prelude

import Aeson (JsonDecodeError)
import Contract.Monad (launchAff_)
import Data.Argonaut (class DecodeJson, decodeJson, parseJson, printJsonDecodeError)
import Data.String (drop)
import Effect.Exception (throw)
import Node.Encoding (Encoding(..))
import Node.FS.Sync (readTextFile)
import Spammer.Db (executeQuery)
import Spammer.Types (KeyFile, CborHex)



startSpammer :: Effect Unit
startSpammer = do
loadGenesisUtxoKeysDb

loadGenesisUtxoKeysDb :: Effect Unit
loadGenesisUtxoKeysDb = do
let
dir = "/home/maxim/work/projects/congested-testnet/cardano-conf/utxo-keys/"
utxo1_skey_file<- readTextFile UTF8 (dir <> "utxo1.skey")
utxo1_pkey_file <- readTextFile UTF8 (dir <> "utxo1.vkey")
utxo2_skey_file <- readTextFile UTF8 (dir <> "utxo2.skey")
utxo2_pkey_file <- readTextFile UTF8 (dir <> "utxo2.vkey")
utxo1_skey_hex <- liftJsonDecodeError (parseKeyFromJsonString utxo1_skey_file)
utxo1_pkey_hex <- liftJsonDecodeError (parseKeyFromJsonString utxo1_pkey_file)
utxo2_skey_hex <- liftJsonDecodeError (parseKeyFromJsonString utxo2_skey_file)
utxo2_pkey_hex <- liftJsonDecodeError (parseKeyFromJsonString utxo2_pkey_file)
let
query = "INSERT INTO pkeys (pkey, pubkey) VALUES " <>
"('" <> utxo1_skey_hex <> "', '" <> utxo1_pkey_hex <> "'), " <>
"('" <> utxo2_skey_hex <> "', '" <> utxo2_pkey_hex <> "') " <>
"ON CONFLICT (pkey) DO NOTHING;"
launchAff_ do
executeQuery query







parseKeyFromJsonString :: String -> Either JsonDecodeError CborHex
parseKeyFromJsonString jsonString = do
keyFile :: KeyFile <- parseJson jsonString >>= decodeJson
let
cborHex = keyFile.cborHex
cborHexWithoutHeader = drop 4 cborHex
pure cborHexWithoutHeader


liftJsonDecodeError :: forall a. DecodeJson a => Either JsonDecodeError a -> Effect a
liftJsonDecodeError eitherErrA = do
case eitherErrA of
Left e -> throw $ printJsonDecodeError e
Right x -> pure x







10 changes: 10 additions & 0 deletions spammer/spammer/src/Types.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Spammer.Types where

-- | Typical key file format
type KeyFile = {
type :: String,
description :: String,
cborHex :: String
}

type CborHex = String

0 comments on commit c53343e

Please sign in to comment.