def create_contract(db, block, tx, msg):
    if len(msg.sender) == 40:   # Sender is contract.
        sender = binascii.unhexlify(msg.sender)
    else:                       # Sender is regular address.
        sender = script.base58_check_decode(msg.sender, config.ADDRESSVERSION)

    if tx.sender != msg.sender:
        block.increment_nonce(msg.sender)
    nonce = utils.encode_int(block.get_nonce(msg.sender) - 1)
    # msg.to = utils.sha3(rlp.encode([sender, nonce]))[12:].encode('hex')
    msg.to = utils.contract_sha3(rlp.encode([sender, nonce]))
    assert not block.get_code(msg.to)

    res, gas, dat = apply_msg(db, block, tx, msg, msg.data)
    if res:
        cursor = db.cursor()
        bindings = {'contract_id': msg.to, 'tx_index': tx.tx_index, 'tx_hash': tx.tx_hash, 'block_index': block.number, 'source': tx.sender, 'code': bytes(dat), 'nonce': nonce}
        sql = '''INSERT INTO contracts VALUES (:contract_id, :tx_index, :tx_hash, :block_index, :source, :code, :nonce)'''
        cursor.execute(sql, bindings)
        return msg.to, gas, dat
    else:
        if tx.sender != msg.sender:
            block.decrement_nonce(msg.sender)
        block.del_account(msg.to)
        return res, gas, dat
Exemple #2
0
def create_contract(db, block, tx, msg):
    if len(msg.sender) == 40:  # Sender is contract.
        sender = binascii.unhexlify(msg.sender)
    else:  # Sender is regular address.
        sender = script.base58_check_decode(msg.sender, config.ADDRESSVERSION)

    if tx.sender != msg.sender:
        block.increment_nonce(msg.sender)
    nonce = utils.encode_int(block.get_nonce(msg.sender) - 1)
    # msg.to = utils.sha3(rlp.encode([sender, nonce]))[12:].encode('hex')
    msg.to = utils.contract_sha3(rlp.encode([sender, nonce]))
    assert not block.get_code(msg.to)

    res, gas, dat = apply_msg(db, block, tx, msg, msg.data)
    if res:
        cursor = db.cursor()
        bindings = {
            'contract_id': msg.to,
            'tx_index': tx.tx_index,
            'tx_hash': tx.tx_hash,
            'block_index': block.number,
            'source': tx.sender,
            'code': bytes(dat),
            'nonce': nonce
        }
        sql = '''INSERT INTO contracts VALUES (:contract_id, :tx_index, :tx_hash, :block_index, :source, :code, :nonce)'''
        cursor.execute(sql, bindings)
        return msg.to, gas, dat
    else:
        if tx.sender != msg.sender:
            block.decrement_nonce(msg.sender)
        block.del_account(msg.to)
        return res, gas, dat
Exemple #3
0
def test_sha3():
    sender = b'\x00\xb625+E\x06\x96\xbe\x10\x0e\xdc!58?\x00\x83\xa4\x99'
    nonce = b''

    b = rlp.encode([sender, nonce])
    assert b == b'\xd6\x94\x00\xb625+E\x06\x96\xbe\x10\x0e\xdc!58?\x00\x83\xa4\x99\x80'

    contract_id = hashlib.sha3_256(b).digest()
    assert contract_id == b'"Y[\xe7^\xe8\xba\r\xcc\xc2\xe5(\xe6\xc9\xed\xa9\x81w\x1a2\x17\xed\xa4\xf9?\x9c\xe3\xc3)\x16\x13\x99'

    contract_id = contract_id[12:]
    assert contract_id == b'\xe6\xc9\xed\xa9\x81w\x1a2\x17\xed\xa4\xf9?\x9c\xe3\xc3)\x16\x13\x99'

    contract_id = binascii.hexlify(contract_id).decode('ascii')
    assert contract_id == 'e6c9eda981771a3217eda4f93f9ce3c329161399'

    assert utils.contract_sha3(b) == 'e6c9eda981771a3217eda4f93f9ce3c329161399'
def test_sha3():
    sender = b'\x00\xb625+E\x06\x96\xbe\x10\x0e\xdc!58?\x00\x83\xa4\x99'
    nonce = b''

    b = rlp.encode([sender, nonce])
    assert b == b'\xd6\x94\x00\xb625+E\x06\x96\xbe\x10\x0e\xdc!58?\x00\x83\xa4\x99\x80'

    contract_id = hashlib.sha3_256(b).digest()
    assert contract_id == b'"Y[\xe7^\xe8\xba\r\xcc\xc2\xe5(\xe6\xc9\xed\xa9\x81w\x1a2\x17\xed\xa4\xf9?\x9c\xe3\xc3)\x16\x13\x99'

    contract_id = contract_id[12:]
    assert contract_id == b'\xe6\xc9\xed\xa9\x81w\x1a2\x17\xed\xa4\xf9?\x9c\xe3\xc3)\x16\x13\x99'

    contract_id = binascii.hexlify(contract_id).decode('ascii')
    assert contract_id == 'e6c9eda981771a3217eda4f93f9ce3c329161399'

    assert utils.contract_sha3(b) == 'e6c9eda981771a3217eda4f93f9ce3c329161399'