Skip to content
Permalink
Browse files

[#356] Remove AddrStakeDistribution

  • Loading branch information...
ruhatch committed Mar 15, 2019
1 parent a88428b commit f4a95848b7ba22d3a2f2cf5b8dad6507b0530687
@@ -46,7 +46,6 @@ library

Cardano.Chain.Common.AddrAttributes
Cardano.Chain.Common.AddrSpendingData
Cardano.Chain.Common.AddrStakeDistribution
Cardano.Chain.Common.Address
Cardano.Chain.Common.AddressHash
Cardano.Chain.Common.Attributes
@@ -7,7 +7,6 @@ import Cardano.Chain.Common.AddrAttributes as X
import Cardano.Chain.Common.Address as X
import Cardano.Chain.Common.AddressHash as X
import Cardano.Chain.Common.AddrSpendingData as X
import Cardano.Chain.Common.AddrStakeDistribution as X
import Cardano.Chain.Common.Attributes as X
import Cardano.Chain.Common.BlockCount as X
import Cardano.Chain.Common.ChainDifficulty as X
@@ -1,6 +1,7 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Chain.Common.AddrAttributes
( AddrAttributes(..)
@@ -10,47 +11,39 @@ where
import Cardano.Prelude

import Data.Text.Lazy.Builder (Builder)
import Formatting (bprint, build, builder)
import Formatting (bprint, builder)
import qualified Formatting.Buildable as B

import Cardano.Binary.Class (Bi, decode, encode)
import qualified Cardano.Binary.Class as Bi
import Cardano.Chain.Common.AddrStakeDistribution (AddrStakeDistribution(..))
import Cardano.Chain.Common.Attributes
(Attributes(..), decodeAttributes, encodeAttributes)
import Cardano.Crypto.HD (HDAddressPayload)

-- | Additional information stored along with address. It's intended
-- to be put into 'Attributes' data type to make it extensible with
-- softfork.
data AddrAttributes = AddrAttributes
{ aaPkDerivationPath :: !(Maybe HDAddressPayload)
, aaStakeDistribution :: !AddrStakeDistribution
} deriving (Eq, Ord, Show, Generic)
newtype AddrAttributes = AddrAttributes
{ aaPkDerivationPath :: Maybe HDAddressPayload
} deriving (Eq, Ord, Show, Generic, NFData, HeapWords)

instance B.Buildable AddrAttributes where
build aa = bprint
("AddrAttributes { stake distribution: ".build.
", derivation path: ".builder.
" }")
(aaStakeDistribution aa)
derivationPathBuilder
where
derivationPathBuilder :: Builder
derivationPathBuilder = case aaPkDerivationPath aa of
Nothing -> "{}"
Just _ -> "{path is encrypted}"

instance NFData AddrAttributes
build aa = bprint
("AddrAttributes { derivation path: " . builder . " }")
derivationPathBuilder
where
derivationPathBuilder :: Builder
derivationPathBuilder = case aaPkDerivationPath aa of
Nothing -> "{}"
Just _ -> "{path is encrypted}"

{- NOTE: Address attributes serialization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Attributes' are conceptually a map, where keys are numbers ('Word8').
For address there are two attributes:
• 0 — stake distribution, defaults to 'BootstrapEraDistr';
• 1 — derivation path, defaults to 'Nothing'.
- 1 - derivation path, defaults to 'Nothing'.
-}

@@ -61,16 +54,9 @@ instance Bi (Attributes AddrAttributes) where
-- toStrict call.
-- Also consider using a custom builder strategy; serialized attributes are
-- probably small, right?
encode attrs@(Attributes { attrData = AddrAttributes derivationPath stakeDistr })
= encodeAttributes listWithIndices attrs
encode attrs@Attributes { attrData = AddrAttributes derivationPath } =
encodeAttributes derivationPathListWithIndices attrs
where
listWithIndices :: [(Word8, AddrAttributes -> LByteString)]
listWithIndices =
stakeDistributionListWithIndices <> derivationPathListWithIndices
stakeDistributionListWithIndices :: [(Word8, AddrAttributes -> LByteString)]
stakeDistributionListWithIndices = case stakeDistr of
BootstrapEraDistr -> []
_ -> [(0, Bi.serialize . aaStakeDistribution)]
derivationPathListWithIndices :: [(Word8, AddrAttributes -> LByteString)]
derivationPathListWithIndices = case derivationPath of
Nothing -> []
@@ -83,22 +69,13 @@ instance Bi (Attributes AddrAttributes) where

decode = decodeAttributes initValue go
where
initValue = AddrAttributes
{ aaPkDerivationPath = Nothing
, aaStakeDistribution = BootstrapEraDistr
}
initValue = AddrAttributes {aaPkDerivationPath = Nothing}
go
:: Word8
-> LByteString
-> AddrAttributes
-> Bi.Decoder s (Maybe AddrAttributes)
go n v acc = case n of
0 -> (\distr -> Just $ acc { aaStakeDistribution = distr })
<$> Bi.deserialize v
1 -> (\deriv -> Just $ acc { aaPkDerivationPath = Just deriv })
<$> Bi.deserialize v
_ -> pure Nothing

instance HeapWords AddrAttributes where
heapWords (AddrAttributes derivationPath stakeDistr) =
heapWords2 derivationPath stakeDistr

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit f4a9584

Please sign in to comment.
You can’t perform that action at this time.