From 64978f88a474627c49eff47605a7d73ce564637b Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 23 May 2025 12:12:15 +0000 Subject: [PATCH] clockchain changes --- kitchen_counter/Blockchain2/Conversion.py | 174 +++++++++++++-------- kitchen_counter/Blockchain2/scriptAudit.py | 163 ++++++++++++------- 2 files changed, 220 insertions(+), 117 deletions(-) diff --git a/kitchen_counter/Blockchain2/Conversion.py b/kitchen_counter/Blockchain2/Conversion.py index f458873..45b9329 100755 --- a/kitchen_counter/Blockchain2/Conversion.py +++ b/kitchen_counter/Blockchain2/Conversion.py @@ -1,7 +1,12 @@ from web3 import Web3 import time from web3.exceptions import TimeExhausted,TransactionNotFound +from .models import FailedTransactions +from django.contrib.auth import get_user_model +User = get_user_model() PRIVATE_KEY = "2c7c09807b78128e00a2ed308c122a0ecc45847ffe0195eca4b4d2463fb11549" +# PRIVATE_KEY = "6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad" + # test network # RPC = 'https://rpc-amoy.polygon.technology/' @@ -10,8 +15,8 @@ PRIVATE_KEY = "2c7c09807b78128e00a2ed308c122a0ecc45847ffe0195eca4b4d2463fb11549" # ABI = '[ { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "indexed": false, "internalType": "string", "name": "project", "type": "string" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "conversion", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "pubkey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteConversion", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "pubKey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadConversionData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getConversion", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct scriptConversionDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" } ]' # # mainnet -# RPC = 'https://polygon-rpc.com' -RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e" +RPC = 'https://polygon-rpc.com' +# RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e" CONTRACT_ADDRESS = '0x552453F2C4e8ED860a6AEC7D6Bde143CF86E396a' CHAIN_ID = 137 ABI = '[ { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "indexed": false, "internalType": "string", "name": "project", "type": "string" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "conversion", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "pubkey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteConversion", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "pubKey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadConversionData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getConversion", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct scriptConversionDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" } ]' @@ -24,69 +29,114 @@ contractInst = web3.eth.contract(address=CAddress, abi=abi) def UploadConversionData(privatekey,pubkey, user_id,project,data): try: acc1 = web3.eth.account.from_key(PRIVATE_KEY).address - def send_transaction( gas_price): - nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions - uploadData = contractInst.functions.UploadConversionData(pubkey, user_id, project, data).build_transaction({ - 'gasPrice': gas_price, - 'chainId': CHAIN_ID, - 'from': acc1, - 'nonce': nonce - }) - signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY) - transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) - print("Transaction hash:", transaction_hash.hex()) - return transaction_hash - try: - acc1 = web3.eth.account.from_key(PRIVATE_KEY).address - initial_gas_price = web3.eth.gas_price - - max_retries = 4 - retries = 0 - - transaction_hash = None - try: - transaction_hash = send_transaction(initial_gas_price) - except ValueError as e: - # If the error is due to "replacement transaction underpriced", handle it - if "replacement transaction underpriced" in str(e): - print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.") - retries += 1 - new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) - transaction_hash = send_transaction(new_gas_price) - elif "max fee per gas less than block base fee" in str(e): - print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.") - retries += 1 - new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) - transaction_hash = send_transaction(new_gas_price) - else: - raise - - while retries < max_retries and transaction_hash: - try: - transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120) - print("Transaction confirmed:") - break - except TimeExhausted: - retries += 1 - print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") - # Increase the gas price by 10% for each retry - new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) - transaction_hash = send_transaction(new_gas_price) - else: - if retries == max_retries: - print("Failed to confirm the transaction after multiple attempts.") - raise TimeoutError("Failed to confirm the transaction after multiple attempts.") - - transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed - tx_id = transaction_hash.hex() + nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions + uploadData = contractInst.functions.UploadConversionData(pubkey, user_id, project, data).build_transaction({ + 'gasPrice': web3.eth.gas_price, + 'chainId': CHAIN_ID, + 'from': acc1, + 'nonce': nonce + }) + signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY) + transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) + print("Transaction hash:", transaction_hash.hex()) + transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120) + transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed + tx_id = transaction_hash.hex() + # Check if the transaction was successful + if transaction_receipt.status == 1: + print("Transaction successful!") return tx_id, str(transaction_fee) - - except Exception as e: - print(f"An error occurred: {e}") - raise + else: + print("Transaction failed!") + obj,_ = FailedTransactions.objects.get_or_create(service_id=project,service_name="Conversion") + creator = User.objects.get(id=user_id) + obj.creator = creator + obj.transaction_type = "UploadConversionData" + obj.transaction_hash = "" + obj.transaction_status = "failed" + obj.transaction_error = "Transaction failed" + obj.transaction_data = str(data) + obj.save() + return False, "Transaction failed" except Exception as e: print({"error":str(e)}) - return e + obj,_ = FailedTransactions.objects.get_or_create(service_id=project,service_name="Conversion") + creator = User.objects.get(id=user_id) + obj.creator = creator + obj.transaction_type = "UploadConversionData" + obj.transaction_hash = "" + obj.transaction_status = "failed" + obj.transaction_error = str(e) + obj.transaction_data = str(data) + obj.save() + return False, str(e) + +# def UploadConversionData(privatekey,pubkey, user_id,project,data): +# try: +# acc1 = web3.eth.account.from_key(PRIVATE_KEY).address +# def send_transaction( gas_price): +# nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions +# uploadData = contractInst.functions.UploadConversionData(pubkey, user_id, project, data).build_transaction({ +# 'gasPrice': gas_price, +# 'chainId': CHAIN_ID, +# 'from': acc1, +# 'nonce': nonce +# }) +# signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY) +# transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) +# print("Transaction hash:", transaction_hash.hex()) +# return transaction_hash +# try: +# acc1 = web3.eth.account.from_key(PRIVATE_KEY).address +# initial_gas_price = web3.eth.gas_price + +# max_retries = 4 +# retries = 0 + +# transaction_hash = None +# try: +# transaction_hash = send_transaction(initial_gas_price) +# except ValueError as e: +# # If the error is due to "replacement transaction underpriced", handle it +# if "replacement transaction underpriced" in str(e): +# print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.") +# retries += 1 +# new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) +# transaction_hash = send_transaction(new_gas_price) +# elif "max fee per gas less than block base fee" in str(e): +# print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.") +# retries += 1 +# new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) +# transaction_hash = send_transaction(new_gas_price) +# else: +# raise + +# while retries < max_retries and transaction_hash: +# try: +# transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120) +# print("Transaction confirmed:") +# break +# except TimeExhausted: +# retries += 1 +# print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") +# # Increase the gas price by 10% for each retry +# new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) +# transaction_hash = send_transaction(new_gas_price) +# else: +# if retries == max_retries: +# print("Failed to confirm the transaction after multiple attempts.") +# raise TimeoutError("Failed to confirm the transaction after multiple attempts.") + +# transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed +# tx_id = transaction_hash.hex() +# return tx_id, str(transaction_fee) + +# except Exception as e: +# print(f"An error occurred: {e}") +# raise +# except Exception as e: +# print({"error":str(e)}) +# return e def getConversion(private_key,user_id,project): ACCOUNT = web3.eth.account.from_key(private_key).address diff --git a/kitchen_counter/Blockchain2/scriptAudit.py b/kitchen_counter/Blockchain2/scriptAudit.py index 883c015..b22ea6a 100755 --- a/kitchen_counter/Blockchain2/scriptAudit.py +++ b/kitchen_counter/Blockchain2/scriptAudit.py @@ -1,7 +1,14 @@ from web3 import Web3 import time from web3.exceptions import TimeExhausted,TransactionNotFound +from .models import FailedTransactions +from django.contrib.auth import get_user_model +User = get_user_model() + + PRIVATE_KEY = "a632f28406579d96879cfc49d55411bb1424dbbae60bf4dd93ad0710b63d6b80" +# PRIVATE_KEY = "6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad" + # RPC = 'https://rpc-amoy.polygon.technology/' # CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881' @@ -20,68 +27,114 @@ CAddress =CONTRACT_ADDRESS abi = ABI contractInst = web3.eth.contract(address=CAddress, abi=abi) +# def UploadScriptAuditData(privatekey,pubkey,user_id,project,data): +# try: +# acc1 = web3.eth.account.from_key(PRIVATE_KEY).address +# def send_transaction( gas_price): +# nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions +# uploadData = contractInst.functions.UploadScriptAuditData(pubkey, user_id, project, data).build_transaction({ +# 'gasPrice': gas_price, +# 'chainId': CHAIN_ID, +# 'from': acc1, +# 'nonce': nonce +# }) +# signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY) +# transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) +# print("Transaction hash:", transaction_hash.hex()) +# return transaction_hash +# acc1 = web3.eth.account.from_key(PRIVATE_KEY).address +# initial_gas_price = web3.eth.gas_price + +# max_retries = 4 +# retries = 0 + +# transaction_hash = None +# try: +# transaction_hash = send_transaction(initial_gas_price) +# except ValueError as e: +# # If the error is due to "replacement transaction underpriced", handle it +# if "replacement transaction underpriced" in str(e): +# print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.") +# retries += 1 +# new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) +# transaction_hash = send_transaction(new_gas_price) +# elif "max fee per gas less than block base fee" in str(e): +# print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.") +# retries += 1 +# new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) +# transaction_hash = send_transaction(new_gas_price) +# else: +# raise + +# while retries < max_retries and transaction_hash: +# try: +# transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120) +# print("Transaction confirmed:") +# break +# except TimeExhausted: +# retries += 1 +# print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") +# # Increase the gas price by 10% for each retry +# new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) +# transaction_hash = send_transaction(new_gas_price) +# else: +# if retries == max_retries: +# print("Failed to confirm the transaction after multiple attempts.") +# raise TimeoutError("Failed to confirm the transaction after multiple attempts.") + +# transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed +# tx_id = transaction_hash.hex() +# return tx_id, str(transaction_fee) +# except Exception as e: +# print({"error":str(e)}) +# return e + def UploadScriptAuditData(privatekey,pubkey,user_id,project,data): + user = User.objects.get(id=user_id) try: acc1 = web3.eth.account.from_key(PRIVATE_KEY).address - def send_transaction( gas_price): - nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions - uploadData = contractInst.functions.UploadScriptAuditData(pubkey, user_id, project, data).build_transaction({ - 'gasPrice': gas_price, - 'chainId': CHAIN_ID, - 'from': acc1, - 'nonce': nonce - }) - signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY) - transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) - print("Transaction hash:", transaction_hash.hex()) - return transaction_hash - acc1 = web3.eth.account.from_key(PRIVATE_KEY).address - initial_gas_price = web3.eth.gas_price - - max_retries = 4 - retries = 0 - - transaction_hash = None - try: - transaction_hash = send_transaction(initial_gas_price) - except ValueError as e: - # If the error is due to "replacement transaction underpriced", handle it - if "replacement transaction underpriced" in str(e): - print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.") - retries += 1 - new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) - transaction_hash = send_transaction(new_gas_price) - elif "max fee per gas less than block base fee" in str(e): - print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.") - retries += 1 - new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) - transaction_hash = send_transaction(new_gas_price) - else: - raise - - while retries < max_retries and transaction_hash: - try: - transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120) - print("Transaction confirmed:") - break - except TimeExhausted: - retries += 1 - print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") - # Increase the gas price by 10% for each retry - new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries)) - transaction_hash = send_transaction(new_gas_price) - else: - if retries == max_retries: - print("Failed to confirm the transaction after multiple attempts.") - raise TimeoutError("Failed to confirm the transaction after multiple attempts.") - + nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions + uploadData = contractInst.functions.UploadScriptAuditData(pubkey, user_id, project, data).build_transaction({ + 'gasPrice': web3.eth.gas_price, + 'chainId': CHAIN_ID, + 'from': acc1, + 'nonce': nonce + }) + signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY) + transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) + print("Transaction hash:", transaction_hash.hex()) + transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120) transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed tx_id = transaction_hash.hex() - return tx_id, str(transaction_fee) + # Check if the transaction was successful + if transaction_receipt.status == 1: + print("Transaction successful!") + return tx_id, str(transaction_fee) + else: + print("Transaction failed!") + obj,_ = FailedTransactions.objects.get_or_create(service_id=project,service_name="ScriptAudit") + creator = User.objects.get(id=user_id) + obj.creator = creator + obj.transaction_type = "UploadScriptAuditData" + obj.transaction_hash = "" + obj.transaction_status = "failed" + obj.transaction_error = "Transaction failed" + obj.transaction_data = str(data) + obj.save() + return False, "Transaction failed" except Exception as e: print({"error":str(e)}) - return e - + obj,_ = FailedTransactions.objects.get_or_create(service_id=project,service_name="ScriptAudit") + creator = User.objects.get(id=user_id) + obj.creator = creator + obj.transaction_type = "UploadScriptAuditData" + obj.transaction_hash = "" + obj.transaction_status = "failed" + obj.transaction_error = "Transaction failed" + obj.transaction_data = str(data) + obj.save() + return False, str(e) + def getScriptAudit(private_key,user_id,project): ACCOUNT = web3.eth.account.from_key(private_key).address try: