Dapp State Database¶
The contractvm framework offer a database interface for saving internal state of a dapp. A fresh empty and private state database is created for every dapp, and it is exploitable in the Core of your dapp. The database is a simple key-value database, where we can store array or complex dict object (similar to a json object).
For instance, we can see a basic usage of database in the dapp.Core of the FirstDapp tutorial:
class BlockStoreCore (dapp.Core):
def __init__ (self, chain, database):
super (BlockStoreCore, self).__init__ (chain, database)
def set (self, key, value):
if self.database.exists (key):
return
else:
self.database.set (key, value)
def get (self, key):
if not self.database.exists (key):
return None
else:
return self.database.get (key)
The Database class is more complex than a simple get/set.
Basic usage¶
-
database.
get
(key)¶ Get the value given its key.
Parameters: key (string) – key to query Return type: array or dict
-
database.
set
(key, value)¶ Set a key with a value.
Parameters: - key (string) – key to set
- value (array or dict) – value to set
-
database.
exists
(key)¶ Check if a key exists: return true if key exist.
Parameters: key (string) – key to check Return type: boolean
-
database.
delete
(key)¶ Delete a key.
Parameters: key (string) – key to delete
Integers¶
-
database.
intinc
(key)¶ If key is set and contains a number, increment the value.
Parameters: key (string) – key to increment
-
database.
decinc
(key)¶ If key is set and contains a number, decrement the value.
Parameters: key (string) – key to decrement
Lists¶
-
database.
listappend
(key, item)¶ If key is an array, append item as array item.
Parameters: - key (string) – array key
- item (dict or array) – item to append
-
database.
listremove
(key, item)¶ If key is an array, remove the element item from the array
Parameters: - key (string) – array key
- item (dict or array) – item to remove
Initialization¶
-
database.
init
(key, value)¶ If not exists, initialize key with value.
Parameters: - key (string) – array key
- value (dict or array) – initialization value
-
database.
getinit
(key, value)¶ If not exists, initialize key with value. Return the previous value or the new value
Parameters: - key (string) – array key
- value (dict or array) – initialization value
Return type: dict or array