-
Notifications
You must be signed in to change notification settings - Fork 0
/
sn-ffi.red
162 lines (129 loc) · 2.37 KB
/
sn-ffi.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Red []
#system [
#import [
"sn_ffi/target/i686-unknown-linux-gnu/debug/libsn_ffi.so" cdecl [
c_safe_default: "_safe_default" [
return: [handle!]
]
c_safe_free: "_safe_free" [
ref [handle!]
]
c_safe_xorurl_base: "_safe_xorurl_base" [
ref [handle!]
return: [c-string!]
]
c_safe_connect: "_safe_connect" [
rt [handle!]
ref [handle!]
params [byte-ptr!]
params_size [integer!]
]
init_runtime: "init_runtime" [
return: [handle!]
]
cstring_free: "cstring_free" [
ptr [c-string!]
]
]
]
tokio_runtime: init_runtime
]
; low-level routines
safe_default: routine [
return: [handle!]
/local ref
] [
ref: handle/box as integer! c_safe_default
as red-handle! SET_RETURN(ref)
]
safe_free: routine [
ref [handle!]
] [
c_safe_free as handle! ref/value
]
safe_xorurl_base: routine [
ref [handle!]
return: [string!]
/local str buffer size
] [
str: c_safe_xorurl_base as handle! ref/value
size: length? str
buffer: string/load str size UTF-8
cstring_free str
as red-string! SET_RETURN(buffer)
]
safe_connect: function [
ref [handle!]
app-keypair ;;[paren! none!]
config-path ;;[file! none!]
bootstrap-config ;;[block!]
] [
params: copy #{}
save/as
params
reduce [app-keypair config-path bootstrap-config]
'redbin
probe length? params
r_safe_connect
ref
probe params
]
r_safe_connect: routine [
ref [handle!]
params [binary!]
] [
c_safe_connect
tokio_runtime
as handle! ref/value
binary/rs-head params
binary/rs-length? params
]
; hi-level code
bls-key: function [
key-bin [binary!]
return: [block!]
] [
key: copy []
foreach b key-bin [
append key b
]
reduce [key]
]
safe!: object [
ref: none
init: does [
ref: safe_default
]
free: does [
safe_free ref
ref: none
]
xorurl-base: does [
safe_xorurl_base ref
]
connect: function [
ip [tuple!]
port [integer!]
] [
genesis-key: bls-key #{
8640 e62c c44e 75cf
4fad c8ee 91b7 4b4c
f0fd 2c09 84fb 0e3a
b40f 0268 0685 7d8c
41f0 1d37 2522 3c55
b1ef 87d6 69f5 e2cc
}
safe_connect
ref
none ;app-keypair
none ;config-path
probe compose/deep [
(genesis-key)
[
(rejoin [ip #":" port])
(rejoin [ip #":" port + 1]) ;-- NODES_TO_CONTACT_PER_STARTUP_BATCH = 3 @ safe_network/src/client/connections/messaging.rs
(rejoin [ip #":" port + 2])
]
]
]
]