예제 #1
0
파일: db.py 프로젝트: mikeshultz/blocks
""" Database models and utilities """
import os
import sys
import random
import psycopg2
from datetime import datetime
from eth_utils.address import is_address
from rawl import RawlBase

from typing import List, Tuple

from blocks.utils import is_256bit_hash, validate_conditions
from blocks.config import LOGGER
from blocks.exceptions import InvalidRange, LockExists

log = LOGGER.getChild('db')

MAX_LOCKS = 50


class ConsumerModel(RawlBase):
    def __init__(self, dsn: str):
        super(ConsumerModel, self).__init__(dsn,
                                            table_name='consumer',
                                            columns=[
                                                'consumer_uuid', 'port',
                                                'address', 'name', 'active'
                                                'last_seen'
                                            ],
                                            pk_name='consumer_uuid')
예제 #2
0
import os
import threading
from time import sleep
from uuid import uuid4
from time import sleep
from datetime import datetime, timedelta
from psycopg2.errors import UniqueViolation
from eth_utils import add_0x_prefix
from web3 import Web3, HTTPProvider

from blocks.config import DSN, JSONRPC_NODE, LOGGER
from blocks.db import BlockModel, TransactionModel
from blocks.enums import WorkerType
from blocks.conductorclient import ConnectionError, ping, job_request, job_submit

log = LOGGER.getChild(__name__)


class TransactionPriming(threading.Thread):
    """ Populate basic tx association data per block. This basically creates
    the link between block and transaction.  tx details are primed by another
    process. """

    def __init__(self):
        super(TransactionPriming, self).__init__()
        self.uuid = str(uuid4())
        self.block_model = BlockModel(DSN)
        self.tx_model = TransactionModel(DSN)
        self.last_ping = None

        if os.environ.get('WEB3_INFURA_API_KEY'):
예제 #3
0
""" Handling of starting threads """
import sys
import time
import random
import signal
from enum import Enum

from blocks.db import LockModel, LockExists, create_initial
from blocks.config import DSN, LOGGER
from blocks.blocks import StoreBlocks
from blocks.transactions import StoreTransactions
from blocks.txprimer import TransactionPriming
from blocks.enums import WorkerType

log = LOGGER.getChild('blocks')


def start_thread(thread_type):
    """ Run the consumer """

    if not isinstance(thread_type, WorkerType):
        raise ValueError("Invalid WorkerType")

    log.info("Checking database.")

    ThreadClass = None
    if thread_type == WorkerType.BLOCK:
        ThreadClass = StoreBlocks
    elif thread_type == WorkerType.TX_PRIME:
        ThreadClass = TransactionPriming
    elif thread_type == WorkerType.TX_DETAIL: