Skip to content

Comments

Jerry#4

Open
Moises88g wants to merge 1 commit intoscy-edu:masterfrom
Moises88g:patch-1
Open

Jerry#4
Moises88g wants to merge 1 commit intoscy-edu:masterfrom
Moises88g:patch-1

Conversation

@Moises88g
Copy link

class Blockchain:
def init(self):
self.current_transactions = []
self.chain = []

    # Crear bloque genesis
    self.new_block(previous_hash=1, proof=100)

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])
    }

    # Resetear la lista de transacciones actuales
    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):
    return hashlib.sha256(json.dumps(block, sort_keys=True).encode()).hexdigest()

@property
def last_block(self):
    return self.chain[-1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant