-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call data coding #167
Call data coding #167
Changes from 6 commits
fe926fa
c1cb9a3
0be8974
f1b24dc
580676d
4175b9d
f3b27b1
434e61a
f344860
ffbb0cf
6d075ef
6a140b2
f1caec9
4f39b2f
73d9494
60a540a
b8ca609
1bdbb7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.build | ||
.DS_store | ||
blockchain-k-plugin | ||
tmp | ||
tmp.* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "blockchain-k-plugin"] | ||
path = blockchain-k-plugin | ||
url = https://github.com/runtimeverification/blockchain-k-plugin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
push "myEndpoint"; | ||
hold_string_from_test_stack; | ||
push "uint64"; | ||
hold_list_values_from_test_stack; | ||
push 1_u64; | ||
hold_list_values_from_test_stack; | ||
mock EncodeOp; | ||
return_value; | ||
check_eq "81922854\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
hold b"c6b6e179\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"; | ||
mock DecodeOp; | ||
return_value; | ||
|
||
check_eq 3_u64 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
```k | ||
|
||
requires "decoding/syntax.md" | ||
requires "decoding/decoder.md" | ||
|
||
module UKM-DECODING | ||
imports UKM-CALLDATA-DECODER | ||
endmodule | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
```k | ||
|
||
module UKM-CALLDATA-DECODER | ||
imports RUST-VALUE-SYNTAX | ||
imports UKM-DECODING-SYNTAX | ||
imports BYTES-HOOKED | ||
imports BYTES-SYNTAX | ||
imports private COMMON-K-CELL | ||
imports private RUST-PREPROCESSING-CONFIGURATION | ||
imports private UKM-PREPROCESSING-CONFIGURATION | ||
imports private RUST-EXECUTION-CONFIGURATION | ||
imports private RUST-REPRESENTATION | ||
imports private RUST-CONVERSIONS-SYNTAX | ||
imports INT | ||
|
||
rule decodeCallData(D:Bytes) => | ||
UKMDecodedCallData1(decodeFunctionSignature(substrBytes(D, 0, 8)), decodeArguments(loadArgumentsFromHash(substrBytes(D, 0, 8)), substrBytes(D, 8, lengthBytes(D)), .List) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs "requires lengthBytes(D) >=Int 8" |
||
|
||
// TODO: Self is being assigned to an integer 0. This should be fixed in case we need | ||
// to make references to self within rust contracts | ||
rule <k> UKMDecodedCallData1(P:PathInExpression, L:List) | ||
=> UKMDecodedCallData1(P, L) | ||
~> UKMDecodedCallData2(P, ListItem(NextId)) | ||
... </k> | ||
<next-value-id> NextId:Int => NextId +Int 1 </next-value-id> | ||
<values> Values:Map => Values[NextId <- u64(Int2MInt(0))] </values> [priority(80)] | ||
|
||
|
||
rule <k> UKMDecodedCallData1(P:PathInExpression, L:List ListItem(ptrValue(_, V))) | ||
~> UKMDecodedCallData2(P:PathInExpression, PL:List) | ||
=> UKMDecodedCallData1(P, L) | ||
~> UKMDecodedCallData2(P, ListItem(NextId) PL) | ||
... </k> | ||
<next-value-id> NextId:Int => NextId +Int 1 </next-value-id> | ||
<values> Values:Map => Values[NextId <- V] </values> [priority(70)] | ||
|
||
rule <k> UKMDecodedCallData1(P:PathInExpression, .List) | ||
=> .K ... </k> | ||
|
||
rule <k> UKMDecodedCallData2(P:PathInExpression, L:List) | ||
=> UKMDecodedCallData(P:PathInExpression, listToPtrList(L)) ... </k> | ||
|
||
rule [[ decodeFunctionSignature(FuncSigHash:Bytes) => P ]] | ||
<ukm-method-hash-to-signatures> | ||
... FuncSigHash |-> P:PathInExpression ... | ||
</ukm-method-hash-to-signatures> | ||
|
||
rule [[ loadArgumentsFromHash(FuncSigHash:Bytes) => loadArgumentsFromHash(P) ]] | ||
<ukm-method-hash-to-signatures> | ||
... FuncSigHash |-> P:PathInExpression ... | ||
</ukm-method-hash-to-signatures> | ||
|
||
rule [[ loadArgumentsFromHash(Method:PathInExpression) => L ]] | ||
<method-name> Method </method-name> | ||
<method-params> (self : $selftype), L:NormalizedFunctionParameterList </method-params> | ||
|
||
rule decodeArguments(((_ : T:Type), R):NormalizedFunctionParameterList, D:Bytes, L:List) => | ||
decodeArguments(R, substrBytes(D, 0, sizeOfType(T)), | ||
ListItem( convertKBytesToPtrValue (T, Bytes2Int ( substrBytes(D, 0, sizeOfType(T)), BE, Unsigned ) ) ) L ) | ||
|
||
rule decodeArguments(.NormalizedFunctionParameterList, _, L:List) => L | ||
|
||
rule convertKBytesToPtrValue(u32, I:Int) => ptrValue(null, u32(Int2MInt(I))) | ||
rule convertKBytesToPtrValue(i32, I:Int) => ptrValue(null, i32(Int2MInt(I))) | ||
rule convertKBytesToPtrValue(i64, I:Int) => ptrValue(null, i64(Int2MInt(I))) | ||
rule convertKBytesToPtrValue(u64, I:Int) => ptrValue(null, u64(Int2MInt(I))) | ||
rule convertKBytesToPtrValue(u128, I:Int) => ptrValue(null, u128(Int2MInt(I))) | ||
|
||
rule sizeOfType(i32) => 32 | ||
rule sizeOfType(u32) => 32 | ||
rule sizeOfType(u64) => 64 | ||
rule sizeOfType(i64) => 64 | ||
rule sizeOfType(u128) => 128 | ||
yanliu18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
endmodule | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
```k | ||
|
||
requires "encoding/syntax.md" | ||
requires "encoding/encoder.md" | ||
|
||
module UKM-ENCODING | ||
imports UKM-CALLDATA-ENCODER | ||
endmodule | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
```k | ||
requires "blockchain-k-plugin/plugin/krypto.md" | ||
|
||
module UKM-CALLDATA-ENCODER | ||
imports private COMMON-K-CELL | ||
imports private UKM-ENCODING-SYNTAX | ||
imports private UKM-PREPROCESSING-ENDPOINTS | ||
imports STRING | ||
imports BYTES | ||
imports INT | ||
imports KRYPTO | ||
|
||
syntax String ::= Identifier2String(Identifier) [function, total, hook(STRING.token2string)] | ||
| Type2String(Type) [function, total, hook(STRING.token2string)] | ||
|
||
rule encodeFunctionSignature (P:PathInExpression, N:NormalizedFunctionParameterList) => | ||
encodeFunctionSignature(convertPathInExprToString(P), convertFuncParamListToStrList(N, .List), "") | ||
|
||
rule convertPathInExprToString(( :: I:Identifier :: R:PathExprSegments):PathInExpression ) => | ||
convertPathInExprToString(R) [priority(80)] | ||
rule convertPathInExprToString(( I:Identifier :: R:PathExprSegments):PathInExpression ) => | ||
convertPathInExprToString(R) [priority(80)] | ||
rule convertPathInExprToString(( I:Identifier :: .PathExprSegments):PathInExpression ) => | ||
Identifier2String(I) [priority(70)] | ||
|
||
rule convertFuncParamListToStrList(((self : _), N:NormalizedFunctionParameterList), .List) => | ||
convertFuncParamListToStrList( N, .List) [priority(60)] | ||
rule convertFuncParamListToStrList(((_ : T:Type), N:NormalizedFunctionParameterList), L:List) => | ||
convertFuncParamListToStrList(N, L ListItem(signatureType(T))) [priority(70)] | ||
rule convertFuncParamListToStrList(.NormalizedFunctionParameterList, L:List) => L | ||
|
||
rule encodeCallData(FN:String, FAT:List, FAL:List) => | ||
encodeFunctionSignature(FN, FAT, "") +Bytes encodeFunctionParams(FAL, FAT, b"") | ||
|
||
// Function signature encoding | ||
rule encodeFunctionSignature(FuncName:String, RL:List, "") => | ||
encodeFunctionSignature("", RL:List, FuncName +String "(") | ||
|
||
rule encodeFunctionSignature("", ListItem(FuncParam:String) RL:List, FS) => | ||
encodeFunctionSignature("", RL, FS +String FuncParam +String ",") [owise] | ||
|
||
// The last param does not have a follow up comma | ||
rule encodeFunctionSignature("", ListItem(FuncParam:String) .List, FS) => | ||
encodeFunctionSignature("", .List, FS +String FuncParam ) | ||
|
||
rule encodeFunctionSignature("", .List, FS) => String2Bytes(substrString(Keccak256(String2Bytes(FS +String ")")), 0, 8)) | ||
|
||
// Function parameters encoding | ||
rule encodeFunctionParams(ListItem(V:Value) ARGS:List, ListItem(T:String) PTYPES:List, B:Bytes) => | ||
encodeFunctionParams(ARGS:List, PTYPES:List, B:Bytes +Bytes convertToKBytes(V, T)) | ||
|
||
rule encodeFunctionParams(.List, .List, B:Bytes) => B | ||
|
||
|
||
// Encoding of individual types | ||
|
||
rule convertToKBytes(i8(V) , "int8") => Int2Bytes(8, MInt2Signed(V), BE:Endianness) | ||
rule convertToKBytes(u8(V) , "uint8") => Int2Bytes(8, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(i16(V), "int16") => Int2Bytes(16, MInt2Signed(V), BE:Endianness) | ||
rule convertToKBytes(u16(V), "uint16") => Int2Bytes(16, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(i32(V), "int32") => Int2Bytes(32, MInt2Signed(V), BE:Endianness) | ||
rule convertToKBytes(u32(V), "uint32") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(i64(V), "int64") => Int2Bytes(64, MInt2Signed(V), BE:Endianness) | ||
rule convertToKBytes(u64(V), "uint64") => Int2Bytes(64, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(u128(V), "uint128") => Int2Bytes(128, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(true, "bool") => Int2Bytes(8, 1, BE:Endianness) | ||
rule convertToKBytes(false, "bool") => Int2Bytes(8, 0, BE:Endianness) | ||
// TODO: as we currently do not support u160 (addresses) or u256, we're converting them to u64 for now | ||
rule convertToKBytes(u64(V), "uint256") => Int2Bytes(256, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(u64(V), "uint160") => Int2Bytes(160, MInt2Unsigned(V), BE:Endianness) | ||
rule convertToKBytes(u64(V), "address") => Int2Bytes(160, MInt2Unsigned(V), BE:Endianness) | ||
|
||
endmodule | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,20 @@ | |
|
||
module UKM-ENCODING-SYNTAX | ||
imports BYTES-SYNTAX | ||
imports LIST | ||
imports RUST-REPRESENTATION | ||
|
||
syntax UKMInstruction ::= "ukmEncodePreprocessedCell" | ||
|
||
syntax Bytes ::= encodeFunctionSignature (PathInExpression, NormalizedFunctionParameterList) [function] | ||
|
||
syntax String ::= convertPathInExprToString(PathInExpression) [function] | ||
syntax List ::= convertFuncParamListToStrList(NormalizedFunctionParameterList, List) [function] | ||
|
||
syntax Bytes ::= encodeCallData (String, List, List) [function] //Function name, argument types, argument list | ||
| encodeFunctionSignature (String, List, String) [function] | ||
| encodeFunctionParams (List, List, Bytes) [function] | ||
| convertToKBytes ( Value , String ) [function] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having partial functions raises all sorts of problems, some of which are philosophical, some of which are practical (e.g. it's easy to create a bug like when evaluating a total function to a partial one without checking that the partial function is actually defined for that input; I can provide more details). I would suggest using total functions unless you are sure that you need partial ones. |
||
|
||
endmodule | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe delete the entire file?