Skip to content

Commit

Permalink
add credit balance to websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
pdxwebdev committed Jan 14, 2021
1 parent ff8d79f commit ca60b10
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
12 changes: 12 additions & 0 deletions yadacoin/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def __init__(self, config):
self.pp = None
self.stratum_pool_port = config.get('stratum_pool_port', 3333)
self.wallet_host_port = config.get('wallet_host_port', 'http://localhost:{}'.format(config['peer_port']))
self.credits_per_share = 5
self.shares_required = True
self.pool_take = .01

async def on_new_block(self, block):
"""Dispatcher for the new bloc event
Expand Down Expand Up @@ -209,6 +212,9 @@ def generate(cls, xprv=None, prv=None, seed=None, child=None, username=None, mon
"username": username or '',
"network": "mainnet",
"wallet_host_port": 'http://localhost:8000',
"credits_per_share": 5,
"shares_required": True,
"pool_take": .01
})

@classmethod
Expand Down Expand Up @@ -250,6 +256,9 @@ def from_dict(cls, config):
cls.jwt_public_key = config.get('jwt_public_key')
cls.sia_api_key = config.get('sia_api_key')
cls.wallet_host_port = config.get('wallet_host_port')
cls.credits_per_share = config.get('credits_per_share', 5)
cls.shares_required = config.get('shares_required', True)
cls.pool_take = config.get('pool_take', .01)

def get_username_signature(self):
from yadacoin.core.transactionutils import TU
Expand Down Expand Up @@ -306,6 +315,9 @@ def to_dict(self):
'jwt_public_key': self.jwt_public_key,
'callbackurl': self.callbackurl,
'wallet_host_port': self.wallet_host_port,
'credits_per_share': self.credits_per_share,
'shares_required': self.shares_required,
'pool_take': self.pool_take,
}

def to_json(self):
Expand Down
3 changes: 2 additions & 1 deletion yadacoin/core/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ async def integrate_block_with_existing_chain(self, block: Block, extra_blocks=N
await self.config.LatestBlock.block_checker() # This will trigger mining pool to generate a new block to mine
if self.config.mp:
await self.config.mp.refresh()
await self.config.nodeShared().send_block(self.config.LatestBlock.block)
if not self.syncing:
await self.config.nodeShared().send_block(self.config.LatestBlock.block)
return True
except Exception as e:
from traceback import format_exc
Expand Down
2 changes: 1 addition & 1 deletion yadacoin/core/miningpoolpayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async def do_payout_for_blocks(self, blocks):
return
if self.config.debug:
self.app_log.debug('do_payout_for_blocks passed address compare {}'.format(block.index))
pool_take = 0.01
pool_take = self.config.pool_take
total_pool_take = coinbase.outputs[0].value * pool_take
total_payout = coinbase.outputs[0].value - total_pool_take
coinbases.append(coinbase)
Expand Down
32 changes: 29 additions & 3 deletions yadacoin/websocket/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tornado import gen, ioloop
from tornado.websocket import WebSocketHandler, WebSocketClosedError
from coincurve import verify_signature
from bitcoin.wallet import P2PKHBitcoinAddress

from yadacoin.core.identity import Identity
from yadacoin.core.peer import Peer, Seed, SeedGateway, ServiceProvider, User, Group
Expand Down Expand Up @@ -79,7 +80,12 @@ async def connect(self, body):

try:
self.config.app_log.info('new {} is valid'.format(peer.__class__.__name__))
await self.write_result('connect_confirm', self.config.peer.to_dict(), body=body)
await self.write_result('connect_confirm', {
'identity': self.config.peer.identity.to_dict,
'shares_required': self.config.shares_required,
'credit_balance': await self.get_credit_balance(),
'server_pool_address': f'{self.config.peer_host}:{self.config.stratum_pool_port}'
}, body=body)
except:
self.config.app_log.error('invalid peer identity signature')
self.close()
Expand All @@ -94,10 +100,19 @@ async def chat_history(self, body):
await self.write_result('chat_history_response', {'chats': sorted(results, key=lambda x: x['time']), 'to': body.get('params', {}).get('to')}, body=body)

async def route_confirm(self, body):
await self.write_result('route_server_confirm', {}, body=body)
credit_balance = await self.get_credit_balance()
await self.write_result('route_server_confirm', {'credit_balance': credit_balance}, body=body)

async def route(self, body):
# our peer SHOULD only ever been a service provider if we're offering a websocket but we'll give other options here
if self.config.shares_required:

credit_balance = await self.get_credit_balance()

if credit_balance <= 0:
await self.write_result('route_server_confirm', {'credit_balance': credit_balance}, body=body)
return

params = body.get('params')
transaction = Transaction.from_dict(params['transaction'])
await self.config.mongo.async_db.miner_transactions.replace_one(
Expand Down Expand Up @@ -171,7 +186,18 @@ async def route(self, body):
self.config.app_log.error('inbound peer is not defined, disconnecting')
self.close()
return {}
await self.write_result('route_server_confirm', {}, body=body)
await self.write_result('route_server_confirm', {'credit_balance': credit_balance}, body=body)

async def get_credit_balance(self):
address = P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(self.peer.identity.public_key))

shares = await self.config.mongo.async_db.shares.count_documents({'address': str(address)})

txns_routed = await self.config.mongo.async_db.miner_transactions.count_documents({'public_key': self.peer.identity.public_key})

credit_balance = shares - (txns_routed * .1)

return credit_balance if credit_balance > 0 else 0.00

async def join_group(self, body):

Expand Down

0 comments on commit ca60b10

Please sign in to comment.