chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit 39c59cceb9f3cf424bdddd7d755630157f7d8aaa
parent 139eb13f719b93e547884d9361f1de6dbb40a320
Author: nolash <dev@holbrook.no>
Date:   Fri, 30 Apr 2021 09:27:21 +0200

Move code to external module

Diffstat:
Dchainlib/eth/owned.py | 73-------------------------------------------------------------------------
Dtests/test_owned.py | 131-------------------------------------------------------------------------------
Dtests/testdata/Owned.bin | 2--
Dtests/testdata/Owned.sol | 32--------------------------------
4 files changed, 0 insertions(+), 238 deletions(-)

diff --git a/chainlib/eth/owned.py b/chainlib/eth/owned.py @@ -1,73 +0,0 @@ -# external imports -from hexathon import ( - add_0x, - ) - -# local imports -from chainlib.eth.contract import ( - ABIContractEncoder, - ABIContractDecoder, - ABIContractType, - abi_decode_single, - ) -from chainlib.jsonrpc import jsonrpc_template -from chainlib.eth.tx import ( - TxFactory, - TxFormat, - ) -from chainlib.eth.constant import ZERO_ADDRESS - -class EIP173(TxFactory): - - def transfer_ownership(self, contract_address, sender_address, new_owner_address, tx_format=TxFormat.JSONRPC): - enc = ABIContractEncoder() - enc.method('transferOwnership') - enc.typ(ABIContractType.ADDRESS) - enc.address(new_owner_address) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address, use_nonce=True) - tx = self.set_code(tx, data) - tx = self.finalize(tx, tx_format) - return tx - - - def owner(self, contract_address, sender_address=ZERO_ADDRESS): - o = jsonrpc_template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('owner') - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - o['params'].append('latest') - return o - - - @classmethod - def parse_owner(self, v): - return abi_decode_single(ABIContractType.ADDRESS, v) - - -class Owned(EIP173): - - def accept_ownership(self, contract_address, sender_address, tx_format=TxFormat.JSONRPC): - enc = ABIContractEncoder() - enc.method('acceptOwnership') - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address, use_nonce=True) - tx = self.set_code(tx, data) - tx = self.finalize(tx, tx_format) - return tx - - - def take_ownership(self, contract_address, sender_address, resource_address, tx_format=TxFormat.JSONRPC): - enc = ABIContractEncoder() - enc.method('takeOwnership') - enc.typ(ABIContractType.ADDRESS) - enc.address(resource_address) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address, use_nonce=True) - tx = self.set_code(tx, data) - tx = self.finalize(tx, tx_format) - return tx diff --git a/tests/test_owned.py b/tests/test_owned.py @@ -1,131 +0,0 @@ -# standard imports -import unittest -import os -import logging - -# local imports -from chainlib.eth.unittest.ethtester import EthTesterCase -from chainlib.eth.nonce import RPCNonceOracle -from chainlib.eth.gas import OverrideGasOracle -from chainlib.connection import RPCConnection -from chainlib.eth.tx import ( - TxFactory, - receipt, - ) -from chainlib.eth.owned import ( - EIP173, - Owned, - ) - -logging.basicConfig(level=logging.DEBUG) -logg = logging.getLogger() - -script_dir = os.path.realpath(os.path.dirname(__file__)) - - -class TestOwned(EthTesterCase): - - def setUp(self): - super(TestOwned, self).setUp() - self.conn = RPCConnection.connect(self.chain_spec, 'default') - nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) - - f = open(os.path.join(script_dir, 'testdata', 'Owned.bin')) - code = f.read() - f.close() - - txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - tx = txf.template(self.accounts[0], None, use_nonce=True) - tx = txf.set_code(tx, code) - (tx_hash_hex, o) = txf.build(tx) - - r = self.conn.do(o) - logg.debug('deployed with hash {}'.format(r)) - - o = receipt(tx_hash_hex) - r = self.conn.do(o) - self.address = r['contract_address'] - - - def test_owned(self): - c = EIP173(self.chain_spec) - o = c.owner(self.address, sender_address=self.accounts[0]) - r = self.conn.do(o) - owner = c.parse_owner(r) - self.assertEqual(owner, self.accounts[0]) - - - def test_transfer_ownership(self): - nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn) - gas_oracle = OverrideGasOracle(limit=8000000, conn=self.conn) - c = EIP173(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) - (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[2], self.accounts[1]) - r = self.conn.do(o) - - o = receipt(tx_hash_hex) - r = self.conn.do(o) - self.assertEqual(r['status'], 0) - - nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) - c = EIP173(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) - (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.accounts[1]) - r = self.conn.do(o) - - o = receipt(tx_hash_hex) - r = self.conn.do(o) - self.assertEqual(r['status'], 1) - - - def test_accept_ownership(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) - gas_oracle = OverrideGasOracle(limit=8000000, conn=self.conn) - c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) - (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.accounts[1]) - r = self.conn.do(o) - - nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn) - c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) - (tx_hash_hex, o) = c.accept_ownership(self.address, self.accounts[2]) - r = self.conn.do(o) - - o = receipt(tx_hash_hex) - r = self.conn.do(o) - self.assertEqual(r['status'], 0) - - nonce_oracle = RPCNonceOracle(self.accounts[1], self.conn) - c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) - (tx_hash_hex, o) = c.accept_ownership(self.address, self.accounts[1]) - r = self.conn.do(o) - - o = receipt(tx_hash_hex) - r = self.conn.do(o) - self.assertEqual(r['status'], 1) - - o = c.owner(self.address, sender_address=self.accounts[0]) - r = self.conn.do(o) - owner = c.parse_owner(r) - self.assertEqual(owner, self.accounts[1]) - - - def test_take_ownership(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) - gas_oracle = OverrideGasOracle(limit=8000000, conn=self.conn) - c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) - (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.address) - r = self.conn.do(o) - - (tx_hash_hex, o) = c.take_ownership(self.address, self.accounts[0], self.address) - r = self.conn.do(o) - - o = receipt(tx_hash_hex) - r = self.conn.do(o) - self.assertEqual(r['status'], 1) - - o = c.owner(self.address, sender_address=self.accounts[0]) - r = self.conn.do(o) - owner = c.parse_owner(r) - self.assertEqual(owner, self.address) - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/testdata/Owned.bin b/tests/testdata/Owned.bin @@ -1 +0,0 @@ -608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610624806100606000396000f3fe608060405234801561001057600080fd5b5060043610610069576000357c0100000000000000000000000000000000000000000000000000000000900480636b5783391461006e57806379ba50971461009e5780638da5cb5b146100bc578063f2fde38b146100da575b600080fd5b6100886004803603810190610083919061040e565b61010a565b60405161009591906104db565b60405180910390f35b6100a661024c565b6040516100b391906104db565b60405180910390f35b6100c4610331565b6040516100d191906104c0565b60405180910390f35b6100f460048036038101906100ef919061040e565b610355565b60405161010191906104db565b60405180910390f35b60008060608373ffffffffffffffffffffffffffffffffffffffff166040516024016040516020818303038152906040527f79ba5097000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516101b691906104a9565b6000604051808303816000865af19150503d80600081146101f3576040519150601f19603f3d011682016040523d82523d6000602084013e6101f8565b606091505b50809250819350505081610241576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610238906104f6565b60405180910390fd5b600192505050919050565b60003373ffffffffffffffffffffffffffffffffffffffff16600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146102a857600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60003373ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146103af57600080fd5b81600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060019050919050565b600081359050610408816105d7565b92915050565b60006020828403121561042057600080fd5b600061042e848285016103f9565b91505092915050565b6104408161053d565b82525050565b61044f8161054f565b82525050565b600061046082610516565b61046a8185610521565b935061047a81856020860161057b565b80840191505092915050565b6000610493600a8361052c565b915061049e826105ae565b602082019050919050565b60006104b58284610455565b915081905092915050565b60006020820190506104d56000830184610437565b92915050565b60006020820190506104f06000830184610446565b92915050565b6000602082019050818103600083015261050f81610486565b9050919050565b600081519050919050565b600081905092915050565b600082825260208201905092915050565b60006105488261055b565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60005b8381101561059957808201518184015260208101905061057e565b838111156105a8576000848401525b50505050565b7f4552525f41434345505400000000000000000000000000000000000000000000600082015250565b6105e08161053d565b81146105eb57600080fd5b5056fea26469706673582212205909b3637c67bdd4ec307d714ab75291de5f5d7ae0872a6fd6889aad21de847464736f6c63430008030033 -\ No newline at end of file diff --git a/tests/testdata/Owned.sol b/tests/testdata/Owned.sol @@ -1,32 +0,0 @@ -pragma solidity ^0.8.0; - -contract Owned { - address public owner; - address newOwner; - - constructor() public { - owner = msg.sender; - } - - function transferOwnership(address _newOwner) public returns (bool) { - require(owner == msg.sender); - newOwner = _newOwner; - return true; - } - - function acceptOwnership() public returns (bool) { - require(newOwner == msg.sender); - owner = msg.sender; - newOwner = address(0); - return true; - } - - function takeOwnership(address _ownable) public returns (bool) { - bool ok; - bytes memory result; - - (ok, result) = _ownable.call(abi.encodeWithSignature("acceptOwnership()")); - require(ok, "ERR_ACCEPT"); - return true; - } -}