|
| 1 | +{-# LANGUAGE DerivingVia #-} |
| 2 | + |
| 3 | +-- | Bindings to the PLT block state implementation found in @plt-scheduler/block_state.rs@. |
| 4 | +module Concordium.PLTScheduler.PLTBlockState ( |
| 5 | + PLTBlockState, |
| 6 | + empty, |
| 7 | + wrapFFIPtr, |
| 8 | + withPLTBlockState, |
| 9 | + migrate, |
| 10 | + Hash, |
| 11 | + -- | Get the inner @SHA256.Hash@. |
| 12 | + innerSha256Hash, |
| 13 | +) where |
| 14 | + |
| 15 | +import qualified Data.Serialize as Serialize |
| 16 | +import qualified Foreign as FFI |
| 17 | + |
| 18 | +import qualified Concordium.Crypto.SHA256 as SHA256 |
| 19 | +import qualified Concordium.GlobalState.ContractStateFFIHelpers as FFI |
| 20 | +import qualified Concordium.GlobalState.Persistent.BlobStore as BlobStore |
| 21 | +import qualified Concordium.Types.HashableTo as Hashable |
| 22 | +import Control.Monad.Trans (lift, liftIO) |
| 23 | +import qualified Data.FixedByteString as FixedByteString |
| 24 | + |
| 25 | +-- | Opaque pointer to a immutable PLT block state save-point managed by the rust library. |
| 26 | +-- |
| 27 | +-- Memory is deallocated using a finalizer. |
| 28 | +newtype PLTBlockState = PLTBlockState (FFI.ForeignPtr PLTBlockState) |
| 29 | + |
| 30 | +-- | Helper function to convert a raw pointer passed by the Rust library into a `PLTBlockState` object. |
| 31 | +wrapFFIPtr :: FFI.Ptr PLTBlockState -> IO PLTBlockState |
| 32 | +wrapFFIPtr blockStatePtr = PLTBlockState <$> FFI.newForeignPtr ffiFreePLTBlockState blockStatePtr |
| 33 | + |
| 34 | +-- | Deallocate a pointer to `PLTBlockState`. |
| 35 | +foreign import ccall unsafe "&ffi_free_plt_block_state" |
| 36 | + ffiFreePLTBlockState :: FFI.FinalizerPtr PLTBlockState |
| 37 | + |
| 38 | +-- | Get temporary access to the block state pointer. The pointer should not be |
| 39 | +-- leaked from the computation. |
| 40 | +-- |
| 41 | +-- This ensures the finalizer is not called until the computation is over. |
| 42 | +withPLTBlockState :: PLTBlockState -> (FFI.Ptr PLTBlockState -> IO a) -> IO a |
| 43 | +withPLTBlockState (PLTBlockState foreignPtr) = FFI.withForeignPtr foreignPtr |
| 44 | + |
| 45 | +-- | Allocate new empty block state |
| 46 | +empty :: (BlobStore.MonadBlobStore m) => m PLTBlockState |
| 47 | +empty = liftIO $ do |
| 48 | + state <- ffiEmptyPLTBlockState |
| 49 | + wrapFFIPtr state |
| 50 | + |
| 51 | +foreign import ccall "ffi_empty_plt_block_state" |
| 52 | + ffiEmptyPLTBlockState :: IO (FFI.Ptr PLTBlockState) |
| 53 | + |
| 54 | +instance (BlobStore.MonadBlobStore m) => BlobStore.BlobStorable m PLTBlockState where |
| 55 | + load = do |
| 56 | + blobRef <- Serialize.get |
| 57 | + pure $! do |
| 58 | + loadCallback <- fst <$> BlobStore.getCallbacks |
| 59 | + liftIO $! do |
| 60 | + blockState <- ffiLoadPLTBlockState loadCallback blobRef |
| 61 | + wrapFFIPtr blockState |
| 62 | + storeUpdate pltBlockState = do |
| 63 | + storeCallback <- snd <$> BlobStore.getCallbacks |
| 64 | + blobRef <- liftIO $ withPLTBlockState pltBlockState $ ffiStorePLTBlockState storeCallback |
| 65 | + return (Serialize.put blobRef, pltBlockState) |
| 66 | + |
| 67 | +-- | Load PLT block state from the given disk reference. |
| 68 | +foreign import ccall "ffi_load_plt_block_state" |
| 69 | + ffiLoadPLTBlockState :: |
| 70 | + -- | Called to read data from blob store. |
| 71 | + FFI.LoadCallback -> |
| 72 | + -- | Reference in the blob store. |
| 73 | + BlobStore.BlobRef PLTBlockState -> |
| 74 | + -- | Pointer to the loaded block state. |
| 75 | + IO (FFI.Ptr PLTBlockState) |
| 76 | + |
| 77 | +-- | Write out the block state using the provided callback, and return a `BlobRef`. |
| 78 | +foreign import ccall "ffi_store_plt_block_state" |
| 79 | + ffiStorePLTBlockState :: |
| 80 | + -- | The provided closure is called to write data to blob store. |
| 81 | + FFI.StoreCallback -> |
| 82 | + -- | Pointer to the block state to write. |
| 83 | + FFI.Ptr PLTBlockState -> |
| 84 | + -- | New reference in the blob store. |
| 85 | + IO (BlobStore.BlobRef PLTBlockState) |
| 86 | + |
| 87 | +instance (BlobStore.MonadBlobStore m) => BlobStore.Cacheable m PLTBlockState where |
| 88 | + cache blockState = do |
| 89 | + loadCallback <- fst <$> BlobStore.getCallbacks |
| 90 | + liftIO $! withPLTBlockState blockState (ffiCachePLTBlockState loadCallback) |
| 91 | + return blockState |
| 92 | + |
| 93 | +-- | Cache block state into memory. |
| 94 | +foreign import ccall "ffi_cache_plt_block_state" |
| 95 | + ffiCachePLTBlockState :: |
| 96 | + -- | Called to read data from blob store. |
| 97 | + FFI.LoadCallback -> |
| 98 | + -- | Pointer to the block state to cache into memory. |
| 99 | + FFI.Ptr PLTBlockState -> |
| 100 | + IO () |
| 101 | + |
| 102 | +-- | The hash of some `PLTBlockState`. |
| 103 | +newtype Hash = Hash {innerSha256Hash :: SHA256.Hash} |
| 104 | + deriving newtype (Eq, Ord, Show, Serialize.Serialize) |
| 105 | + |
| 106 | +instance (BlobStore.MonadBlobStore m) => Hashable.MHashableTo m Hash PLTBlockState where |
| 107 | + getHashM blockState = do |
| 108 | + loadCallback <- fst <$> BlobStore.getCallbacks |
| 109 | + ((), hash) <- |
| 110 | + liftIO $ |
| 111 | + withPLTBlockState blockState $ |
| 112 | + FixedByteString.createWith . ffiHashPLTBlockState loadCallback |
| 113 | + return $ Hash (SHA256.Hash hash) |
| 114 | + |
| 115 | +-- | Compute the hash of the block state. |
| 116 | +foreign import ccall "ffi_hash_plt_block_state" |
| 117 | + ffiHashPLTBlockState :: |
| 118 | + -- | Called to read data from blob store. |
| 119 | + FFI.LoadCallback -> |
| 120 | + -- | Pointer to the block state to write. |
| 121 | + FFI.Ptr PLTBlockState -> |
| 122 | + -- | Pointer to write destination of the hash |
| 123 | + FFI.Ptr FFI.Word8 -> |
| 124 | + IO () |
| 125 | + |
| 126 | +-- | Run migration during a protocol update. |
| 127 | +migrate :: |
| 128 | + (BlobStore.SupportMigration m t) => |
| 129 | + -- | Current block state |
| 130 | + PLTBlockState -> |
| 131 | + -- | New migrated block state |
| 132 | + t m PLTBlockState |
| 133 | +migrate currentState = do |
| 134 | + loadCallback <- fst <$> lift BlobStore.getCallbacks |
| 135 | + storeCallback <- snd <$> BlobStore.getCallbacks |
| 136 | + newState <- liftIO $ withPLTBlockState currentState $ ffiMigratePLTBlockState loadCallback storeCallback |
| 137 | + liftIO $ PLTBlockState <$> FFI.newForeignPtr ffiFreePLTBlockState newState |
| 138 | + |
| 139 | +-- | Migrate PLT block state from one blob store to another. |
| 140 | +foreign import ccall "ffi_migrate_plt_block_state" |
| 141 | + ffiMigratePLTBlockState :: |
| 142 | + -- | Called to read data from the old blob store. |
| 143 | + FFI.LoadCallback -> |
| 144 | + -- | Called to write data to the new blob store. |
| 145 | + FFI.StoreCallback -> |
| 146 | + -- | Pointer to the old block state. |
| 147 | + FFI.Ptr PLTBlockState -> |
| 148 | + -- | Pointer to the new block state. |
| 149 | + IO (FFI.Ptr PLTBlockState) |
0 commit comments