blockchain-0.0.3: Generic blockchain implementation.

Safe HaskellNone
LanguageHaskell2010

Data.Blockchain.Types

Contents

Synopsis

Documentation

data Blockchain a Source #

Core blockchain data type. Uses a validation tag to declare if it is known to abide by expected blockchain rules. Will be either Blockchain Validated or Blockchain Unvalidated.

Note: both Blockchain Validated and Blockchain Unvalidated can be serialized to json, while only Blockchain Unvalidated can be deserialized from json.

Construction

construct :: BlockchainConfig -> BlockchainNode -> Blockchain Unvalidated Source #

Constructs an unvalidated blockchain from a config and a node. Allows arbitrary blockchains to be constructed. However, blockchains are generally not useful until validated.

validate :: Blockchain Unvalidated -> Either ValidationException (Blockchain Validated) Source #

Validates a blockchain. Returns a ValidationException if provided blockchain does not meet expected rules.

unvalidate :: Blockchain Validated -> Blockchain Unvalidated Source #

Isomorphic - useful for sending api responses

addBlock :: Block -> Blockchain Validated -> Either BlockException (Blockchain Validated) Source #

Adds a block to a validated blockchain. Returns a BlockException if block is not able to be inserted into the blockchain.

Validation

Chain inspection

data BlockHeader Source #

Instances

Eq BlockHeader Source # 
Show BlockHeader Source # 
Generic BlockHeader Source # 

Associated Types

type Rep BlockHeader :: * -> * #

ToJSON BlockHeader Source # 
FromJSON BlockHeader Source # 
ToHash BlockHeader Source # 
type Rep BlockHeader Source # 

data BlockchainConfig Source #

Instances

Eq BlockchainConfig Source # 
Show BlockchainConfig Source # 
Generic BlockchainConfig Source # 
ToJSON BlockchainConfig Source # 
FromJSON BlockchainConfig Source # 
type Rep BlockchainConfig Source # 
type Rep BlockchainConfig = D1 (MetaData "BlockchainConfig" "Data.Blockchain.Types.BlockchainConfig" "blockchain-0.0.3-B6M8cyvqobVDH0prNNm9JF" False) (C1 (MetaCons "BlockchainConfig" PrefixI True) ((:*:) ((:*:) (S1 (MetaSel (Just Symbol "initialDifficulty") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Difficulty)) ((:*:) (S1 (MetaSel (Just Symbol "difficulty1Target") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Hex256)) (S1 (MetaSel (Just Symbol "targetSecondsPerBlock") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Word)))) ((:*:) (S1 (MetaSel (Just Symbol "difficultyRecalculationHeight") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Word)) ((:*:) (S1 (MetaSel (Just Symbol "initialMiningReward") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Word)) (S1 (MetaSel (Just Symbol "miningRewardHalvingHeight") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Word))))))

defaultConfig :: BlockchainConfig Source #

A reasonable default config to use for testing. Mines blocks quickly and changes difficulty and rewards frequently. Note: reward will go to zero after 1100 blocks, which will take about 180 minutes of mining.

defaultConfig :: BlockchainConfig
defaultConfig = BlockchainConfig
    { initialDifficulty             = Difficulty 1
    , difficulty1Target             = hex256LeadingZeros 4
    , targetSecondsPerBlock         = 10
    , difficultyRecalculationHeight = 50
    , initialMiningReward           = 1024
    , miningRewardHalvingHeight     = 100
    }

targetReward :: BlockchainConfig -> Word -> Word Source #

Calculates the target reward for a blockchain. Uses the longest chain.

targetDifficulty :: BlockchainConfig -> [Block] -> Difficulty Source #

Calculates the target difficulty for a blockchain. Uses the longest chain.

newtype Difficulty Source #

Constructors

Difficulty 

Instances

Bounded Difficulty Source # 
Enum Difficulty Source # 
Eq Difficulty Source # 
Integral Difficulty Source # 
Num Difficulty Source # 
Ord Difficulty Source # 
Real Difficulty Source # 
Show Difficulty Source # 
ToJSON Difficulty Source # 
FromJSON Difficulty Source # 

newtype Hex256 Source #

Constructors

Hex256 

Fields

Instances

Bounded Hex256 Source # 
Enum Hex256 Source # 
Eq Hex256 Source # 

Methods

(==) :: Hex256 -> Hex256 -> Bool #

(/=) :: Hex256 -> Hex256 -> Bool #

Integral Hex256 Source # 
Num Hex256 Source # 
Ord Hex256 Source # 
Real Hex256 Source # 
Show Hex256 Source # 
ToJSON Hex256 Source # 
FromJSON Hex256 Source # 

hex256LeadingZeros :: Word -> Hex256 Source #

Create a Hex256 value with the specificed amount of leading zeros. Useful for creating a difficulty1Target when creating a blockchain.

>>> hex256LeadingZeros 4
0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

newtype CoinbaseTransaction Source #

data TransactionIn Source #

Constructors

TransactionIn 

Fields

data TransactionOutRef Source #

Pointer to a specific TransactionOut

Instances

Eq TransactionOutRef Source # 
Show TransactionOutRef Source # 
Generic TransactionOutRef Source # 
Hashable TransactionOutRef Source # 
ToJSON TransactionOutRef Source # 
FromJSON TransactionOutRef Source # 
type Rep TransactionOutRef Source # 
type Rep TransactionOutRef = D1 (MetaData "TransactionOutRef" "Data.Blockchain.Types.Transaction" "blockchain-0.0.3-B6M8cyvqobVDH0prNNm9JF" False) (C1 (MetaCons "TransactionOutRef" PrefixI True) ((:*:) (S1 (MetaSel (Just Symbol "transactionHash") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Either (Hash CoinbaseTransaction) (Hash Transaction)))) (S1 (MetaSel (Just Symbol "transactionOutIndex") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Word))))

data TransactionOut Source #

Constructors

TransactionOut 

Fields