<commit_before>(* Copyright (c) 2015 The Qeditas developers *)
(* Distributed under the MIT software license, see the accompanying
   file COPYING or http://www.opensource.org/licenses/mit-license.php. *)

open Ser
open Hash
open Mathdata

(*** If the obligation is Some(alpha,n,r), then the way to spend the asset is for alpha to sign after block n.
 If r is true then the asset was a reward and can be forfeited as a consequence of double signing.
 ***)
type obligation = (payaddr * int64 * bool) option

type preasset =
  | Currency of int64
  | Bounty of int64
  | OwnsObj of payaddr * int64 option
  | OwnsProp of payaddr * int64 option
  | OwnsNegProp
  | RightsObj of termaddr * int64
  | RightsProp of termaddr * int64
  | Marker
  | TheoryPublication of payaddr * hashval * theoryspec
  | SignaPublication of payaddr * hashval * hashval option * signaspec
  | DocPublication of payaddr * hashval * hashval option * doc

(*** asset is (assetid,birthday,obligation,preasset) ***)
type asset = hashval * int64 * obligation * preasset

let assetid ((h,bh,obl,u):asset) : hashval = h
let assetobl ((h,bh,obl,u):asset) : obligation = obl
let assetpre ((h,bh,obl,u):asset) : preasset = u

let hashpreasset u =
  match u with
  | Currency(v) -> hashtag (hashint64 v) 256l
  | Bounty(v) -> hashtag (hashint64 v) 257l
  | OwnsObj(a,None) -> hashtag (hashpayaddr a) 258l
  | OwnsObj(a,Some(v)) -> hashtag (hashpair (hashpayaddr a) (hashint64 v)) 259l
  | OwnsProp(a,None) -> hashtag (hashpayaddr a) 260l
  | OwnsProp(a,Some(v)) -> hashtag (hashpair (hashpayaddr a) (hashint64 v)) 261l
  | OwnsNegProp -> hashint32 262l
  | RightsObj(a,v) -> hashtag (hashpair (hashtermaddr a) (hashint64 v)) 263l
  | RightsProp(a,v) -> hashtag (hashpair (hashtermaddr a) (hashint64 v)) 264l
  | Marker -> hashint32 265l
  | TheoryPublication(a,nonce,ths) -> hashtag (hashpair (hashpayaddr a) (hashpair nonce (hashtheoryspec ths))) 266l
  | SignaPublication(a,nonce,th,s) -> hashtag (hashpair (hashpayaddr a) (hashpair nonce (hashopair2 th (hashsignaspec s)))) 267l
  | DocPublication(a,nonce,th,d) -> hashtag (hashpair (hashpayaddr a) (hashpair nonce (hashopair2 th (hashdoc d)))) 268l

let hashobligation (x:obligation) : hashval option =
  match x with
  | None -> None
  | Some(a,n,r) -> Some(hashpair (hashpayaddr a) (hashtag (hashint64 n) (if r then 1025l else 1024l)))

let hashasset a =
  let (h,bh,obl,u) = a in
  hashopair1 (hashpair h (hashpair (hashint64 bh) (hashpreasset u))) (hashobligation obl)

type addr_assetid = addr * hashval
type addr_preasset = addr * (obligation * preasset)
type addr_asset = addr * asset

let hash_addr_assetid (alpha,h) = hashpair (hashaddr alpha) h
let hash_addr_preasset (alpha,(obl,u)) = hashpair (hashaddr alpha) (hashopair2 (hashobligation obl) (hashpreasset u))
let hash_addr_asset (alpha,a) = hashpair (hashaddr alpha) (hashasset a)

let rec new_assets bday alpha aul txh i : asset list =
  match aul with
  | [] -> []
  | (beta,(obl,u))::aur when beta = alpha ->
      (hashpair txh (hashint32 i),bday,obl,u)::new_assets bday alpha aur txh (Int32.add i 1l)
  | _::aur -> new_assets bday alpha aur txh (Int32.add i 1l)

let rec remove_assets al spent : asset list =
  match al with
  | [] -> []
  | a::ar when List.mem (assetid a) spent -> remove_assets ar spent
  | a::ar -> a::remove_assets ar spent

let rec get_spent alpha inpl : hashval list =
  match inpl with
  | [] -> []
  | (beta,a)::inpr when alpha = beta -> a::get_spent alpha inpr
  | (beta,a)::inpr -> get_spent alpha inpr

let rec add_vout bday txh outpl i =
  match outpl with
  | [] -> []
  | (alpha,(obl,u))::outpr -> (alpha,(hashpair txh (hashint32 i),bday,obl,u))::add_vout bday txh outpr (Int32.add i 1l)

let preasset_value u =
  match u with
  | Currency v -> Some v
  | Bounty v -> Some v
  | _ -> None

let asset_value u = preasset_value (assetpre u)

let asset_value_sum al =
  List.fold_right Int64.add (List.map (fun a -> match asset_value a with Some v -> v | None -> 0L) al) 0L

let rec output_signaspec_uses_objs (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,SignaPublication(_,_,th,d)))::outpr ->
      List.map (fun (h,tph) -> (h,hashopair2 th (hashpair h tph))) (signaspec_uses_objs d)
      @ output_signaspec_uses_objs outpr
  | _::outpr -> output_signaspec_uses_objs outpr
  | [] -> []

