Skip to content

Commit

Permalink
Merge pull request #12021 from MinaProtocol/feature/txn-hash-arbitrar…
Browse files Browse the repository at this point in the history
…y-txn-id

Generate hash from arbitrary transaction id
  • Loading branch information
mrmr1993 authored Oct 24, 2022
2 parents ffef7ea + a62f883 commit d6436be
Show file tree
Hide file tree
Showing 53 changed files with 556 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/app/archive/lib/extensional.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ module Block = struct
; accounts_created :
(Account_id.Stable.V2.t * Currency.Fee.Stable.V1.t) list
; tokens_used :
(Token_id.Stable.V1.t * Account_id.Stable.V2.t option) list
(Token_id.Stable.V2.t * Account_id.Stable.V2.t option) list
}
[@@deriving yojson, equal]

Expand Down
15 changes: 7 additions & 8 deletions src/app/cli/src/init/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2073,18 +2073,17 @@ let hash_transaction =
let open Command.Let_syntax in
Command.basic
~summary:"Compute the hash of a transaction from its transaction ID"
(let%map_open transaction =
(let%map_open transaction_id =
flag "--transaction-id" ~doc:"ID ID of the transaction to hash"
(required string)
in
fun () ->
let signed_command =
Signed_command.of_base64 transaction |> Or_error.ok_exn
in
let hash =
Transaction_hash.hash_command (Signed_command signed_command)
in
printf "%s\n" (Transaction_hash.to_base58_check hash) )
match Transaction_hash.hash_of_transaction_id transaction_id with
| Ok hash ->
printf "%s\n" (Transaction_hash.to_base58_check hash)
| Error err ->
Format.eprintf "Could not hash transaction: %s@."
(Error.to_string_hum err) )

let humanize_graphql_error
~(graphql_endpoint : Uri.t Cli_lib.Flag.Types.with_name) = function
Expand Down
2 changes: 1 addition & 1 deletion src/app/cli/src/init/dune
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
graphql-cohttp
cohttp-async
graphql-async
archive_lib
;;local libraries
archive_lib
mina_wire_types
perf_histograms
one_or_two
Expand Down
2 changes: 1 addition & 1 deletion src/lib/base58_check/version_bytes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let state_body_hash : t = '\x11'
let transaction_hash : t = '\x12'

(* used only to deserialize transaction ids, pre-Berkeley hard fork *)
let signed_command_legacy : t = '\x13'
let signed_command_v1 : t = '\x13'

let user_command_memo : t = '\x14'

Expand Down
18 changes: 12 additions & 6 deletions src/lib/crypto/kimchi_backend/common/field.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,16 @@ module type S = sig
end

module type S_with_version = sig
[%%versioned:
module Stable : sig
[@@@no_toplevel_latest_type]

module V1 : sig
[@@@with_all_version_tags]

type t [@@deriving version, sexp, bin_io, compare, yojson, hash, equal]
end

module Latest = V1
end
end]

include S with type t = Stable.Latest.t
end
Expand All @@ -143,10 +146,15 @@ module Make (F : Input_intf) :

let size_in_bits = size_in_bits ()

[%%versioned_binable
module Stable = struct
module V1 = struct
[@@@with_all_version_tags]

type t = (F.t[@version_asserted]) [@@deriving version]

let to_latest = Fn.id

include
Binable.Of_binable
(Bigint)
Expand Down Expand Up @@ -200,9 +208,7 @@ module Make (F : Input_intf) :
| _ ->
Error "expected hex string"
end

module Latest = V1
end
end]

include (
Stable.Latest : module type of Stable.Latest with type t := Stable.Latest.t )
Expand Down
7 changes: 7 additions & 0 deletions src/lib/currency/currency.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ module Make_str (A : Wire_types.Concrete) = struct
if num mod 10 = 0 && num <> 0 then go (num_stripped_zeros + 1) (num / 10)
else (num_stripped_zeros, num)
in

let whole = Unsigned.div amount precision_exp in
let remainder = Unsigned.to_int (Unsigned.rem amount precision_exp) in
if Int.(remainder = 0) then to_string whole
Expand Down Expand Up @@ -895,6 +896,8 @@ module Make_str (A : Wire_types.Concrete) = struct
[@@@no_toplevel_latest_type]

module V1 = struct
[@@@with_all_version_tags]

type t = Unsigned_extended.UInt64.Stable.V1.t
[@@deriving sexp, compare, hash, equal]

