Skip to content

Commit

Permalink
[WIP] block authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhtz committed Apr 19, 2023
1 parent c39ad55 commit bbd3f42
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ library
, filepath >= 1.4
, ghc-compact >= 0.1
, hashable >= 1.3
, hashes >= 0.2.3
, heaps >= 0.3
, hourglass >=0.2
, http-client >= 0.5
Expand Down
5 changes: 5 additions & 0 deletions src/Chainweb/BlockHeader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import Control.Monad.Catch
import Data.Aeson
import Data.Aeson.Types (Parser)
import Data.Function (on)
import Data.Hash.SipHash
import Data.Hashable
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HS
Expand Down Expand Up @@ -820,6 +821,10 @@ _blockPow :: BlockHeader -> PowHash
_blockPow h = powHash (_blockChainwebVersion h)
$ runPutS $ encodeBlockHeaderWithoutHash h

_blockAuth :: SipKey -> BlockHeader -> SipHash
_blockAuth k h = hasByteString @SipHash k
$ runPutS $ encodeBlockHeaderWithoutHash h

blockPow :: Getter BlockHeader PowHash
blockPow = to _blockPow
{-# INLINE blockPow #-}
Expand Down
12 changes: 11 additions & 1 deletion src/Chainweb/BlockHeader/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,22 @@ powDisabled = case unsafeDupablePerformIO $ lookupEnv "DISABLE_POW_VALIDATION" o
Just{} -> True
{-# NOINLINE powDisabled #-}

blockAuthenticationKey :: Maybe SipHashKey
blockAuthenticationKey = case unsafeDupablePerformIO $ lookupEnv "BLOCK_AUTHENTICATION_KEY" of
Nothing -> False
Just{} -> True
{-# NOINLINE powDisabled #-}

prop_block_pow :: BlockHeader -> Bool
prop_block_pow b
| isGenesisBlockHeader b = True
-- Genesis block headers are not mined. So there's not need for POW
| _blockChainwebVersion b == Development && powDisabled = True
| otherwise = checkTarget (_blockTarget b) (_blockPow b)
| _blockChainwebVersion b == Development && powDisabled = True
| _blockChainwebVersion b == Mainnet01 =
checkTarget (_blockTarget b) (_blockPow b)

| Mainnet01 = checkTarget (_blockTarget b) (_blockPow b)

prop_block_hash :: BlockHeader -> Bool
prop_block_hash b = _blockHash b == computeBlockHash b
Expand Down

0 comments on commit bbd3f42

Please sign in to comment.