Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add the concept of incompatible schemas #1800

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 0 additions & 7 deletions eth/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ class TransactionKey(rlp.Serializable):
class BaseChainDB(BaseHeaderDB):
db: BaseAtomicDB = None

@abstractmethod
def __init__(self, db: BaseAtomicDB) -> None:
raise NotImplementedError("ChainDB classes must implement this method")

#
# Header API
#
Expand Down Expand Up @@ -166,9 +162,6 @@ def persist_trie_data_dict(self, trie_data_dict: Dict[Hash32, bytes]) -> None:


class ChainDB(HeaderDB, BaseChainDB):
def __init__(self, db: BaseAtomicDB) -> None:
self.db = db

#
# Header API
#
Expand Down
8 changes: 6 additions & 2 deletions eth/db/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
BaseAtomicDB,
BaseDB,
)
from eth.db.schema import SchemaV1
from eth.db.schema import SchemaV1, Schemas, ensure_schema
from eth.rlp.headers import BlockHeader
from eth.validation import (
validate_block_number,
Expand All @@ -44,8 +44,12 @@
class BaseHeaderDB(ABC):
db: BaseAtomicDB = None

def __init__(self, db: BaseAtomicDB) -> None:
def __init__(self, db: BaseAtomicDB,
expected_schema: Schemas = Schemas.DEFAULT) -> None:
self.db = db
self.schema = expected_schema

ensure_schema(db, expected_schema)

#
# Canonical Chain API
Expand Down
39 changes: 39 additions & 0 deletions eth/db/schema.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
from abc import ABC, abstractmethod
from enum import Enum

from eth.exceptions import (
SchemaDoesNotMatchError,
SchemaNotRecognizedError,
)

from eth.db.backends.base import BaseDB

from eth_typing import (
BlockNumber,
Hash32,
)


class Schemas(Enum):
DEFAULT = b'default'
TURBO = b'turbo'


class BaseSchema(ABC):
@staticmethod
@abstractmethod
Expand Down Expand Up @@ -45,3 +58,29 @@ def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
@staticmethod
def make_transaction_hash_to_block_lookup_key(transaction_hash: Hash32) -> bytes:
return b'transaction-hash-to-block:%s' % transaction_hash


class SchemaTurbo(SchemaV1):
current_schema_lookup_key: bytes = b'current-schema'


def get_schema(db: BaseDB) -> Schemas:
try:
current_schema = db[SchemaTurbo.current_schema_lookup_key]
except KeyError:
return Schemas.DEFAULT

try:
return Schemas(current_schema)
except ValueError:
raise SchemaNotRecognizedError(current_schema)


def set_schema(db: BaseDB, schema: Schemas) -> None:
db.set(SchemaTurbo.current_schema_lookup_key, schema.value)


def ensure_schema(db: BaseDB, expected_schema: Schemas) -> None:
reported_schema = get_schema(db)
if reported_schema != expected_schema:
raise SchemaDoesNotMatchError(reported_schema)
12 changes: 12 additions & 0 deletions eth/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class PyEVMError(Exception):
pass


class SchemaNotRecognizedError(PyEVMError):
"""
The database uses a schema which this version of py-evm does not support.
"""


class SchemaDoesNotMatchError(PyEVMError):
"""
The database schema does not match the expected schema. It might need to be migrated.
"""


class VMNotFound(PyEVMError):
"""
Raised when no VM is available for the provided block number.
Expand Down
6 changes: 5 additions & 1 deletion eth/vm/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from eth.db.backends.base import (
BaseAtomicDB,
)
from eth.db.schema import ensure_schema, Schemas
from eth.exceptions import StateRootNotFound
from eth.tools.logging import (
ExtendedDebugLogger,
Expand Down Expand Up @@ -86,11 +87,14 @@ def __init__(
self,
db: BaseAtomicDB,
execution_context: ExecutionContext,
state_root: bytes) -> None:
state_root: bytes,
expected_schema: Schemas = Schemas.DEFAULT) -> None:
self._db = db
self.execution_context = execution_context
self._account_db = self.get_account_db_class()(db, state_root)

ensure_schema(db, expected_schema)

#
# Logging
#
Expand Down