Skip to content

Storage Guide

Antonio Yang edited this page Feb 26, 2022 · 3 revisions

Storage Guide

The basic storage unit of Ethereum is Bytes32, this means you still need store 32 bytes to store a byte of data. There are some low level concern on the storage, but it should not be required for a developer to build some application on top of blockchain network. This why Sewup try to provide kv, rdb features and try to do some abstraction on the storage. However, the thing developer still keep in mind is that the storage space on blockchain is expensive than traditional web, due to multiple data copy in the clusters.

In SewUp, the Raw structure is the basic storage unit. It also a wrapper of Bytes32. It is easy to convert from Address, unsigned integers, str, &str, String, &String, Vec<u8>, [u8], Address, unsigned integer types or into Bytes32, Bytes20. Row is the list structure of Raw, and Raw can try_from Row, on the other hand Raw can convert into Row.

For example,

let proposals_bucket = storage.bucket::<usize, Proposal>("proposals")?
let raw = Raw::from(777usize);
let lucky_number: usize = raw.into();

Currently, there is no constant, singleton, and array-like storage.
Here are some suggestions:

  • treat the constant as the same as the Rust constance
  • use bucket to store one global object, and all singleton variables can store in it
  • use usize or other unsigned integer type as key of bucket, such that the bucket can store and iterate items like array-like storage

If you want to store a limited length of String, you can use the SizedString! and SizedString, this macro help you use correct size of Raw to store the String.

For example,

let ss = sewup::types::SizedString::new(10).from_str("hello").unwrap();
assert!(ss.len() == 5);
assert!(ss.capacity() == 10);
assert_eq!(ss.to_utf8_string().unwrap(), "hello");

If you want to debug the storage, you can use ewasm_storage_debug!() or ewasm_storage_debug!(point1) to inspect the contract storage as following image. debug storage