let rec output_signaspec_uses_props (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,SignaPublication(_,_,th,d)))::outpr ->
      List.map (fun h -> (h,hashopair2 th h)) (signaspec_uses_props d)
      @ output_signaspec_uses_props outpr
  | _::outpr -> output_signaspec_uses_props outpr
  | [] -> []

let rec output_doc_uses_objs (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,DocPublication(_,_,th,d)))::outpr ->
      List.map (fun (h,tph) -> (h,hashopair2 th (hashpair h tph))) (doc_uses_objs d)
      @ output_doc_uses_objs outpr
  | _::outpr -> output_doc_uses_objs outpr
  | [] -> []

let rec output_doc_uses_props (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,DocPublication(_,_,th,d)))::outpr ->
      List.map (fun h -> (h,hashopair2 th h)) (doc_uses_props d)<commit_msg>modified the functions to compute what objs and props are used to wrap the theory specific versions with a tag<commit_after>(* Copyright (c) 2015 The Qeditas developers *)
(* Distributed under the MIT software license, see the accompanying
   file COPYING or http://www.opensource.org/licenses/mit-license.php. *)

open Ser
open Hash
open Mathdata

(*** If the obligation is Some(alpha,n,r), then the way to spend the asset is for alpha to sign after block n.
 If r is true then the asset was a reward and can be forfeited as a consequence of double signing.
 ***)
type obligation = (payaddr * int64 * bool) option

type preasset =
  | Currency of int64
  | Bounty of int64
  | OwnsObj of payaddr * int64 option
  | OwnsProp of payaddr * int64 option
  | OwnsNegProp
  | RightsObj of termaddr * int64
  | RightsProp of termaddr * int64
  | Marker
  | TheoryPublication of payaddr * hashval * theoryspec
  | SignaPublication of payaddr * hashval * hashval option * signaspec
  | DocPublication of payaddr * hashval * hashval option * doc

(*** asset is (assetid,birthday,obligation,preasset) ***)
type asset = hashval * int64 * obligation * preasset

let assetid ((h,bh,obl,u):asset) : hashval = h
let assetobl ((h,bh,obl,u):asset) : obligation = obl
let assetpre ((h,bh,obl,u):asset) : preasset = u

let hashpreasset u =
  match u with
  | Currency(v) -> hashtag (hashint64 v) 256l
  | Bounty(v) -> hashtag (hashint64 v) 257l
  | OwnsObj(a,None) -> hashtag (hashpayaddr a) 258l
  | OwnsObj(a,Some(v)) -> hashtag (hashpair (hashpayaddr a) (hashint64 v)) 259l
  | OwnsProp(a,None) -> hashtag (hashpayaddr a) 260l
  | OwnsProp(a,Some(v)) -> hashtag (hashpair (hashpayaddr a) (hashint64 v)) 261l
  | OwnsNegProp -> hashint32 262l
  | RightsObj(a,v) -> hashtag (hashpair (hashtermaddr a) (hashint64 v)) 263l
  | RightsProp(a,v) -> hashtag (hashpair (hashtermaddr a) (hashint64 v)) 264l
  | Marker -> hashint32 265l
  | TheoryPublication(a,nonce,ths) -> hashtag (hashpair (hashpayaddr a) (hashpair nonce (hashtheoryspec ths))) 266l
  | SignaPublication(a,nonce,th,s) -> hashtag (hashpair (hashpayaddr a) (hashpair nonce (hashopair2 th (hashsignaspec s)))) 267l
  | DocPublication(a,nonce,th,d) -> hashtag (hashpair (hashpayaddr a) (hashpair nonce (hashopair2 th (hashdoc d)))) 268l

let hashobligation (x:obligation) : hashval option =
  match x with
  | None -> None
  | Some(a,n,r) -> Some(hashpair (hashpayaddr a) (hashtag (hashint64 n) (if r then 1025l else 1024l)))

let hashasset a =
  let (h,bh,obl,u) = a in
  hashopair1 (hashpair h (hashpair (hashint64 bh) (hashpreasset u))) (hashobligation obl)

type addr_assetid = addr * hashval
type addr_preasset = addr * (obligation * preasset)
type addr_asset = addr * asset

let hash_addr_assetid (alpha,h) = hashpair (hashaddr alpha) h
let hash_addr_preasset (alpha,(obl,u)) = hashpair (hashaddr alpha) (hashopair2 (hashobligation obl) (hashpreasset u))
let hash_addr_asset (alpha,a) = hashpair (hashaddr alpha) (hashasset a)

let rec new_assets bday alpha aul txh i : asset list =
  match aul with
  | [] -> []
  | (beta,(obl,u))::aur when beta = alpha ->
      (hashpair txh (hashint32 i),bday,obl,u)::new_assets bday alpha aur txh (Int32.add i 1l)
  | _::aur -> new_assets bday alpha aur txh (Int32.add i 1l)

let rec remove_assets al spent : asset list =
  match al with
  | [] -> []
  | a::ar when List.mem (assetid a) spent -> remove_assets ar spent
  | a::ar -> a::remove_assets ar spent

let rec get_spent alpha inpl : hashval list =
  match inpl with
  | [] -> []
  | (beta,a)::inpr when alpha = beta -> a::get_spent alpha inpr
  | (beta,a)::inpr -> get_spent alpha inpr

let rec add_vout bday txh outpl i =
  match outpl with
  | [] -> []
  | (alpha,(obl,u))::outpr -> (alpha,(hashpair txh (hashint32 i),bday,obl,u))::add_vout bday txh outpr (Int32.add i 1l)

let preasset_value u =
  match u with
  | Currency v -> Some v
  | Bounty v -> Some v
  | _ -> None

let asset_value u = preasset_value (assetpre u)

let asset_value_sum al =
  List.fold_right Int64.add (List.map (fun a -> match asset_value a with Some v -> v | None -> 0L) al) 0L

let rec output_signaspec_uses_objs (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,SignaPublication(_,_,th,d)))::outpr ->
      List.map (fun (h,tph) -> (h,hashtag (hashopair2 th (hashpair h tph)) 32l)) (signaspec_uses_objs d)
      @ output_signaspec_uses_objs outpr
  | _::outpr -> output_signaspec_uses_objs outpr
  | [] -> []

let rec output_signaspec_uses_props (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,SignaPublication(_,_,th,d)))::outpr ->
      List.map (fun h -> (h,hashtag (hashopair2 th h) 33l)) (signaspec_uses_props d)
      @ output_signaspec_uses_props outpr
  | _::outpr -> output_signaspec_uses_props outpr
  | [] -> []

let rec output_doc_uses_objs (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,DocPublication(_,_,th,d)))::outpr ->
      List.map (fun (h,tph) -> (h,hashtag (hashopair2 th (hashpair h tph)) 32l)) (doc_uses_objs d)
      @ output_doc_uses_objs outpr
  | _::outpr -> output_doc_uses_objs outpr
  | [] -> []

let rec output_doc_uses_props (outpl:addr_preasset list) : (termaddr * termaddr) list =
  match outpl with
  | (_,(_,DocPublication(_,_,th,d)))::outpr ->
      List.map (fun h -> (h,hashtag (hashopair2 th h) 33l)) (doc_uses_props d)
{'fraction_non_alphanumeric': 0.12761958462550513, 'fraction_numerical': 0.02180246217460765, 'mean_word_length': 4.161008729388943, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1299600', 'n_tokens_mistral': 4135, 'n_tokens_neox': 3911, 'n_words': 1148}