Exemplo n.º 1
0
def applytx(blockdata, txdata, debug=0):
    block = blocks.Block(blockdata.decode('hex'))
    tx = transactions.Transaction.parse(txdata.decode('hex'))
    if debug:
        processblock.debug = 1
    o = processblock.apply_tx(block, tx)
    return block.serialize().encode('hex'), ''.join(o).encode('hex')
Exemplo n.º 2
0
def applytx(blockdata, txdata, debug=0):
    block = blocks.Block(blockdata.decode('hex'))
    tx = transactions.Transaction.parse(txdata.decode('hex'))
    if debug:
        processblock.debug = 1
    o = processblock.apply_tx(block, tx)
    return block.serialize().encode('hex'), ''.join(o).encode('hex')
Exemplo n.º 3
0
def applytx(blockdata, txdata, debug=0, limit=2 ** 100):
    block = blocks.Block(blockdata.decode('hex'))
    tx = transactions.Transaction.parse(txdata.decode('hex'))
    if tx.startgas > limit:
        raise Exception("Transaction is asking for too much gas!")
    if debug:
        processblock.debug = 1
    o = processblock.apply_tx(block, tx)
    return block.serialize().encode('hex'), ''.join(o).encode('hex')
Exemplo n.º 4
0
def applytx(blockdata, txdata, debug=0, limit=2 ** 100):
    block = blocks.Block.hex_deserialize(blockdata)
    tx = transactions.Transaction.hex_deserialize(txdata)
    if tx.startgas > limit:
        raise Exception("Transaction is asking for too much gas!")
    if debug:
        processblock.debug = 1
    success, o = processblock.apply_tx(block, tx)
    return {
        "block": block.hex_serialize(),
        "result": ''.join(o).encode('hex') if tx.to else ''.join(o)
    }
Exemplo n.º 5
0
    def deserialize_child(self, rlpdata):
        """
        deserialization w/ replaying transactions
        """
        header_args, transaction_list, uncles = rlp.decode(rlpdata)
        assert len(header_args) == len(block_structure)
        kargs = dict(transaction_list=transaction_list, uncles=uncles)
        # Deserialize all properties
        for i, (name, typ, default) in enumerate(block_structure):
            kargs[name] = utils.decoders[typ](header_args[i])

        block = Block.init_from_parent(self,
                                       kargs['coinbase'],
                                       extra_data=kargs['extra_data'],
                                       timestamp=kargs['timestamp'])
        block.finalize()  # this is the first potential state change
        # replay transactions
        for tx_serialized, _state_root, _gas_used_encoded in transaction_list:
            tx = transactions.Transaction.deserialize(tx_serialized)
            processblock.apply_tx(block, tx)
            assert _state_root == block.state.root
            assert utils.decode_int(_gas_used_encoded) == block.gas_used

        # checks
        assert block.prevhash == self.hash
        assert block.state.root == kargs['state_root']
        assert block.tx_list_root == kargs['tx_list_root']
        assert block.gas_used == kargs['gas_used']
        assert block.gas_limit == kargs['gas_limit']
        assert block.timestamp == kargs['timestamp']
        assert block.difficulty == kargs['difficulty']
        assert block.number == kargs['number']
        assert block.extra_data == kargs['extra_data']
        assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash']

        block.uncles_hash = kargs['uncles_hash']
        block.nonce = kargs['nonce']
        block.min_gas_price = kargs['min_gas_price']

        return block
