Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit 061ec78

Browse files
committed
Revert "Attempt to produce SPV proofs in the valid history window (#2280)"
This reverts commit 60912bc.
1 parent 60912bc commit 061ec78

File tree

2 files changed

+15
-58
lines changed

2 files changed

+15
-58
lines changed

src/Chainweb/SPV/CreateProof.hs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import Chainweb.SPV
5656
import Chainweb.TreeDB
5757
import Chainweb.Utils
5858
import Chainweb.Version
59-
import Chainweb.Version.Guards
6059
import Chainweb.WebBlockHeaderDB
6160

6261
import Chainweb.Storage.Table
@@ -110,7 +109,7 @@ createTransactionProof_
110109
-- ^ The index of the transaction in the block
111110
-> IO (TransactionProof SHA512t_256)
112111
createTransactionProof_ headerDb payloadDb tcid scid bh i = do
113-
trgHeader <- findTrgHeader headerDb tcid scid bh
112+
trgHeader <- minimumTrgHeader headerDb tcid scid bh
114113
TransactionProof tcid
115114
<$> createPayloadProof_ transactionProofPrefix headerDb payloadDb tcid scid bh i trgHeader
116115

@@ -216,7 +215,7 @@ createTransactionOutputProof_
216215
-- ^ The index of the transaction in the block
217216
-> IO (TransactionOutputProof SHA512t_256)
218217
createTransactionOutputProof_ headerDb payloadDb tcid scid bh i = do
219-
trgHeader <- findTrgHeader headerDb tcid scid bh
218+
trgHeader <- minimumTrgHeader headerDb tcid scid bh
220219
TransactionOutputProof tcid
221220
<$> createPayloadProof_ outputProofPrefix headerDb payloadDb tcid scid bh i trgHeader
222221

@@ -481,11 +480,7 @@ crumbsToChain db srcCid trgHeader
481480
let !adjIdx = fromJuste $ blockHashRecordChainIdx (view blockAdjacentHashes cur) h
482481
go adjpHdr t ((adjIdx, cur) : acc)
483482

484-
-- | The minimum number of blocks before a newly created proof root expires.
485-
proofSlack :: BlockHeight
486-
proofSlack = 100
487-
488-
findTrgHeader
483+
minimumTrgHeader
489484
:: WebBlockHeaderDb
490485
-> ChainId
491486
-- ^ target chain. The proof asserts that the subject is included in
@@ -496,26 +491,8 @@ findTrgHeader
496491
-- ^ The block height of the transaction, i.e. the subject on the source
497492
-- chain.
498493
-> IO BlockHeader
499-
findTrgHeader headerDb tcid scid bh = do
494+
minimumTrgHeader headerDb tcid scid bh = do
500495
trgHeadHeader <- maxEntry trgChain
501-
let trgHeadHeight = trgHeadHeader ^. blockHeight
502-
let trgHeight = int $
503-
case minimumBlockHeaderHistory (_chainwebVersion headerDb) trgHeadHeight of
504-
Nothing ->
505-
minTrgHeight
506-
Just minHist -> do
507-
-- if there is a block header history limit by now, then any
508-
-- proof with a target outside of that window will not be
509-
-- usable. so ideally, the proof we produce is in that
510-
-- window. that is no guarantee that such a proof is small
511-
-- enough to store on-chain, if the subject is far in the
512-
-- past, but it's better to try than do nothing.
513-
let trgLowestKnownHeight = max trgHeadHeight (int minHist) - int minHist
514-
-- we don't use the lowest known height directly, we try to
515-
-- increase it by a few block heights to avoid the proof
516-
-- immediately expiring before it can be used.
517-
let trgLowestSafeHeight = min trgHeadHeight (trgLowestKnownHeight + proofSlack)
518-
max minTrgHeight trgLowestSafeHeight
519496
seekAncestor trgChain trgHeadHeader trgHeight >>= \case
520497
Just x -> return $! x
521498
Nothing -> throwM $ SpvExceptionTargetNotReachable
@@ -527,11 +504,12 @@ findTrgHeader headerDb tcid scid bh = do
527504
}
528505
where
529506
trgChain = headerDb ^?! ixg tcid
530-
minTrgHeight
507+
trgHeight
531508
| srcGraph == trgGraph = int bh + int srcDistance
532509
| otherwise = int bh + int srcDistance + int trgDistance
533510
-- This assumes that graph changes are at least graph-diameter
534511
-- blocks appart.
512+
535513
srcGraph = chainGraphAt headerDb bh
536514
srcDistance = length $ shortestPath tcid scid srcGraph
537515
trgGraph = chainGraphAt headerDb (bh + int srcDistance)

