-
Notifications
You must be signed in to change notification settings - Fork 631
[release/1.3.1] [CO-354] Introduce NetworkMagic
#3558
[release/1.3.1] [CO-354] Introduce NetworkMagic
#3558
Conversation
N.B. this PR contains commits which are part of PR #3556. Thus, the "Files Changed" is inflated, and if someone reviews this before #3556 is merged, it would be simplest to review only the "Introduce EDIT: |
core/src/Pos/Core/NetworkMagic.hs
Outdated
b2 = fromIntegral $ shift (shift w 8) (-24) | ||
b3 = fromIntegral $ shift (shift w 16) (-24) | ||
b4 = fromIntegral $ shift (shift w 24) (-24) | ||
in b1 + b2 + b3 + b4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make a decision about whether we should use a proper hash function instead of this ad-hoc checksum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mhuesch I'm in agreement with using a standard hash or checksum function.
1ba4152
to
dcbaef3
Compare
638ec44
to
d08468f
Compare
d08468f
to
8b43db7
Compare
@@ -272,3 +273,6 @@ sendTxsFromFile diffusion txsFile = do | |||
(topsortTxAuxes txAuxes) | |||
let submitOne = submitTxRaw diffusion | |||
mapM_ submitOne sortedTxAuxes | |||
|
|||
fixedNM :: NetworkMagic | |||
fixedNM = NMNothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this term repeated instead of defined once and imported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is removed in a subsequent PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@parsonsmatt this was defined multiple times so that we could more easily track which files had the hardcoded variable. This outlined the frontier where we needed configuration passed to.
They were removed in #3561
-- mhueschen: although it makes no sense for an identifier to have a sign, we're | ||
-- opting to maintain consistency with `ProtocolMagicId`, rather than risk subtle | ||
-- conversion bugs. | ||
data NetworkMagic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like this type name. What is Magic
? What does it mean, what does it do, why do we care about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A constant numerical or text value used to identify a file format or protocol;
It's the magic number which identifies a blockchain. I'd say a better name would be BlockchainMagic
, since "network" has other connotations. It has nothing to do with the communication network.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open to modifying this. NetworkMagic
was a suggestion from @dcoutts which we ran with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. NetworkMagic
is probably the best name for it.
@@ -253,6 +254,9 @@ instance Arbitrary AddrStakeDistribution where | |||
CoinPortion <$> choose (1, max 1 (limit - 1)) | |||
genPortions (n - 1) (portion : res) | |||
|
|||
instance Arbitrary NetworkMagic where | |||
arbitrary = oneof [pure NMNothing, NMJust <$> arbitrary] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will use the arbitrary
of Int32
which permits negative values. Is that what we want here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type should probably be Word32
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProtocolMagic
uses Int32, which I agree is a bad choice and should be Word32
. I'm fairly sure that fromIntegral
will do the conversion we want, but I also worry that we could be bitten by mixing Word32
/Int32
types in ProtocolMagic
and NetworkMagic
.
|
||
-- mhueschen: although it makes no sense for an identifier to have a sign, we're | ||
-- opting to maintain consistency with `ProtocolMagicId`, rather than risk subtle | ||
-- conversion bugs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be as correct as possible and provide small locally safe functions for conversion. In this case, providing NMJust !Word32
would be Right, and we could have a function ProtocolMagicId -> NetworkMagic
that handles it correctly.
Alternatively, a smart constructor like x :: Int32 -> Maybe NetworkMagic
that fails on < 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we want negative values. The type of ProtocolMagic
's identifier should really be a Word32
, but for historical reasons is an Int32
. The point of the identifier is to disambiguate networks, so negative values also serve that purpose.
} | ||
let addrType = ATPubKey | ||
return Address {..} | ||
|
||
instance HasProtocolConstants => ArbitraryUnsafe SlotId where | ||
arbitraryUnsafe = SlotId <$> arbitraryUnsafe <*> arbitraryUnsafe | ||
|
||
instance ArbitraryUnsafe NetworkMagic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also have negative ints in the field due to generic deriving.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #3558 (comment) - I think this is OK.
SharedSeed (..), SlotLeaders, StakeholderId, StakesList, | ||
TxFeePolicy (..), TxSizeLinear (..), addressHash, | ||
coinPortionDenominator, makeAddress, makePubKeyAddress, | ||
mkMultiKeyDistr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stylish-haskell config is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is against the release/1.3.1
branch, so the stylish-haskell rules are the old ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ what Erik said. It bothered me too, but I didn't want to reformat all of the imports. This particular modification occurred when @intricate moved code from develop
to release/1.3.1
without re-running stylish-haskell in #3540. I added a intermediate commit to correct the style, so it's consistent with release/1.3.1
.
|
||
|
||
fixedNM :: NetworkMagic | ||
fixedNM = NMNothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this should definitely be relocated with the type definition as an alias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import Test.Pos.Util.Golden (discoverGolden, eachOf, goldenTestCanonicalJSONDec, | ||
goldenTestJSON, goldenTestJSONDec) | ||
import Test.Pos.Util.Tripping (discoverRoundTrip, roundTripsAesonBuildable, | ||
roundTripsAesonShow, roundTripsCanonicalJSONShow) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to fix your stylish-haskell config :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
core/src/Pos/Core/NetworkMagic.hs
Outdated
-- conversion bugs. | ||
data NetworkMagic | ||
= NMNothing | ||
| NMJust !Int32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be Word32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. However I'm a little scared about making ProtocolMagic
and NetworkMagic
have different types. #3558 (comment)
Thanks for the clarifications; PR approved :) |
@parsonsmatt thanks for the review! |
Building on the previous commit, we add the `NetworkMagic` datatype, which allows us to discriminate addresses originating from different networks. In order to make this commit as simple and easy-to-review as possible, I stub out the added arguments of type `NetworkMagic` with a dummy variable: `fixedNM`. This puts of the (fairly involved) work of threading `NetworkMagic` values from the nearest occurrence of `ProtocolMagic` to where said values are needed. The remainder of the work will come in a separate commit. --- Merged with: [CO-354] Make use of Blake2b_256 for constructing NetworkMagic [CO-354] NetworkMagic: use Int32 rather than Word8 We opt to keep the full 32bits of the ProtocolMagicId, instead of truncating to 8 bits. This reduces the probability of collisions. While we would ideally use a Word32 (since sign has no meaning in identifier-space), we're stuck using Int32 because that is how ProtocolMagicId is defined. --- Co-authored-by: Michael Hueschen <[email protected]> Co-authored-by: Luke Nadur <[email protected]>
8b43db7
to
b12682e
Compare
Description
Building on the previous commit, we add the
NetworkMagic
datatype,which allows us to discriminate addresses originating from different
networks.
In order to make this commit as simple and easy-to-review as possible,
I stub out the added arguments of type
NetworkMagic
with a dummyvariable:
fixedNM
. This puts off the (fairly involved) work ofthreading
NetworkMagic
values from the nearest occurrence ofProtocolMagic
to where said values are needed.The remainder of the work will come in a separate commit.
Linked issue
https://iohk.myjetbrains.com/youtrack/issue/CO-354
Type of change
Developer checklist
^ there is no 1.3.1 section in the CHANGELOG, and this is dev work on a release branch, so I'm skipping this.
Testing checklist
^ no tests were added because these changes introduce new capabilities which are not used yet. Thus the behavior is the same, and the existing tests should be adequate.