Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…askell

Conflicts:
	exec_src/Main.hs
	src/Blockchain/Mining.hs
  • Loading branch information
jim committed May 21, 2015
2 parents 6133500 + 1e3f502 commit 61d2c49
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
5 changes: 3 additions & 2 deletions ethereum-client-haskell.cabal
Expand Up @@ -37,7 +37,7 @@ executable ethereumH
, directory
, either
, entropy
, ethereum-data-leveldb
, ethereum-data-sql
, ethereum-encryption
, ethereum-merkle-patricia-db
, ethereum-rlp
Expand All @@ -54,6 +54,7 @@ executable ethereumH
, transformers
, vector
, ansi-wl-pprint
, hminer
, ethereum-client-haskell
main-is: Main.hs
C-sources: fastNonceFinder/nonceFinder.c
Expand All @@ -75,7 +76,7 @@ library
, directory
, either
, entropy
, ethereum-data-leveldb
, ethereum-data-sql
, ethereum-encryption
, ethereum-merkle-patricia-db
, ethereum-rlp
Expand Down
71 changes: 50 additions & 21 deletions exec_src/Main.hs
Expand Up @@ -12,16 +12,16 @@ import Crypto.Types.PubKey.ECC
import Crypto.Random
import qualified Data.ByteString as B
import Data.Time.Clock
import Network.Haskoin.Crypto hiding (Address)
import qualified Network.Haskoin.Internals as H
import Numeric
import System.Entropy
import System.Environment
import System.IO

import Blockchain.Frame
import Blockchain.UDP hiding (Ping, Pong)
import Blockchain.UDP hiding (Ping,Pong)
import Blockchain.RLPx

import Blockchain.Data.RLP
import Blockchain.BlockChain
import Blockchain.BlockSynchronizer
import Blockchain.Communication
Expand All @@ -43,11 +43,16 @@ import Blockchain.PeerUrls
import Blockchain.SHA
--import Blockchain.SigningTools
import Blockchain.Util

import qualified Data.ByteString.Base16 as B16
--import Debug.Trace

prvKey::PrvKey
Just prvKey = makePrvKey 0xac3e8ce2ef31c3f45d5da860bcd9aee4b37a05c5a3ddee40dd061620c3dab380
import Data.Word
import Data.Bits
import Data.Maybe
import Cache

prvKey::H.PrvKey
Just prvKey = H.makePrvKey 0xac3e8ce2ef31c3f45d5da860bcd9aee4b37a05c5a3ddee40dd061620c3dab380

getNextBlock::Block->UTCTime->ContextM Block
getNextBlock b ts = do
Expand Down Expand Up @@ -156,14 +161,16 @@ readAndOutput = do
mkHello::Point->IO Message
mkHello peerId = do
--let peerId = B.replicate 64 0xFF -- getEntropy 64
return Hello {
version = 3,
let hello = Hello {
version = 4,
clientId = "Ethereum(G)/v0.6.4//linux/Haskell",
capability = [ETH ethVersion], -- , SHH shhVersion],
port = 30303,
port = 0,
nodeId = peerId
}

-- putStrLn $ show $ wireMessage2Obj hello
-- putStrLn $ show $ rlpSerialize $ snd (wireMessage2Obj hello)
return hello
{-
createTransaction::Transaction->ContextM SignedTransaction
createTransaction t = do
Expand All @@ -177,6 +184,13 @@ createTransactions transactions = do
liftIO $ withSource devURandom $ signTransaction prvKey t{tNonce=n}
-}

intToBytes::Integer->[Word8]
intToBytes x = map (fromIntegral . (x `shiftR`)) [256-8, 256-16..0]

pointToBytes::Point->[Word8]
pointToBytes (Point x y) = intToBytes x ++ intToBytes y
pointToBytes PointO = error "pointToBytes got value PointO, I don't know what to do here"

doit::EthCryptM ContextM ()
doit = do
liftIO $ putStrLn "Connected"
Expand Down Expand Up @@ -211,14 +225,21 @@ theCurve::Curve
theCurve = getCurveByName SEC_p256k1


hPubKeyToPubKey::H.PubKey->Point
hPubKeyToPubKey (H.PubKey hPoint) = Point (fromIntegral x) (fromIntegral y)
where
x = fromMaybe (error "getX failed in prvKey2Address") $ H.getX hPoint
y = fromMaybe (error "getY failed in prvKey2Address") $ H.getY hPoint
hPubKeyToPubKey (H.PubKeyU _) = error "PubKeyU not supported in hPubKeyToPUbKey yet"

main::IO ()
main = do
args <- getArgs

let serverNum =
case args of
(arg:_) -> arg
[] -> "1" --Just default to poc-8.ethdev.com
[] -> "1" --Just default to poc-9.ethdev.com

let (ipAddress, thePort) = ipAddresses !! read serverNum

Expand All @@ -227,24 +248,32 @@ main = do
let g = cprgCreate entropyPool :: SystemRNG
(myPriv, _) = generatePrivate g $ getCurveByName SEC_p256k1

