Skip to content

Commit

Permalink
Implement RLP native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pause125 committed Apr 6, 2024
1 parent abe587a commit 9664b7a
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ argon2 = "0.5.2"
rpassword = "7.2.0"
fixed-hash = "0.8.0"
uint = "0.9.5"
open-fastrlp = "0.1.4"
rlp = "0.5.2"
const-hex = "1.6.2"
cached = "0.43.0"
diesel = { version = "2.1.0", features = [
Expand Down
1 change: 0 additions & 1 deletion crates/rooch-rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ bigdecimal = { workspace = true }
fixed-hash = { workspace = true }
uint = { workspace = true }
bytes = { workspace = true }
open-fastrlp = { workspace = true }
const-hex = { workspace = true }
bitcoin = { workspace = true }
bitcoincore-rpc = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions moveos/moveos-stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ petgraph = { workspace = true }
parking_lot = { workspace = true }
itertools = { workspace = true }
ciborium = { workspace = true }
rlp = { workspace = true }
primitive-types = { workspace = true }

move-binary-format = { workspace = true }
move-bytecode-utils = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion moveos/moveos-stdlib/moveos-stdlib/doc/rlp.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ https://ethereum.org/nl/developers/docs/data-structures-and-encoding/rlp/



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="rlp.md#0x2_rlp_from_bytes">from_bytes</a>&lt;MoveValue&gt;(bytes: &<a href="">vector</a>&lt;u8&gt;): MoveValue
<pre><code>#[data_struct(#[MoveValue])]
<b>public</b> <b>fun</b> <a href="rlp.md#0x2_rlp_from_bytes">from_bytes</a>&lt;MoveValue&gt;(bytes: <a href="">vector</a>&lt;u8&gt;): MoveValue
</code></pre>
4 changes: 3 additions & 1 deletion moveos/moveos-stdlib/moveos-stdlib/sources/rlp.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
module moveos_std::rlp{

native public fun to_bytes<MoveValue>(value: &MoveValue): vector<u8>;
public(friend) native fun from_bytes<MoveValue>(bytes: &vector<u8>): MoveValue;

#[data_struct(MoveValue)]
public native fun from_bytes<MoveValue>(bytes: vector<u8>): MoveValue;

}
105 changes: 105 additions & 0 deletions moveos/moveos-stdlib/moveos-stdlib/tests/rlp_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[test_only]
module moveos_std::rlp_tests {
use std::debug;
use std::vector;
use std::bcs;
use std::string::String;
use moveos_std::rlp;

#[data_struct]
struct PrimaryStruct has copy, drop {
a: u8,
b: u16,
c: u32,
d: u64,
e: u128,
f: u256,
me: address,
}

#[data_struct]
struct ChildStruct has copy, drop {
c: PrimaryStruct,
}

#[data_struct]
struct NestedStruct has copy, drop {
child: ChildStruct,
v: vector<PrimaryStruct>,
}

#[test]
fun test_basic() {
let bytes = rlp::to_bytes(&0u8);
assert!(bytes == x"80", 1);

let bytes = rlp::to_bytes(&x"0400"); // 1024
assert!(bytes == x"820400", 2);

let bytes = rlp::to_bytes(&1024u64);
assert!(bytes == x"820400", 3);

let bytes = rlp::to_bytes(&b"dog");
assert!(bytes == x"83646f67", 4);

let bytes = rlp::to_bytes<String>(&std::string::utf8(b"dog"));
assert!(bytes == x"c483646f67", 5);

let v = vector::empty<u32>();
vector::push_back<u32>(&mut v, 1);
vector::push_back<u32>(&mut v, 2);
let bytes = rlp::to_bytes(&v);
assert!(bytes == x"c20102", 6);

let bytes = rlp::to_bytes(&@0x42);
assert!(bytes == x"a00000000000000000000000000000000000000000000000000000000000000042", 7);
}

fun primary_struct_instantiation(offset: u8): PrimaryStruct {
let v = vector::empty<u16>();
vector::push_back<u16>(&mut v, 1);
vector::push_back<u16>(&mut v, 2);
PrimaryStruct {
a: 1 + offset,
b: 256,
c: 3 + (offset as u32),
d: 4 + (offset as u64),
e: 5 + (offset as u128),
f: 6 + (offset as u256),
me: @0x42
}
}

fun nested_struct_instantiation(): NestedStruct {
let v = vector::empty<PrimaryStruct>();
vector::push_back<PrimaryStruct>(&mut v, primary_struct_instantiation(1));
let child = ChildStruct { c: primary_struct_instantiation(2)};
let data = NestedStruct { child: child, v: v };
data
}

#[test]
fun test_primary_struct() {
let data = primary_struct_instantiation(1);
let bytes = rlp::to_bytes(&data);
debug::print(&bytes);
let decoded_data = rlp::from_bytes<PrimaryStruct>(bytes);
assert!(decoded_data == data, 1);
}

#[test]
fun test_nested_struct() {
let data = nested_struct_instantiation();
let bytes = rlp::to_bytes(&data);
debug::print(&bytes);
let decoded_data = rlp::from_bytes<NestedStruct>(bytes);
assert!(decoded_data.child == data.child, 1);
assert!(vector::borrow(&decoded_data.v, 0) == vector::borrow(&data.v, 0), 2);
}
}
Loading

0 comments on commit 9664b7a

Please sign in to comment.