{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Network.Haskoin.Store.Data.KeyValue where
import Control.Monad.Reader
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Default
import Data.Hashable
import Data.Int
import Data.Serialize as S
import Data.Word
import Database.RocksDB.Query
import GHC.Generics
import Haskoin
import Network.Haskoin.Store.Data
data AddrTxKey
= AddrTxKey { addrTxKey :: !AddressTx }
| AddrTxKeyA { addrTxKeyA :: !Address }
deriving (Show, Eq, Ord, Generic, Hashable)
instance Serialize AddrTxKey
where
put AddrTxKey {addrTxKey = AddressTx { addressTxAddress = a
, addressTxBlock = b
, addressTxHash = t
}} = do
putWord8 0x05
put a
put b
put t
put AddrTxKeyA {addrTxKeyA = a} = do
putWord8 0x05
put a
get = do
guard . (== 0x05) =<< getWord8
a <- get
b <- get
t <- get
return
AddrTxKey
{ addrTxKey =
AddressTx
{ addressTxAddress = a
, addressTxBlock = b
, addressTxHash = t
}
}
instance Key AddrTxKey
instance KeyValue AddrTxKey ()
data AddrOutKey
= AddrOutKey { addrOutKeyA :: !Address
, addrOutKeyB :: !BlockRef
, addrOutKeyP :: !OutPoint }
| AddrOutKeyA { addrOutKeyA :: !Address }
deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize AddrOutKey where
put AddrOutKey {addrOutKeyA = a, addrOutKeyB = b, addrOutKeyP = p} = do
putWord8 0x06
put a
put b
put p
put AddrOutKeyA {addrOutKeyA = a} = do
putWord8 0x06
put a
get = do
guard . (== 0x06) =<< getWord8
AddrOutKey <$> get <*> get <*> get
instance Key AddrOutKey
data OutVal = OutVal
{ outValAmount :: !Word64
, outValScript :: !ByteString
} deriving (Show, Read, Eq, Ord, Generic, Hashable, Serialize)
instance KeyValue AddrOutKey OutVal
newtype TxKey = TxKey
{ txKey :: TxHash
} deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize TxKey where
put (TxKey h) = do
putWord8 0x02
put h
get = do
guard . (== 0x02) =<< getWord8
TxKey <$> get
instance Key TxKey
instance KeyValue TxKey Transaction
newtype UnspentKey
= UnspentKey { unspentKey :: OutPoint }
deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize UnspentKey where
put UnspentKey {unspentKey = k} = do
putWord8 0x09
put k
get = do
guard . (== 0x09) =<< getWord8
UnspentKey <$> get
instance Key UnspentKey
instance KeyValue UnspentKey OutVal
data MemKey
= MemKey { memTime :: !PreciseUnixTime, memKey :: !TxHash }
| MemKeyS
deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize MemKey where
put (MemKey t h) = do
putWord8 0x07
put t
put h
put MemKeyS = putWord8 0x07
get = do
guard . (== 0x07) =<< getWord8
MemKey <$> get <*> get
instance Key MemKey
instance KeyValue MemKey ()
newtype BlockKey = BlockKey
{ blockKey :: BlockHash
} deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize BlockKey where
put (BlockKey h) = do
putWord8 0x01
put h
get = do
guard . (== 0x01) =<< getWord8
BlockKey <$> get
instance KeyValue BlockKey BlockData
newtype HeightKey = HeightKey
{ heightKey :: BlockHeight
} deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize HeightKey where
put (HeightKey height) = do
putWord8 0x03
put height
get = do
guard . (== 0x03) =<< getWord8
HeightKey <$> get
instance Key HeightKey
instance KeyValue HeightKey [BlockHash]
newtype BalKey
= BalKey { balanceKey :: Address }
deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize BalKey where
put BalKey {balanceKey = a} = do
putWord8 0x04
put a
get = do
guard . (== 0x04) =<< getWord8
BalKey <$> get
instance Key BalKey
data BalVal = BalVal
{ balValAmount :: !Word64
, balValZero :: !Int64
, balValCount :: !Word64
} deriving (Show, Read, Eq, Ord, Generic, Hashable, Serialize)
instance Default BalVal where
def = BalVal {balValAmount = 0, balValZero = 0, balValCount = 0}
instance KeyValue BalKey BalVal
data BestKey =
BestKey
deriving (Show, Read, Eq, Ord, Generic, Hashable)
instance Serialize BestKey where
put BestKey = put (B.replicate 32 0x00)
get = do
guard . (== B.replicate 32 0x00) =<< getBytes 32
return BestKey
instance Key BestKey
instance KeyValue BestKey BlockHash
data VersionKey =
VersionKey
deriving (Eq, Show, Read, Ord, Generic, Hashable)
instance Serialize VersionKey where
put VersionKey = putWord8 0x0a
get = do
guard . (== 0x0a) =<< getWord8
return VersionKey
instance Key VersionKey
instance KeyValue VersionKey Word32