RGB-WG / rgb-std

RGB standard libs for WASM & low-level integrations (no FS/networking)
https://rgb.tech
Apache License 2.0
66 stars 30 forks source link

storage extension #226

Closed cymqqqq closed 3 months ago

cymqqqq commented 4 months ago

Background: there are strict_serialize_to_file and to_strict_serialized methods in the strict_type, at the 0.11-beta.6 now, we have an fs module to store index, state, and stash data into a local dat format file. (strict_serialize_to_file is used here)

But if people want to store the bytes data in their KV database but not the file, such that I want to set the key as the contract id, and the value as the schema bytes data. Then I can use the to_strict_serialized method to serialize the schema into bytes, then store it in my local leveldb or other KV database.

And when I want to construct the stock runtime, then I can first load the bytes data from the KV database, then use the from_strict_serialized method to deserialize the bytes into the index, stash, and state struct. (Finally, I can build a stock runtime with them).

So I want to ask if there are any possibilities for us to add to_strict_serialized, and from_strict_serialized into a new storage module(like fs), then we can add new store_bytes, and load_bytes traits into stash, state, and index provider. Such as: impl StoreBytes for MemState { fn store_bytes(&self, bytes_data: Vec<u8>) -> Result<(), SerializeError> { self.to_strict_serialized::<U32>(bytes_data); } }

impl LoadBytes for MemState { fn load_bytes(bytes_data: Vec<u8>) -> Result<Self, DeserializeError> { Self::from_strict_serialized::<U32>(bytes_data) } }