Есть ли кто-нибудь, кто может показать мне [закрыто]Python

Программы на Python
Anonymous
Есть ли кто-нибудь, кто может показать мне [закрыто]

Сообщение Anonymous »

Питон

Код: Выделить всё

import hashlib

import json

from time import time

from flask import Flask, jsonify, request

class Blockchain:

def __init__(self):

self.chain = []

self.current_transactions = []

self.new_block(previous_hash='1', proof=100)  # Create the genesis block

def new_block(self, proof, previous_hash=None):

block = {

'index': len(self.chain) + 1,

'timestamp': time(),

'transactions': self.current_transactions,

'proof': proof,

'previous_hash': previous_hash or self.hash(self.chain[-1]),

}

self.current_transactions = []

self.chain.append(block)

return block

def new_transaction(self, sender, recipient, amount):

self.current_transactions.append({

'sender': sender,

'recipient': recipient,

'amount': amount,

})

return self.last_block['index'] + 1

@staticmethod

def hash(block):

block_string = json.dumps(block, sort_keys=True).encode()

return hashlib.sha256(block_string).hexdigest()

@property

def last_block(self):

return self.chain[-1]

# Create a Flask web server

app = Flask(__name__)

# Create an instance of the Blockchain

blockchain = Blockchain()

@app.route('/mine', methods=['GET'])

def mine():

last_block = blockchain.last_block

last_proof = last_block['proof']

proof = blockchain.proof_of_work(last_proof)

blockchain.new_transaction(

sender="0",  # This signifies that this node has mined a new coin

recipient="your_address",  # Replace with your address

amount=1,  # Amount of MyCoin awarded

)

block = blockchain.new_block(proof, blockchain.hash(last_block))

response = {

'message': 'New Block Forged',

'index': block['index'],

'transactions': block['transactions'],

'proof': block['proof'],

'previous_hash': block['previous_hash'],

}

return jsonify(response), 200

@app.route('/transactions/new', methods=['POST'])

def new_transaction():

values = request.get_json()

required = ['sender', 'recipient', 'amount']

if not all(k in values for k in required):

return 'Missing values', 400

index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])

response = {'message': f'Transaction will be added to Block {index}'}

return jsonify(response), 201

@app.route('/chain', methods=['GET'])

def full_chain():

response = {

'chain': blockchain.chain,

'length': len(blockchain.chain),

}

return jsonify(response), 200

if __name__ == '__main__':

app.run(host='0.0.0.0', port=5000)

```

### Step 3: Running the Blockchain

1. Save the code in a file named `blockchain.py`.

2.  Run the server using the command:

```bash

python blockchain.py

import time

class Block:

def __init__(self, index, proof_no, prev_hash, data, timestamp=None):

self.index = index

self.proof_no = proof_no

self.prev_hash = prev_hash

self.data = data

self.timestamp = timestamp or time.time()

@property

def calculate_hash(self):

block_string = f"{self.index}{self.proof_no}{self.prev_hash}{self.data}{self.timestamp}"

return hashlib.sha256(block_string.encode()).hexdigest()

class BlockChain:

def __init__(self):

self.chain = []

self.construct_genesis()

def construct_genesis(self):

# Create the initial block (genesis block)

pass  # Your implementation here

def construct_block(self, proof_no, prev_hash):

# Create a new block and add it to the chain

pass  # Your implementation here

@staticmethod

def check_validity():

# Check whether the blockchain is valid

pass  # Your implementation here

def new_data(self, sender, recipient, quantity):

# Add a new transaction to the data of the transactions

pass  # Your implementation here

@staticmethod

def construct_proof_of_work(prev_proof):

# Protect the blockchain from attack

pass  # Your implementation here

@property

def last_block(self):

# Return the last block in the chain

return self.chain[-1]
Помочь создать приложение и сделать его более широким

Подробнее здесь: https://stackoverflow.com/questions/787 ... an-show-me

Вернуться в «Python»