Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.
Open

Qa #104

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
196 commits
Select commit Hold shift + click to select a range
ee06bed
miner addition changes
kousthubraja Feb 27, 2022
0e4348a
miner addition changes
kousthubraja Feb 27, 2022
e0197c6
rebase
kousthubraja Feb 28, 2022
23f0b59
miner add transaction, mine timer
kousthubraja Feb 28, 2022
a7eb13b
miner list filtering
kousthubraja Feb 28, 2022
f366b6f
fix import
kousthubraja Feb 28, 2022
1e81a61
more committee selection changes
Mar 1, 2022
2dd7c8a
Merge branch 'main' into miner-update-transactions
kousthubraja Mar 7, 2022
7e40bc5
block timers, empty block, miner validation
kousthubraja Mar 9, 2022
cf23380
block, receipt timings, committee selection
kousthubraja Mar 13, 2022
065e589
Merge branch 'qa' of github.com:asqisys/newrl into qa
kousthubraja Mar 14, 2022
d5b0c98
Merge pull request #94 from asqisys/miner-update-transactions
kousthubraja Mar 14, 2022
c5291dc
optimise transaction propogation
kousthubraja Mar 14, 2022
02d259b
Merge pull request #106 from asqisys/optimize-transaction-propagation
kousthubraja Mar 14, 2022
412b894
review changes, fixes
kousthubraja Mar 15, 2022
769b231
Merge pull request #108 from asqisys/miner-update-transactions
kousthubraja Mar 15, 2022
263bd1c
propagate miner addition
kousthubraja Mar 15, 2022
6883af0
change run-updater
kousthubraja Mar 15, 2022
9ff0145
set ASQI as initial mining node
kousthubraja Mar 15, 2022
bd1494c
fix block sync on startup edge case
kousthubraja Mar 16, 2022
d71ede9
turn off empty block mining for debugging
kousthubraja Mar 16, 2022
f436691
- update software random delay
kousthubraja Mar 18, 2022
709ef96
Merge pull request #111 from asqisys/misc-changes
kousthubraja Mar 18, 2022
e1bf37e
block addition after committee consensus for the miner
kousthubraja Mar 19, 2022
be7e214
receipt addition, block broadcast after consensus
kousthubraja Mar 19, 2022
273966d
fix tests
kousthubraja Mar 19, 2022
56abe0b
fixes
kousthubraja Mar 19, 2022
4f0f079
intial doa draft
Prannav-k Mar 24, 2022
119b486
fix receipt validation
kousthubraja Mar 25, 2022
820f8c3
remove nonse calc, add to chain before consensus
kousthubraja Mar 25, 2022
b398807
fix receipt validator key
kousthubraja Mar 25, 2022
8a29996
block_receive change
kousthubraja Mar 25, 2022
83ebec4
block broadcast more changes
kousthubraja Mar 25, 2022
9c7c286
ignore receive existing blocks
kousthubraja Mar 25, 2022
beac08e
address caching, broadcast logs, commitee receipts
kousthubraja Mar 25, 2022
2f706d3
fix a circular dependency
kousthubraja Mar 25, 2022
dd2709d
reduce miner broadcast time
kousthubraja Mar 25, 2022
a815466
increase miner upate interval
kousthubraja Mar 25, 2022
e44f794
increase block size
kousthubraja Mar 25, 2022
e57327c
- skip miner broadcast older than cutoff
kousthubraja Mar 26, 2022
d5ccc54
Merge pull request #112 from asqisys/block-addition-after-committee-a…
kousthubraja Mar 27, 2022
cc51dff
DAO Master Commit Ver 1.0
Vaibhav083 Mar 28, 2022
c7de5a0
doa manager deploy and exec
Prannav-k Mar 28, 2022
3399387
DAO Master Commit Ver 2.0 (memberShip Dao)
Vaibhav083 Mar 29, 2022
88af62b
fix cuttoff epoch
kousthubraja Mar 29, 2022
ccc61c9
intial doa draft
Prannav-k Mar 24, 2022
41cdc42
DAO Master Commit Ver 1.0
Vaibhav083 Mar 28, 2022
dfbb7fa
doa manager deploy and exec
Prannav-k Mar 28, 2022
342c3bb
DAO Master Commit Ver 2.0 (memberShip Dao)
Vaibhav083 Mar 29, 2022
857131d
Merge branch 'dao' of https://github.com/asqisys/newrl into dao
Vaibhav083 Mar 29, 2022
8047a91
fix timeout on chain sync
kousthubraja Mar 29, 2022
2337718
Merge pull request #113 from asqisys/block-addition-after-committee-a…
kousthubraja Mar 29, 2022
3fe9e8a
fix miner cutoff time
kousthubraja Mar 30, 2022
b38db5f
update propagation fix, node_info api
kousthubraja Mar 30, 2022
828f92c
fix miner eligibility cutoff
kousthubraja Mar 30, 2022
c956c43
time_stats in node info
kousthubraja Mar 31, 2022
f9d9c72
- reduce miner broadcast interval
kousthubraja Mar 31, 2022
6ea8871
DAO Master Commit Ver 2.1 (memberShip Dao)
Vaibhav083 Mar 31, 2022
8feedec
cleanup, seggregate, error handling APIs
kousthubraja Mar 31, 2022
c84934a
fix miner update broadcast failure retry
kousthubraja Mar 31, 2022
a285be8
script changes
kousthubraja Apr 1, 2022
bfa0f5b
- delete transaction on block acceptance
kousthubraja Apr 1, 2022
cd70c4e
- enable internal mining clock
kousthubraja Apr 4, 2022
2fbb0ba
- liberal conesnsus when committee too small
kousthubraja Apr 4, 2022
1fa5404
receipt cleanup on block inclusion
kousthubraja Apr 4, 2022
139523d
- change default fee currency to NWRL
kousthubraja Apr 6, 2022
8a36af3
fix sync loop on block receive
kousthubraja Apr 6, 2022
abeb8d8
ignore receipt during block hash calculation
kousthubraja Apr 6, 2022
067c368
fix creator_wallet add to chain
kousthubraja Apr 7, 2022
1ede604
- software version
kousthubraja Apr 7, 2022
e0a3581
enable empty block mining by committee members
kousthubraja Apr 7, 2022
4483e0b
enable no block timeout for committee
kousthubraja Apr 7, 2022
f520914
update version
kousthubraja Apr 7, 2022
29abd49
broadcast bug fix
kousthubraja Apr 7, 2022
a394af7
fix block broadcaster if no committee present
kousthubraja Apr 7, 2022
e5ff303
temporary hack for validation in 0 committee
kousthubraja Apr 7, 2022
fdf14c2
fix invalid block rejection
kousthubraja Apr 8, 2022
ce0d637
more hash transmission fixes
kousthubraja Apr 10, 2022
8d87a86
sync fix
kousthubraja Apr 10, 2022
1edbf0d
p2p whisper broadcast block
kousthubraja Apr 10, 2022
15ad260
DAO Master Commit Ver 2.2 (memberShip Dao)
Vaibhav083 Apr 11, 2022
233c77e
- sync hashing change
kousthubraja Apr 11, 2022
bb0a68d
- empty block timeout fix
kousthubraja Apr 11, 2022
bc89b4c
voting scheme max_yes logic implementation
Prannav-k Apr 12, 2022
eac1f45
vote on proposal added flow comments
Prannav-k Apr 12, 2022
fbc0f21
DAO Master Commit Ver 2.3 (memberShip Dao)
Vaibhav083 Apr 12, 2022
b518965
DAO Master Commit Ver 2.3 (Duplicate Check)
Vaibhav083 Apr 12, 2022
e05bec5
consistent block storage in temp
kousthubraja Apr 12, 2022
86e1685
clear tmp api
kousthubraja Apr 12, 2022
60e83e1
skip miner validation for empty block by committee
kousthubraja Apr 12, 2022
7aefece
vote method update
Prannav-k Apr 12, 2022
b18455e
Merge branch 'dao' of github.com:asqisys/newrl into dao
Prannav-k Apr 13, 2022
898dc6f
voting scheme params
Prannav-k Apr 13, 2022
261ade8
- transaction in block to include signatures
kousthubraja Apr 13, 2022
0602d85
revert liberal consensus for timeout blocks
kousthubraja Apr 13, 2022
957f0a5
fix transaction validation during acceptance
kousthubraja Apr 13, 2022
fc6fc69
fix transaction delete during block acceptance
kousthubraja Apr 13, 2022
da2f8ad
increase miner broadcast time cutoff
kousthubraja Apr 14, 2022
ae584c4
force sync via api
kousthubraja Apr 14, 2022
8f076cb
add transaction signatures to db for sync
kousthubraja Apr 14, 2022
526c516
block broadcast to all for small committee size
kousthubraja Apr 14, 2022
5c7177d
fix get_blocks to include transaction signs
kousthubraja Apr 14, 2022
a5add47
- global internal clock
kousthubraja Apr 18, 2022
00d6621
revert one block on sync conflict
kousthubraja Apr 18, 2022
a62edad
fix empty block mining index
kousthubraja Apr 18, 2022
74af3b3
move emtpy block mining to internal clock
kousthubraja Apr 18, 2022
7f67de9
min_yes votes cl
Prannav-k Apr 19, 2022
c59611a
propagate original block on receive
kousthubraja Apr 19, 2022
95b9465
pass cursor to get_last_block
kousthubraja Apr 19, 2022
6d5a6ea
invalid/empty block changes, tests
kousthubraja Apr 20, 2022
35882d4
correct block broadcast after receipt addition
kousthubraja Apr 20, 2022
967eca9
log optimization
kousthubraja Apr 20, 2022
29d1fc8
global timing changes
kousthubraja Apr 21, 2022
71aa588
Revert "global timing changes"
kousthubraja Apr 21, 2022
ccb2a70
reenable mining on accept
kousthubraja Apr 21, 2022
2bd8b6a
tweak global clock timings
kousthubraja Apr 21, 2022
85376de
Merge branch 'qa' into dao
Vaibhav083 Apr 21, 2022
078c450
DAO Master Commit Ver 2.4 state validation
Vaibhav083 Apr 21, 2022
82f4813
log creation and stream
Prannav-k Apr 22, 2022
a98c20b
Merge branch 'asqisys:qa' into qa
Prannav-k Apr 22, 2022
1b27b3d
DAO changes Basic Version For Devnet
Vaibhav083 Apr 22, 2022
ddcfd5e
use internal timer for block mining
kousthubraja Apr 22, 2022
7d13655
timers info
kousthubraja Apr 22, 2022
9738166
fix timers display API
kousthubraja Apr 22, 2022
fe92072
DAO changes Basic Version For DbUpdater WalletAddress Key Typo Fix
Vaibhav083 Apr 22, 2022
1b7a140
DAO changes Basic Version For DbUpdater Returning -1 as valid address
Vaibhav083 Apr 22, 2022
105f122
DAO changes Basic Version For DbUpdater Returning -1 as valid address
Vaibhav083 Apr 22, 2022
9eacab6
Merge pull request #118 from asqisys/dao
kousthubraja Apr 22, 2022
e892cd2
- more timer enhancements
kousthubraja Apr 23, 2022
7cbed45
fix receive timeout condition
kousthubraja Apr 23, 2022
0982131
- correct sentitnel node empty block timestamp
kousthubraja Apr 23, 2022
09ac380
correct peer cleanup
kousthubraja Apr 23, 2022
617ab69
disable empty block mining by committe
kousthubraja Apr 23, 2022
5d72c99
fix miner validation from sentinel node
kousthubraja Apr 23, 2022
df1d154
timeout block acceptance changes
kousthubraja Apr 23, 2022
a2220a1
fix network address caching
kousthubraja Apr 23, 2022
b667661
fix consensus when no block is available
kousthubraja Apr 23, 2022
305eead
Merge branch 'qa' into qa
kousthubraja Apr 25, 2022
d7bbfc9
Merge pull request #119 from Prannav-k/qa
kousthubraja Apr 25, 2022
95b9715
fix node failure due to log dependency
kousthubraja Apr 25, 2022
c3cb00e
- max peer broadcast
kousthubraja Apr 25, 2022
a205134
remove token_value and add tokendecimal
kousthubraja Apr 26, 2022
0984d2b
Added signstr in dao manager
Prannav-k Apr 26, 2022
376f759
Merge pull request #121 from asqisys/dao
kousthubraja Apr 26, 2022
5ec9fd3
remove dead peers fix
kousthubraja Apr 26, 2022
dcd0e62
error handle state updates by smart contract
kousthubraja Apr 26, 2022
cd3a0c4
DAO changes On Behalf Pranav
Vaibhav083 Apr 27, 2022
0ecdba2
Dao voting method definitions
Prannav-k Apr 27, 2022
e14f40e
Merge branch 'dao' of github.com:asqisys/newrl into dao
Prannav-k Apr 27, 2022
f72d617
Fixed duplicate logger bug
Prannav-k Apr 28, 2022
025c992
Merge branch 'asqisys:qa' into qa
Prannav-k Apr 28, 2022
e2b7096
added fetch tokens staked amount
Prannav-k Apr 28, 2022
f9a00a9
token code name in dao
Prannav-k Apr 28, 2022
734d97c
token_code in dao
Prannav-k Apr 28, 2022
a7871f7
Merge pull request #122 from Prannav-k/qa
kousthubraja Apr 29, 2022
1a75c81
DAO changes Function Defs
Vaibhav083 May 2, 2022
695310c
optimize broadcasts, store sent nodes in payload
kousthubraja May 5, 2022
0bd5ad6
fix broadcast exclusion list
kousthubraja May 5, 2022
37bc5c2
DAO changes Function For Token Based Voting Initial Commit
Vaibhav083 May 5, 2022
db660aa
remove minutes from log file name
kousthubraja May 5, 2022
9411878
documentations, test scripts
kousthubraja May 5, 2022
551a4b2
init network trust manager
kousthubraja May 7, 2022
e6d6a08
reduce node info details for quick response
kousthubraja May 7, 2022
0d24e38
make receive APIs blocking
kousthubraja May 7, 2022
0bf34ac
DAO changes Fund DAO scirpts fix (proposal_data fix)
Vaibhav083 May 10, 2022
f60be47
make sync blocking
kousthubraja May 10, 2022
306235b
dao voting proposal list
Prannav-k May 10, 2022
8417998
Merge branch 'dao' of github.com:asqisys/newrl into dao
Prannav-k May 10, 2022
b1bf9de
Merge pull request #120 from asqisys/remove-token-value-created
kousthubraja May 10, 2022
f03e5ac
Refactored token dao calss names
Prannav-k May 10, 2022
47f12f1
Token DAO changes (class name renamed)
Vaibhav083 May 10, 2022
a2cdd7c
token fund dao class rename
Prannav-k May 10, 2022
6323ade
Merge branch 'dao' of github.com:asqisys/newrl into dao
Prannav-k May 10, 2022
f261911
Merge branch 'qa' into dao
kousthubraja May 10, 2022
0dc9a86
disable dangerous system APIs
kousthubraja May 12, 2022
8abd871
better logging
kousthubraja May 13, 2022
1d038cd
enable protected revert-chain api
kousthubraja May 13, 2022
1fca3c4
change updater logging
kousthubraja May 13, 2022
928f47c
script changes
kousthubraja May 17, 2022
3716488
peer addition from dns
kousthubraja May 17, 2022
05f637f
Token DAO changes (Fund Based DAO Implemented)
Vaibhav083 May 18, 2022
916da45
Token DAO changes (Two Way Transfer Bug With DAO)
Vaibhav083 May 19, 2022
bcef77c
Merge pull request #123 from asqisys/dao
kousthubraja May 19, 2022
8dea13b
init network trust manager
kousthubraja May 7, 2022
9ee4cd8
Merge branch 'network-trust-score' of github.com:asqisys/newrl into n…
kousthubraja May 23, 2022
a04833f
log size based rotation and name change
Prannav-k May 25, 2022
738b595
receipts in states, trust score updates
kousthubraja May 25, 2022
f160d14
fix token doubling bug on type4 transactions
kousthubraja May 25, 2022
17bbc6b
DAO_Manager bug while generating dao address (Fix)
Vaibhav083 May 26, 2022
9a245f3
Merge pull request #128 from asqisys/authorizeContract
kousthubraja May 26, 2022
07b4b8f
Merge pull request #129 from asqisys/dao
kousthubraja May 26, 2022
cbb0792
Merge pull request #126 from asqisys/fix-type4-transaction-token-doub…
kousthubraja May 27, 2022
162f5e5
Merge pull request #124 from asqisys/log-size-based-rotation-and-name…
kousthubraja May 27, 2022
9018468
revert comments
kousthubraja May 27, 2022
f5fc928
Merge pull request #125 from asqisys/network-trust-score
kousthubraja May 27, 2022
15cf296
Merge pull request #131 from asqisys/main
kousthubraja May 27, 2022
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ venv/
__pycache__/

