Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

add support to rocksdb #151

Merged
merged 4 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions neo/Implementations/Blockchains/RocksDB/CachedScriptTable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from neo.VM.Mixins import ScriptTableMixin


class CachedScriptTable(ScriptTableMixin):

contracts = None

def __init__(self, contracts):
self.contracts = contracts

def GetScript(self, script_hash):

contract = self.contracts.TryGet(script_hash)

if contract is not None:
return contract.Code.Script

return None

def GetContractState(self, script_hash):

contract = self.contracts.TryGet(script_hash)

return contract
170 changes: 170 additions & 0 deletions neo/Implementations/Blockchains/RocksDB/DBCollection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import binascii
import inspect
from logzero import logger

from neo.Implementations.Blockchains.RocksDB.DBPrefix import DBPrefix


class DBCollection():

DB = None
# SN = None
Prefix = None

ClassRef = None

Collection = {}

Changed = []
Deleted = []

_built_keys = False

DebugStorage = False

def __init__(self, db, sn, prefix, class_ref):

self.DB = db

self.Prefix = prefix

self.ClassRef = class_ref

self.Collection = {}
self.Changed = []
self.Deleted = []

@property
def Keys(self):
if not self._built_keys:
self._BuildCollectionKeys()

return self.Collection.keys()

@property
def Current(self):
try:
ret = {}
for key, val in self.Collection.items():
if val is not None:
ret[key] = val
return ret
except Exception as e:
logger.error("error getting items %s " % e)

return {}

def _BuildCollectionKeys(self):
for key in self.DB.iterator(prefix=self.Prefix, include_value=False):
key = key[1:]
if key not in self.Collection.keys():
self.Collection[key] = None

def Commit(self, wb, destroy=True):

for keyval in self.Changed:
item = self.Collection[keyval]
if item:
self.DB.put(self.Prefix + keyval, self.Collection[keyval].ToByteArray())
for keyval in self.Deleted:
self.DB.delete(self.Prefix + keyval)
self.Collection[keyval] = None
if destroy:
self.Destroy()
else:
self.Changed = []
self.Deleted = []

def GetAndChange(self, keyval, new_instance=None, debug_item=False):

item = self.TryGet(keyval)

if item is None:
if new_instance is None:
item = self.ClassRef()
else:
item = new_instance

self.Add(keyval, item)

self.MarkChanged(keyval)

return item

def GetOrAdd(self, keyval, new_instance):

item = new_instance

if keyval in self.Deleted:
self.Deleted.remove(keyval)

self.Add(keyval, item)

return item

def GetItemBy(self, keyval):
return self.GetAndChange(keyval)

def TryGet(self, keyval):

if keyval in self.Deleted:
return None

if keyval in self.Collection.keys():
item = self.Collection[keyval]
if item is None:
item = self._GetItem(keyval)
self.MarkChanged(keyval)
return item

# otherwise, chekc in the database
key = self.DB.get(self.Prefix + keyval)

# if the key is there, get the item
if key is not None:

self.MarkChanged(keyval)

item = self._GetItem(keyval)

return item

return None

def _GetItem(self, keyval):
if keyval in self.Deleted:
return None

try:
buffer = self.DB.get(self.Prefix + keyval)
if buffer:
item = self.ClassRef.DeserializeFromDB(binascii.unhexlify(buffer))
self.Collection[keyval] = item
return item
return None
except Exception as e:
logger.error("Could not deserialize item from key %s : %s" % (keyval, e))

return None

def Add(self, keyval, item):
self.Collection[keyval] = item
self.MarkChanged(keyval)

def Remove(self, keyval):
if keyval not in self.Deleted:
self.Deleted.append(keyval)

def MarkChanged(self, keyval):
if keyval not in self.Changed:
self.Changed.append(keyval)

def Destroy(self):
self.DB = None
# self.SN = None
self.Collection = None
self.ClassRef = None
self.Prefix = None
self.Deleted = None
self.Changed = None
logger = None
18 changes: 18 additions & 0 deletions neo/Implementations/Blockchains/RocksDB/DBPrefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class DBPrefix:

DATA_Block = b'\x01'
DATA_Transaction = b'\x02'

ST_Account = b'\x40'
ST_Coin = b'\x44'
ST_SpentCoin = b'\x45'
ST_Validator = b'\x48'
ST_Asset = b'\x4c'
ST_Contract = b'\x50'
ST_Storage = b'\x70'

IX_HeaderHashList = b'\x80'

SYS_CurrentBlock = b'\xc0'
SYS_CurrentHeader = b'\xc1'
SYS_Version = b'\xf0'
Loading