Store Contract States
Storage Hierarchy
Overview
A Phat contract can utilize three kinds of storage solutions: local cache, contract state, and any external storage services with HTTP requests. We list the differences between the two native storage solutions in Phat Contract as follows.
Persistent | Consistent | Transparently Encrypted | Cost | |
---|---|---|---|---|
Local Cache | ❌ | ❌ | ✅ | Zero |
Contract State | ✅ | ✅ | ✅ | Gas fee |
Local Cache
The local cache is stored in the worker’s memory. As its name, it should only be used as a cache since all cached data is not shared across different workers, and can be lost if the worker is down. Also, you may experience unexpected value cleanup if the memory usage of the worker is high.
The cache can be easily accessed in Pink extension. You can read and write cache in query handlers, but reading cache in transaction handlers is forbidden: the result of cache reading is nondeterministic.
Contract State
Here is how you store simple values as your contract state:
Unlike other ink contracts in which such values are stored publicly on-chain, in Phat Contract, all your contract state is transparently encrypted for you.
Technically speaking, your Phat contract state is not persistently stored anywhere (not even on chain). In Phat contract, any transaction handler functions must be deterministic, so any worker can restore the contract state by replaying all the historical transactions in order (since they are stored on chain). Such design is called Event Sourcing and you can learn more about it in Section 2.2 of our whitepaper.
External Storage Services
With its HTTP support, you can connect to any storage services you like in your Phat contract.
For simple key-value storage, pink-s3 enables you to store data in any storage service with S3-API support.
Such storage service providers include:
Amazon S3 - 5GB, 12 months free
4everland - 5GB free on IPFS and 100MB Free on Arweave
Storj - 150GB free
Filebase - 5GB free
If you want a relational database like MySql, you can also use DBaaS services like
PingCap, which is based on TiDB
Last updated