.vscode

.idea
.DS_Store

# Byte-compiled / optimized / DLL files
Expand Down Expand Up @@ -172,4 +172,6 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

logs/
12 changes: 11 additions & 1 deletion app/codes/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
from ...nvalues import ZERO_ADDRESS


def get_node_wallet_public():
wallet = get_wallet()
if wallet:
return {
'address': wallet['address'],
'public': wallet['public']
}
else:
return None


def get_node_wallet_address():
wallet = get_wallet()
if wallet:
Expand Down Expand Up @@ -36,7 +47,6 @@ def get_auth():
'public': wallet['public'],
}
auth_data['signature'] = sign_object(private_key, auth_data)
print('auth', auth_data)
return auth_data
except:
auth_data = {}
Expand Down
125 changes: 108 additions & 17 deletions app/codes/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

import sqlite3

from ..constants import NEWRL_DB
from app.codes.clock.global_time import get_corrected_time_ms
from app.codes.receiptmanager import update_receipts_in_state

from .fs.temp_manager import remove_block_from_temp
from ..constants import BLOCK_TIME_INTERVAL_SECONDS, NEWRL_DB, NO_BLOCK_TIMEOUT
from .utils import get_time_ms
from .crypto import calculate_hash
from .state_updater import update_db_states
from .state_updater import update_db_states, update_trust_scores
from .utils import get_time_ms
from .auth.auth import get_node_wallet_address
from .fs.mempool_manager import remove_transaction_from_mempool


