Skip to content

Commit b355135

Browse files
committed
aptos: initial commit
1 parent 7d16007 commit b355135

File tree

8 files changed

+519
-0
lines changed

8 files changed

+519
-0
lines changed

.github/workflows/aptos.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: aptos
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
name: Aptos Test
9+
runs-on: ubuntu-latest
10+
# https://github.com/aptos-labs/aptos-core/releases/tag/aptos-cli-v7.2.0
11+
# https://github.com/aptos-labs/aptos-core/commit/35102f5f33c69b8e48e030243a09edad80cbd946
12+
container: aptoslabs/tools:devnet_35102f5f33c69b8e48e030243a09edad80cbd946@sha256:06503b21b53ad904c7d689c47a78259b4891fa0d1c6932550c784bddc0d3cda0
13+
defaults:
14+
run:
15+
working-directory: aptos
16+
steps:
17+
- uses: actions/checkout@v4
18+
- run: sh ci.sh

aptos/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*/.aptos
2+
*/.coverage_map.mvcov
3+
*/.trace
4+
*/build

aptos/ci.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
for dir in */ # list directories in the form "/tmp/dirname/"
4+
do
5+
dir=${dir%*/} # remove the trailing "/"
6+
cd ${dir}
7+
aptos init --skip-faucet
8+
sh -c "aptos move test --coverage --named-addresses ${dir}=default"
9+
sh -c "aptos move coverage summary --named-addresses ${dir}=default | grep \"Move Coverage: 100.00\""
10+
cd ..
11+
done