putStrLn $ "Attempting to connect to " ++ show ipAddress ++ ":" ++ show thePort
let myPublic = calculatePublic theCurve myPriv
-- putStrLn $ "my pubkey is: " ++ show myPublic
putStrLn $ "my pubkey is: " ++ (show $ B16.encode $ B.pack $ pointToBytes myPublic)

liftIO $ putStrLn $ "Attempting to connect to " ++ show ipAddress ++ ":" ++ show thePort

putStr "Obtaining server public key.... "
hFlush stdout
-- putStrLn $ "my UDP pubkey is: " ++ (show $ H.derivePubKey $ prvKey)
putStrLn $ "my NodeID is: " ++ (show $ B16.encode $ B.pack $ pointToBytes $ hPubKeyToPubKey $ H.derivePubKey prvKey)

otherPubKey@(Point x y) <- liftIO $ getServerPubKey ipAddress thePort
putStrLn "Obtained"

putStrLn $ "server public key is: " ++ showHex x "" ++ showHex y ""

cxt <- initContext "h"
-- putStrLn $ "server public key is : " ++ (show otherPubKey)
putStrLn $ "server public key is : " ++ (show $ B16.encode $ B.pack $ pointToBytes otherPubKey)

cch <- mkCache 1024 "seed"

runResourceT $ do
dbs <- openDBs "h"
_ <- flip runStateT dbs $
flip runStateT cxt $
cxt <- openDBs "h"
_ <- flip runStateT cxt $
flip runStateT (Context [] 0 [] cch False) $
runEthCryptM myPriv otherPubKey ipAddress (fromIntegral thePort) $ do
let myPublic = calculatePublic theCurve myPriv


sendMsg =<< liftIO (mkHello myPublic)

doit
return ()

10 changes: 9 additions & 1 deletion src/Blockchain/BlockChain.hs
Expand Up @@ -76,6 +76,12 @@ nextDifficulty oldDifficulty oldTime newTime = max nextDiff' minimumDifficulty
nextGasLimit::Integer->Integer->Integer
nextGasLimit oldGasLimit oldGasUsed = max (max 125000 3141592) ((oldGasLimit * 1023 + oldGasUsed *6 `quot` 5) `quot` 1024)

nextGasLimitDelta::Integer->Integer
nextGasLimitDelta oldGasLimit = oldGasLimit `div` 1024

minGasLimit::Integer
minGasLimit = 125000

checkUnclesHash::Block->Bool
checkUnclesHash b = blockDataUnclesHash (blockBlockData b) == hash (rlpSerialize $ RLPArray (rlpEncode <$> blockBlockUncles b))

Expand All @@ -99,7 +105,9 @@ checkParentChildValidity Block{blockBlockData=c} Block{blockBlockData=p} = do
$ fail $ "Block difficulty is wrong: got '" ++ show (blockDataDifficulty c) ++ "', expected '" ++ show (nextDifficulty (blockDataDifficulty p) (blockDataTimestamp p) (blockDataTimestamp c)) ++ "'"
unless (blockDataNumber c == blockDataNumber p + 1)
$ fail $ "Block number is wrong: got '" ++ show (blockDataNumber c) ++ ", expected '" ++ show (blockDataNumber p + 1) ++ "'"
unless (blockDataGasLimit c == nextGasLimit (blockDataGasLimit p) (blockDataGasUsed p))
unless ((blockDataGasLimit c <= (blockDataGasLimit p) + (nextGasLimitDelta (blockDataGasLimit p)))
&& (blockDataGasLimit c >= ((blockDataGasLimit p) - (nextGasLimitDelta (blockDataGasLimit p))))
&& (blockDataGasLimit c >= minGasLimit))
$ fail $ "Block gasLimit is wrong: got '" ++ show (blockDataGasLimit c) ++ "', expected '" ++ show (nextGasLimit (blockDataGasLimit p) (blockDataGasUsed p)) ++ "'"
return ()

Expand Down
2 changes: 1 addition & 1 deletion src/Blockchain/Data/Wire.hs
Expand Up @@ -236,7 +236,7 @@ wireMessage2Obj Hello { version = ver,
capability = cap,
port = p,
nodeId = nId } =
(0x0, RLPArray [
(128, RLPArray [
rlpEncode $ toInteger ver,
rlpEncode cId,
RLPArray $ rlpEncode <$> cap,
Expand Down
3 changes: 1 addition & 2 deletions src/Blockchain/Mining.hs
@@ -1,6 +1,6 @@

module Blockchain.Mining (
nonceIsValid'
-- nonceIsValid'
) where


Expand All @@ -21,7 +21,6 @@ import Hashimoto

import Debug.Trace


powFunc'::Cache->Block->IO Integer
powFunc' cache b =
--trace (show $ headerHashWithoutNonce b) $
Expand Down

0 comments on commit 61d2c49

Please sign in to comment.