mirror of
git://holbrook.no/eth-address-index
synced 2024-11-30 11:56:45 +01:00
Refactor interfaces
This commit is contained in:
parent
66e03ff0cc
commit
2b207135d7
1
solidity/AddressDeclarator.bin
Normal file
1
solidity/AddressDeclarator.bin
Normal file
File diff suppressed because one or more lines are too long
1
solidity/AddressDeclarator.json
Normal file
1
solidity/AddressDeclarator.json
Normal file
@ -0,0 +1 @@
|
||||
[{"inputs":[{"internalType":"bytes32[]","name":"_descriptions","type":"bytes32[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"},{"internalType":"bytes32","name":"_proof","type":"bytes32"}],"name":"addDeclaration","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_declarator","type":"address"},{"internalType":"address","name":"_target","type":"address"}],"name":"declaration","outputs":[{"internalType":"bytes32[]","name":"","type":"bytes32[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_target","type":"address"},{"internalType":"uint256","name":"_idx","type":"uint256"}],"name":"declarator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"declaratorCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
|
55
solidity/AddressDeclarator.sol
Normal file
55
solidity/AddressDeclarator.sol
Normal file
@ -0,0 +1,55 @@
|
||||
pragma solidity >=0.6.12;
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
contract AddressDeclarator {
|
||||
|
||||
struct declaratorItem {
|
||||
address signer;
|
||||
bytes32[] content;
|
||||
}
|
||||
|
||||
mapping( address => uint256[] ) declaratorItemsIndex;
|
||||
mapping( address => uint256 ) public declaratorCount;
|
||||
mapping( address => mapping ( address => declaratorItem ) ) declarationByDeclaratorIndex;
|
||||
declaratorItem[] declaratorItems;
|
||||
|
||||
constructor(bytes32[] memory _descriptions) {
|
||||
for (uint i; i < _descriptions.length; i++) {
|
||||
addDeclaration(msg.sender, _descriptions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) {
|
||||
declaratorItem storage item;
|
||||
|
||||
item = declarationByDeclaratorIndex[msg.sender][_subject];
|
||||
if (item.signer == address(0)) {
|
||||
item.signer = msg.sender;
|
||||
declaratorItemsIndex[_subject].push(declaratorItems.length);
|
||||
declaratorItems.push(item);
|
||||
declaratorCount[_subject]++;
|
||||
}
|
||||
item.content.push(_proof);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function declarator(address _target, uint256 _idx) public view returns ( address ) {
|
||||
uint256 idx;
|
||||
declaratorItem storage item;
|
||||
|
||||
idx = declaratorItemsIndex[_target][_idx];
|
||||
item = declaratorItems[idx];
|
||||
|
||||
return item.signer;
|
||||
}
|
||||
|
||||
function declaration(address _declarator, address _target) public view returns ( bytes32[] memory ) {
|
||||
declaratorItem storage item;
|
||||
|
||||
item = declarationByDeclaratorIndex[_declarator][_target];
|
||||
|
||||
return item.content;
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
all:
|
||||
solc TokenEndorser.sol --abi | awk 'NR>3' > TokenEndorser.json
|
||||
solc TokenEndorser.sol --bin | awk 'NR>3' > TokenEndorser.bin
|
||||
truncate -s -1 TokenEndorser.bin
|
||||
solc AddressDeclarator.sol --abi | awk 'NR>3' > AddressDeclarator.json
|
||||
solc AddressDeclarator.sol --bin | awk 'NR>3' > AddressDeclarator.bin
|
||||
truncate -s -1 AddressDeclarator.bin
|
||||
|
||||
test: all
|
||||
python test.py
|
||||
|
@ -19,60 +19,80 @@ provider = web3.Web3.EthereumTesterProvider(instance)
|
||||
w3 = web3.Web3(provider)
|
||||
|
||||
|
||||
f = open('TokenEndorser.bin', 'r')
|
||||
f = open('AddressDeclarator.bin', 'r')
|
||||
bytecode = f.read()
|
||||
f.close()
|
||||
|
||||
f = open('TokenEndorser.json', 'r')
|
||||
f = open('AddressDeclarator.json', 'r')
|
||||
abi = json.load(f)
|
||||
f.close()
|
||||
|
||||
token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
|
||||
#token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
|
||||
|
||||
c = w3.eth.contract(abi=abi, bytecode=bytecode)
|
||||
tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
|
||||
#tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
|
||||
|
||||
declarations = [
|
||||
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
|
||||
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
|
||||
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
|
||||
]
|
||||
|
||||
# Deployment is a self-signed declaration
|
||||
tx_hash = c.constructor(declarations[0]).transact({'from': w3.eth.accounts[0]})
|
||||
r = w3.eth.getTransactionReceipt(tx_hash)
|
||||
logg.debug('contract {} initial token {}'.format(r.contractAddress, token_address))
|
||||
logg.debug('contract {}'.format(r.contractAddress))
|
||||
|
||||
c = w3.eth.contract(abi=abi, address=r.contractAddress)
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
|
||||
# Initial token will fail in any case
|
||||
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
|
||||
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
|
||||
assert r == 1
|
||||
|
||||
fail = False
|
||||
try:
|
||||
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
|
||||
except:
|
||||
fail = True
|
||||
pass
|
||||
r = c.functions.declarator(w3.eth.accounts[0], 0).call()
|
||||
assert r == w3.eth.accounts[0]
|
||||
|
||||
if not fail:
|
||||
raise RuntimeError('expected fail on register same token to same address')
|
||||
r = c.functions.declaration(w3.eth.accounts[0], w3.eth.accounts[0]).call()
|
||||
assert r[0].hex() == declarations[0][0][2:]
|
||||
assert r[1].hex() == declarations[0][1][2:]
|
||||
|
||||
|
||||
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[1]})
|
||||
# Add first declaration for 0 by 2
|
||||
c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][0]).transact({'from': w3.eth.accounts[2]})
|
||||
|
||||
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
|
||||
assert r == 2
|
||||
|
||||
r = c.functions.declarator(w3.eth.accounts[0], 1).call()
|
||||
assert r == w3.eth.accounts[2]
|
||||
|
||||
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
|
||||
assert r[0].hex() == declarations[1][0][2:]
|
||||
|
||||
|
||||
h = hashlib.new('sha256')
|
||||
h.update(bytes.fromhex(token_address[2:]))
|
||||
h.update(bytes.fromhex(w3.eth.accounts[0][2:]))
|
||||
z = h.digest()
|
||||
# Add second declaration for 0 by 2
|
||||
c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][1]).transact({'from': w3.eth.accounts[2]})
|
||||
|
||||
assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
|
||||
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
|
||||
assert r == 2
|
||||
|
||||
|
||||
another_token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
|
||||
c.functions.add(another_token_address, d).transact({'from':w3.eth.accounts[0]})
|
||||
|
||||
assert c.functions.endorsers(w3.eth.accounts[0], 0).call() == 1
|
||||
assert c.functions.endorsers(w3.eth.accounts[1], 0).call() == 1
|
||||
assert c.functions.endorsers(w3.eth.accounts[0], 1).call() == 2
|
||||
|
||||
assert c.functions.tokens(1).call() == token_address
|
||||
assert c.functions.tokens(2).call() == another_token_address
|
||||
|
||||
assert c.functions.tokenIndex(token_address).call() == 1
|
||||
assert c.functions.tokenIndex(another_token_address).call() == 2
|
||||
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
|
||||
assert r[0].hex() == declarations[1][0][2:]
|
||||
assert r[1].hex() == declarations[1][1][2:]
|
||||
|
||||
|
||||
# Add first declaration for 1 by 2
|
||||
c.functions.addDeclaration(w3.eth.accounts[1], declarations[2][0]).transact({'from': w3.eth.accounts[2]})
|
||||
|
||||
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
|
||||
assert r == 2
|
||||
|
||||
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
|
||||
assert r == 1
|
||||
|
||||
r = c.functions.declarator(w3.eth.accounts[1], 0).call()
|
||||
assert r == w3.eth.accounts[2]
|
||||
|
||||
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[1]).call()
|
||||
assert r[0].hex() == declarations[2][0][2:]
|
||||
|
||||
|
78
solidity/tokenendorser_test.py
Normal file
78
solidity/tokenendorser_test.py
Normal file
@ -0,0 +1,78 @@
|
||||
import logging
|
||||
import os
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
import web3
|
||||
import eth_tester
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
|
||||
'gas_limit': 9000000,
|
||||
})
|
||||
backend = eth_tester.PyEVMBackend(eth_params)
|
||||
instance = eth_tester.EthereumTester(backend)
|
||||
provider = web3.Web3.EthereumTesterProvider(instance)
|
||||
w3 = web3.Web3(provider)
|
||||
|
||||
|
||||
f = open('TokenEndorser.bin', 'r')
|
||||
bytecode = f.read()
|
||||
f.close()
|
||||
|
||||
f = open('TokenEndorser.json', 'r')
|
||||
abi = json.load(f)
|
||||
f.close()
|
||||
|
||||
token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
|
||||
|
||||
c = w3.eth.contract(abi=abi, bytecode=bytecode)
|
||||
tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
|
||||
r = w3.eth.getTransactionReceipt(tx_hash)
|
||||
logg.debug('contract {} initial token {}'.format(r.contractAddress, token_address))
|
||||
|
||||
c = w3.eth.contract(abi=abi, address=r.contractAddress)
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
|
||||
# Initial token will fail in any case
|
||||
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
|
||||
|
||||
fail = False
|
||||
try:
|
||||
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
|
||||
except:
|
||||
fail = True
|
||||
pass
|
||||
|
||||
if not fail:
|
||||
raise RuntimeError('expected fail on register same token to same address')
|
||||
|
||||
|
||||
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[1]})
|
||||
|
||||
|
||||
h = hashlib.new('sha256')
|
||||
h.update(bytes.fromhex(token_address[2:]))
|
||||
h.update(bytes.fromhex(w3.eth.accounts[0][2:]))
|
||||
z = h.digest()
|
||||
|
||||
assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
|
||||
|
||||
|
||||
another_token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
|
||||
c.functions.add(another_token_address, d).transact({'from':w3.eth.accounts[0]})
|
||||
|
||||
assert c.functions.endorsers(w3.eth.accounts[0], 0).call() == 1
|
||||
assert c.functions.endorsers(w3.eth.accounts[1], 0).call() == 1
|
||||
assert c.functions.endorsers(w3.eth.accounts[0], 1).call() == 2
|
||||
|
||||
assert c.functions.tokens(1).call() == token_address
|
||||
assert c.functions.tokens(2).call() == another_token_address
|
||||
|
||||
assert c.functions.tokenIndex(token_address).call() == 1
|
||||
assert c.functions.tokenIndex(another_token_address).call() == 2
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user