Mina中的树结构

1. 引言

前序博客有:

Mina中包含的树结构主要有:

  • 1)账号树
  • 2)Pending coinbase树
  • 3)scan state树

2. Mina中的账号树

在(生成转账交易snark时需用到的)Transaction_witness.t结构的“ledger”字段,为Mina_ledger.Sparse_ledger.Stable.V2.t 结构,本质为Mina的账号树:【同时,

  • 在scan_state的叶子节点,其结构为Transaction_with_witness.t —— 其"ledger_witness"字段也为账号树。
  • zkApp交易snark时需用到的Parties_segment_witness结构中的“global_ledger”和“local_state_init.ledger”字段也为Mina账号树。

( * Transition_witness.t结构为: *)
type t =
  { transaction : Mina_transaction.Transaction.Stable.V2.t (* 本文只考虑Signed_command类型交易 *)
  ; ledger : Mina_ledger.Sparse_ledger.Stable.V2.t (* 包含indexes(由account_id和int组成的数组)、depth(int结构)、tree(由ledger_hash和account组成的数结构)三个字段。 *)
  ; protocol_state_body : Mina_state.Protocol_state.Body.Value.Stable.V2.t (* 包含genesis_state_hash/blockchain_state/consensus_state/constants这4个字段。 *)
  ; init_stack : Mina_base.Pending_coinbase.Stack_versioned.Stable.V1.t  (* data为Field.t,state(包含init/curr)也均为Field.t *) 
  ; status : Mina_base.Transaction_status.Stable.V2.t (* 表示交易状态,为Appplied或Failed枚举类型。 *)
  }
(* ledger字段类型,Mina_ledger.Sparse_ledger.Stable.V2.t 结构为:*)
type t =
  ( Ledger_hash.Stable.V1.t (* 为哈希值,Field.t *)
  , Account_id.Stable.V2.t (* 为压缩公钥与Pickles.Backend.Tick.Field.Stable.V1.t组合,Public_key.Compressed.Stable.V1.t * Digest.Stable.V1.t *)
  , Account.Stable.V2.t ) (* Account结构中包含了:public_key/token_id/token_permissions/token_symbol/balance/nonce/receipt_chain_hash/delegate/voting_for/timing/permissions/zkapp/zkapp_uri字段 *)
  Sparse_ledger_lib.Sparse_ledger.T.Stable.V2.t
type ('hash, 'key, 'account) t =
    { indexes : ('key * int) list (* indexes为由account_id和int组成的数组 *)
    ; depth : int
    ; tree : ('hash, 'account) Tree.Stable.V1.t (* 由ledger_hash和account组成的树结构。 *)
    }
(* tree结构,('hash, 'account) Tree.Stable.V1.t 为: *)
type ('hash, 'account) t = (* 为树结构,节点递归 *)
    | Account of 'account
    | Hash of 'hash
    | Node of 'hash * ('hash, 'account) t * ('hash, 'account) t
  
(* Account.Stable.V2.t结构为: *)
type t =
    ( Public_key.Compressed.Stable.V1.t (* 为压缩公钥 *)
    , Token_id.Stable.V1.t (* 为Pickles.Backend.Tick.Field.Stable.V1.t,对应Tick曲线的scalar域 *)
    , Token_permissions.Stable.V1.t (* 分为Token_owned和Not_owned两种枚举类型,分别有disable_new_accounts和account_disabled bool字段。 *)
    , Token_symbol.Stable.V1.t  (* 为string *)
    , Balance.Stable.V1.t (* 为Unsigned.UInt64.t *)
    , Nonce.Stable.V1.t (* 为Unsigned_extended.UInt32.t *)
    , Receipt.Chain_hash.Stable.V1.t (* 为哈希值,Field.t *)
    , Public_key.Compressed.Stable.V1.t option (* 为option,压缩公钥 *)
    , State_hash.Stable.V1.t (* 为哈希值,Field.t *)
    , Timing.Stable.V1.t (* 分为Untimed和Timed两种类型,其中Timed包含:initial_minimum_balance(为Unsigned.UInt64.t类型)、cliff_time(为global_slot,Unsigned_extended.UInt32.t类型)、cliff_amount(为Unsigned.UInt64.t类型)、vesting_period(为global_slot,Unsigned_extended.UInt32.t类型)、vesting_increment(为Unsigned.UInt64.t类型)这5个字段。 *)
    , Permissions.Stable.V2.t (* 包含edit_state、send、receive、set_delegate、set_permissions、set_verification_key、set_zkapp_uri、edit_sequence_state、set_token_symbol、increment_nonce、set_voting_for这11个字段,均为Auth_required枚举类型——None | Either | Proof | Signature | Impossible。 *)
    , Zkapp_account.Stable.V2.t option (* 为option,Zkapp_account包含:app_state(为Zkapp_basic.F.Stable.V1.t Vector.Vector_8.Stable.V1.t类型,为8 fields of 32 bytes each of arbitrary storage。)、verification_key(为option,包含data和hash2个字段,其中data为Side_loaded_verification_key类型)、zkapp_version(版本信息,为Unsigned_extended.UInt32.t类型)、sequence_state(为Pickles_types.Vector.Vector_5.Stable.V1.t为5 fields of 32 bytes each of arbitrary storage.。)、last_sequqnece_slot(为global_slot,Unsigned_extended.UInt32.t类型)、proved_state(为bool值)这6个字段。 *)
    , string )
    (* TODO: Cache the digest of this? *)
    Poly.Stable.V2.t
type ( 'pk  (* 为压缩公钥 *)
       , 'id  (* 为Pickles.Backend.Tick.Field.Stable.V1.t,对应Tick曲线的scalar域 *)
       , 'token_permissions  (* 分为Token_owned和Not_owned两种枚举类型,分别有disable_new_accounts和account_disabled bool字段。 *)
       , 'token_symbol (* 为string *)
       , 'amount  (* 为Unsigned.UInt64.t *)
       , 'nonce (* 为Unsigned_extended.UInt32.t *)
       , 'receipt_chain_hash (* 为哈希值,Field.t *)
       , 'delegate (* 为option,压缩公钥 *)
       , 'state_hash (* 为哈希值,Field.t *)
       , 'timing  (* 分为Untimed和Timed两种类型,其中Timed包含:initial_minimum_balance(为Unsigned.UInt64.t类型)、cliff_time(为global_slot,Unsigned_extended.UInt32.t类型)、cliff_amount(为Unsigned.UInt64.t类型)、vesting_period(为global_slot,Unsigned_extended.UInt32.t类型)、vesting_increment(为Unsigned.UInt64.t类型)这5个字段。 *)
       , 'permissions  (* 包含edit_state、send、receive、set_delegate、set_permissions、set_verification_key、set_zkapp_uri、edit_sequence_state、set_token_symbol、increment_nonce、set_voting_for这11个字段,均为Auth_required枚举类型——None | Either | Proof | Signature | Impossible。 *)
       , 'zkapp_opt (* 为option,Zkapp_account包含:app_state(为Zkapp_basic.F.Stable.V1.t Vector.Vector_8.Stable.V1.t类型,为8 fields of 32 bytes each of arbitrary storage。)、verification_key(为option,包含data和hash2个字段,其中data为Side_loaded_verification_key类型)、zkapp_version(版本信息,为Unsigned_extended.UInt32.t类型)、sequence_state(为Pickles_types.Vector.Vector_5.Stable.V1.t为5 fields of 32 bytes each of arbitrary storage。)、last_sequqnece_slot(为global_slot,Unsigned_extended.UInt32.t类型)、proved_state(为bool值)这6个字段。 *)
       , 'zkapp_uri ) (* 为string *)
       t =
    { public_key : 'pk   (* 为压缩公钥 *)
    ; token_id : 'id  (* 为Pickles.Backend.Tick.Field.Stable.V1.t,对应Tick曲线的scalar域 *)
    ; token_permissions : 'token_permissions  (* 分为Token_owned和Not_owned两种枚举类型,分别有disable_new_accounts和account_disabled bool字段。 *)
    ; token_symbol : 'token_symbol  (* 为string *)
    ; balance : 'amount  (* 为Unsigned.UInt64.t *)
    ; nonce : 'nonce  (* 为Unsigned_extended.UInt32.t *)
    ; receipt_chain_hash : 'receipt_chain_hash  (* 为哈希值,Field.t *)
    ; delegate : 'delegate (* 为option,压缩公钥 *)
    ; voting_for : 'state_hash (* 为哈希值,Field.t *)
    ; timing : 'timing  (* 分为Untimed和Timed两种类型,其中Timed包含:initial_minimum_balance(为Unsigned.UInt64.t类型)、cliff_time(为global_slot,Unsigned_extended.UInt32.t类型)、cliff_amount(为Unsigned.UInt64.t类型)、vesting_period(为global_slot,Unsigned_extended.UInt32.t类型)、vesting_increment(为Unsigned.UInt64.t类型)这5个字段。 *)
    ; permissions : 'permissions  (* 包含edit_state、send、receive、set_delegate、set_permissions、set_verification_key、set_zkapp_uri、edit_sequence_state、set_token_symbol、increment_nonce、set_voting_for这11个字段,均为Auth_required枚举类型——None | Either | Proof | Signature | Impossible。 *)
    ; zkapp : 'zkapp_opt (* 为option,Zkapp_account包含:app_state(为Zkapp_basic.F.Stable.V1.t Vector.Vector_8.Stable.V1.t类型,为8 fields of 32 bytes each of arbitrary storage。)、verification_key(为option,包含data和hash2个字段,其中data为Side_loaded_verification_key类型)、zkapp_version(版本信息,为Unsigned_extended.UInt32.t类型)、sequence_state(为Pickles_types.Vector.Vector_5.Stable.V1.t为5 fields of 32 bytes each of arbitrary storage。)、last_sequqnece_slot(为global_slot,Unsigned_extended.UInt32.t类型)、proved_state(为bool值)这6个字段。 *)
    ; zkapp_uri : 'zkapp_uri  (* 为string *)
    }

Mina的账号中包含“zk_app”字段,其为Zkapp_account.Stable.V2.t option结构:

(* Zkapp_account.Stable.V2.t结构为: *)
	type t =
      ( Zkapp_state.Value.Stable.V1.t  (* 为8 fields of 32 bytes each of arbitrary storage.*)
      , ( Side_loaded_verification_key.Stable.V2.t
        , F.Stable.V1.t ) (* 为F.t结构 *)
        With_hash.Stable.V1.t (* 包含data和hash两个字段。 *)
        option
      , Mina_numbers.Zkapp_version.Stable.V1.t (* 为Unsigned_extended.UInt32.t结构 *)
      , F.Stable.V1.t  (* 为F.t结构 *)
      , Mina_numbers.Global_slot.Stable.V1.t  (* 为Unsigned_extended.UInt32.t结构 *)
      , bool )
      Poly.Stable.V2.t

	type ('app_state, 'vk, 'zkapp_version, 'field, 'slot, 'bool) t =
        { app_state : 'app_state  (* 为8 fields of 32 bytes each of arbitrary storage. *)
        ; verification_key : 'vk  (* option,可包含data(包含max_width、wrap_index和wrap_vk三个字段)和hash两个字段。
        其中wrap_index字段中包含sigma_comm/coefficients_comm/generic_comm/psm_comm/complete_add_comm/mul_comm/emul_comm/endomul_scalar_comm这8个字段。 *)
        ; zkapp_version : 'zkapp_version (* 为Unsigned_extended.UInt32.t结构 *)
        ; sequence_state : 'field Pickles_types.Vector.Vector_5.Stable.V1.t   (* 为5 fields of 32 bytes each of arbitrary storage.*)
        ; last_sequence_slot : 'slot  (* 为Unsigned_extended.UInt32.t结构 *)
        ; proved_state : 'bool
        }

(* 《1》 app_state字段,为Zkapp_state.Value.Stable.V1.t结构: *)
type t = Zkapp_basic.F.Stable.V1.t V.Stable.V1.t (* Zkapp_basic.F.Stable.V1.t 为F.t结构 *)
type 'a t = 'a Vector.Vector_8.Stable.V1.t
type 'a t = ('a, Nat.N8.n) vec (* 为8 fields of 32 bytes each of arbitrary storage. *)

(* 《2》verification_key字段,为With_hash.Stable.V1.t结构: *)
type ('a, 'h) t = { data : 'a; hash : 'h } (* hash字段为F.t结构 *)
(* 其中data字段,为 Side_loaded_verification_key.Stable.V2.t结构为:*)
	type t =
      ( G.Stable.V1.t (* 为曲线上的point。 *)
      , unit )
      Pickles_base.Side_loaded_verification_key.Poly.Stable.V2.t
type ('g, 'vk) t = (* 其中‘vk为 uint。 *)
        { max_width : Width.Stable.V1.t (* 对应为char类型 *)
        ; wrap_index : 'g Plonk_verification_key_evals.Stable.V2.t (* 包含sigma_comm/coefficients_comm/generic_comm/psm_comm/complete_add_comm/mul_comm/emul_comm/endomul_scalar_comm这8个字段。 *)
        ; wrap_vk : 'vk option (* 其中‘vk为 uint。 *)
        }
(* wrap_index字段,为Plonk_verification_key_evals.Stable.V2.t结构: *)
type 'comm t = (* 'comm对应为 G.Stable.V1.t结构 *)
      { sigma_comm : 'comm Plonk_types.Permuts_vec.Stable.V1.t
      ; coefficients_comm : 'comm Plonk_types.Columns_vec.Stable.V1.t
      ; generic_comm : 'comm
      ; psm_comm : 'comm
      ; complete_add_comm : 'comm
      ; mul_comm : 'comm
      ; emul_comm : 'comm
      ; endomul_scalar_comm : 'comm
      }

3. Mina中的pending coinbase树

Mina中的pending coinbase树

	  type t =
        ( Hash_versioned.Stable.V1.t (* 为Field.t结构 *)
        , Stack_id.Stable.V1.t (* 为int结构 *)
        , Stack_versioned.Stable.V1.t ) (* 包含data(Field.t结构)和state(包含init和curr字段,均为Field.t(stack_hash)结构)字段。 *)
        Sparse_ledger_lib.Sparse_ledger.T.Stable.V2.t
        
      type ('hash, 'key, 'account) t = (* 其中'hash为哈希值Field.t结构,’key为int结构对应stack_id,‘account对应为Stack_versioned结构 *)
        { indexes : ('key * int) list (* 由stack_id(int结构)和int组成的2元list *)
        ; depth : int
        ; tree : ('hash, 'account) Tree.Stable.V1.t (* ‘account对应为Stack_versioned结构,包含data(Field.t结构)和state(包含init和curr字段,均为Field.t(stack_hash)结构)字段。 *)
        }
(* 其中’account, Stack_versioned.Stable.V1.t结构为: *)
type t =
        (Coinbase_stack.Stable.V1.t, State_stack.Stable.V1.t) Poly.Stable.V1.t
type ('data_stack, 'state_stack) t = (* 其中'data_stack为Field.t结构,‘state_stack中包含init和curr字段,均为Field.t(stack_hash)结构。 *)
          { data : 'data_stack; state : 'state_stack }

4. Mina中的scan state树

Mina中的scan state树结构为Transaction_snark_scan_state,其“trees”字段为由base和merge组成的scan state树:【其叶子节点base的"ledger_witness"字段为账号树】

type t =
      ( Ledger_proof_with_sok_message.Stable.V2.t
      , Transaction_with_witness.Stable.V2.t )
      Parallel_scan.State.Stable.V1.t

	type ('merge, 'base) t = (* 其中'merge为Ledger_proof_with_sok_message.Stable.V2.t结构, 'base为Transaction_with_witness.Stable.V2.t结构*)
          { trees :
              ( 'merge Merge.Stable.V1.t
              , 'base Base.Stable.V1.t )
              Tree.Stable.V1.t
              Non_empty_list.Stable.V1.t
          ; acc : ('merge * 'base list) option
          ; curr_job_seq_no : int
          ; max_base_jobs : int
          ; delay : int
          }

猜你喜欢

转载自blog.csdn.net/mutourend/article/details/125781167