This commit is contained in:
Dmitry Zuikov 2024-03-27 09:59:05 +03:00
parent 1781202d49
commit 89400efefa
3 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,7 @@
module HBS2.Git.Oracle.Facts where module HBS2.Git.Oracle.Facts where
import HBS2.Git.Oracle.Prelude import HBS2.Git.Oracle.Prelude
import HBS2.Hash
import Data.Word import Data.Word
import Codec.Serialise import Codec.Serialise
@ -63,3 +64,4 @@ instance Pretty GitRepoHeadFact where
instance Pretty GitRepoHeadVersionFact where instance Pretty GitRepoHeadVersionFact where
pretty (GitRepoHeadVersionFact1 v) = pretty v pretty (GitRepoHeadVersionFact1 v) = pretty v

View File

@ -84,8 +84,9 @@ runOracleIndex auPk = do
Right hxs -> do Right hxs -> do
for_ hxs $ \htx -> void $ runMaybeT do for_ hxs $ \htx -> void $ runMaybeT do
done <- liftIO $ withDB db (isTxProcessed (HashVal htx)) done <- liftIO $ withDB db (isTxProcessed (HashVal htx))
guard (not done) done1 <- liftIO $ withDB db (isTxProcessed (processedRepoTx (gitLwwRef,htx)))
guard (not done && not done1)
getBlock sto (fromHashRef htx) >>= toMPlus getBlock sto (fromHashRef htx) >>= toMPlus
<&> deserialiseOrFail @(RefLogUpdate L4Proto) <&> deserialiseOrFail @(RefLogUpdate L4Proto)
>>= toMPlus >>= toMPlus
@ -139,7 +140,7 @@ runOracleIndex auPk = do
debug $ "posted tx" <+> pretty (hashObject @HbSync (serialise f)) debug $ "posted tx" <+> pretty (hashObject @HbSync (serialise f))
-- FIXME: ASAP-wait-refchan-actually-updated -- FIXME: ASAP-wait-refchan-actually-updated
pause @'Seconds 0.25 -- pause @'Seconds 0.25
updateState updateState
@ -294,6 +295,7 @@ updateState = do
chan <- asks _refchanId chan <- asks _refchanId
rchanAPI <- asks _refchanAPI rchanAPI <- asks _refchanAPI
sto <- asks _storage sto <- asks _storage
db <- asks _db
void $ runMaybeT do void $ runMaybeT do
@ -306,6 +308,8 @@ updateState = do
Right txs -> do Right txs -> do
-- FIXME: skip-already-processed-blocks -- FIXME: skip-already-processed-blocks
for_ txs $ \htx -> void $ runMaybeT do for_ txs $ \htx -> void $ runMaybeT do
done <- liftIO $ withDB db (isTxProcessed (HashVal htx))
guard (not done)
getBlock sto (fromHashRef htx) getBlock sto (fromHashRef htx)
>>= toMPlus >>= toMPlus
<&> deserialiseOrFail @(RefChanUpdate L4Proto) <&> deserialiseOrFail @(RefChanUpdate L4Proto)
@ -320,7 +324,7 @@ updateState = do
<&> snd <&> snd
<&> deserialiseOrFail @GitRepoFacts . LBS.fromStrict <&> deserialiseOrFail @GitRepoFacts . LBS.fromStrict
>>= toMPlus >>= toMPlus
>>= lift . S.yield >>= lift . S.yield . (htx,)
let rf = [ (HashRef (hashObject $ serialise f), f) let rf = [ (HashRef (hashObject $ serialise f), f)
| f@GitRepoFact1{} <- universeBi facts | f@GitRepoFact1{} <- universeBi facts
@ -338,8 +342,11 @@ updateState = do
transactional do transactional do
for_ done $ \(r,t) -> do for_ done $ \w -> do
debug $ red "DONE" <+> pretty (r,t) insertTxProcessed (processedRepoTx w)
for_ facts $ \(t,_) -> do
insertTxProcessed (HashVal t)
for_ (HM.toList rf) $ \(k, GitRepoFact1{..}) -> do for_ (HM.toList rf) $ \(k, GitRepoFact1{..}) -> do

View File

@ -1,12 +1,16 @@
module HBS2.Git.Oracle.State where module HBS2.Git.Oracle.State where
import HBS2.Git.Oracle.Prelude import HBS2.Git.Oracle.Prelude
import HBS2.Hash
import DBPipe.SQLite import DBPipe.SQLite
import Data.Coerce import Data.Coerce
import Text.InterpolatedString.Perl6 (qc) import Text.InterpolatedString.Perl6 (qc)
import Data.Word import Data.Word
processedRepoTx :: (LWWRefKey HBS2Basic, HashRef) -> HashVal
processedRepoTx w = HashVal $ HashRef $ hashObject @HbSync (serialise w)
evolveDB :: MonadUnliftIO m => DBPipeM m () evolveDB :: MonadUnliftIO m => DBPipeM m ()
evolveDB = do evolveDB = do
debug $ yellow "evolveDB" debug $ yellow "evolveDB"
@ -124,8 +128,10 @@ insertTxProcessed hash = do
isTxProcessed :: MonadUnliftIO m => HashVal -> DBPipeM m Bool isTxProcessed :: MonadUnliftIO m => HashVal -> DBPipeM m Bool
isTxProcessed hash = do isTxProcessed hash = do
results <- select [qc| results <- select [qc|
select 1 from txprocessed where hash = ? select 1 from txprocessed where hash = ? limit 1
|] (Only hash) |] (Only hash)
pure $ not $ null (results :: [Only Int]) pure $ not $ null (results :: [Only Int])