class Blockchain:
Expand All @@ -20,7 +25,7 @@ class Blockchain:
def __init__(self) -> None:
self.chain = []

def create_block(self, cur, block, block_hash):
def create_block(self, cur, block, block_hash, creator_wallet=None):
"""Create a block and store to db"""
transactions_hash = self.calculate_hash(block['text']['transactions'])
db_block_data = (
Expand All @@ -29,9 +34,10 @@ def create_block(self, cur, block, block_hash):
block['proof'],
block['previous_hash'],
block_hash,
creator_wallet,
transactions_hash
)
cur.execute('INSERT OR IGNORE INTO blocks (block_index, timestamp, proof, previous_hash, hash, transactions_hash) VALUES (?, ?, ?, ?, ?, ?)', db_block_data)
cur.execute('INSERT OR IGNORE INTO blocks (block_index, timestamp, proof, previous_hash, hash, creator_wallet, transactions_hash) VALUES (?, ?, ?, ?, ?, ?, ?)', db_block_data)
return block

def get_block(self, block_index):
Expand All @@ -40,13 +46,19 @@ def get_block(self, block_index):
cur = con.cursor()
block_cursor = cur.execute(
'SELECT * FROM blocks where block_index=?', (block_index,)).fetchone()

if block_cursor is None:
return None
block = dict(block_cursor)

