Beispiel #1
0
    def create_block(self):

        # trans = []
        # temp = []
        # for i in range(len(self.transactions)):
        # 	a = self.transactions[i]
        # 	transaction = Transaction(self.nodes[a[1]], self.nodes[a[2]], a[3], a[4])
        # 	transaction.signTransaction()
        # 	temp.append(transaction)

        # temp.sort(key = lambda x: x.creation_time)

        # for i in range(self.block_size):
        # 	a = temp.pop(0)
        # 	trans.append(a)

        trans = []
        self.transactions.sort(key=lambda x: x[4])
        for i in range(self.block_size):
            a = self.transactions.pop(0)
            transaction = Transaction(self.nodes[a[1]], self.nodes[a[2]], a[3],
                                      a[4])
            transaction.signTransaction()
            trans.append(transaction)

        block1 = block(time.time(), trans, "")
        currHash = self.bitcoin.current_hash()
        block1.set_prev_hash(currHash)
        block1.mineBlock(3)
        self.block_lock.acquire()
        if (self.block_messages == []):
            self.block_lock.release()
            self.bitcoin.addNewBlock(block1)
            for i in range(len(self.nodes)):
                if (i != self.walletId):
                    nodes[i].block_lock.acquire()
                    nodes[i].block_messages.append(block1)
                    nodes[i].block_lock.release()
        else:
            for i in range(len(self.block_messages)):
                self.bitcoin.addNewBlock(self.block_messages[i])
            self.block_lock.release()
Beispiel #2
0
# Your private key goes here
priv_key = keys.gen_private_key(curve.secp256k1)

# From that we can calculate your public key(which doubles as your wallet address)
pub_key = keys.get_public_key(priv_key, curve.secp256k1)
myWalletAddress = hex(int(str(pub_key.x) + str(pub_key.y)))

# Create new instance of Blockchain class
print('Initialize coin')
rebelCoin = Blockchain()

# Create a transaction & sign it with your key
print('1st trans')
tx1 = Transaction(myWalletAddress, '123456789', 100)
tx1.signTransaction(priv_key)
# These try/except blocks are neccessary for now due to a bug i have where
# there's either an ecdsa error or a key error, but reruning fixes it
try:
    try:
        rebelCoin.addTransaction(tx1)
    except ecdsa.EcdsaError:
        rebelCoin.addTransaction(tx1)
except:
    rebelCoin.addTransaction(tx1)
print()

# Mine block
rebelCoin.minePendingTransactions(myWalletAddress)

# Create second transaction