aptos/executor/Move.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "executor"
3+
version = "1.0.0"
4+
license = "Apache 2.0"
5+
authors = ["Wormhole Labs"]
6+
7+
[addresses]
8+
executor = "_"
9+
10+
[dev-addresses]
11+
12+
[dependencies.AptosFramework]
13+
git = "https://github.com/aptos-labs/aptos-framework.git"
14+
rev = "mainnet"
15+
subdir = "aptos-framework"
16+
17+
[dev-dependencies]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
module executor::executor {
4+
use aptos_framework::aptos_coin::{AptosCoin};
5+
use aptos_framework::coin::{Self, Coin};
6+
use aptos_framework::event;
7+
use aptos_std::from_bcs;
8+
use executor::bytes;
9+
use executor::cursor;
10+
11+
const CHAIN_ID: u16 = 22;
12+
13+
const E_QUOTE_SRC_CHAIN_MISMATCH: u64 = 0;
14+
const E_QUOTE_DST_CHAIN_MISMATCH: u64 = 1;
15+
const E_QUOTE_EXPIRED: u64 = 2;
16+
17+
#[event]
18+
struct RequestForExecution has drop, store {
19+
quoter_address: vector<u8>,
20+
amt_paid: u64,
21+
dst_chain: u16,
22+
dst_addr: address,
23+
refund_addr: address,
24+
signed_quote: vector<u8>,
25+
request_bytes: vector<u8>,
26+
relay_instructions: vector<u8>,
27+
}
28+
29+
public fun request_execution(
30+
amount: Coin<AptosCoin>,
31+
dst_chain: u16,
32+
dst_addr: address, // akin to bytes32
33+
refund_addr: address,
34+
signed_quote_bytes: vector<u8>,
35+
request_bytes: vector<u8>,
36+
relay_instructions: vector<u8>
37+
) {
38+
let cursor = cursor::new(signed_quote_bytes);
39+
bytes::take_bytes(&mut cursor, 4); // prefix
40+
let quoter_address = bytes::take_bytes(&mut cursor, 20);
41+
let payee_address = from_bcs::to_address(bytes::take_bytes(&mut cursor, 32));
42+
let quote_src_chain = bytes::take_u16_be(&mut cursor);
43+
assert!(quote_src_chain == CHAIN_ID, E_QUOTE_SRC_CHAIN_MISMATCH);
44+
let quote_dst_chain = bytes::take_u16_be(&mut cursor);
45+
assert!(quote_dst_chain == dst_chain, E_QUOTE_DST_CHAIN_MISMATCH);
46+
let expiry_time = bytes::take_u64_be(&mut cursor);
47+
assert!(expiry_time > aptos_framework::timestamp::now_seconds(), E_QUOTE_EXPIRED);
48+
cursor::take_rest(cursor);
49+
let amt_paid = coin::value<AptosCoin>(&amount);
50+
coin::deposit(payee_address, amount);
51+
event::emit(RequestForExecution {
52+
quoter_address,
53+
amt_paid,
54+
dst_chain,
55+
dst_addr,
56+
refund_addr,
57+
signed_quote: signed_quote_bytes,
58+
request_bytes,
59+
relay_instructions
60+
});
61+
}
62+
}
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Adapted from https://github.com/wormhole-foundation/wormhole/blob/cc9d8222858827afa9f4780164fcce32ba0dfb3a/sui/wormhole/sources/utils/bytes.move
3+
4+
/// This module implements a library that serializes and deserializes specific
5+
/// types into a buffer (i.e. `vector<u8>`). For serialization, the first
6+
/// argument will be of `&mut vector<u8>`. For deserialization, the first
7+
/// argument will be of `&mut Cursor<u8>` (see `executor::cursor` for more
8+
/// details).
9+
module executor::bytes {
10+
use std::bcs::{Self};
11+
use std::vector;
12+
use executor::cursor::{Self, Cursor};
13+
14+
public fun push_u8(buf: &mut vector<u8>, v: u8) {
15+
vector::push_back<u8>(buf, v);
16+
}
17+
18+
public fun push_u16_be(buf: &mut vector<u8>, value: u16) {
19+
push_reverse(buf, value);
20+
}
21+
22+
public fun push_u32_be(buf: &mut vector<u8>, value: u32) {
23+
push_reverse(buf, value);
24+
}
25+
26+
public fun push_u64_be(buf: &mut vector<u8>, value: u64) {
27+
push_reverse(buf, value);
28+
}
29+
30+
public fun push_u128_be(buf: &mut vector<u8>, value: u128) {
31+
push_reverse(buf, value);
32+
}
33+
34+
public fun push_u256_be(buf: &mut vector<u8>, value: u256) {
35+
push_reverse(buf, value);
36+
}
37+
38+
public fun take_u8(cur: &mut Cursor<u8>): u8 {
39+
cursor::poke(cur)
40+
}
41+
42+
public fun take_u16_be(cur: &mut Cursor<u8>): u16 {
43+
let out = 0;
44+
let i = 0;
45+
while (i < 2) {
46+
out = (out << 8) + (cursor::poke(cur) as u16);
47+
i = i + 1;
48+
};
49+
out
50+
}
51+
52+
public fun take_u32_be(cur: &mut Cursor<u8>): u32 {
53+
let out = 0;
54+
let i = 0;
55+
while (i < 4) {
56+
out = (out << 8) + (cursor::poke(cur) as u32);
57+
i = i + 1;
58+
};
59+
out
60+
}
61+
62+
public fun take_u64_be(cur: &mut Cursor<u8>): u64 {
63+
let out = 0;
64+
let i =0;
65+
while (i < 8) {
66+
out = (out << 8) + (cursor::poke(cur) as u64);
67+
i = i + 1;
68+
};
69+
out
70+
}
71+
72+
public fun take_u128_be(cur: &mut Cursor<u8>): u128 {
73+
let out = 0;
74+
let i = 0;
75+
while (i < 16) {
76+
out = (out << 8) + (cursor::poke(cur) as u128);
77+
i = i + 1;
78+
};
79+
out
80+
}
81+
82+
public fun take_u256_be(cur: &mut Cursor<u8>): u256 {
83+
let out = 0;
84+
let i = 0;
85+
while (i < 32) {
86+
out = (out << 8) + (cursor::poke(cur) as u256);
87+
i = i + 1;
88+
};
89+
out
90+
}
91+
92+
public fun take_bytes(cur: &mut Cursor<u8>, num_bytes: u64): vector<u8> {
93+
let out = vector::empty();
94+
let i = 0;
95+
while (i < num_bytes) {
96+
vector::push_back(&mut out, cursor::poke(cur));
97+
i = i + 1;
98+
};
99+
out
100+
}
101+
102+
fun push_reverse<T: drop>(buf: &mut vector<u8>, v: T) {
103+
let data = bcs::to_bytes(&v);
104+
vector::reverse(&mut data);
105+
vector::append(buf, data);
106+
}
107+
}
108+
109+
#[test_only]
110+
module executor::bytes_tests {
111+
use std::vector;
112+
use executor::bytes::{Self};
113+
use executor::cursor::{Self};
114+
115+
#[test]
116+
fun test_push_u8(){
117+
let u = 0x12;
118+
let s = vector::empty();
119+
bytes::push_u8(&mut s, u);
120+
let cur = cursor::new(s);
121+
let p = bytes::take_u8(&mut cur);
122+
cursor::destroy_empty(cur);
123+
assert!(p==u, 0);
124+
}
125+
126+
#[test]
127+
fun test_push_u16_be(){
128+
let u = 0x1234;
129+
let s = vector::empty();
130+
bytes::push_u16_be(&mut s, u);
131+
let cur = cursor::new(s);
132+
let p = bytes::take_u16_be(&mut cur);
133+
cursor::destroy_empty(cur);
134+
assert!(p==u, 0);
135+
}
136+
137+
#[test]
138+
fun test_push_u32_be(){
139+
let u = 0x12345678;
140+
let s = vector::empty();
141+
bytes::push_u32_be(&mut s, u);
142+
let cur = cursor::new(s);
143+
let p = bytes::take_u32_be(&mut cur);
144+
cursor::destroy_empty(cur);
145+
assert!(p==u, 0);
146+
}
147+
148+
#[test]
149+
fun test_push_u64_be(){
150+
let u = 0x1234567812345678;
151+
let s = vector::empty();
152+
bytes::push_u64_be(&mut s, u);
153+
let cur = cursor::new(s);
154+
let p = bytes::take_u64_be(&mut cur);
155+
cursor::destroy_empty(cur);
156+
assert!(p==u, 0);
157+
}
158+
159+
#[test]
160+
fun test_push_u128_be(){
161+
let u = 0x12345678123456781234567812345678;
162+
let s = vector::empty();
163+
bytes::push_u128_be(&mut s, u);
164+
let cur = cursor::new(s);
165+
let p = bytes::take_u128_be(&mut cur);
166+
cursor::destroy_empty(cur);
167+
assert!(p==u, 0);
168+
}
169+
170+
#[test]
171+
fun test_push_u256_be(){
172+
let u =
173+
0x4738691759099793746170047375612500000000000000000000000000009876;
174+
let s = vector::empty();
175+
bytes::push_u256_be(&mut s, u);
176+
assert!(
177+
s == x"4738691759099793746170047375612500000000000000000000000000009876",
178+
0
179+
);
180+
}
181+
182+
#[test]
183+
fun test_take_u8() {
184+
let cursor = cursor::new(x"99");
185+
let byte = bytes::take_u8(&mut cursor);
186+
assert!(byte==0x99, 0);
187+
cursor::destroy_empty(cursor);
188+
}
189+
190+
#[test]
191+
fun test_take_u16_be() {
192+
let cursor = cursor::new(x"9987");
193+
let u = bytes::take_u16_be(&mut cursor);
194+
assert!(u == 0x9987, 0);
195+
cursor::destroy_empty(cursor);
196+
}
197+
198+
#[test]
199+
fun test_take_u32_be() {
200+
let cursor = cursor::new(x"99876543");
201+
let u = bytes::take_u32_be(&mut cursor);
202+
assert!(u == 0x99876543, 0);
203+
cursor::destroy_empty(cursor);
204+
}
205+
206+
#[test]
207+
fun test_take_u64_be() {
208+
let cursor = cursor::new(x"1300000025000001");
209+
let u = bytes::take_u64_be(&mut cursor);
210+
assert!(u == 0x1300000025000001, 0);
211+
cursor::destroy_empty(cursor);
212+
}
213+
214+
#[test]
215+
fun test_take_u128_be() {
216+
let cursor = cursor::new(x"130209AB2500FA0113CD00AE25000001");
217+
let u = bytes::take_u128_be(&mut cursor);
218+
assert!(u == 0x130209AB2500FA0113CD00AE25000001, 0);
219+
cursor::destroy_empty(cursor);
220+
}
221+
222+
#[test]
223+
fun test_to_bytes() {
224+
let cursor = cursor::new(b"hello world");
225+
let hello = bytes::take_bytes(&mut cursor, 5);
226+
bytes::take_u8(&mut cursor);
227+
let world = bytes::take_bytes(&mut cursor, 5);
228+
assert!(hello == b"hello", 0);
229+
assert!(world == b"world", 0);
230+
cursor::destroy_empty(cursor);
231+
}
232+
233+
}

0 commit comments

Comments
 (0)