transactions_cursor = cur.execute(
'SELECT * FROM transactions where block_index=?', (block_index,)).fetchall()
transactions = [dict(ix) for ix in transactions_cursor]
transactions = list(map(lambda t:
{'transaction': t, 'signatures': [] if t['signatures'] is None else json.loads(t['signatures'])},
transactions))
block['text'] = {
'transactions': transactions
'transactions': transactions,
}

return block
Expand Down Expand Up @@ -96,11 +108,10 @@ def mine_block(self, cur, text, fees=0):

block = {
'index': last_block_index + 1,
'timestamp': get_time_ms(),
'timestamp': get_corrected_time_ms(),
'proof': 0,
'text': text,
'creator_wallet': get_node_wallet_address(),
'fees': fees,
'previous_hash': last_block_hash
}

Expand All @@ -110,6 +121,54 @@ def mine_block(self, cur, text, fees=0):
block = self.create_block(cur, block, block_hash)
return block

def propose_block(self, cur, text):
"""Propose a new block and not add to chain"""
last_block_cursor = cur.execute(
'SELECT block_index, hash FROM blocks ORDER BY block_index DESC LIMIT 1')
last_block = last_block_cursor.fetchone()
last_block_index = last_block[0] if last_block is not None else 0
last_block_hash = last_block[1] if last_block is not None else 0
print(f'Proposing a block with index {last_block_index + 1}')

