123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)typestaker=Stake_repr.staker=|SingleofContract_repr.t*Signature.public_key_hash|SharedofSignature.public_key_hashtypebalance=|ContractofContract_repr.t|Block_fees|Depositsofstaker|Unstaked_depositsofstaker*Cycle_repr.t|Nonce_revelation_rewards|Attesting_rewards|Baking_rewards|Baking_bonuses|Storage_fees|Double_signing_punishments|Lost_attesting_rewardsofSignature.Public_key_hash.t*bool*bool|Liquidity_baking_subsidies|Burned|CommitmentsofBlinded_public_key_hash.t|Bootstrap|Invoice|Initial_commitments|Minted|Frozen_bondsofContract_repr.t*Bond_id_repr.t|Sc_rollup_refutation_punishments|Sc_rollup_refutation_rewardsletbalance_encoding~use_legacy_attestation_name=letopenData_encodinginletcase=function|Tagtag->(* The tag was used by old variant. It have been removed in
protocol proposal O, it can be unblocked in the future. *)lettx_rollup_reserved_tag=[22;23]inassert(not@@List.exists(Compare.Int.equaltag)tx_rollup_reserved_tag);case(Tagtag)|_asc->casecindef(ifuse_legacy_attestation_namethen"operation_metadata_with_legacy_attestation_name.alpha.balance"else"operation_metadata.alpha.balance")@@union[case(Tag0)~title:"Contract"(obj2(req"kind"(constant"contract"))(req"contract"Contract_repr.encoding))(functionContractc->Some((),c)|_->None)(fun((),c)->Contractc);case(Tag2)~title:"Block_fees"(obj2(req"kind"(constant"accumulator"))(req"category"(constant"block fees")))(functionBlock_fees->Some((),())|_->None)(fun((),())->Block_fees);case(Tag4)~title:"Deposits"(obj3(req"kind"(constant"freezer"))(req"category"(constant"deposits"))(req"staker"Stake_repr.staker_encoding))(functionDepositsstaker->Some((),(),staker)|_->None)(fun((),(),staker)->Depositsstaker);case(Tag5)~title:"Nonce_revelation_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"nonce revelation rewards")))(functionNonce_revelation_rewards->Some((),())|_->None)(fun((),())->Nonce_revelation_rewards);(* 6 was for Double_signing_evidence_rewards that has been removed.
https://gitlab.com/tezos/tezos/-/merge_requests/7758 *)case(Tag7)~title:(ifuse_legacy_attestation_namethen"Endorsing_rewards"else"Attesting_rewards")(obj2(req"kind"(constant"minted"))(req"category"(constant(ifuse_legacy_attestation_namethen"endorsing rewards"else"attesting rewards"))))(functionAttesting_rewards->Some((),())|_->None)(fun((),())->Attesting_rewards);case(Tag8)~title:"Baking_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"baking rewards")))(functionBaking_rewards->Some((),())|_->None)(fun((),())->Baking_rewards);case(Tag9)~title:"Baking_bonuses"(obj2(req"kind"(constant"minted"))(req"category"(constant"baking bonuses")))(functionBaking_bonuses->Some((),())|_->None)(fun((),())->Baking_bonuses);case(Tag11)~title:"Storage_fees"(obj2(req"kind"(constant"burned"))(req"category"(constant"storage fees")))(functionStorage_fees->Some((),())|_->None)(fun((),())->Storage_fees);case(Tag12)~title:"Double_signing_punishments"(obj2(req"kind"(constant"burned"))(req"category"(constant"punishments")))(functionDouble_signing_punishments->Some((),())|_->None)(fun((),())->Double_signing_punishments);case(Tag13)~title:(ifuse_legacy_attestation_namethen"Lost_endorsing_rewards"else"Lost_attesting_rewards")(obj5(req"kind"(constant"burned"))(req"category"(constant(ifuse_legacy_attestation_namethen"lost endorsing rewards"else"lost attesting rewards")))(req"delegate"Signature.Public_key_hash.encoding)(req"participation"Data_encoding.bool)(req"revelation"Data_encoding.bool))(function|Lost_attesting_rewards(d,p,r)->Some((),(),d,p,r)|_->None)(fun((),(),d,p,r)->Lost_attesting_rewards(d,p,r));case(Tag14)~title:"Liquidity_baking_subsidies"(obj2(req"kind"(constant"minted"))(req"category"(constant"subsidy")))(functionLiquidity_baking_subsidies->Some((),())|_->None)(fun((),())->Liquidity_baking_subsidies);case(Tag15)~title:"Burned"(obj2(req"kind"(constant"burned"))(req"category"(constant"burned")))(functionBurned->Some((),())|_->None)(fun((),())->Burned);case(Tag16)~title:"Commitments"(obj3(req"kind"(constant"commitment"))(req"category"(constant"commitment"))(req"committer"Blinded_public_key_hash.encoding))(functionCommitmentsbpkh->Some((),(),bpkh)|_->None)(fun((),(),bpkh)->Commitmentsbpkh);case(Tag17)~title:"Bootstrap"(obj2(req"kind"(constant"minted"))(req"category"(constant"bootstrap")))(functionBootstrap->Some((),())|_->None)(fun((),())->Bootstrap);case(Tag18)~title:"Invoice"(obj2(req"kind"(constant"minted"))(req"category"(constant"invoice")))(functionInvoice->Some((),())|_->None)(fun((),())->Invoice);case(Tag19)~title:"Initial_commitments"(obj2(req"kind"(constant"minted"))(req"category"(constant"commitment")))(functionInitial_commitments->Some((),())|_->None)(fun((),())->Initial_commitments);case(Tag20)~title:"Minted"(obj2(req"kind"(constant"minted"))(req"category"(constant"minted")))(functionMinted->Some((),())|_->None)(fun((),())->Minted);case(Tag21)~title:"Frozen_bonds"(obj4(req"kind"(constant"freezer"))(req"category"(constant"bonds"))(req"contract"Contract_repr.encoding)(req"bond_id"Bond_id_repr.encoding))(functionFrozen_bonds(c,r)->Some((),(),c,r)|_->None)(fun((),(),c,r)->Frozen_bonds(c,r));case(Tag24)~title:"Smart_rollup_refutation_punishments"(obj2(req"kind"(constant"burned"))(req"category"(constant"smart_rollup_refutation_punishments")))(function|Sc_rollup_refutation_punishments->Some((),())|_->None)(fun((),())->Sc_rollup_refutation_punishments);case(Tag25)~title:"Smart_rollup_refutation_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"smart_rollup_refutation_rewards")))(function|Sc_rollup_refutation_rewards->Some((),())|_->None)(fun((),())->Sc_rollup_refutation_rewards);case(Tag26)~title:"Unstaked_deposits"(obj4(req"kind"(constant"freezer"))(req"category"(constant"unstaked_deposits"))(req"staker"Stake_repr.staker_encoding)(req"cycle"Cycle_repr.encoding))(function|Unstaked_deposits(staker,cycle)->Some((),(),staker,cycle)|_->None)(fun((),(),staker,cycle)->Unstaked_deposits(staker,cycle));]letbalance_encoding_with_legacy_attestation_name=balance_encoding~use_legacy_attestation_name:trueletbalance_encoding=balance_encoding~use_legacy_attestation_name:falseletis_not_zeroc=not(Compare.Int.equalc0)letcompare_balancebabb=match(ba,bb)with|Contractca,Contractcb->Contract_repr.comparecacb|Depositssa,Depositssb->Stake_repr.compare_stakersasb|Unstaked_deposits(sa,ca),Unstaked_deposits(sb,cb)->Compare.or_else(Stake_repr.compare_stakersasb)(fun()->Cycle_repr.comparecacb)|Lost_attesting_rewards(pkha,pa,ra),Lost_attesting_rewards(pkhb,pb,rb)->letc=Signature.Public_key_hash.comparepkhapkhbinifis_not_zerocthencelseletc=Compare.Bool.comparepapbinifis_not_zerocthencelseCompare.Bool.comparerarb|Commitmentsbpkha,Commitmentsbpkhb->Blinded_public_key_hash.comparebpkhabpkhb|Frozen_bonds(ca,ra),Frozen_bonds(cb,rb)->letc=Contract_repr.comparecacbinifis_not_zerocthencelseBond_id_repr.comparerarb|_,_->letindexb=matchbwith|Contract_->0|Block_fees->1|Deposits_->2|Unstaked_deposits_->3|Nonce_revelation_rewards->4|Attesting_rewards->5|Baking_rewards->6|Baking_bonuses->7|Storage_fees->8|Double_signing_punishments->9|Lost_attesting_rewards_->10|Liquidity_baking_subsidies->11|Burned->12|Commitments_->13|Bootstrap->14|Invoice->15|Initial_commitments->16|Minted->17|Frozen_bonds_->18|Sc_rollup_refutation_punishments->19|Sc_rollup_refutation_rewards->20(* don't forget to add parameterized cases in the first part of the function *)inCompare.Int.compare(indexba)(indexbb)typebalance_update=DebitedofTez_repr.t|CreditedofTez_repr.tletis_zero_update=functionDebitedt|Creditedt->Tez_repr.(t=zero)letbalance_update_encoding=letopenData_encodingindef"operation_metadata.alpha.balance_update"@@obj1(req"change"(conv(function|Creditedv->Tez_repr.to_mutezv|Debitedv->Int64.neg(Tez_repr.to_mutezv))(Json.wrap_error@@funv->ifCompare.Int64.(v<0L)thenmatchTez_repr.of_mutez(Int64.negv)with|Somev->Debitedv|None->assertfalse(* [of_mutez z] is [None] iff [z < 0] *)elsematchTez_repr.of_mutezvwith|Somev->Creditedv|None->assertfalse(* same *))int64))typeupdate_origin=|Block_application|Protocol_migration|Subsidy|Simulationletcompare_update_originoaob=letindexo=matchowith|Block_application->0|Protocol_migration->1|Subsidy->2|Simulation->3inCompare.Int.compare(indexoa)(indexob)letupdate_origin_encoding=letopenData_encodingindef"operation_metadata.alpha.update_origin"@@obj1@@req"origin"@@union[case(Tag0)~title:"Block_application"(constant"block")(functionBlock_application->Some()|_->None)(fun()->Block_application);case(Tag1)~title:"Protocol_migration"(constant"migration")(functionProtocol_migration->Some()|_->None)(fun()->Protocol_migration);case(Tag2)~title:"Subsidy"(constant"subsidy")(functionSubsidy->Some()|_->None)(fun()->Subsidy);case(Tag3)~title:"Simulation"(constant"simulation")(functionSimulation->Some()|_->None)(fun()->Simulation);]typebalance_updates=(balance*balance_update*update_origin)listletbalance_updates_encoding_with_legacy_attestation_name=letopenData_encodingindef"operation_metadata_with_legacy_attestation_name.alpha.balance_updates"@@list(conv(function|balance,balance_update,update_origin->((balance,balance_update),update_origin))(fun((balance,balance_update),update_origin)->(balance,balance_update,update_origin))(merge_objs(merge_objsbalance_encoding_with_legacy_attestation_namebalance_update_encoding)update_origin_encoding))letbalance_updates_encoding=letopenData_encodingindef"operation_metadata.alpha.balance_updates"@@list(conv(function|balance,balance_update,update_origin->((balance,balance_update),update_origin))(fun((balance,balance_update),update_origin)->(balance,balance_update,update_origin))(merge_objs(merge_objsbalance_encodingbalance_update_encoding)update_origin_encoding))moduleBalanceMap=structincludeMap.Make(structtypet=balance*update_originletcompare(ba,ua)(bb,ub)=letc=compare_balancebabbinifis_not_zerocthencelsecompare_update_originuaubend)letupdate_rkey(f:'aoption->'boptiontzresult)map=f(findkeymap)>>?function|Somev->ok(addkeyvmap)|None->ok(removekeymap)endletgroup_balance_updatesbalance_updates=List.fold_left_e(funacc(b,update,o)->(* Do not do anything if the update is zero *)ifis_zero_updateupdatethenokaccelseBalanceMap.update_r(b,o)(function|None->ok(Someupdate)|Somebalance->(match(balance,update)with|Crediteda,Debitedb|Debitedb,Crediteda->(* Remove the binding since it just fell down to zero *)ifTez_repr.(a=b)thenokNoneelseifTez_repr.(a>b)thenTez_repr.(a-?b)>>?funupdate->ok(Some(Creditedupdate))elseTez_repr.(b-?a)>>?funupdate->ok(Some(Debitedupdate))|Crediteda,Creditedb->Tez_repr.(a+?b)>>?funupdate->ok(Some(Creditedupdate))|Debiteda,Debitedb->Tez_repr.(a+?b)>>?funupdate->ok(Some(Debitedupdate))))acc)BalanceMap.emptybalance_updates>>?funmap->ok(BalanceMap.fold(fun(b,o)uacc->(b,u,o)::acc)map[])