This repository was archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Miner update transactions #102
Open
kousthubraja
wants to merge
10
commits into
main
Choose a base branch
from
miner-update-transactions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ee06bed
miner addition changes
kousthubraja 0e4348a
miner addition changes
kousthubraja e0197c6
rebase
kousthubraja 23f0b59
miner add transaction, mine timer
kousthubraja a7eb13b
miner list filtering
kousthubraja 1e81a61
more committee selection changes
2dd7c8a
Merge branch 'main' into miner-update-transactions
kousthubraja 7e40bc5
block timers, empty block, miner validation
kousthubraja cf23380
block, receipt timings, committee selection
kousthubraja 412b894
review changes, fixes
kousthubraja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| """Miner update functions""" | ||
| import sqlite3 | ||
| import random | ||
|
|
||
| from .utils import get_last_block_hash | ||
| # from .p2p.outgoing import propogate_transaction_to_peers | ||
| from .p2p.utils import get_my_address | ||
| from ..constants import COMMITTEE_SIZE, IS_TEST, NEWRL_DB, TIME_MINER_BROADCAST_INTERVAL | ||
| from .auth.auth import get_wallet | ||
| from .signmanager import sign_transaction | ||
| from ..ntypes import TRANSACTION_MINER_ADDITION | ||
| from .utils import get_time_ms | ||
| from .transactionmanager import Transactionmanager | ||
| from .validator import validate | ||
|
|
||
|
|
||
| def miner_addition_transaction(wallet=None, my_address=None): | ||
| if wallet is None: | ||
| wallet = get_wallet() | ||
| if my_address is None: | ||
| my_address = get_my_address() | ||
| timestamp = get_time_ms() | ||
| transaction_data = { | ||
| 'timestamp': timestamp, | ||
| 'type': TRANSACTION_MINER_ADDITION, | ||
| 'currency': "NWRL", | ||
| 'fee': 0.0, | ||
| 'descr': "Miner addition", | ||
| 'valid': 1, | ||
| 'block_index': 0, | ||
| 'specific_data': { | ||
| 'wallet_address': wallet['address'], | ||
| 'network_address': my_address, | ||
| 'broadcast_timestamp': timestamp | ||
| } | ||
| } | ||
|
|
||
| transaction_manager = Transactionmanager() | ||
| transaction_data = {'transaction': transaction_data, 'signatures': []} | ||
| transaction_manager.transactioncreator(transaction_data) | ||
| transaction = transaction_manager.get_transaction_complete() | ||
| signed_transaction = sign_transaction(wallet, transaction) | ||
| return signed_transaction | ||
|
|
||
|
|
||
| def get_miner_status(wallet_address): | ||
| con = sqlite3.connect(NEWRL_DB) | ||
| cur = con.cursor() | ||
| miner_cursor = cur.execute( | ||
| 'SELECT wallet_address, network_address, last_broadcast_timestamp FROM miners WHERE wallet_address=?', (wallet_address, )).fetchone() | ||
| if miner_cursor is None: | ||
| return None | ||
| miner_info = { | ||
| 'wallet_address': miner_cursor[0], | ||
| 'network_address': miner_cursor[1], | ||
| 'broadcast_timestamp': miner_cursor[2] | ||
| } | ||
| return miner_info | ||
|
|
||
|
|
||
| def get_my_miner_status(): | ||
| wallet = get_wallet() | ||
| my_status = get_miner_status(wallet['address']) | ||
| return my_status | ||
|
|
||
|
|
||
| def broadcast_miner_update(): | ||
| transaction = miner_addition_transaction() | ||
| validate(transaction) | ||
|
|
||
|
|
||
| def get_eligible_miners(): | ||
| last_block = get_last_block_hash() | ||
| # last_block_epoch = 0 | ||
| # try: | ||
| # # Need try catch to support older block timestamps | ||
| # last_block_epoch = int(last_block['timestamp']) | ||
| # except: | ||
| # pass | ||
| # if last_block: | ||
| # cutfoff_epoch = last_block_epoch - TIME_MINER_BROADCAST_INTERVAL | ||
| # else: | ||
| # cutfoff_epoch = 0 | ||
| last_block_epoch = int(last_block['timestamp']) | ||
| cutfoff_epoch = last_block_epoch - TIME_MINER_BROADCAST_INTERVAL | ||
|
|
||
| con = sqlite3.connect(NEWRL_DB) | ||
| con.row_factory = sqlite3.Row | ||
| cur = con.cursor() | ||
| miner_cursor = cur.execute( | ||
| '''SELECT wallet_address, network_address, last_broadcast_timestamp | ||
| FROM miners | ||
| WHERE last_broadcast_timestamp > ? | ||
| ORDER BY wallet_address ASC''', (cutfoff_epoch, )).fetchall() | ||
| miners = [dict(m) for m in miner_cursor] | ||
| con.close() | ||
| return miners | ||
|
|
||
|
|
||
| def get_miner_for_current_block(): | ||
| last_block = get_last_block_hash() | ||
|
|
||
| if not last_block: | ||
| return | ||
|
|
||
| random.seed(last_block['index']) | ||
|
|
||
| committee_list = get_committee_for_current_block() | ||
|
|
||
| return random.choice(committee_list) | ||
|
|
||
| # return committee_list[0] | ||
|
|
||
|
|
||
| def get_committee_for_current_block(): | ||
| last_block = get_last_block_hash() | ||
|
|
||
| if not last_block: | ||
| return | ||
|
|
||
| random.seed(last_block['index']) | ||
|
|
||
| miners = get_eligible_miners() | ||
| committee_size = min(COMMITTEE_SIZE, len(miners)) | ||
| committee = random.sample(miners, k=committee_size) | ||
| return committee | ||
|
|
||
|
|
||
| def should_i_mine(): | ||
| my_wallet = get_wallet() | ||
| miner = get_miner_for_current_block() | ||
| if miner['wallet_address'] == my_wallet['address']: | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def am_i_in_current_committee(): | ||
| my_wallet_address = get_wallet()['address'] | ||
| committee = get_committee_for_current_block() | ||
|
|
||
| found = list(filter(lambda w: w['wallet_address'] == my_wallet_address, committee)) | ||
| if len(found) == 0: | ||
| return False | ||
| return True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this function get_all_eligible_nodes()?
the get_committee_for_current_block() seems to be the right function to get the committee itself.