|
| 1 | +package com.example.linkedlist.onchain; |
| 2 | + |
| 3 | +import com.bloxbean.cardano.julc.core.PlutusData; |
| 4 | +import com.bloxbean.cardano.julc.ledger.*; |
| 5 | +import com.bloxbean.cardano.julc.stdlib.Builtins; |
| 6 | +import com.bloxbean.cardano.julc.stdlib.annotation.OnchainLibrary; |
| 7 | +import com.bloxbean.cardano.julc.stdlib.lib.ByteStringLib; |
| 8 | +import com.bloxbean.cardano.julc.stdlib.lib.OutputLib; |
| 9 | +import com.bloxbean.cardano.julc.stdlib.lib.ValuesLib; |
| 10 | + |
| 11 | +import java.math.BigInteger; |
| 12 | + |
| 13 | +/** |
| 14 | + * Reusable on-chain validation logic for a linked list stored as UTXOs. |
| 15 | + * Each node is a separate UTXO holding a unique NFT; nodes link via datum fields. |
| 16 | + * <p> |
| 17 | + * Methods are ordered bottom-up (helpers before callers) for single-pass compilation. |
| 18 | + */ |
| 19 | +@OnchainLibrary |
| 20 | +public class LinkedListLib { |
| 21 | + |
| 22 | + record ListElement(PlutusData userData, byte[] nextKey) {} |
| 23 | + |
| 24 | + // === Helpers === |
| 25 | + |
| 26 | + static byte[] extractNodeKey(byte[] tokenName, int prefixLen) { |
| 27 | + return ByteStringLib.drop(tokenName, prefixLen); |
| 28 | + } |
| 29 | + |
| 30 | + static byte[] buildTokenName(byte[] prefix, byte[] key) { |
| 31 | + return ByteStringLib.append(prefix, key); |
| 32 | + } |
| 33 | + |
| 34 | + static boolean isRootToken(byte[] tokenName, byte[] rootKey) { |
| 35 | + return tokenName.equals(rootKey); |
| 36 | + } |
| 37 | + |
| 38 | + static boolean requireListTokensMintedOrBurned(Value mint, byte[] policyId) { |
| 39 | + return ValuesLib.containsPolicy(mint, policyId); |
| 40 | + } |
| 41 | + |
| 42 | + // === Validation === |
| 43 | + |
| 44 | + static boolean validateInit(TxOut rootOutput, Value mint, byte[] policyId, |
| 45 | + byte[] rootKey, Address scriptAddr) { |
| 46 | + boolean atScript = Builtins.equalsData(rootOutput.address(), scriptAddr); |
| 47 | + |
| 48 | + ListElement datum = (ListElement)(Object) OutputLib.getInlineDatum(rootOutput); |
| 49 | + boolean emptyNext = datum.nextKey().equals(Builtins.emptyByteString()); |
| 50 | + |
| 51 | + BigInteger rootQty = ValuesLib.assetOf(mint, policyId, rootKey); |
| 52 | + boolean rootMinted = rootQty.compareTo(BigInteger.ONE) == 0; |
| 53 | + |
| 54 | + BigInteger mintCount = ValuesLib.countTokensWithQty(mint, policyId, BigInteger.ONE); |
| 55 | + boolean onlyOne = mintCount.compareTo(BigInteger.ONE) == 0; |
| 56 | + |
| 57 | + return atScript && emptyNext && rootMinted && onlyOne; |
| 58 | + } |
| 59 | + |
| 60 | + static boolean validateDeinit(TxOut rootInputResolved, Value mint, byte[] policyId, |
| 61 | + byte[] rootKey) { |
| 62 | + ListElement datum = (ListElement)(Object) OutputLib.getInlineDatum(rootInputResolved); |
| 63 | + boolean emptyNext = datum.nextKey().equals(Builtins.emptyByteString()); |
| 64 | + |
| 65 | + BigInteger rootQty = ValuesLib.assetOf(mint, policyId, rootKey); |
| 66 | + boolean rootBurned = rootQty.compareTo(BigInteger.ONE.negate()) == 0; |
| 67 | + |
| 68 | + BigInteger burnCount = ValuesLib.countTokensWithQty(mint, policyId, BigInteger.ONE.negate()); |
| 69 | + boolean onlyOne = burnCount.compareTo(BigInteger.ONE) == 0; |
| 70 | + |
| 71 | + return emptyNext && rootBurned && onlyOne; |
| 72 | + } |
| 73 | + |
| 74 | + static boolean validateInsert(TxOut anchorInputResolved, TxOut contAnchorOutput, |
| 75 | + TxOut newElementOutput, Value mint, byte[] policyId, |
| 76 | + byte[] rootKey, byte[] prefix, int prefixLen, |
| 77 | + Address scriptAddr) { |
| 78 | + // Discover anchor's NFT token name |
| 79 | + byte[] anchorTokenName = ValuesLib.findTokenName( |
| 80 | + anchorInputResolved.value(), policyId, BigInteger.ONE); |
| 81 | + boolean anchorIsRoot = isRootToken(anchorTokenName, rootKey); |
| 82 | + |
| 83 | + // Discover newly minted NFT token name and extract key |
| 84 | + byte[] newTokenName = ValuesLib.findTokenName(mint, policyId, BigInteger.ONE); |
| 85 | + byte[] newKey = extractNodeKey(newTokenName, prefixLen); |
| 86 | + |
| 87 | + // Token name must be prefix + key |
| 88 | + byte[] expectedName = buildTokenName(prefix, newKey); |
| 89 | + boolean nameCorrect = newTokenName.equals(expectedName); |
| 90 | + |
| 91 | + // Anchor NFT preserved in continuing output |
| 92 | + BigInteger contQty = ValuesLib.assetOf(contAnchorOutput.value(), policyId, anchorTokenName); |
| 93 | + boolean anchorPreserved = contQty.compareTo(BigInteger.ONE) == 0; |
| 94 | + |
| 95 | + // Continuing anchor and new element must be at script address |
| 96 | + boolean contAtScript = Builtins.equalsData(contAnchorOutput.address(), scriptAddr); |
| 97 | + boolean newAtScript = Builtins.equalsData(newElementOutput.address(), scriptAddr); |
| 98 | + |
| 99 | + // Extract datums |
| 100 | + ListElement anchorOld = (ListElement)(Object) OutputLib.getInlineDatum(anchorInputResolved); |
| 101 | + ListElement contAnchor = (ListElement)(Object) OutputLib.getInlineDatum(contAnchorOutput); |
| 102 | + ListElement newElement = (ListElement)(Object) OutputLib.getInlineDatum(newElementOutput); |
| 103 | + |
| 104 | + // Anchor userData unchanged |
| 105 | + boolean dataUnchanged = Builtins.equalsData(anchorOld.userData(), contAnchor.userData()); |
| 106 | + |
| 107 | + // Continuing anchor's nextKey = new node's key |
| 108 | + boolean contNextOk = contAnchor.nextKey().equals(newKey); |
| 109 | + |
| 110 | + // New element's nextKey = anchor's old nextKey |
| 111 | + boolean newNextOk = newElement.nextKey().equals(anchorOld.nextKey()); |
| 112 | + |
| 113 | + // Ordering: anchorKey < newKey (skip if root), newKey < oldNextKey (skip if end) |
| 114 | + byte[] oldNextKey = anchorOld.nextKey(); |
| 115 | + boolean insertAtEnd = oldNextKey.equals(Builtins.emptyByteString()); |
| 116 | + byte[] anchorKey = extractNodeKey(anchorTokenName, prefixLen); |
| 117 | + boolean orderOk = (anchorIsRoot || ByteStringLib.lessThan(anchorKey, newKey)) |
| 118 | + && (insertAtEnd || ByteStringLib.lessThan(newKey, oldNextKey)); |
| 119 | + |
| 120 | + // Exactly 1 new NFT minted under this policy |
| 121 | + BigInteger mintCount = ValuesLib.countTokensWithQty(mint, policyId, BigInteger.ONE); |
| 122 | + boolean exactlyOne = mintCount.compareTo(BigInteger.ONE) == 0; |
| 123 | + |
| 124 | + return nameCorrect && anchorPreserved && contAtScript && newAtScript |
| 125 | + && dataUnchanged && contNextOk && newNextOk && orderOk && exactlyOne; |
| 126 | + } |
| 127 | + |
| 128 | + static boolean validateRemove(TxOut anchorInputResolved, TxOut removingInputResolved, |
| 129 | + TxOut contAnchorOutput, Value mint, byte[] policyId, |
| 130 | + byte[] rootKey, byte[] prefix, int prefixLen, |
| 131 | + Address scriptAddr) { |
| 132 | + // Discover token names |
| 133 | + byte[] anchorTokenName = ValuesLib.findTokenName( |
| 134 | + anchorInputResolved.value(), policyId, BigInteger.ONE); |
| 135 | + byte[] removingTokenName = ValuesLib.findTokenName( |
| 136 | + removingInputResolved.value(), policyId, BigInteger.ONE); |
| 137 | + byte[] removingKey = extractNodeKey(removingTokenName, prefixLen); |
| 138 | + |
| 139 | + // Extract datums |
| 140 | + ListElement anchorDatum = (ListElement)(Object) OutputLib.getInlineDatum(anchorInputResolved); |
| 141 | + ListElement removingDatum = (ListElement)(Object) OutputLib.getInlineDatum(removingInputResolved); |
| 142 | + ListElement contDatum = (ListElement)(Object) OutputLib.getInlineDatum(contAnchorOutput); |
| 143 | + |
| 144 | + // Anchor must link to the removing node |
| 145 | + boolean anchorLinksToRemoving = anchorDatum.nextKey().equals(removingKey); |
| 146 | + |
| 147 | + // Anchor NFT preserved in continuing output |
| 148 | + BigInteger contQty = ValuesLib.assetOf(contAnchorOutput.value(), policyId, anchorTokenName); |
| 149 | + boolean anchorPreserved = contQty.compareTo(BigInteger.ONE) == 0; |
| 150 | + |
| 151 | + // Continuing anchor at script address |
| 152 | + boolean contAtScript = Builtins.equalsData(contAnchorOutput.address(), scriptAddr); |
| 153 | + |
| 154 | + // Anchor userData unchanged |
| 155 | + boolean dataUnchanged = Builtins.equalsData(anchorDatum.userData(), contDatum.userData()); |
| 156 | + |
| 157 | + // Continuing anchor's nextKey = removing node's nextKey (skip over) |
| 158 | + boolean skipCorrect = contDatum.nextKey().equals(removingDatum.nextKey()); |
| 159 | + |
| 160 | + // Removing node's NFT burned |
| 161 | + BigInteger burnQty = ValuesLib.assetOf(mint, policyId, removingTokenName); |
| 162 | + boolean nftBurned = burnQty.compareTo(BigInteger.ONE.negate()) == 0; |
| 163 | + |
| 164 | + // Exactly 1 token burned |
| 165 | + BigInteger burnCount = ValuesLib.countTokensWithQty(mint, policyId, BigInteger.ONE.negate()); |
| 166 | + boolean exactlyOneBurned = burnCount.compareTo(BigInteger.ONE) == 0; |
| 167 | + |
| 168 | + return anchorLinksToRemoving && anchorPreserved && contAtScript && dataUnchanged |
| 169 | + && skipCorrect && nftBurned && exactlyOneBurned; |
| 170 | + } |
| 171 | +} |
0 commit comments