diff --git a/DEVELOP.md b/DEVELOP.md index 2dd5814..87c9484 100644 --- a/DEVELOP.md +++ b/DEVELOP.md @@ -41,12 +41,12 @@ ape accounts import security_team To finally deploy the contract use ```bash cd ape -# on abi changes: ape run scripts/deploy_anvil.py --network http://localhost:8545 ``` On modifications of the contract run: ```bash +# on abi changes: vyper -f abi contracts/distro.vy > .build/distro.json python3 test_distro_contract.py ``` diff --git a/ape/contracts/distro.vy b/ape/contracts/distro.vy index b3731ae..cd1da5b 100644 --- a/ape/contracts/distro.vy +++ b/ape/contracts/distro.vy @@ -47,7 +47,7 @@ struct my_product_build : kind: uint8 verified: bool -product_builds: HashMap[bytes32, my_product_build] +product_builds: HashMap[String[128], my_product_build] current_verification: HashMap[String[19], String[128]] @@ -63,7 +63,7 @@ def __init__(_product_creator: address, _official_validator: address, _security_ @external def add_product(name: String[16], git_ref: String[64]) -> uint256: # Only product creator is allowed to add a new product - assert msg.sender == self.product_creator or msg.sender == self.foundation_owner + assert msg.sender == self.product_creator # we have not reached our limit yet assert self.next_product < max_value(uint256) # add the product @@ -77,29 +77,27 @@ def add_product(name: String[16], git_ref: String[64]) -> uint256: @external def add_product_build(git_ref: String[64], kind: uint8, verification: String[128]): # Only product creator is allowed to add a new product - assert msg.sender == self.product_creator or msg.sender == self.foundation_owner - - hash: bytes32 = keccak256(verification) + assert msg.sender == self.product_creator # build is not yet registered - assert self.product_builds[hash].product_id == 0 + assert self.product_builds[verification].product_id == 0 # find the product for product_id: uint256 in range(max_value(uint256)): assert product_id < self.next_product if self.products[product_id].git_ref == git_ref: - self.product_builds[hash].product_id = product_id - cur_hash: String[19] = concat(self.products[product_id].name, uint2str(kind)) + self.product_builds[verification].product_id = product_id + current_key: String[19] = concat(self.products[product_id].name, uint2str(kind)) # set current verification - self.current_verification[cur_hash] = verification + self.current_verification[current_key] = verification break # we found a product now - assert self.product_builds[hash].product_id != 0 + assert self.product_builds[verification].product_id != 0 - self.product_builds[hash].kind = kind - self.product_builds[hash].verified = False + self.product_builds[verification].kind = kind + self.product_builds[verification].verified = False @view @@ -110,8 +108,7 @@ def get_product(product_id: uint256) -> my_product: @view @external def get_product_build(verification: String[128]) -> my_product_build: - hash: bytes32 = keccak256(verification) - return self.product_builds[hash] + return self.product_builds[verification] @view @external @@ -134,7 +131,6 @@ def set_critical(product_id: uint256, critical: bool): def add_attestation(verification: String[128]): # We have currently just a single official validator assert msg.sender == self.official_validator - hash: bytes32 = keccak256(verification) - self.product_builds[hash].verified = True + self.product_builds[verification].verified = True diff --git a/ape/streamlit_app.py b/ape/streamlit_app.py index 8bb36ad..a24780f 100644 --- a/ape/streamlit_app.py +++ b/ape/streamlit_app.py @@ -30,6 +30,13 @@ result = get_product_build(verification) st.write(f'Result: {result}') +st.header('Function: current_product_build') +name = st.text_input('Enter name', key='current_product_build_name') +kind = st.number_input('Enter kind', step=1, key='current_product_build_kind') +if st.button('Execute current_product_build', key='current_product_build_button'): + result = current_product_build(name, kind) + st.write(f'Result: {result}') + st.header('Function: get_product_counter') if st.button('Execute get_product_counter', key='get_product_counter_button'): result = get_product_counter() diff --git a/ape/web3_script.py b/ape/web3_script.py index aa48814..d54e5dd 100644 --- a/ape/web3_script.py +++ b/ape/web3_script.py @@ -8,7 +8,7 @@ assert w3.is_connected(), 'Failed to connect to the Ethereum client.' -contract_address = '0x12aEdb6639C160B051be89B77717F46eafac282b' +contract_address = '0x0e075cB6f980203F8e930a0B527CDbE07F303eAD' abi = [ { "stateMutability": "nonpayable", @@ -42,7 +42,7 @@ }, { "name": "kind", - "type": "uint256" + "type": "uint8" }, { "name": "verification", @@ -103,7 +103,7 @@ }, { "name": "kind", - "type": "uint256" + "type": "uint8" }, { "name": "verified", @@ -113,6 +113,27 @@ } ] }, + { + "stateMutability": "view", + "type": "function", + "name": "current_product_build", + "inputs": [ + { + "name": "name", + "type": "string" + }, + { + "name": "kind", + "type": "uint8" + } + ], + "outputs": [ + { + "name": "", + "type": "string" + } + ] + }, { "stateMutability": "view", "type": "function", @@ -284,6 +305,9 @@ def get_product(product_id): def get_product_build(verification): return contract.functions.get_product_build(verification).call() +def current_product_build(name, kind): + return contract.functions.current_product_build(name, kind).call() + def get_product_counter(): return contract.functions.get_product_counter().call() diff --git a/src/suse-distro-blockchain/suse-distro-check.py b/src/suse-distro-blockchain/suse-distro-check.py index 24b24b4..08ebf33 100644 --- a/src/suse-distro-blockchain/suse-distro-check.py +++ b/src/suse-distro-blockchain/suse-distro-check.py @@ -35,7 +35,7 @@ print("We have no contact to our local blockchain provider") exit(1) -print(f"Used chain ID: {w3.eth.chain_id}, @block: {w3.eth.block_number}") +print(f"Used chain ID: {w3.eth.chain_id}, @block: {w3.eth.block_number}, contract: {contract_address}") if w3.eth.chain_id != chainid: print(f"Wrong chain ID: {w3.eth.chain_id} and we expect {chainid}") diff --git a/test_distro_contract.py b/test_distro_contract.py index c5cad91..bc78520 100644 --- a/test_distro_contract.py +++ b/test_distro_contract.py @@ -4,21 +4,30 @@ from eth_account import Account contract = boa.load('ape/contracts/distro.vy', boa.env.eoa, boa.env.eoa, boa.env.eoa) -print(contract) + +git_sha="8a645f5782b507202c75ee7fbeaf7bb21d34dd5c2eda4118bb76a31a39226e30" +primary_sha="562a8a6891053541a9cb4d6b252e0cdde4b9da98fc99e87aaeac3ecccb05b91755a2ffcdfd3cdd3e3a55fc5deb96157be4565f4e1c4eb177f7b08075a15e2b70" +product_name="example-1" +build_kind=1 + print("Setting balance...") boa.env.set_balance(boa.env.eoa, 1000 * 10**18) -print("Add Product...") -product_id = contract.add_product("SLFO-16", "0x123") +product_id = contract.add_product(product_name, git_sha) print(f"Created Product with ID {product_id}") -contract.add_product_build("0x123", 1, "yxc") +contract.add_product_build(git_sha, build_kind, primary_sha) print(f"Created Product Build") -product_build = contract.get_product_build("yxc") +product_build = contract.get_product_build(primary_sha) if product_build[0] != 1 or product_build[1] != 1: print("Product Build not found!") exit(1) print("Found Product Build") -verification = contract.current_product_build("SLFO-16", 1) -if verification != "yxc": +product = contract.get_product(product_build[0]) +if product[0] != product_name or product[1] != git_sha: + print("Product Build not found!") + exit(1) +print("Found Product") +verification = contract.current_product_build(product_name, build_kind) +if verification != primary_sha: print("Is not current verification!") exit(1) print("Product is verified to be current")