123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2019-2020 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2021 DaiLambda, Inc. <contact@dailambda.jp> *)(* *)(* 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. *)(* *)(*****************************************************************************)letlogfmt=Logging.logNoticefmt(* Remove me after protocol H *)moduleFlatten_storage_for_H=struct(* /tree_abs_key/key/*/*/*/*/*
=> /tree_abs_key/key/rename( */*/*/*/* )
*)letflatten~tree~key~depth~rename~init=Raw_context.Tree.foldtreekey~depth:(`Eqdepth)~init~f:(funold_keytreedst_tree->letnew_key=renameold_keyinRaw_context.Tree.add_treedst_treenew_keytree)>>=fundst_tree->(* rm -rf $index_key
mv $tmp_index_key $index_key *)Raw_context.Tree.add_treetreekeydst_tree(* /abs_key/*(depth')/mid_key/*(depth)
=> /abs_key/*(depth')/mid_key/rename( *(depth) )
*)letfold_flattenctxtabs_keydepth'mid_key~depth~rename=Raw_context.fold~depth:(`Eqdepth')ctxtabs_key~init:ctxt~f:(funkeytreectxt->(* tree at /abs_key/*(depth') *)flatten~tree~key:mid_key~depth~rename~init:(Raw_context.Tree.emptyctxt)>>=funtree->Raw_context.add_treectxt(abs_key@key)tree)letflatten_storagectxt=log"flattening the context storage: this operation may take several minutes";letrecdropnxs=match(n,xs)with|(0,_)->xs|(_,[])->assertfalse|(_,_::xs)->drop(n-1)xsinletrename_blake2b=function|n1::n2::n3::n4::n5::n6::rest->String.concat""[n1;n2;n3;n4;n5;n6]::rest|_->assertfalseinletrename_public_key_hash=function|(("ed25519"|"secp256k1"|"p256")ask)::n1::n2::n3::n4::n5::n6::rest->k::String.concat""[n1;n2;n3;n4;n5;n6]::rest|_->assertfalsein(* /contracts/index/xx/xx/xx/xx/xx/xx/yyyyyyyyyy
=> /contracts/index/yyyyyyyyyy
*)fold_flattenctxt["contracts";"index"]0[]~depth:7~rename:(drop6)>>=functxt->(* *)(* /contracts/index/yyyyyyyyyy/delegated/xx/xx/xx/xx/xx/xx/zzzzzzzzzz
=> /contracts/index/yyyyyyyyyy/delegated/zzzzzzzzzz
*)fold_flattenctxt["contracts";"index"]1["delegated"]~depth:7~rename:(drop6)>>=functxt->(* *)(* /big_maps/index/xx/xx/xx/xx/xx/xx/n
=> /big_maps/index/n
*)fold_flattenctxt["big_maps";"index"]0[]~depth:7~rename:(drop6)>>=functxt->(* *)(* /big_maps/index/n/contents/yy/yy/yy/yy/yy/yyyyyyyy
=> /big_maps/index/n/contents/yyyyyyyyyyyyyyyyyy
*)fold_flattenctxt["big_maps";"index"]1["contents"]~depth:6~rename:rename_blake2b>>=functxt->(* *)(* /rolls/index/x/y/n
=> /rolls/index/n
*)fold_flattenctxt["rolls";"index"]0[]~depth:3~rename:(drop2)>>=functxt->(* *)(* /rolls/owner/current/x/y/n
=> /rolls/owner/current/n
*)fold_flattenctxt["rolls";"owner";"current"]0[]~depth:3~rename:(drop2)>>=functxt->(* *)(* /rolls/owner/snapshot/n1/n2/x/y/n3
=> /rolls/owner/snapshot/n1/n2/n3
*)fold_flattenctxt["rolls";"owner";"snapshot"]2[]~depth:3~rename:(drop2)>>=functxt->(* *)(* /commitments/xx/xx/xx/xx/xx/xxxxxx
=> /commitments/xxxxxxxxxxxxxxxx
*)fold_flattenctxt["commitments"]0[]~depth:6~rename:rename_blake2b>>=functxt->(* *)(* /votes/listings/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /votes/listings/kk/xxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["votes";"listings"]0[]~depth:7~rename:rename_public_key_hash>>=functxt->(* *)(* /votes/ballots/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /votes/ballots/KK/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["votes";"ballots"]0[]~depth:7~rename:rename_public_key_hash>>=functxt->(* *)(* /votes/proposals_count/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /votes/proposals_count/kk/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["votes";"proposals_count"]0[]~depth:7~rename:rename_public_key_hash>>=functxt->(* *)(* /votes/proposals/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /votes/proposals/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["votes";"proposals"]0[]~depth:6~rename:rename_blake2b>>=functxt->(* *)(* /votes/proposals/yyyyyyyyyyyyyyyyyyyy/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /votes/proposals/yyyyyyyyyyyyyyyyyyyy/KK/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["votes";"proposals"]1[]~depth:7~rename:rename_public_key_hash>>=functxt->(* *)(* /delegates/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /delegates/KK/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["delegates"]0[]~depth:7~rename:rename_public_key_hash>>=functxt->(* *)(* /active_delegates_with_rolls/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /active_delegates_with_rolls/KK/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["active_delegates_with_rolls"]0[]~depth:7~rename:rename_public_key_hash>>=functxt->(* *)(* /delegates_with_frozen_balance/n/kk/xx/xx/xx/xx/xx/xx/xxxxxxxx
=> /delegates_with_frozen_balance/n/KK/xxxxxxxxxxxxxxxxxxxx
*)fold_flattenctxt["delegates_with_frozen_balance"]1[]~depth:7~rename:rename_public_key_hash>|=functxt->log"context storage flattening completed";ctxtend(*
To add invoices, you can use a helper function like this one:
(** Invoice a contract at a given address with a given amount. Returns the
updated context and a balance update receipt (singleton list). The address
must be a valid base58 hash, otherwise this is no-op and returns an empty
receipts list.
Do not fail if something goes wrong.
*)
let invoice_contract ctxt ~address ~amount_mutez =
match Tez_repr.of_mutez amount_mutez with
| None ->
Lwt.return (ctxt, [])
| Some amount -> (
Contract_repr.of_b58check address
>>?= (fun recipient ->
Contract_storage.credit ctxt recipient amount
>|=? fun ctxt ->
( ctxt,
Receipt_repr.
[(Contract recipient, Credited amount, Protocol_migration)] ))
>|= function Ok res -> res | Error _ -> (ctxt, []) )
*)letprepare_first_blockctxt~typecheck~level~timestamp~fitness=Raw_context.prepare_first_block~level~timestamp~fitnessctxt>>=?fun(previous_protocol,ctxt)->matchprevious_protocolwith|Genesisparam->(* This is the genesis protocol: initialise the state *)Commitment_storage.initctxtparam.commitments>>=?functxt->Roll_storage.initctxt>>=?functxt->Seed_storage.initctxt>>=?functxt->Contract_storage.initctxt>>=?functxt->Bootstrap_storage.initctxt~typecheck?ramp_up_cycles:param.security_deposit_ramp_up_cycles?no_reward_cycles:param.no_reward_cyclesparam.bootstrap_accountsparam.bootstrap_contracts>>=?functxt->Roll_storage.init_first_cyclesctxt>>=?functxt->Vote_storage.initctxt~start_position:(Level_storage.currentctxt).level_position>>=?functxt->Storage.Block_priority.initctxt0>>=?functxt->Vote_storage.update_listingsctxt>>=?functxt->(* Must be called after other originations since it unsets the origination nonce.*)Liquidity_baking_migration.initctxt~typecheck>>=?fun(ctxt,operation_results)->Storage.Pending_migration.Operation_results.initctxtoperation_results|Granada_010->Flatten_storage_for_H.flatten_storagectxt>>=returnletpreparectxt~level~predecessor_timestamp~timestamp~fitness=Raw_context.prepare~level~predecessor_timestamp~timestamp~fitnessctxt>>=?functxt->Storage.Pending_migration.removectxt