test/unit/Chainweb/Test/Pact5/RemotePactTest.hs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -363,65 +363,44 @@ spvExpirationTest baseRdb _step = runResourceT $ do
363363
send fx v srcChain [initiator]
364364
let initiatorReqKey = cmdToRequestKey initiator
365365

366-
-- You have to wait at least a diameter to get the block.
366+
-- You have to wait at least N blocks before attempting to run the continuation,
367367
-- where N is the diameter of the graph + some constant. Here, we just run 10 blocks,
368368
-- because it is less than the minimumBlockHeaderHistory of the test version, but
369369
-- more than sufficient for the target chain to be aware of the source xchain transfer.
370370
let waitBlocks :: Integral a => a
371-
waitBlocks = int $ diameter (chainGraphAt v maxBound) + 1
371+
waitBlocks = 10
372372
let expirationWindow = fromMaybe (error "missing minimumBlockHeaderHistory") (minimumBlockHeaderHistory v maxBound)
373373
when (int expirationWindow < waitBlocks + diameter (chainGraphAt v maxBound)) $ assertFailure "test version has a minimumBlockHeaderHistory that is too short to test"
374374

375375
replicateM_ waitBlocks $ advanceAllChains_ fx
376-
377376
[Just sendCr] <- pollWithDepth fx v srcChain [initiatorReqKey] (Just (ConfirmationDepth 0))
378377
let cont = fromMaybe (error "missing continuation") (_crContinuation sendCr)
379-
380-
TransactionOutputProofB64 expiringSpvProof <- spvTxOutProof fx v targetChain srcChain initiatorReqKey
381-
let expiringContMsg = ContMsg
378+
TransactionOutputProofB64 spvProof <- spvTxOutProof fx v targetChain srcChain initiatorReqKey
379+
let contMsg = ContMsg
382380
{ _cmPactId = _peDefPactId cont
383381
, _cmStep = succ $ _peStep cont
384382
, _cmRollback = _peStepHasRollback cont
385383
, _cmData = PUnit
386-
, _cmProof = Just (ContProof (T.encodeUtf8 expiringSpvProof))
384+
, _cmProof = Just (ContProof (T.encodeUtf8 spvProof))
387385
}
388386

387+
-- We have already run an extra 10 blocks since initiating the transfer.
389388
-- We need to wait for the full window to expire.
389+
390390
-- still accessible at remainingWindow + diameter + 1 so we have to go one beyond
391391
let remainingWindow = int @_ @Int (int expirationWindow - waitBlocks + diameter (chainGraphAt v maxBound) + 2)
392392
replicateM_ remainingWindow $ advanceAllChains_ fx
393393

394-
expiringRecv <- buildTextCmd v
395-
$ set cbRPC (mkCont expiringContMsg)
396-
$ defaultCmd targetChain
397-
send fx v targetChain [expiringRecv]
398-
let expiringRecvReqKey = cmdToRequestKey expiringRecv
399-
advanceAllChains_ fx
400-
poll fx v targetChain [expiringRecvReqKey]
401-
>>= P.match (_head . _Just)
402-
? P.checkAll
403-
[ P.fun _crResult ? P.match _PactResultErr ? P.fun _peMsg ? P.equals "Continuation error: spv verification failed: target header is not in the chain or is out of bounds"
404-
]
405-
406-
TransactionOutputProofB64 spvWorkingProof <- spvTxOutProof fx v targetChain srcChain initiatorReqKey
407-
let currentContMsg = ContMsg
408-
{ _cmPactId = _peDefPactId cont
409-
, _cmStep = succ $ _peStep cont
410-
, _cmRollback = _peStepHasRollback cont
411-
, _cmData = PUnit
412-
, _cmProof = Just (ContProof (T.encodeUtf8 spvWorkingProof))
413-
}
414-
415394
recv <- buildTextCmd v
416-
$ set cbRPC (mkCont currentContMsg)
395+
$ set cbRPC (mkCont contMsg)
417396
$ defaultCmd targetChain
418397
send fx v targetChain [recv]
419398
let recvReqKey = cmdToRequestKey recv
420399
advanceAllChains_ fx
421400
poll fx v targetChain [recvReqKey]
422401
>>= P.match (_head . _Just)
423402
? P.checkAll
424-
[ P.fun _crResult ? P.match _PactResultOk P.succeed
403+
[ P.fun _crResult ? P.match _PactResultErr ? P.fun _peMsg ? P.equals "Continuation error: spv verification failed: target header is not in the chain or is out of bounds"
425404
]
426405

427406

0 commit comments

Comments
 (0)