Esempio n. 1
0
from twisted.internet import defer
from twisted.internet import reactor
from twisted.names import client
import random
import time

from stratum.services import GenericService, signature, synchronous
import stratum.pubsub as pubsub

import stratum.logger as logger
log = logger.get_logger('example')

class ExampleService(GenericService):
    service_type = 'example'
    service_vendor = 'Stratum'
    is_default = True

    def hello_world(self):
        return "Hello world!"
    hello_world.help_text = "Returns string 'Hello world!'"
    hello_world.params = []

    @signature
    def ping(self, payload):
        return payload
    ping.help_text = "Returns signed message with the payload given by the client."
    ping.params = [('payload', 'mixed', 'This payload will be sent back to the client.'),]
    
    @synchronous
    def synchronous(self, how_long):
        '''This can use blocking calls, because it runs in separate thread'''
Esempio n. 2
0
from twisted.internet import defer

from stratum import settings
import stratum.logger as logger  # Python3
log = logger.get_logger('proxy')  # Python3


class Job(object):
    def __init__(self):
        self.params = ''

    @classmethod
    def build_from_pool(cls, getWorkParams):
        '''Build job object from Stratum server broadcast'''
        job = Job()
        job.params = getWorkParams
        return job


class JobRegistry(object):
    def __init__(self, f, f1, f2, f3):
        self.f = f
        self.f1 = f1
        self.f2 = f2
        self.f3 = f3
        self.jobs = None
        # stop mining after 6 minutes if internet disconnected
        if settings.COIN == "ETH":
            self.coinTimeout = 360
        else:
            self.coinTimeout = 900  # For expanse 15 minutes waiting for new job
Esempio n. 3
0
from twisted.internet.protocol import ServerFactory
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor, defer, endpoints

import stratum.socksclient as socksclient  # Python3
import stratum.custom_exceptions  # Python3
from stratum.protocol import Protocol, ClientProtocol  # Python3
from stratum.event_handler import GenericEventHandler  # Python3

import stratum.logger as logger  # Python3
log = logger.get_logger('socket_transport')


def sockswrapper(proxy, dest):
    endpoint = endpoints.TCP4ClientEndpoint(reactor, dest[0], dest[1])
    return socksclient.SOCKSWrapper(reactor, proxy[0], proxy[1], endpoint)


class SocketTransportFactory(ServerFactory):
    def __init__(self,
                 debug=False,
                 signing_key=None,
                 signing_id=None,
                 event_handler=GenericEventHandler,
                 tcp_proxy_protocol_enable=False):
        self.debug = debug
        self.signing_key = signing_key
        self.signing_id = signing_id
        self.event_handler = event_handler
        self.protocol = Protocol
Esempio n. 4
0
from twisted.internet import reactor
from stratum.event_handler import GenericEventHandler
from mining_libs.jobs import Job

import stratum.version as _version
from stratum import logger
log = logger.get_logger('proxy')


class ClientMiningService(GenericEventHandler):
    job_registry = None  # Reference to JobRegistry instance
    timeout = None  # Reference to IReactorTime object

    @classmethod
    def reset_timeout(cls):
        if cls.timeout != None:
            if not cls.timeout.called:
                cls.timeout.cancel()
            cls.timeout = None

        cls.timeout = reactor.callLater(180, cls.on_timeout)

    @classmethod
    def on_timeout(cls):
        '''
            Try to reconnect to the pool after 3 minutes of no activity on the connection.
            It will also drop all Stratum connections to sub-miners
            to indicate connection issues.
        '''
        log.error("Connection to upstream pool timed out")
        cls.reset_timeout()
Esempio n. 5
0
from twisted.web.resource import Resource
from twisted.web.server import Request, Session, NOT_DONE_YET
from twisted.internet import defer
from twisted.python.failure import Failure
import hashlib
import json
import string

import helpers
import semaphore
from protocol import Protocol, RequestCounter
from event_handler import GenericEventHandler
import settings

import stratum.logger as logger  # Python3
log = logger.get_logger('http_transport')


class Transport(object):
    def __init__(self, session_id, lock):
        self.buffer = []
        self.session_id = session_id
        self.lock = lock
        self.push_url = None  # None or full URL for HTTP Push
        self.peer = None

        # For compatibility with generic transport, not used in HTTP transport
        self.disconnecting = False

    def getPeer(self):
        return self.peer
Esempio n. 6
0
import socket
import traceback

from twisted.protocols.basic import LineOnlyReceiver
from twisted.internet import defer, reactor, error
from twisted.python.failure import Failure

#import services
import stratum.stats as stats
import stratum.signature as signature
import stratum.custom_exceptions as custom_exceptions
import stratum.connection_registry as connection_registry
import stratum.settings as settings

import stratum.logger as logger
log = logger.get_logger('protocol')


class RequestCounter(object):
    def __init__(self):
        self.on_finish = defer.Deferred()
        self.counter = 0

    def set_count(self, cnt):
        self.counter = cnt

    def decrease(self):
        self.counter -= 1
        if self.counter <= 0:
            self.finish()
Esempio n. 7
0
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
import random
import string

import stratum.custom_exceptions as custom_exceptions
import stratum.logger as logger
log = logger.get_logger('irc')

# Reference to open IRC connection
_connection = None


def get_connection():
    if _connection:
        return _connection

    raise custom_exceptions.IrcClientException("IRC not connected")


class IrcLurker(irc.IRCClient):
    def connectionMade(self):
        irc.IRCClient.connectionMade(self)
        self.peers = {}

        global _connection
        _connection = self

    def get_peers(self):
        return self.peers.values()
Esempio n. 8
0
import time
from stratum import logger
log = logger.get_logger('stats')


class PeerStats(object):
    '''Stub for server statistics'''
    counter = 0
    changes = 0

    @classmethod
    def client_connected(cls, ip):
        cls.counter += 1
        cls.changes += 1

        cls.print_stats()

    @classmethod
    def client_disconnected(cls, ip):
        cls.counter -= 1
        cls.changes += 1

        cls.print_stats()

    @classmethod
    def print_stats(cls):
        if cls.counter and float(cls.changes) / cls.counter < 0.05:
            # Print connection stats only when more than
            # 5% connections change to avoid log spam
            return
import time

from twisted.internet import defer
from stratum import settings
import stratum.logger as logger

log = logger.get_logger('BasicShareLimiter')

# import DBInterface
# dbi = DBInterface.DBInterface()
# dbi.clear_worker_diff()
''' This is just a customized ring buffer '''


class SpeedBuffer:
    def __init__(self, size_max):
        self.max = size_max
        self.data = []
        self.cur = 0

    def append(self, x):
        self.data.append(x)
        self.cur += 1
        if len(self.data) == self.max:
            self.cur = 0
            self.__class__ = SpeedBufferFull

    def avg(self):
        return sum(self.data) / self.cur

    def pos(self):