Example #1
0
    def run(cls, config, mongo, to, value):
        Peers.init(config, mongo, config.network)

        try:
            transaction = TransactionFactory(
                config,
                mongo,
                fee=0.01,
                public_key=config.public_key,
                private_key=config.private_key,
                outputs=[Output(to=to, value=value)])
        except NotEnoughMoneyException as e:
            print "not enough money yet"
            return
        except:
            raise
        try:
            transaction.transaction.verify()
        except:
            print 'transaction failed'
        TU.save(transaction.transaction)
        print 'Transaction generated successfully. Sending:', value, 'To:', to
        for peer in Peers.peers:
            try:
                socketIO = SocketIO(peer.host,
                                    peer.port,
                                    wait_for_connection=False)
                chat_namespace = socketIO.define(ChatNamespace, '/chat')
                chat_namespace.emit('newtransaction',
                                    transaction.transaction.to_dict())
                socketIO.disconnect()
                print 'Sent to:', peer.host, peer.port
            except Exception as e:
                print e
Example #2
0
    def run(cls, config, mongo):
        used_inputs = []
        new_inputs = []
        for x in mongo.site_db.faucet.find({'active': True}):
            balance = BU.get_wallet_balance(config, mongo, x['address'])
            if balance >= 25:
                mongo.site_db.faucet.update({'_id': x['_id']}, {
                    'active': False,
                    'address': x['address']
                })

                continue
            last_id_in_blockchain = x.get('last_id')
            if last_id_in_blockchain and not mongo.db.blocks.find({
                    'transactions.id':
                    last_id_in_blockchain
            }).count():

                continue

            try:
                transaction = TransactionFactory(
                    config,
                    mongo,
                    block_height=BU.get_latest_block(config, mongo)['index'],
                    fee=0.01,
                    public_key=config.public_key,
                    private_key=config.private_key,
                    outputs=[Output(to=x['address'], value=5)])
            except NotEnoughMoneyException as e:
                print "not enough money yet"
                return
            except Exception as e:
                print x
            try:
                transaction.transaction.verify()
            except:
                mongo.site_db.failed_faucet_transactions.insert(
                    transaction.transaction.to_dict())
                print 'faucet transaction failed'
            TU.save(config, mongo, transaction.transaction)
            x['last_id'] = transaction.transaction.transaction_signature
            mongo.site_db.faucet.update({'_id': x['_id']}, x)
            print 'saved. sending...', x['address']
            for peer in Peers.peers:
                try:
                    socketIO = SocketIO(peer.host,
                                        peer.port,
                                        wait_for_connection=False)
                    chat_namespace = socketIO.define(ChatNamespace, '/chat')
                    chat_namespace.emit('newtransaction',
                                        transaction.transaction.to_dict())
                    socketIO.disconnect()
                except Exception as e:
                    print e
Example #3
0
    def run(cls, config, mongo, to, value):
        Peers.init(config, mongo, config.network)

        try:
            transaction = TransactionFactory(config,
                                             mongo,
                                             block_height=BU.get_latest_block(
                                                 config, mongo)['index'],
                                             fee=0.01,
                                             public_key=config.public_key,
                                             private_key=config.private_key,
                                             outputs=[{
                                                 'to': to,
                                                 'value': value
                                             }])
        except NotEnoughMoneyException as e:
            print "not enough money yet"
            return
        except:
            raise
        try:
            transaction.transaction.verify()
        except:
            print 'transaction failed'
        TU.save(config, mongo, transaction.transaction)
        print 'Transaction generated successfully. Sending:', value, 'To:', to
        for peer in Peers.peers:
            try:
                with SocketIO(peer.host,
                              peer.port,
                              ChatNamespace,
                              wait_for_connection=False) as socketIO:
                    chat_namespace = socketIO.define(ChatNamespace, '/chat')
                    chat_namespace.emit('newtransaction',
                                        transaction.transaction.to_dict())
                    socketIO.disconnect()
                    print 'Sent to:', peer.host, peer.port
            except Exception as e:
                print e
Example #4
0
    def do_payout_for_block(self, block):
        # check if we already paid out

        already_used = self.already_used(block.get_coinbase())
        if already_used:
            self.mongo.db.shares.remove({'index': block.index})
            return

        existing = self.mongo.db.share_payout.find_one({'index': block.index})
        if existing:
            pending = self.mongo.db.miner_transactions.find_one(
                {'inputs.id': block.get_coinbase().transaction_signature})
            if pending:
                return
            else:
                # rebroadcast
                transaction = Transaction.from_dict(
                    self.config, self.mongo,
                    BU.get_latest_block(self.config, self.mongo)['index'],
                    existing['txn'])
                TU.save(self.config, self.mongo, transaction)
                self.broadcast_transaction(transaction)
                return

        try:
            shares = self.get_share_list_for_height(block.index)
        except Exception as e:
            print e
            return

        total_reward = block.get_coinbase()
        if total_reward.outputs[0].to != self.config.address:
            return
        pool_take = 0.01
        total_pool_take = total_reward.outputs[0].value * pool_take
        total_payout = total_reward.outputs[0].value - total_pool_take

        outputs = []
        for address, x in shares.iteritems():
            exists = self.mongo.db.share_payout.find_one({
                'index':
                block.index,
                'txn.outputs.to':
                address
            })
            if exists:
                raise PartialPayoutException(
                    'this index has been partially paid out.')

            payout = total_payout * x['payout_share']
            outputs.append({'to': address, 'value': payout})

        try:
            transaction = TransactionFactory(
                self.config,
                self.mongo,
                block_height=block.index,
                fee=0.0001,
                public_key=self.config.public_key,
                private_key=self.config.private_key,
                inputs=[{
                    'id': total_reward.transaction_signature
                }],
                outputs=outputs)
        except NotEnoughMoneyException as e:
            print "not enough money yet"
            return
        except Exception as e:
            print e

        try:
            transaction.transaction.verify()
        except:
            raise
            print 'faucet transaction failed'

        TU.save(self.config, self.mongo, transaction.transaction)
        self.mongo.db.share_payout.insert({
            'index':
            block.index,
            'txn':
            transaction.transaction.to_dict()
        })

        self.broadcast_transaction(transaction.transaction)