Skip to content

Commit

Permalink
Make block hash/tx sig independent from locale
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Dec 17, 2019
1 parent 1db7583 commit 661dff5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
13 changes: 11 additions & 2 deletions CHANGES.md
Expand Up @@ -95,6 +95,14 @@ To be released.
- Fixed a bug where `BlockChain<T>.GetNextTxNonce()` had returned invalid tx
nonce. [[#718]]
- Fixed a bug where mined transactions were staged again. [[#719]]
- Fixed a bug where `Block<T>.Hash` property, `Block<T>.Mine()` method,
`Block<T>.FromBencodex()` method, `Block<T>.ToBencodex()` method,
`Transaction<T>.Id` property, `Transaction<T>.Signature` property,
`Transaction<T>.Create()` method, `Transaction<T>.FromBencodex()` method,
and `Transaction<T>.ToBencodex()` method had been non-deterministic on
some `CultureInfo.CurrentCulture` (e.g., `ar_SA`, `fr_FR`, `th_TH`)
so that it had caused network protocol incompatibilities.
[[#734]]

[#604]: https://github.com/planetarium/libplanet/issues/604
[#613]: https://github.com/planetarium/libplanet/issues/613
Expand All @@ -117,8 +125,9 @@ To be released.
[#718]: https://github.com/planetarium/libplanet/pull/718
[#719]: https://github.com/planetarium/libplanet/pull/719
[#725]: https://github.com/planetarium/libplanet/pull/725
[#726]: https://github.com/planetarium/libplanet/pull/726
[#727]: https://github.com/planetarium/libplanet/pull/727
[#726]: https://github.com/planetarium/libplanet/pull/72
[#727]: https://github.com/planetarium/libplanet/pull/7276
[#734]: https://github.com/planetarium/libplanet/pull/734


Version 0.7.0
Expand Down
12 changes: 11 additions & 1 deletion Libplanet.Tests/Common/Action/DumbAction.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Threading;
using Bencodex.Types;
Expand Down Expand Up @@ -112,7 +113,16 @@ public IAccountStateDelta Execute(IActionContext context)
if (Idempotent)
{
var splitedItems = items is null ? new[] { item } : (items + "," + item).Split(',');
items = string.Join(",", splitedItems.OrderBy(x => float.Parse(x.Substring(4))));
items = string.Join(
",",
splitedItems.OrderBy(x =>
float.Parse(
x.Substring(4),
NumberStyles.Float,
CultureInfo.InvariantCulture
)
)
);
}
else
{
Expand Down
13 changes: 12 additions & 1 deletion Libplanet/Action/IAction.cs
Expand Up @@ -269,10 +269,21 @@ public interface IAction
/// or networking. These bring an action indeterministic. You maybe
/// fine to log messages for debugging purpose, but equivalent messages
/// could be logged multiple times.</para>
/// <para>Lastly, although it might be surprising, <a
/// <para>Although it might be surprising, <a
/// href="https://wp.me/p1fTCO-kT">floating-point arithmetics are
/// underspecified so that it can make different results on different
/// machines, platforms, runtimes, compilers, and builds</a>.</para>
/// <para>Lastly, you need to be aware and keep in mind that there
/// is a global state named <see
/// cref="System.Globalization.CultureInfo.CurrentCulture"/> on .NET;
/// if you format numbers, dates and times, currencies, or other such
/// things into strings and parse these strings back these can rely on
/// <see cref="System.Globalization.CultureInfo.CurrentCulture"/>,
/// so that the same action make different results on two differently
/// configured systems like Thai language and French language.
/// In order to make these types of conversions deterministic,
/// you have to explicitly pass <see
/// cref="System.Globalization.CultureInfo.InvariantCulture"/>.</para>
/// <para>For more on determinism in general, please read also <a
/// href="https://tendermint.com/docs/spec/abci/abci.html#determinism"
/// >Tendermint ABCI's docs on determinism</a>.</para>
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Blocks/Block.cs
Expand Up @@ -485,7 +485,7 @@ bool includeTransactionData
);
var rawBlock = new RawBlock(
index: Index,
timestamp: Timestamp.ToString(TimestampFormat),
timestamp: Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture),
nonce: Nonce.ToByteArray(),
miner: Miner?.ToByteArray(),
difficulty: Difficulty,
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Tx/Transaction.cs
Expand Up @@ -665,7 +665,7 @@ internal RawTransaction ToRawTransaction(bool includeSign)
updatedAddresses: UpdatedAddresses.Select(a =>
a.ByteArray).ToImmutableArray(),
publicKey: PublicKey.Format(false).ToImmutableArray(),
timestamp: Timestamp.ToString(TimestampFormat),
timestamp: Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture),
actions: Actions.Select(a => a.PlainValue)
);

Expand Down

0 comments on commit 661dff5

Please sign in to comment.