Use the ledger's context or a specific map?
- Leonid_RozenbergOP
Consider an author trying to maintain a library on the ledger with
template Book with author : Party isin : Text chapters : [Int] where signatory author key (author, isin) : (Party, Text) maintainer key._1 template Chapter with author : Party bookIsin : Text chapter : Int text : Text bookId : ContractId Book where signatory author key (author, bookIsin, chapter) : (Party, Text, Int) maintainer key._1Now the author uses the CreateAndExercise pattern to upload the library to the ledger. But within the processing of the upload, there are two options to keep track of the association between
Books andChapters: via the ledger or via a specific Map.template UploadLibrary with author : Party books : [(Text, [Int])] -- isin, chapters chapters : [(Text, Int, Text)] -- isin, chapter, text where signatory author choice CreateViaLedger : ([ContractId Book], [ContractId Chapter]) controller author do bookIds <- forA books (\(isin, chapters) -> create Book with ..) chapterIds <- forA chapters (\(bookIsin, chapter, text) -> do -- books have been created Some bookId <- lookupByKey @Book (author, bookIsin) create Chapter with ..) return (bookIds, chapterIds) choice CreateViaMap : ([ContractId Book], [ContractId Chapter]) controller author do (bookMap, bookIds) <- foldlA (\(bookMap, bookIds) (isin, chapters) -> do bookId <- create Book with .. return (insert isin bookId bookMap, bookId::bookIds) ) (mempty, []) books chapterIds <- forA chapters (\(bookIsin, chapter, text) -> do let Some bookId = lookup bookIsin bookMap create Chapter with ..) return (bookIds, chapterIds)What do you think is the better approach ?
- The two methods are not equivalent as the former allows one to send updates on
Chapters outside of theUploadLibrarypayload; those that might exist previously. - Is there a performance tradeoff to using the
lookupByKeyinstead of the regularMap.lookup?
- The two methods are not equivalent as the former allows one to send updates on
- Stephen
Unless you can exploit the semantics of a
Chapterin a ledger-specific way, e.g. by having someChapters observable to different parties from their associatedBooks, I don’t see much value in having separateChaptercontracts here.lookupin aMapis faster than looking up by contract key for small maps. The only timelookupByKeywill have a performance advantage is when theMapis larger or modified sufficiently frequently; the wholeMapis included with each transmitted copy of the contract that contains it, after all, and therefore you get knock-on effects like, if you build up a map one element at a time, one exercise at a time, you get n² ledger space usage. That doesn’t seem relevant at all for this example, though.