rocklabs-io / ic-py

Python Agent Library for the DFINITY Internet Computer
MIT License
127 stars 26 forks source link

Is there a read_state implementation for system calls? #40

Closed bodily11 closed 2 years ago

bodily11 commented 2 years ago

Making query and update calls are great, but sometimes I want to make system calls, which requires read_state syntax as can be seen here: https://smartcontracts.org/docs/interface-spec/index.html#http-read-state

Is this interface implemented in the Python agent? I know the JavaScript agent tests supported some read_state calls, but the documentation around it still wasn't very good.

Thanks!

ccyanxyz commented 2 years ago

https://github.com/rocklabs-io/ic-py/blob/main/ic/agent.py#L107

bodily11 commented 2 years ago

Yeah, beautiful. That is the one that I found as well. Although "time" is the only path that I could get to work. I kept getting CBOR decode issues if I tried any other paths. Are other paths other than "time" supported? Like canister_ranges? Or subnet information?

Myse1f commented 2 years ago

Canister info and subnet informatoin are supported. Text in paths must be blob and principal in the paths must be bytes-form, e.g

cert = ag.read_state_raw("gvbup-jyaaa-aaaah-qcdwa-cai", [["subnet".encode(), Principal.from_str("w4asl-4nmyj-qnr7c-6cqq4-tkwmt-o26di-iupkq-vx4kt-asbrx-jzuxh-4ae").bytes, "canister_ranges".encode()]])
ret = lookup(["subnet".encode(), Principal.from_str("w4asl-4nmyj-qnr7c-6cqq4-tkwmt-o26di-iupkq-vx4kt-asbrx-jzuxh-4ae").bytes, "canister_ranges".encode()], cert)
print(cbor2.loads(ret))

Actually, read_state_raw is too low level. Maybe we can absract it into easy-to-use function later.

Myse1f commented 2 years ago

41

You can look into the code in test_readstate.py and try ur own code to check whether get correct result.