block = {
'index': last_block_index + 1,
'timestamp': get_corrected_time_ms(),
'proof': 0,
'text': text,
'creator_wallet': get_node_wallet_address(),
'previous_hash': last_block_hash
}
return block

def mine_empty_block(self, new_block_timestamp=None):
"""Mine an empty block"""
print("Mining empty block")
con = sqlite3.connect(NEWRL_DB)
cur = con.cursor()
last_block_cursor = cur.execute(
'SELECT block_index, hash, timestamp FROM blocks ORDER BY block_index DESC LIMIT 1')
last_block = last_block_cursor.fetchone()
con.close()
last_block_index = last_block[0] if last_block is not None else 0
last_block_hash = last_block[1] if last_block is not None else 0
last_block_timestamp = last_block[2] if last_block is not None else 0

EMPTY_BLOCK_NONCE = 42

if new_block_timestamp is None:
new_block_timestamp = int(last_block_timestamp) + (BLOCK_TIME_INTERVAL_SECONDS + NO_BLOCK_TIMEOUT) * 1000

block = {
'index': last_block_index + 1,
'timestamp': new_block_timestamp,
'proof': EMPTY_BLOCK_NONCE,
'text': {"transactions": [], "signatures": []},
'creator_wallet': None,
'previous_hash': last_block_hash
}

return block

def get_latest_ts(self, cur=None):
"""Get the timestamp of latest block"""
should_close_db_conn = False
Expand All @@ -129,12 +188,14 @@ def get_latest_ts(self, cur=None):
return ts


def add_block(cur, block, block_hash=None):
def add_block(cur, block, block_hash):
"""Add a block to db, add transactions and update states"""
last_block = get_last_block(cur)
if last_block is not None and last_block['hash'] != block['previous_hash']:
print('Previous block hash does not match current block data')
return
# Needed for backward compatibility of blocks
block_index = block['block_index'] if 'block_index' in block else block['index']
if not block_hash:
block_hash = block['hash'] if 'hash' in block else ''
# transactions_hash = block['transactions_hash'] if 'transactions_hash' in block else ''
transactions_hash = calculate_hash(block['text']['transactions'])
print('Adding block', block_index)
Expand All @@ -144,10 +205,19 @@ def add_block(cur, block, block_hash=None):
block['proof'],
block['previous_hash'],
block_hash,
block['creator_wallet'],
transactions_hash
)
cur.execute('INSERT OR IGNORE INTO blocks (block_index, timestamp, proof, previous_hash, hash, transactions_hash) VALUES (?, ?, ?, ?, ?, ?)', db_block_data)
cur.execute('INSERT OR IGNORE INTO blocks (block_index, timestamp, proof, previous_hash, hash, creator_wallet, transactions_hash) VALUES (?, ?, ?, ?, ?, ?, ?)', db_block_data)
update_db_states(cur, block)
# update_receipts_in_state(cur, block)
# update_trust_scores(cur, block)

