Skip to content

Commit

Permalink
Fix relative handles in containers (#38)
Browse files Browse the repository at this point in the history
* Add test: fxdao test_set_admin

* Add python bindings for `SCAddress`

* Set Version: 0.1.32

* Convert rel handles to abs when creating maps and vecs

* Set Version: 0.1.34

---------

Co-authored-by: devops <[email protected]>
  • Loading branch information
bbyalcinkaya and devops authored Sep 16, 2024
1 parent 7c1a33b commit 2c0fc8d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.33
0.1.34
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "komet"
version = "0.1.33"
version = "0.1.34"
description = "K tooling for the Soroban platform"
authors = [
"Runtime Verification, Inc. <[email protected]>",
Expand Down
4 changes: 4 additions & 0 deletions src/komet/kast/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def sc_bytes(b: bytes) -> KInner:
return KApply('SCVal:Bytes', [token(b)])


def sc_address(address: KInner) -> KInner:
return KApply('SCVal:Address', [address])


def sc_vec(l: Iterable[KInner]) -> KInner:
return KApply('SCVal:Vec', list_of(l))

Expand Down
20 changes: 19 additions & 1 deletion src/komet/kdist/soroban-semantics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ If `SCV` is a small value, `allocObject(SCV)` returns a small `HostVal` directly
rule [loadObject-rel]:
<instrs> loadObject(VAL)
=> loadObject(RELS {{ getIndex(VAL) }} orDefault HostVal(0))
=> loadObject(rel2abs(RELS, VAL))
...
</instrs>
<relativeObjects> RELS </relativeObjects>
Expand Down Expand Up @@ -371,6 +371,24 @@ If `SCV` is a small value, `allocObject(SCV)` returns a small `HostVal` directly
```

- rel2abs, rel2absMany: Convert relative handles to absolute handles

```k
syntax HostVal ::= rel2abs(List, HostVal) [function, total, symbol(rel2abs)]
// ------------------------------------------------------------------------
rule rel2abs(RELS, HV) => RELS {{ getIndex(HV) }} orDefault HV
requires isObject(HV) andBool isRelativeObjectHandle(HV)
rule rel2abs(_, HV) => HV
[owise]
syntax List ::= rel2absMany(List, List) [function, total, symbol(rel2absMany)]
// --------------------------------------------------------------------------------
rule rel2absMany(RELS, ListItem(HV) L) => ListItem(rel2abs(RELS, HV)) rel2absMany(RELS, L)
rule rel2absMany(_, L) => L
[owise]
```

### Auxiliary functions

```k
Expand Down
3 changes: 2 additions & 1 deletion src/komet/kdist/soroban-semantics/host/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ The function returns a `HostVal` pointing to the new map object.
ScMap(
mapFromLists(
KEYS,
Bytes2Vals(VALS_BS)
rel2absMany(RELS, Bytes2Vals(VALS_BS))
)
)
)
~> returnHostVal
...
</instrs>
<hostStack> KEYS : VALS_BS : S => S </hostStack>
<relativeObjects> RELS </relativeObjects>
requires size(KEYS) ==Int lengthBytes(VALS_BS) /Int 8
```
Expand Down
9 changes: 8 additions & 1 deletion src/komet/kdist/soroban-semantics/host/vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,15 @@ module HOST-VECTOR
<hostStack> U32(VALS_POS) : U32(LEN) : S => S </hostStack>
rule [vecNewFromLinearMemoryAux]:
<instrs> vecNewFromLinearMemoryAux => allocObject(ScVec(Bytes2Vals(BS))) ... </instrs>
<instrs> vecNewFromLinearMemoryAux
=> allocObject(
ScVec(
rel2absMany(RELS, Bytes2Vals(BS))
)
) ...
</instrs>
<hostStack> BS : S => S </hostStack>
<relativeObjects> RELS </relativeObjects>
syntax Bytes ::= Vals2Bytes(List) [function, total]
Expand Down
43 changes: 43 additions & 0 deletions src/komet/scval.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from pyk.prelude.utils import token

from .kast.syntax import (
account_id,
contract_id,
sc_address,
sc_bool,
sc_bytes,
sc_i32,
Expand Down Expand Up @@ -120,6 +123,30 @@ def to_kast(self) -> KInner:
return sc_bytes(self.val)


@dataclass(frozen=True)
class AccountId:
val: bytes

def to_kast(self) -> KInner:
return account_id(self.val)


@dataclass(frozen=True)
class ContractId:
val: bytes

def to_kast(self) -> KInner:
return contract_id(self.val)


@dataclass(frozen=True)
class SCAddress(SCValue):
val: AccountId | ContractId

def to_kast(self) -> KInner:
return sc_address(self.val.to_kast())


@dataclass(frozen=True)
class SCVec(SCValue):
val: tuple[SCValue]
Expand Down Expand Up @@ -150,6 +177,7 @@ def to_kast(self) -> KInner:
'u256': 'SCU256Type',
'symbol': 'SCSymbolType',
'bytes': 'SCBytesType',
'address': 'SCAddressType',
'vec': 'SCVecType',
'map': 'SCMapType',
}
Expand Down Expand Up @@ -330,6 +358,21 @@ def as_var(cls, name: str) -> tuple[KInner, tuple[KInner, ...]]:
return KApply('SCVal:Bytes', [KVariable(name, KSort('Bytes'))]), ()


@dataclass
class SCAddressType(SCMonomorphicType):
def strategy(self) -> strategies.SearchStrategy:
def target(p: bool, val: bytes) -> SCAddress:
if p:
return SCAddress(ContractId(val))
return SCAddress(AccountId(val))

return strategies.builds(target, strategies.booleans(), strategies.binary(min_size=32, max_size=32))

@classmethod
def as_var(cls, name: str) -> tuple[KInner, tuple[KInner, ...]]:
return KApply('SCVal:Address', [KVariable(name, KSort('Address'))]), ()


@dataclass
class SCVecType(SCType):
element: SCType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ impl TestFxDAOContract {
true
}

pub fn test_set_admin(env: Env, addr: Address) -> bool {
let fxdao_addr: Address = env.storage().instance().get(&FXDAO_KEY).unwrap();
let client = VaultsContract::Client::new(&env, &fxdao_addr);

client.set_admin(&addr);

let core_state = client.get_core_state();
core_state.admin == addr
}
}

mod test;

0 comments on commit 2c0fc8d

Please sign in to comment.