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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use anyhow::{bail, Error as AnyError};
use fuel_core_interfaces::model::BlockHeight;
use fuels_types::coin::Coin;
use fuels_types::message::Message;
use std::fmt;
use std::io::Write;
use std::net::{Ipv4Addr, SocketAddr};
use std::time::Duration;
use tokio::sync::oneshot;

use portpicker::is_free;
use portpicker::pick_unused_port;

use fuel_chain_config::{BlockProduction, ChainConfig, StateConfig};
use fuel_gql_client::client::FuelClient;
use fuel_gql_client::fuel_tx::ConsensusParameters;
use fuel_gql_client::fuel_vm::consts::WORD_SIZE;
use fuel_types::Word;
use serde::de::Error;
use serde::{Deserializer, Serializer};
use serde_json::Value;
use serde_with::{DeserializeAs, SerializeAs};
use std::process::Stdio;
use tempfile::NamedTempFile;
use tokio::process::Command;

use crate::utils::{get_coin_configs, get_message_configs};

#[derive(Clone, Copy, Debug)]
pub struct Config {
    pub addr: SocketAddr,
    pub utxo_validation: bool,
    pub manual_blocks_enabled: bool,
    pub vm_backtrace: bool,
    pub silent: bool,
}

impl Config {
    pub fn local_node() -> Self {
        Self {
            addr: SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 0),
            utxo_validation: false,
            manual_blocks_enabled: false,
            vm_backtrace: false,
            silent: true,
        }
    }
}

pub type InternalDaBlockHeight = u64;

pub(crate) struct HexType;

impl<T: AsRef<[u8]>> SerializeAs<T> for HexType {
    fn serialize_as<S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serde_hex::serialize(value, serializer)
    }
}

impl<'de, T, E> DeserializeAs<'de, T> for HexType
where
    for<'a> T: TryFrom<&'a [u8], Error = E>,
    E: fmt::Display,
{
    fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
    {
        serde_hex::deserialize(deserializer)
    }
}

pub mod serde_hex {
    use std::{convert::TryFrom, fmt};

    use hex::{FromHex, ToHex};
    use serde::de::Error;
    use serde::{Deserializer, Serializer};

    pub fn serialize<T, S>(target: T, ser: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
        T: ToHex,
    {
        let s = format!("0x{}", target.encode_hex::<String>());
        ser.serialize_str(&s)
    }

    pub fn deserialize<'de, T, E, D>(des: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
        for<'a> T: TryFrom<&'a [u8], Error = E>,
        E: fmt::Display,
    {
        let raw_string: String = serde::Deserialize::deserialize(des)?;
        let stripped_prefix = raw_string.trim_start_matches("0x");
        let bytes: Vec<u8> = FromHex::from_hex(stripped_prefix).map_err(D::Error::custom)?;
        let result = T::try_from(bytes.as_slice()).map_err(D::Error::custom)?;
        Ok(result)
    }
}

pub(crate) struct HexNumber;

impl SerializeAs<u64> for HexNumber {
    fn serialize_as<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let bytes = value.to_be_bytes();
        serde_hex::serialize(bytes, serializer)
    }
}

impl<'de> DeserializeAs<'de, Word> for HexNumber {
    fn deserialize_as<D>(deserializer: D) -> Result<Word, D::Error>
    where
        D: Deserializer<'de>,
    {
        let mut bytes: Vec<u8> = serde_hex::deserialize(deserializer)?;
        match bytes.len() {
            len if len > WORD_SIZE => {
                return Err(D::Error::custom(format!(
                    "value cant exceed {} bytes",
                    WORD_SIZE
                )));
            }
            len if len < WORD_SIZE => {
                // pad if length < word size
                bytes = (0..WORD_SIZE - len)
                    .map(|_| 0u8)
                    .chain(bytes.into_iter())
                    .collect();
            }
            _ => {}
        }
        // We've already verified the bytes.len == WORD_SIZE, force the conversion here.
        Ok(Word::from_be_bytes(
            bytes.try_into().expect("byte lengths checked"),
        ))
    }
}

impl SerializeAs<BlockHeight> for HexNumber {
    fn serialize_as<S>(value: &BlockHeight, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let number: u64 = (*value).into();
        HexNumber::serialize_as(&number, serializer)
    }
}

impl<'de> DeserializeAs<'de, BlockHeight> for HexNumber {
    fn deserialize_as<D>(deserializer: D) -> Result<BlockHeight, D::Error>
    where
        D: Deserializer<'de>,
    {
        let number: u64 = HexNumber::deserialize_as(deserializer)?;
        Ok(number.into())
    }
}