for transaction in block['text']['transactions']:
transaction = transaction['transaction']
transaction_code = transaction['transaction_code'] if 'transaction_code' in transaction else transaction['trans_code']
remove_transaction_from_mempool(transaction_code)
remove_block_from_temp(block_index)


def get_last_block_index():
Expand All @@ -162,20 +232,41 @@ def get_last_block_index():
return last_block[0] if last_block is not None else 0


def get_last_block_hash():
def get_last_block(cur=None):
"""Get last block hash from db"""
con = sqlite3.connect(NEWRL_DB)
cur = con.cursor()
cursor_opened_inside = False
if cur is None:
con = sqlite3.connect(NEWRL_DB)
cur = con.cursor()
cursor_opened_inside = True
last_block_cursor = cur.execute(
'SELECT block_index, hash FROM blocks ORDER BY block_index DESC LIMIT 1'
'SELECT block_index, hash, timestamp FROM blocks ORDER BY block_index DESC LIMIT 1'
)
last_block = last_block_cursor.fetchone()
con.close()

if cursor_opened_inside:
con.close()

if last_block is not None:
return {
'index': last_block[0],
'hash': last_block[1]
'hash': last_block[1],
'timestamp': last_block[2]
}
else:
return None


def block_exists(block_index):
con = sqlite3.connect(NEWRL_DB)
cur = con.cursor()
block_cursor = cur.execute(
'SELECT * FROM blocks where block_index=?', (block_index,)).fetchone()

if block_cursor is not None:
block_exists = True
else:
block_exists = False

con.close()
return block_exists
42 changes: 42 additions & 0 deletions app/codes/chainscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,66 @@ def download_state():

balances_cursor = cur.execute('SELECT * FROM balances').fetchall()
balances = [dict(ix) for ix in balances_cursor]

contracts_cursor = cur.execute('SELECT * FROM contracts').fetchall()
contracts = [dict(ix) for ix in contracts_cursor]

state = {
'wallets': wallets,
'tokens': tokens,
'balances': balances,
'contracts': contracts
}
return state


def get_block(block_index):
chain = Blockchain()
return chain.get_block(block_index)

def get_transaction(transaction_code):
con = sqlite3.connect(NEWRL_DB)
con.row_factory = sqlite3.Row
cur = con.cursor()
transaction_cursor = cur.execute(
'SELECT * FROM transactions where transaction_code=?', (transaction_code,)).fetchone()
if transaction_cursor is None:
return None
return dict(transaction_cursor)


def get_wallet(wallet_address):
con = sqlite3.connect(NEWRL_DB)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur = cur.execute(
'SELECT * FROM wallets where wallet_address=?', (wallet_address,)).fetchone()
if cur is None:
return None
return dict(cur)


def get_token(token_code):
con = sqlite3.connect(NEWRL_DB)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur = cur.execute(
'SELECT * FROM tokens where tokencode=?', (token_code,)).fetchone()
if cur is None:
return None
return dict(cur)

def get_contract(contract_address):
con = sqlite3.connect(NEWRL_DB)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur = cur.execute(
'SELECT * FROM contracts where address=?', (contract_address,)).fetchone()
if cur is None:
return None
return dict(cur)


def download_chain():
con = sqlite3.connect(NEWRL_DB)
con.row_factory = sqlite3.Row
Expand Down
Loading