Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschroeter committed Nov 23, 2024
1 parent 5d54b83 commit 79e7166
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
28 changes: 12 additions & 16 deletions ape/contracts/distro.vy
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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


7 changes: 7 additions & 0 deletions ape/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 27 additions & 3 deletions ape/web3_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

assert w3.is_connected(), 'Failed to connect to the Ethereum client.'

contract_address = '0x12aEdb6639C160B051be89B77717F46eafac282b'
contract_address = '0x0e075cB6f980203F8e930a0B527CDbE07F303eAD'
abi = [
{
"stateMutability": "nonpayable",
Expand Down Expand Up @@ -42,7 +42,7 @@
},
{
"name": "kind",
"type": "uint256"
"type": "uint8"
},
{
"name": "verification",
Expand Down Expand Up @@ -103,7 +103,7 @@
},
{
"name": "kind",
"type": "uint256"
"type": "uint8"
},
{
"name": "verified",
Expand All @@ -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",
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/suse-distro-blockchain/suse-distro-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
23 changes: 16 additions & 7 deletions test_distro_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 79e7166

Please sign in to comment.