pub fn get_node_config_json(
    coins: Vec<Coin>,
    messages: Vec<Message>,
    chain_config: Option<ChainConfig>,
    consensus_parameters_config: Option<ConsensusParameters>,
) -> Value {
    let coin_configs = get_coin_configs(coins);
    let messages = get_message_configs(messages);

    let chain_config = {
        let chain_config = chain_config.unwrap_or_else(|| ChainConfig {
            chain_name: "local_testnet".to_string(),
            block_production: BlockProduction::ProofOfAuthority {
                trigger: Default::default(),
            },
            block_gas_limit: 1000000000,
            initial_state: Some(StateConfig {
                coins: Some(coin_configs),
                contracts: None,
                messages: Some(messages),
                height: None,
            }),
            ..ChainConfig::local_testnet()
        });

        if let Some(transaction_parameters) = consensus_parameters_config {
            ChainConfig {
                transaction_parameters,
                ..chain_config
            }
        } else {
            chain_config
        }
    };

    serde_json::to_value(&chain_config).expect("Failed to build `ChainConfig` JSON")
}

fn write_temp_config_file(config: Value) -> NamedTempFile {
    let config_file = NamedTempFile::new();

    let _ = writeln!(
        config_file.as_ref().unwrap().as_file(),
        "{}",
        &config.to_string()
    );

    config_file.unwrap()
}

pub async fn new_fuel_node(
    coins: Vec<Coin>,
    messages: Vec<Message>,
    config: Config,
    chain_config: Option<ChainConfig>,
    consensus_parameters_config: Option<ConsensusParameters>,
) {
    // Create a new one-shot channel for sending single values across asynchronous tasks.
    let (tx, rx) = oneshot::channel();

    tokio::spawn(async move {
        let config_json =
            get_node_config_json(coins, messages, chain_config, consensus_parameters_config);
        let temp_config_file = write_temp_config_file(config_json);

        let port = &config.addr.port().to_string();
        let mut args = vec![
            "run", // `fuel-core` is now run with `fuel-core run`
            "--ip",
            "127.0.0.1",
            "--port",
            port,
            "--db-type",
            "in-memory",
            "--chain",
            temp_config_file.path().to_str().unwrap(),
        ];

        if config.utxo_validation {
            args.push("--utxo-validation");
        }

        if config.manual_blocks_enabled {
            args.push("--manual_blocks_enabled");
        }

        if config.vm_backtrace {
            args.push("--vm-backtrace");
        }

        // Warn if there is more than one binary in PATH.
        let binary_name = "fuel-core";
        let paths = which::which_all(binary_name)
            .unwrap_or_else(|_| panic!("failed to list '{}' binaries", binary_name))
            .collect::<Vec<_>>();
        let path = paths
            .first()
            .unwrap_or_else(|| panic!("no '{}' in PATH", binary_name));
        if paths.len() > 1 {
            eprintln!(
                "found more than one '{}' binary in PATH, using '{}'",
                binary_name,
                path.display()
            );
        }

        let mut command = Command::new(path);
        command.stdin(Stdio::null());
        if config.silent {
            command.stdout(Stdio::null()).stderr(Stdio::null());
        }
        let mut running_node = command
            .args(args)
            .kill_on_drop(true)
            .spawn()
            .expect("error: Couldn't read fuel-core: No such file or directory. Please check if fuel-core library is installed.");

        let client = FuelClient::from(config.addr);
        server_health_check(&client).await;
        // Sending single to RX to inform that the fuel core node is ready.
        tx.send(()).unwrap();

        running_node.wait().await
    });
    // Awaiting a signal from Tx that informs us if the fuel-core node is ready.
    rx.await.unwrap();
}

pub async fn server_health_check(client: &FuelClient) {
    let mut attempts = 5;
    let mut healthy = client.health().await.unwrap_or(false);

    while attempts > 0 && !healthy {
        healthy = client.health().await.unwrap_or(false);
        tokio::time::sleep(Duration::from_millis(100)).await;
        attempts -= 1;
    }

    if !healthy {
        panic!("error: Could not connect to fuel core server.")
    }
}

pub fn get_socket_address() -> SocketAddr {
    let free_port = pick_unused_port().expect("No ports free");
    SocketAddr::new("127.0.0.1".parse().unwrap(), free_port)
}

pub struct FuelService {
    pub bound_address: SocketAddr,
}

impl FuelService {
    pub async fn new_node(config: Config) -> Result<Self, AnyError> {
        let requested_port = config.addr.port();

        let bound_address = if requested_port == 0 {
            get_socket_address()
        } else if is_free(requested_port) {
            config.addr
        } else {
            bail!("Error: Address already in use");
        };

        new_fuel_node(
            vec![],
            vec![],
            Config {
                addr: bound_address,
                ..config
            },
            None,
            None,
        )
        .await;

        Ok(FuelService { bound_address })
    }
}