Skip to content

Commit

Permalink
locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Max00355 committed Nov 22, 2013
1 parent 8584aa8 commit 8959764
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.1.5
=====

* Added DB locking to further prevent corruption.
* Nodes now only communicate with nodes that are on the same version.


Expand Down
6 changes: 6 additions & 0 deletions check_coin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import sqlite3
import config
import re
import os
import time

def check_coin(obj, data):
"""
Expand Down Expand Up @@ -70,9 +72,13 @@ def confirm_coin(obj, data):
difficulty = check.fetchall()[0][0]
db.execute("SELECT * FROM coins WHERE hash=?", [data['hash']])
if data['hash'].startswith("1"*int(difficulty)) and len(data['hash']) == 128 and not db.fetchall() and hashlib.sha512(data['plain']).hexdigest() == data['hash']:
while os.path.exists("db.lock"):
time.sleep(0.1)
open("db.lock", 'w')
db_.execute("UPDATE difficulty SET level=? WHERE level=?", [data['difficulty'], difficulty])
db_.execute("INSERT INTO coins (starter, hash, address) VALUES (?, ?, ?)", [data['starter'], data['hash'], data['address']])
db_.commit()
os.remove("db.lock")
else:
return

6 changes: 6 additions & 0 deletions get_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import base64
import hashlib
import register, get_nodes
import time
import os

def get_db(obj, data):
db = sqlite3.connect("db.db").cursor()
Expand Down Expand Up @@ -76,5 +78,9 @@ def get_db_send():
else:
break
if not no:
while os.path.exists("db.lock"):
time.sleep(0.1)
open("db.lock", 'w')
with open("db.db", 'wb') as file:
file.write(out)
os.remove("db.lock")
6 changes: 6 additions & 0 deletions get_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import base64
import hashlib
import register, get_db
import os
import time

def get_nodes(obj, data):
with open("nodes.db", 'rb') as file:
Expand Down Expand Up @@ -73,5 +75,9 @@ def get_nodes_send(god=False):
else:
break
if not no:
while os.path.exists("nodes.lock"):
time.sleep(0.1)
open("nodes.lock", 'w')
with open("nodes.db", 'wb') as file:
file.write(out)
os.remove("nodes.lock")
6 changes: 6 additions & 0 deletions register.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import random
import sqlite3
import get_nodes, get_db
import os
import time

def register(obj, data):
while os.path.exists("nodes.lock"):
time.sleep(0.1)
open("nodes.lock", 'w')
check = sqlite3.connect("nodes.db")
c = check.execute("SELECT * FROM data WHERE address=?", [data['address']])
if c.fetchall():
check.execute("UPDATE data SET relay=?, ip=?, port=?, public=?, version=? WHERE address=?", [data['relay'], data['ip'], data['port'], data['address'], data['public'], data['version']])
else:
check.execute("INSERT INTO data (address, ip, port, relay, public, version) VALUES (?, ?, ?, ?, ?, ?)", [data['address'], data['ip'], data['port'], data['relay'], data['public'], data['version']])
check.commit()
os.remove("nodes.lock")

def register_send(god=False):
node = sqlite3.connect("nodes.db")
Expand Down
9 changes: 7 additions & 2 deletions send_coin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import re
import hashlib
import config
import os
import time

def send_coin_send(address, amount):
"""
Expand Down Expand Up @@ -89,9 +91,12 @@ def send_coin_do(out):


def send_coin(obj, data):


while os.path.exists("db.lock"):
time.sleep(0.1)
open("db.lock", 'w')
db = sqlite3.connect('db.db')
db.execute('UPDATE coins SET address=?, starter=? WHERE hash=?', [data['for'], data['starter'], data['hash']])
db.execute("INSERT INTO transactions (to_, from_, hash) VALUES (?, ?, ?)", [data['for'], data['starter'], data['hash']])
db.commit()

os.remove("db.lock")

0 comments on commit 8959764

Please sign in to comment.