Exemplo n.º 6
0
    def deserialize_child(self, rlpdata):
        """
        deserialization w/ replaying transactions
        """
        header_args, transaction_list, uncles = rlp.decode(rlpdata)
        assert len(header_args) == len(block_structure)
        kargs = dict(transaction_list=transaction_list, uncles=uncles)
        # Deserialize all properties
        for i, (name, typ, default) in enumerate(block_structure):
            kargs[name] = utils.decoders[typ](header_args[i])

        block = Block.init_from_parent(self, kargs['coinbase'],
                                       extra_data=kargs['extra_data'],
                                       timestamp=kargs['timestamp'])
        block.finalize()  # this is the first potential state change
        # replay transactions
        for tx_serialized, _state_root, _gas_used_encoded in transaction_list:
            tx = transactions.Transaction.deserialize(tx_serialized)
            processblock.apply_tx(block, tx)
            assert _state_root == block.state.root
            assert utils.decode_int(_gas_used_encoded) == block.gas_used

        # checks
        assert block.prevhash == self.hash
        assert block.state.root == kargs['state_root']
        assert block.tx_list_root == kargs['tx_list_root']
        assert block.gas_used == kargs['gas_used']
        assert block.gas_limit == kargs['gas_limit']
        assert block.timestamp == kargs['timestamp']
        assert block.difficulty == kargs['difficulty']
        assert block.number == kargs['number']
        assert block.extra_data == kargs['extra_data']
        assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash']

        block.uncles_hash = kargs['uncles_hash']
        block.nonce = kargs['nonce']
        block.min_gas_price = kargs['min_gas_price']

        return block
Exemplo n.º 7
0
    def handle_new_dc(self, dc):
        nonce, to_addr, from_addr, sig, msghash, msgsize, data = self.unpackage_dc(dc)
        print data
        if self.dcp_list.has_key(to_addr):
            print 'addr exists! adding dc'
            self._add_dc_to_dcp(to_addr, from_addr, sig, msghash, msgsize, data)
        elif self.route_risk:
            print 'addr does not exist.  adding new dcp'
            self.init_dcp(to_addr)
            self._add_dc_to_dcp(to_addr, from_addr, sig, msghash, msgsize, data)
        else:
            return

        self.remove_old_tx(to_addr)
        self.dcp_list[to_addr].nonce = self.miner.block.get_nonce(self_addr) # add nonce here since we removed a transaction (maybeS)
        success, ans = processblock.apply_tx(self.miner.block, self.dcp_list[to_addr])
        print success, ans 
Exemplo n.º 8
0
    def handle_new_dc(self, dc):
        nonce, to_addr, from_addr, sig, msghash, msgsize, data = self.unpackage_dc(
            dc)
        print data
        if self.dcp_list.has_key(to_addr):
            print 'addr exists! adding dc'
            self._add_dc_to_dcp(to_addr, from_addr, sig, msghash, msgsize,
                                data)
        elif self.route_risk:
            print 'addr does not exist.  adding new dcp'
            self.init_dcp(to_addr)
            self._add_dc_to_dcp(to_addr, from_addr, sig, msghash, msgsize,
                                data)
        else:
            return

        self.remove_old_tx(to_addr)
        self.dcp_list[to_addr].nonce = self.miner.block.get_nonce(
            self_addr
        )  # add nonce here since we removed a transaction (maybeS)
        success, ans = processblock.apply_tx(self.miner.block,
                                             self.dcp_list[to_addr])
        print success, ans
Exemplo n.º 9
0
import blocks as b
import transactions as t
import utils as u
import sys

k = u.sha3('cow')
v = u.privtoaddr(k)

k2 = u.sha3('horse')
v2 = u.privtoaddr(k2)

print "Starting boring transfer test"
blk = b.Block.genesis({ v: 10**18 })
# Give tx2 some money
tx = t.Transaction(0,10**16,10**12,1000,v2,'').sign(k)
pb.apply_tx(blk,tx)
print "New balance of v1: ", blk.get_balance(v)
print "New balance of v2: ", blk.get_balance(v2)

print "Starting namecoin tests"
blk = b.Block.genesis({ v: 10**18 })
scode1 = '''
if !contract.storage[msg.data[0]]:
    contract.storage[msg.data[0]] = msg.data[1]
    return(1)
else:
    return(0)
'''
code1 = serpent.compile(scode1)
print "AST", serpent.rewrite(serpent.parse(scode1))
print "Assembly", serpent.compile_to_assembly(scode1)
Exemplo n.º 10
0
import blocks as b
import transactions as t
import utils as u
import sys

