Update format of generated genesis.json

Newer format introduced in Tendermint 0.19.0 is now used. Also it is
now possible to specify the genesis_time field.
parent 37cc3e87
Pipeline #28894693 passed with stages
in 13 minutes and 55 seconds
......@@ -14,13 +14,14 @@ PROJECT = ercoin
PROJECT_DESCRIPTION = A simple cryptocurrency using Tendermint
PROJECT_VERSION = 0.1.0
DEPS = abci_server datum dynarec enacl erlsha2 gb_merkle_trees jiffy nist_beacon
DEPS = abci_server datum dynarec enacl erlsha2 gb_merkle_trees iso8601 jiffy nist_beacon
dep_abci_server = git https://github.com/KrzysiekJ/abci_server.git 2f38de3b21dfb463ec33f54f6c08b7ce63c27f56
dep_datum = git https://github.com/fogfish/datum.git 4.3.3
dep_dynarec = git https://github.com/dieswaytoofast/dynarec.git 1f477
dep_enacl = git https://github.com/jlouis/enacl.git f650c72b028e46dbbed35f94e33ebe1e8db5d7eb
dep_erlsha2 = git https://github.com/vinoski/erlsha2 e3434b33cfeea02609bbf877954d856d895b9e1d
dep_gb_merkle_trees = git https://github.com/KrzysiekJ/gb_merkle_trees.git 1687f8be1187cf0964e63012b175b286af69c21a
dep_iso8601 = git https://github.com/erlsci/iso8601.git 1.3.1
dep_jiffy = git https://github.com/davisp/jiffy.git 0.14.11
dep_nist_beacon = git https://gitlab.com/KrzysiekJ/nist_beacon v0.1.1
......
......@@ -18,6 +18,7 @@
-export(
[binary_to_hex/1,
data_to_genesis_json/1,
data_to_genesis_json/2,
initial_distribution/2,
initial_data/5,
sk_to_priv_validator_json/1]).
......@@ -70,23 +71,36 @@ hexstr_to_bin([X,Y|Tail], Acc) ->
{ok, [Byte], []} = io_lib:fread("~16u", [X,Y]),
hexstr_to_bin(Tail, <<Acc/binary, Byte>>).
%% In version 0.19.0 Tendermint changed the serialization format used for genesis.json and priv_validator.json.
%% This is not resembled below. To create file compatible with the newer format, use scripts/wire2amino.go from Tendermint.
%% @doc Convert data to a genesis.json file for Tendermint.
-spec timestamp_to_iso(timestamp()) -> binary().
timestamp_to_iso(Timestamp) ->
Seconds1970 = Timestamp div 1000,
GregorianSeconds = calendar:date_to_gregorian_days(1970, 1, 1) * 86400 + Seconds1970,
Datetime = calendar:gregorian_seconds_to_datetime(GregorianSeconds),
iso8601:format(Datetime).
-spec now_timestamp() -> timestamp().
now_timestamp() ->
(calendar:datetime_to_gregorian_seconds(calendar:universal_time()) - calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}})) * 1000.
-spec data_to_genesis_json(data()) -> binary().
data_to_genesis_json(Genesis) ->
data_to_genesis_json(Data) ->
data_to_genesis_json(Data, now_timestamp()).
%% @doc Convert data and a starting timestamp to a genesis.json file for Tendermint.
-spec data_to_genesis_json(data(), timestamp()) -> binary().
data_to_genesis_json(Genesis, Timestamp) ->
Options = [pretty],
Validators =
[{[{<<"power">>, Power},
{<<"name">>, <<"">>},
{<<"pub_key">>,
{[{<<"type">>, <<"ed25519">>},
{<<"data">>, binary_to_hex(PK)}]}}]}
{[{<<"type">>, <<"AC26791624DE60">>},
{<<"value">>, base64:encode(PK)}]}}]}
|| {PK, <<Power/integer, _/binary>>} <- gb_merkle_trees:to_orddict(Genesis#data.validators)],
ToEncode =
{[{<<"app_hash">>, <<"">>},
{<<"app_state">>, base64:encode(term_to_binary(Genesis, [{compressed, 9}]))},
{<<"genesis_time">>, <<"0001-01-01T00:00:00.000Z">>},
{<<"genesis_time">>, timestamp_to_iso(Timestamp)},
{<<"chain_id">>, <<"ercoin-test">>},
{<<"validators">>, Validators}]},
jiffy:encode(ToEncode, Options).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment