forked from jacklevin74/xenminer
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:shanhaicoder/XENGPUMiner
# Conflicts: # miner.py
- Loading branch information
Showing
11 changed files
with
1,026 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -6,3 +6,5 @@ blockchain.db-journal | |
hash_rates/ | ||
.idea | ||
logs/ | ||
dist/ | ||
miner.spec |
This file contains 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,130 @@ | ||
import sqlite3 | ||
from trie import HexaryTrie | ||
|
||
class SQLiteDB: | ||
def __init__(self, db_path): | ||
self.conn = sqlite3.connect(db_path) | ||
self.cursor = self.conn.cursor() | ||
self.setup() | ||
|
||
def setup(self): | ||
self.cursor.execute("CREATE TABLE IF NOT EXISTS trie_data (key TEXT UNIQUE, value TEXT)") | ||
self.cursor.execute("CREATE TABLE IF NOT EXISTS block_meta (block_id INTEGER PRIMARY KEY, root_hash TEXT)") | ||
self.conn.commit() | ||
|
||
def __getitem__(self, key): | ||
self.cursor.execute("SELECT value FROM trie_data WHERE key=?", (key.hex(),)) | ||
value = self.cursor.fetchone() | ||
if value: | ||
return bytes.fromhex(value[0]) | ||
else: | ||
raise KeyError(key) | ||
|
||
def __setitem__(self, key, value): | ||
hex_key = key.hex() | ||
hex_value = value.hex() | ||
self.cursor.execute("INSERT OR IGNORE INTO trie_data (key, value) VALUES (?, ?)", (hex_key, hex_value)) | ||
self.conn.commit() | ||
|
||
def set_root_hash(self, block_id, root_hash): | ||
self.cursor.execute("REPLACE INTO block_meta (block_id, root_hash) VALUES (?, ?)", (block_id, root_hash.hex())) | ||
self.conn.commit() | ||
|
||
def get_root_hash(self, block_id): | ||
self.cursor.execute("SELECT root_hash FROM block_meta WHERE block_id=?", (block_id,)) | ||
root_hash = self.cursor.fetchone() | ||
return bytes.fromhex(root_hash[0]) if root_hash else None | ||
|
||
def close(self): | ||
self.conn.close() | ||
|
||
class AccountManager: | ||
def __init__(self, db_path): | ||
self.db = SQLiteDB(db_path) | ||
self.trie = HexaryTrie(self.db) | ||
|
||
def set_balances(self, balances, block_id): | ||
for account, balance in balances.items(): | ||
key = account.encode() | ||
value = str(balance).encode() | ||
self.trie[key] = value | ||
self.db.set_root_hash(block_id, self.trie.root_hash) | ||
|
||
def get_balance(self, account): | ||
key = account.encode() | ||
return int(self.trie.get(key) or b'0') | ||
|
||
def credit_balances(self, credits, block_id): | ||
new_balances = {} | ||
for account, amount in credits.items(): | ||
balance = self.get_balance(account) | ||
new_balances[account] = balance + amount | ||
self.set_balances(new_balances, block_id) | ||
|
||
def debit_balances(self, debits, block_id): | ||
new_balances = {} | ||
for account, amount in debits.items(): | ||
balance = self.get_balance(account) | ||
if balance < amount: | ||
raise ValueError(f"Insufficient balance for account {account}") | ||
new_balances[account] = balance - amount | ||
self.set_balances(new_balances, block_id) | ||
|
||
|
||
def rebuild_trie(self, block_id): | ||
root_hash = self.db.get_root_hash(block_id) | ||
self.trie = HexaryTrie(self.db, root_hash) | ||
|
||
|
||
import time | ||
if __name__ == "__main__": | ||
manager = AccountManager('ptrie.db') | ||
|
||
# Operations with block_id 1 | ||
manager.set_balances({ | ||
'0xSomeAccount1': 100, | ||
'0xSomeAccount2': 200 | ||
}, 1) | ||
|
||
# Start measuring time for credit_balances loop | ||
start_time_credit_balances = time.time() | ||
|
||
# Create 100 transactions | ||
for i in range(2, 100000): | ||
manager.credit_balances({ | ||
'0xSomeAccount1': 1, | ||
'0xSomeAccount2': 1 | ||
}, i) | ||
|
||
# Print progress every 10,000 iterations | ||
if i % 10000 == 0: | ||
print(f"credit_balances progress: {i}/1000000") | ||
|
||
# Calculate and print the rate for credit_balances loop | ||
end_time_credit_balances = time.time() | ||
duration_credit_balances = end_time_credit_balances - start_time_credit_balances | ||
rate_credit_balances = 1000000 / duration_credit_balances | ||
print(f"credit_balances runs per second: {rate_credit_balances:.2f}") | ||
|
||
# Start measuring time for rebuild_trie loop | ||
start_time_rebuild_trie = time.time() | ||
|
||
# Loop to rebuild the trie for block_ids from 50 to 101 and print the balance for each | ||
for i in range(50, 100000): | ||
manager.rebuild_trie(i) | ||
|
||
# Print progress every 10,000 iterations | ||
if (i - 50) % 10000 == 0: | ||
print(f"rebuild_trie progress: {i}/1000000") | ||
|
||
print(f"Balance for block_id {i} (Account1):", manager.get_balance('0xSomeAccount1')) | ||
print(f"Balance for block_id {i} (Account2):", manager.get_balance('0xSomeAccount2')) | ||
|
||
# Calculate and print the rate for rebuild_trie loop | ||
end_time_rebuild_trie = time.time() | ||
duration_rebuild_trie = end_time_rebuild_trie - start_time_rebuild_trie | ||
rate_rebuild_trie = (1000000 - 50) / duration_rebuild_trie | ||
print(f"rebuild_trie runs per second: {rate_rebuild_trie:.2f}") | ||
|
||
# Close the db connection | ||
manager.db.close() |
Binary file not shown.
This file contains 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,3 @@ | ||
module example.com/xenminer | ||
|
||
go 1.18 |
This file contains 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,56 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func hashValue(value string) string { | ||
hash := sha256.Sum256([]byte(value)) | ||
return hex.EncodeToString(hash[:]) | ||
} | ||
|
||
func buildMerkleTree(elements []string, merkleTree map[string][2]string) string { | ||
if len(elements) == 1 { | ||
return elements[0] | ||
} | ||
|
||
var newElements []string | ||
for i := 0; i < len(elements); i += 2 { | ||
left := elements[i] | ||
var right string | ||
if i+1 < len(elements) { | ||
right = elements[i+1] | ||
} else { | ||
right = left | ||
} | ||
combined := left + right | ||
newHash := hashValue(combined) | ||
merkleTree[newHash] = [2]string{left, right} | ||
newElements = append(newElements, newHash) | ||
} | ||
return buildMerkleTree(newElements, merkleTree) | ||
} | ||
|
||
func TestHashValue(t *testing.T) { | ||
expected := sha256.Sum256([]byte("hello")) | ||
if hashValue("hello") != hex.EncodeToString(expected[:]) { | ||
t.Errorf("hashValue function not working properly") | ||
} | ||
} | ||
|
||
func TestBuildMerkleTree(t *testing.T) { | ||
elements := []string{"a", "b", "c", "d"} | ||
merkleTree := make(map[string][2]string) | ||
root := buildMerkleTree(elements, merkleTree) | ||
fmt.Printf("Merkle Root: %s\n", root) | ||
|
||
expectedRoot := hashValue(hashValue("a"+"b") + hashValue("c"+"d")) | ||
if root != expectedRoot { | ||
t.Errorf("buildMerkleTree function not working properly") | ||
} | ||
// Add more tests to validate the structure of merkleTree if necessary. | ||
} | ||
|
This file contains 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 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.