k = u.sha3('cow')
v = u.privtoaddr(k)

k2 = u.sha3('horse')
v2 = u.privtoaddr(k2)

print "Starting boring transfer test"
blk = b.Block.genesis({v: 10**18})
# Give tx2 some money
tx = t.Transaction(0, 10**16, 10**12, 1000, v2, '').sign(k)
pb.apply_tx(blk, tx)
print "New balance of v1: ", blk.get_balance(v)
print "New balance of v2: ", blk.get_balance(v2)

print "Starting namecoin tests"
blk = b.Block.genesis({v: 10**18})
scode1 = '''
if !contract.storage[msg.data[0]]:
    contract.storage[msg.data[0]] = msg.data[1]
    return(1)
else:
    return(0)
'''
code1 = serpent.compile(scode1)
print "AST", serpent.rewrite(serpent.parse(scode1))
print "Assembly", serpent.compile_to_assembly(scode1)
Exemplo n.º 11
0
 def apply_transaction(self, tx):
     return processblock.apply_tx(self, tx)
Exemplo n.º 12
0
# nonce,gasprice,startgas,to,value,data,v,r,s
tx = t.Transaction(0, gasprice, startgas, v2, u.denoms.finney * 10, '').sign(k)

assert blk in set([blk])
assert tx in set([tx])
assert tx.hex_hash() == \
    t.Transaction.deserialize(tx.serialize()).hex_hash()
assert tx.hex_hash() == \
    t.Transaction.hex_deserialize(tx.hex_serialize()).hex_hash()
assert tx in set([tx])

assert not tx in blk.get_transactions()

print("Balance of v1: ", blk.get_balance(v), v)
success, res = pb.apply_tx(blk, tx)
assert tx in blk.get_transactions()
print("applied transaction", success, res)
print("New balance of v1: ", blk.get_balance(v), v)
print("New balance of v2: ", blk.get_balance(v2), v2)
print("New balance of coinbase: ", blk.get_balance(blk.coinbase), blk.coinbase)
print('Transactions in block', blk, blk.get_transactions())


assert blk.hex_hash() == \
    b.Block.hex_deserialize(blk.hex_serialize()).hex_hash()

assert blk.get_balance(v) == u.denoms.finney * 990
assert blk.get_balance(v2) == u.denoms.finney * 10

print("Starting namecoin tests")
Exemplo n.º 13
0
# nonce,gasprice,startgas,to,value,data,v,r,s
tx = t.Transaction(0, gasprice, startgas, v2, u.denoms.finney * 10, '').sign(k)

assert blk in set([blk])
assert tx in set([tx])
assert tx.hex_hash() == \
    t.Transaction.deserialize(tx.serialize()).hex_hash()
assert tx.hex_hash() == \
    t.Transaction.hex_deserialize(tx.hex_serialize()).hex_hash()
assert tx in set([tx])

assert not tx in blk.get_transactions()

print("Balance of v1: ", blk.get_balance(v), v)
success, res = pb.apply_tx(blk, tx)
assert tx in blk.get_transactions()
print("applied transaction", success, res)
print("New balance of v1: ", blk.get_balance(v), v)
print("New balance of v2: ", blk.get_balance(v2), v2)
print("New balance of coinbase: ", blk.get_balance(blk.coinbase), blk.coinbase)
print ('Transactions in block', blk, blk.get_transactions())


assert blk.hex_hash() == \
    b.Block.hex_deserialize(blk.hex_serialize()).hex_hash()


assert blk.get_balance(v) == u.denoms.finney * 990
assert blk.get_balance(v2) == u.denoms.finney * 10
Exemplo n.º 14
0
 def apply_transaction(self, tx):
     return processblock.apply_tx(self, tx)