[{"data":1,"prerenderedAt":138},["ShallowReactive",2],{"\u002Fblog\u002F2020-11-15-watching-deposits-on-a-utxo-chain":3},{"id":4,"title":5,"archiveType":6,"body":7,"category":122,"contextDate":123,"cover":124,"coverAlt":125,"date":126,"description":127,"extension":128,"featured":129,"historicalDate":126,"historicalLabel":123,"meta":130,"navigation":131,"path":132,"publishedDate":123,"seo":133,"stem":134,"thumbnail":123,"workPeriod":135,"writtenDate":136,"__hash__":137},"blogs\u002Fblog\u002F2020-11-15-watching-deposits-on-a-utxo-chain.md","The service between a full node and an app: watching deposits on a UTXO chain","retrospective",{"type":8,"value":9,"toc":112},"minimark",[10,14,17,22,25,28,32,35,38,49,53,56,59,63,66,89,93,96,105,109],[11,12,13],"p",{},"A full node answers questions about the chain. Is this block valid? Is this output unspent? An application needs answers to a different question: has this customer paid, and can I act on it yet? Something has to sit between the two, and much of my work on Trivechain lives in that layer: the mining pool I built, the block explorer I reworked, and the TriveAsset SDK.",[11,15,16],{},"The rules for that layer are the same whoever builds it, so they are worth setting down.",[18,19,21],"h2",{"id":20},"indexes-the-node-does-not-keep-by-default","Indexes the node does not keep by default",[11,23,24],{},"Trivechain's node is built to validate, not to search. Asking it which coins have arrived at an address needs an address index, and in the core code that index is off by default. Our Insight explorer only works against a node started with four indexes switched on in its configuration: transactions, addresses, timestamps and spent outputs, three of which are off by default.",[11,26,27],{},"That leaves the first design decision: lean on the node's indexes, or keep your own database. A service's own database can hold what the node never will: which customer an address belongs to, what has already been credited, and the hash of the block each deposit was seen in. That last field is easy to leave out, and it is the one that matters when the chain changes its mind.",[18,29,31],{"id":30},"when-a-deposit-counts","When a deposit counts",[11,33,34],{},"A transaction in the mempool can be shown as pending. It should never be credited. Once it is in a block, the question is how deep it has to be, and that is a policy chosen before the money arrives, weighed against the amount at stake and how cheaply someone could rewrite that many blocks.",[11,36,37],{},"The chain already gives a few answers. Trivechain's masternode registration guide, which I first wrote last year, asks for the 10,000 TRVC collateral to reach 15 confirmations before it is registered. The core code will not let a block reward be spent until it is 100 blocks deep, so a pool that pays its miners before a reward matures is paying out a reward that a reorganisation can still take back.",[11,39,40,41,48],{},"Ethereum's deposit contract for its new proof-of-stake chain went live on 4 November, and the ",[42,43,47],"a",{"href":44,"rel":45},"https:\u002F\u002Fgithub.com\u002Fethereum\u002Feth2.0-specs\u002Fblob\u002Fv1.0.0\u002Fspecs\u002Fphase0\u002Fvalidator.md",[46],"nofollow","specification"," behind it will not process a deposit until it is at least 2,048 Ethereum blocks deep, about eight hours, followed by a round of voting on top. Different chain, same idea: decide the depth in advance and write it into the code.",[18,50,52],{"id":51},"reorganisations","Reorganisations",[11,54,55],{},"Store the block hash and height with every deposit. When a new block arrives, check that its parent is the block you last processed. If it is not, walk back to the last block both branches share, unwind every block after it, mark those deposits unconfirmed again, and scan forward along the new branch.",[11,57,58],{},"Usually the same transactions reappear in the new branch, at different heights. Occasionally one does not, because a conflicting spend of the same coins was mined instead. A service that credited on one confirmation finds out at this point, and has nothing left to do but chase the money.",[18,60,62],{"id":61},"crediting-that-is-safe-to-replay","Crediting that is safe to replay",[11,64,65],{},"A deposit service will see the same deposit more than once: after a restart, while rescanning after a reorganisation, or when a notification arrives twice. Crediting has to be idempotent, meaning a second attempt changes nothing.",[67,68,69,77,83],"ul",{},[70,71,72,76],"li",{},[73,74,75],"strong",{},"Key every credit by its outpoint."," The transaction ID and output index together identify one output exactly once. Put a unique constraint on that pair, so a replay finds the row and stops.",[70,78,79,82],{},[73,80,81],{},"Keep your own cursor."," Store the last processed height and block hash, and resume from there. Notifications pushed from the node are a prompt to look, not a record of what happened, because they are lost while the service is down.",[70,84,85,88],{},[73,86,87],{},"Record withdrawals before sending them."," Give each withdrawal an ID and save it before building the transaction, then save the signed transaction before broadcasting it. A retry after a crash rebroadcasts that transaction instead of signing a second one.",[18,90,92],{"id":91},"outputs-that-carry-more-than-coins","Outputs that carry more than coins",[11,94,95],{},"On Trivechain an output can also carry a TriveAsset. A watcher that reads only TRVC amounts sees a small payment. One that reads the TriveAsset data sees an asset transfer. A service has to decide which it is watching, and it must not sweep asset-bearing outputs into ordinary change.",[11,97,98,99,104],{},"The ",[42,100,103],{"href":101,"rel":102},"https:\u002F\u002Ftrivechain.github.io\u002Ftriveasset-protocol",[46],"TriveAsset protocol"," makes asset IDs meaningful at zero confirmations, which is what allows an issuance and a transfer in the same transaction. That is a convenience for building transactions. It does not make an unconfirmed transfer final, and the same depth rules apply to assets as to coins.",[18,106,108],{"id":107},"the-contract-in-the-middle","The contract in the middle",[11,110,111],{},"None of this is specific to one product. It is the agreement between a chain that can still rewrite its last few blocks and an application that must not pay twice. The node's job is to follow the valid chain. The layer in between has to keep the application's books consistent with whichever chain the node ends up following.",{"title":113,"searchDepth":114,"depth":114,"links":115},"",2,[116,117,118,119,120,121],{"id":20,"depth":114,"text":21},{"id":30,"depth":114,"text":31},{"id":51,"depth":114,"text":52},{"id":61,"depth":114,"text":62},{"id":91,"depth":114,"text":92},{"id":107,"depth":114,"text":108},"Architecture",null,"\u002Fimg\u002Fblog-covers\u002F2020-11-15-watching-deposits-on-a-utxo-chain.webp","A server joined by a glass tube to a tray, blue beads travelling along it and a row of slots half filled.","2020-11-15","What sits between a Trivechain full node and an application that needs to know when coins arrive: indexes, confirmation depth, reorganisations and crediting that is safe to replay.","md",false,{},true,"\u002Fblog\u002F2020-11-15-watching-deposits-on-a-utxo-chain",{"title":5,"description":127},"blog\u002F2020-11-15-watching-deposits-on-a-utxo-chain","2020","2026-11-07","IFHctc3y0Y9AL2zTS_vHEnZDa7xh1siHXuVPhrnJ-qo",1794838285835]