cic-cli/cic/contract/base.py

79 lines
1.7 KiB
Python
Raw Normal View History

2021-10-09 19:56:29 +02:00
# standard imports
import os
2021-10-10 21:30:18 +02:00
import hashlib
2021-10-09 19:56:29 +02:00
2022-03-01 09:17:17 +01:00
mod_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
2021-10-09 19:56:29 +02:00
root_dir = os.path.join(mod_dir, '..')
data_dir = os.path.join(mod_dir, 'data')
schema_dir = os.path.join(mod_dir, 'schema')
class Data:
2021-11-29 13:18:28 +01:00
"""Base class for all parts of the token data deployment.
"""
2021-10-09 19:56:29 +02:00
__default_version = 0
def __init__(self):
self.dirty = False
self.inited = False
self.__version = self.__default_version
2021-10-10 21:30:18 +02:00
self.__hasher = self.__basehasher
def __basehasher(self, v):
h = hashlib.sha256()
h.update(v)
return h.digest()
def hash(self, v):
2021-11-29 13:18:28 +01:00
"""Compute digest of the given data
:param v: Data to hash
:type v: bytes
:rtype: bytes
:return: Hashed data
"""
2021-10-10 21:30:18 +02:00
return self.__hasher(v)
2021-10-09 19:56:29 +02:00
def load(self):
2021-11-29 13:18:28 +01:00
"""Prevents overwriting data from settings if data state has changed.
:raises RuntimeError: If state is dirty
"""
2021-10-09 19:56:29 +02:00
if self.dirty:
raise RuntimeError('Object contains uncommitted changes')
def start(self):
2021-11-29 13:18:28 +01:00
"""Prevents double initialization of data item.
:raises RuntimeError: If already initialized
"""
2021-10-09 19:56:29 +02:00
if self.inited:
raise RuntimeError('Object already initialized')
2021-10-10 14:49:22 +02:00
def verify(self):
2021-11-29 13:18:28 +01:00
"""Verify data state (noop)
"""
2021-10-10 14:49:22 +02:00
return True
2021-10-09 19:56:29 +02:00
def version(self):
2021-11-29 13:18:28 +01:00
"""Return version of data schema.
"""
2021-10-09 19:56:29 +02:00
return self.__version
2021-10-09 20:37:54 +02:00
def set_version(self, version):
2021-11-29 13:18:28 +01:00
"""Set version of data schema. The version number is a single integer.
2021-10-09 20:37:54 +02:00
2021-11-29 13:18:28 +01:00
:param version: version
:type version: int
"""
self.__version = version