Expand All @@ -917,6 +920,8 @@ module Make_str (A : Wire_types.Concrete) = struct
[%%versioned:
module Stable : sig
module V1 : sig
[@@@with_all_version_tags]

type t = A.t [@@deriving sexp, compare, hash, equal, yojson]

(* not automatically derived *)
Expand Down Expand Up @@ -1027,6 +1032,8 @@ module Make_str (A : Wire_types.Concrete) = struct
[@@@no_toplevel_latest_type]

module V1 = struct
[@@@with_all_version_tags]

type t = Unsigned_extended.UInt64.Stable.V1.t
[@@deriving sexp, compare, hash, equal, yojson]

Expand Down
4 changes: 4 additions & 0 deletions src/lib/currency/intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ module type Full = sig
[%%versioned:
module Stable : sig
module V1 : sig
[@@@with_all_version_tags]

type t [@@deriving sexp, compare, hash, yojson, equal]

(* not automatically derived *)
Expand Down Expand Up @@ -341,6 +343,8 @@ module type Full = sig
[%%versioned:
module Stable : sig
module V1 : sig
[@@@with_all_version_tags]

type t [@@deriving sexp, compare, hash, equal, yojson]

(* not automatically derived *)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_base/account.ml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ module Binable_arg = struct
module V2 = struct
type t =
( Public_key.Compressed.Stable.V1.t
, Token_id.Stable.V1.t
, Token_id.Stable.V2.t
, Token_permissions.Stable.V1.t
, Token_symbol.Stable.V1.t
, Balance.Stable.V1.t
Expand Down
8 changes: 6 additions & 2 deletions src/lib/mina_base/account_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ module Make_str (_ : Wire_types.Concrete) = struct

[%%define_locally Stable.Latest.(of_yojson, to_yojson)]

include Comparable.Make_binable (Stable.Latest)
include Hashable.Make_binable (Stable.Latest)
module Binables = struct
include Comparable.Make_binable (Stable.Latest)
include Hashable.Make_binable (Stable.Latest)
end

include Binables

let to_input : t -> _ Random_oracle_input.Chunked.t =
Random_oracle_input.Chunked.field
Expand Down
9 changes: 7 additions & 2 deletions src/lib/mina_base/account_id_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ module type S = sig

include Stringable.S with type t := t

include Comparable_binable with type t := t
(* so we can easily import these into Token_id *)
module Binables : sig
include Comparable_binable with type t := t

include Hashable_binable with type t := t
include Hashable_binable with type t := t
end

include module type of Binables

val to_input : t -> Snark_params.Tick.Field.t Random_oracle.Input.Chunked.t

Expand Down
12 changes: 6 additions & 6 deletions src/lib/mina_base/account_update.ml
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ module Body = struct
module V1 = struct
type t =
{ public_key : Public_key.Compressed.Stable.V1.t
; token_id : Token_id.Stable.V1.t
; token_id : Token_id.Stable.V2.t
; update : Update.Stable.V1.t
; balance_change :
(Amount.Stable.V1.t, Sgn.Stable.V1.t) Signed_poly.Stable.V1.t
Expand Down Expand Up @@ -899,7 +899,7 @@ module Body = struct
module V1 = struct
type t =
{ public_key : Public_key.Compressed.Stable.V1.t
; token_id : Token_id.Stable.V1.t
; token_id : Token_id.Stable.V2.t
; update : Update.Stable.V1.t
; balance_change :
(Amount.Stable.V1.t, Sgn.Stable.V1.t) Signed_poly.Stable.V1.t
Expand All @@ -910,7 +910,7 @@ module Body = struct
; call_depth : int
; preconditions : Preconditions.Stable.V1.t
; use_full_commitment : bool
; caller : Token_id.Stable.V1.t
; caller : Token_id.Stable.V2.t
; authorization_kind : Authorization_kind.Stable.V1.t
}
[@@deriving annot, sexp, equal, yojson, hash, compare, fields]
Expand Down Expand Up @@ -954,7 +954,7 @@ module Body = struct
module V1 = struct
type t =
{ public_key : Public_key.Compressed.Stable.V1.t
; token_id : Token_id.Stable.V1.t
; token_id : Token_id.Stable.V2.t
; update : Update.Stable.V1.t
; balance_change :
(Amount.Stable.V1.t, Sgn.Stable.V1.t) Signed_poly.Stable.V1.t
Expand All @@ -980,7 +980,7 @@ module Body = struct
module V1 = struct
type t = Mina_wire_types.Mina_base.Account_update.Body.V1.t =
{ public_key : Public_key.Compressed.Stable.V1.t
; token_id : Token_id.Stable.V1.t
; token_id : Token_id.Stable.V2.t
; update : Update.Stable.V1.t
; balance_change :
(Amount.Stable.V1.t, Sgn.Stable.V1.t) Signed_poly.Stable.V1.t
Expand All @@ -990,7 +990,7 @@ module Body = struct
; call_data : Pickles.Backend.Tick.Field.Stable.V1.t
; preconditions : Preconditions.Stable.V1.t
; use_full_commitment : bool
; caller : Token_id.Stable.V1.t
; caller : Token_id.Stable.V2.t
; authorization_kind : Authorization_kind.Stable.V1.t
}
[@@deriving annot, sexp, equal, yojson, hash, hlist, compare, fields]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_base/fee_excess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ end
module Stable = struct
module V1 = struct
type t =
( Token_id.Stable.V1.t
( Token_id.Stable.V2.t
, (Fee.Stable.V1.t, Sgn.Stable.V1.t) Signed_poly.Stable.V1.t )
Poly.Stable.V1.t
[@@deriving compare, equal, hash, sexp, yojson]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_base/fee_transfer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Make_str (A : Wire_types.Concrete) = struct
type t = A.Single.V2.t =
{ receiver_pk : Public_key.Compressed.Stable.V1.t
; fee : Currency.Fee.Stable.V1.t
; fee_token : Token_id.Stable.V1.t
; fee_token : Token_id.Stable.V2.t
}
[@@deriving sexp, compare, equal, yojson, hash]

Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_base/fee_transfer_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module type Full = sig
type t = private
{ receiver_pk : Public_key.Compressed.Stable.V1.t
; fee : Currency.Fee.Stable.V1.t
; fee_token : Token_id.Stable.V1.t
; fee_token : Token_id.Stable.V2.t
}
[@@deriving bin_io, sexp, compare, equal, yojson, version, hash]
end
Expand Down
4 changes: 4 additions & 0 deletions src/lib/mina_base/payment_payload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Poly = struct
end

module V1 = struct
[@@@with_all_version_tags]

type ('public_key, 'token_id, 'amount) t =
{ source_pk : 'public_key
; receiver_pk : 'public_key
Expand All @@ -42,6 +44,8 @@ module Stable = struct
end

module V1 = struct
[@@@with_all_version_tags]

type t =
( Public_key.Compressed.Stable.V1.t
, Token_id.Stable.V1.t
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mina_base/payment_payload.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ module Stable : sig
end

module V1 : sig
[@@@with_all_version_tags]

type t =
( Public_key.Compressed.Stable.V1.t
, Token_id.Stable.V1.t
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mina_base/signature.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ end
[%%versioned
module Stable = struct
module V1 = struct
[@@@with_all_version_tags]

type t =
( (Field.t[@version_asserted])
, (Inner_curve.Scalar.t[@version_asserted]) )
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mina_base/signature.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ open Snark_params.Tick
[%%versioned:
module Stable : sig
module V1 : sig
[@@@with_all_version_tags]

type t = Field.t * Inner_curve.Scalar.t
[@@deriving sexp, equal, compare, hash]

Expand Down
31 changes: 24 additions & 7 deletions src/lib/mina_base/signed_command.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Make_str (_ : Wire_types.Concrete) = struct
[%%versioned
module Stable = struct
module V1 = struct
[@@@with_all_version_tags]

type ('payload, 'pk, 'signature) t =
( 'payload
, 'pk
Expand All @@ -37,6 +39,15 @@ module Make_str (_ : Wire_types.Concrete) = struct
module Stable = struct
[@@@with_top_version_tag]

(* DO NOT DELETE VERSIONS!
so we can always get transaction hashes from old transaction ids
the version linter should be checking this
IF YOU CREATE A NEW VERSION:
update Transaction_hash.hash_of_transaction_id to handle it
add hash_signed_command_vn for that version
*)

module V2 = struct
type t =
( Payload.Stable.V2.t
Expand Down Expand Up @@ -64,6 +75,8 @@ module Make_str (_ : Wire_types.Concrete) = struct
end

module V1 = struct
[@@@with_all_version_tags]

type t =
( Payload.Stable.V1.t
, Public_key.Stable.V1.t
Expand All @@ -73,15 +86,11 @@ module Make_str (_ : Wire_types.Concrete) = struct

(* don't need to coerce old commands to new ones *)
let to_latest _ = failwith "Not implemented"

let description = "Signed command"

let version_byte = Base58_check.Version_bytes.signed_command_legacy
end
end]

(* type of signed commands, pre-Berkeley hard fork *)
type t_legacy = Stable.V1.t
type t_v1 = Stable.V1.t

type _unused = unit
constraint (Payload.t, Public_key.t, Signature.t) Poly.t = t
Expand Down Expand Up @@ -369,9 +378,17 @@ module Make_str (_ : Wire_types.Concrete) = struct
`If_this_is_used_it_should_have_a_comment_justifying_it t

(* so we can deserialize Base58Check transaction ids created before Berkeley hard fork *)
module Base58_check_legacy = Codable.Make_base58_check (Stable.V1)
module V1_all_tagged = struct
include Stable.V1.With_all_version_tags

let description = "Signed command"

let version_byte = Base58_check.Version_bytes.signed_command_v1
end

module Base58_check_v1 = Codable.Make_base58_check (V1_all_tagged)

let of_base58_check_exn_legacy = Base58_check_legacy.of_base58_check
let of_base58_check_exn_v1 = Base58_check_v1.of_base58_check

(* give transaction ids have version tag *)
include Codable.Make_base64 (Stable.Latest.With_top_version_tag)
Expand Down
Loading

0 comments on commit d6436be

Please sign in to comment.