feat: add basic blockchain
All checks were successful
Test CI / test speed (push) Successful in 1m48s
All checks were successful
Test CI / test speed (push) Successful in 1m48s
This commit is contained in:
parent
6c240b1237
commit
e8e7c59579
59
src/eth_logger.py
Normal file
59
src/eth_logger.py
Normal file
@ -0,0 +1,59 @@
|
||||
from web3 import Web3
|
||||
import json
|
||||
|
||||
rpc_url = "https://ethereum-holesky-rpc.publicnode.com"
|
||||
chain = Web3(Web3.HTTPProvider(rpc_url))
|
||||
contract_address = "0x642C23F91bf8339044A00251BC09d1D98110C433"
|
||||
contract_abi = json.loads('''[
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "string",
|
||||
"name": "msg",
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"name": "messageLog",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "string",
|
||||
"name": "text",
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"name": "logmessage",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bool",
|
||||
"name": "",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}
|
||||
]''')
|
||||
contract = chain.eth.contract(address=contract_address, abi=contract_abi)
|
||||
wallet_address = "0xe02666Cb63b3645E7B03C9082a24c4c1D7C9EFf6"
|
||||
pk = "ae66ae3711a69079efd3d3e9b55f599ce7514eb29dfe4f9551404d3f361438c6"
|
||||
|
||||
|
||||
def call_eth_logger(wallet_address, pk, message: str):
|
||||
transaction = contract.functions.logmessage(message).build_transaction({
|
||||
'chainId': 17000,
|
||||
'gas': 30000,
|
||||
'gasPrice': chain.to_wei('10', 'gwei'),
|
||||
'nonce': chain.eth.get_transaction_count(wallet_address, 'pending'),
|
||||
})
|
||||
signed_tx = chain.eth.account.sign_transaction(transaction, private_key=pk)
|
||||
tx_hash = chain.eth.send_raw_transaction(signed_tx.raw_transaction)
|
||||
print(tx_hash)
|
||||
receipt = chain.eth.wait_for_transaction_receipt(tx_hash)
|
||||
transfer_event = contract.events.messageLog().process_receipt(receipt)
|
||||
for event in transfer_event:
|
||||
print(event['args']['msg'])
|
50
src/node.py
50
src/node.py
@ -1,12 +1,18 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
import requests
|
||||
from contextlib import asynccontextmanager
|
||||
import socket
|
||||
import asyncio
|
||||
from pydantic import BaseModel
|
||||
from tpre import capsule, ReEncrypt
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import requests
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from eth_logger import call_eth_logger
|
||||
from tpre import ReEncrypt, capsule
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@ -16,6 +22,8 @@ async def lifespan(_: FastAPI):
|
||||
clear()
|
||||
|
||||
|
||||
message_list = []
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
server_address = "http://60.204.236.38:8000/server"
|
||||
id = 0
|
||||
@ -117,6 +125,17 @@ async def user_src(message: Req):
|
||||
capsule = message.capsule
|
||||
ct = message.ct
|
||||
|
||||
payload = {
|
||||
"source_ip": source_ip,
|
||||
"dest_ip": dest_ip,
|
||||
"capsule": capsule,
|
||||
"ct": ct,
|
||||
"rk": message.rk,
|
||||
}
|
||||
# 将消息详情记录到区块链
|
||||
global message_list
|
||||
message_list.append(payload)
|
||||
|
||||
byte_length = (ct.bit_length() + 7) // 8
|
||||
capsule_ct = (capsule, ct.to_bytes(byte_length))
|
||||
|
||||
@ -151,7 +170,24 @@ async def send_user_des_message(
|
||||
print("send stauts:", response.text)
|
||||
|
||||
|
||||
def log_message():
|
||||
while True:
|
||||
global message_list
|
||||
payload = json.dumps(message_list)
|
||||
message_list = []
|
||||
call_eth_logger(wallet_address, wallet_pk, payload)
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
wallet_address = (
|
||||
"0xe02666Cb63b3645E7B03C9082a24c4c1D7C9EFf6" # 修改成要使用的钱包地址/私钥
|
||||
)
|
||||
wallet_pk = "ae66ae3711a69079efd3d3e9b55f599ce7514eb29dfe4f9551404d3f361438c6"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn # pylint: disable=e0401
|
||||
|
||||
threading.Thread(target=log_message).start()
|
||||
|
||||
uvicorn.run("node:app", host="0.0.0.0", port=8001, reload=True, log_level="debug")
|
||||
|
Loading…
x
Reference in New Issue
Block a user