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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * ‌
 * Hedera Rust SDK
 * ​
 * Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
 * ​
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ‍
 */

use std::borrow::Cow;
use std::str;

use hedera_proto::services;
use num_bigint::{
    BigInt,
    BigUint,
};

use crate::protobuf::ToProtobuf;
use crate::{
    AccountId,
    ContractId,
    ContractLogInfo,
    ContractNonceInfo,
    FromProtobuf,
};

/// The result returned by a call to a smart contract function.
#[derive(Debug, Clone)]
pub struct ContractFunctionResult {
    /// The smart contract instance whose function was called.
    pub contract_id: ContractId,

    /// The new contract's 20-byte EVM address.
    pub evm_address: Option<ContractId>,

    /// The raw bytes returned by the function.
    pub bytes: Vec<u8>,

    /// Message if there was an error during smart contract execution.
    pub error_message: Option<String>,

    /// Bloom filter for record.
    pub bloom: Vec<u8>,

    /// Units of gas used to execute contract.
    pub gas_used: u64,

    /// The amount of gas available for the call.
    pub gas: u64,

    /// Number of HBAR sent (the function must be payable if this is nonzero).
    pub hbar_amount: u64,

    /// The parameters passed into the contract call.
    pub contract_function_parameters_bytes: Vec<u8>,

    /// The account that is the "sender." If not present it is the accountId from the transactionId.
    pub sender_account_id: Option<AccountId>,

    /// Logs that this call and any called functions produced.
    pub logs: Vec<ContractLogInfo>,

    /// A list of updated contract account nonces containing the new nonce value for each contract account.
    /// This is always empty in a ContractLocalCallQuery response, since no internal creations can happen in a static EVM call.
    pub contract_nonces: Vec<ContractNonceInfo>,

    /// If not null this field specifies what the value of the signer account nonce is post transaction execution.
    /// For transactions that don't update the signer nonce, this field should be null.
    pub signer_nonce: Option<u64>,
}

impl ContractFunctionResult {
    const SLOT_SIZE: usize = 32;

    #[must_use]
    fn get_fixed_bytes<const N: usize>(&self, slot: usize) -> Option<&[u8; N]> {
        self.get_fixed_bytes_at(slot * Self::SLOT_SIZE + (Self::SLOT_SIZE - N))
    }

    // fixme(sr): name is weird, but I can't think of a better one.
    // basically, there's `get_fixed_bytes` which works off of "slots" (multiples of 32 bytes), and this version, which can be from anywhere.
    #[must_use]
    fn get_fixed_bytes_at<const N: usize>(&self, offset: usize) -> Option<&[u8; N]> {
        self.bytes.get(offset..).and_then(|it| it.get(..N)).map(|it| it.try_into().unwrap())
    }

    // fixme(sr): name is weird, but I can't think of a better one.
    #[must_use]
    fn get_u32_at(&self, offset: usize) -> Option<u32> {
        self.get_fixed_bytes_at(28 + offset).map(|it| u32::from_be_bytes(*it))
    }

    #[must_use]
    fn offset_len_pair(&self, offset: usize) -> Option<(usize, usize)> {
        let offset = self.get_u32(offset)? as usize;
        let len = self.get_u32_at(offset)? as usize;
        Some((offset, len))
    }

    /// Get the whole raw function result.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    // note: This would be best named `get_str_lossy` but consistency :/
    /// Get the value at `index` as a solidity `string`.
    ///
    /// Theoretically, all strings here should be utf8, but this function does _lossy_ conversion.
    #[must_use]
    pub fn get_str(&self, index: usize) -> Option<Cow<str>> {
        self.get_bytes(index).map(String::from_utf8_lossy)
    }
    /// Get the value at `index` as a solidity `string[]`.
    ///
    /// Theoretically, all strings here should be utf8, but this function does _lossy_ conversion.
    #[must_use]
    pub fn get_str_array(&self, index: usize) -> Option<Vec<Cow<str>>> {
        let (offset, len) = self.offset_len_pair(index)?;

        let mut v = Vec::with_capacity(len);
        for i in 0..len {
            let str_offset =
                self.get_u32_at(offset + Self::SLOT_SIZE + (i * Self::SLOT_SIZE))? as usize;
            let str_offset = offset + str_offset + Self::SLOT_SIZE;
            let len = self.get_u32_at(str_offset)? as usize;

            let bytes =
                self.bytes.get((str_offset + Self::SLOT_SIZE)..).and_then(|it| it.get(..len))?;

            v.push(String::from_utf8_lossy(bytes));
        }

        Some(v)
    }

    /// Get the value at `index` as solidity `bytes`.
    #[must_use]
    pub fn get_bytes(&self, index: usize) -> Option<&[u8]> {
        let (offset, len) = self.offset_len_pair(index)?;
        self.bytes.get((offset + Self::SLOT_SIZE)..).and_then(|it| it.get(..len))
    }

    /// Get the value at `index` as solidity `bytes32`.
    ///
    /// This is the native word size for the solidity ABI.
    #[must_use]
    pub fn get_bytes32(&self, index: usize) -> Option<&[u8; 32]> {
        self.get_fixed_bytes(index)
    }

    /// Get the value at `index` as a solidity `address` and then hex-encode the result.
    #[must_use]
    pub fn get_address(&self, index: usize) -> Option<String> {
        self.get_fixed_bytes::<20>(index).map(hex::encode)
    }

    /// Get the value at `index` as a solidity `bool`.
    #[must_use]
    pub fn get_bool(&self, index: usize) -> Option<bool> {
        self.get_u8(index).map(|it| it != 0)
    }

    /// Get the value at `index` as a solidity `u8`.
    #[must_use]
    pub fn get_u8(&self, index: usize) -> Option<u8> {
        self.get_fixed_bytes(index).copied().map(u8::from_be_bytes)
    }

    /// Get the value at `index` as a solidity `i8`.
    #[must_use]
    pub fn get_i8(&self, index: usize) -> Option<i8> {
        self.get_fixed_bytes(index).copied().map(i8::from_be_bytes)
    }

    /// Get the value at `index` as a solidity `u32`.
    pub fn get_u32(&self, index: usize) -> Option<u32> {
        self.get_fixed_bytes(index).copied().map(u32::from_be_bytes)
    }

    /// Get the value at `index` as a solidity `i32`.
    #[must_use]
    pub fn get_i32(&self, index: usize) -> Option<i32> {
        self.get_fixed_bytes(index).copied().map(i32::from_be_bytes)
    }

    /// Get the value at `index` as a solidity `u64`.
    #[must_use]
    pub fn get_u64(&self, index: usize) -> Option<u64> {
        self.get_fixed_bytes(index).copied().map(u64::from_be_bytes)
    }

    /// Get the value at `index` as a solidity `i64`.
    #[must_use]
    pub fn get_i64(&self, index: usize) -> Option<i64> {
        self.get_fixed_bytes(index).copied().map(i64::from_be_bytes)
    }

    /// Get the value at `index` as a solidity `u256` (`uint`).
    ///
    /// This is the native unsigned integer size for the solidity ABI.
    #[must_use]
    pub fn get_u256(&self, index: usize) -> Option<BigUint> {
        self.get_bytes32(index).map(|it| BigUint::from_bytes_be(it))
    }

    /// Get the value at `index` as a solidity `i256` (`int`).
    ///
    /// This is the native unsigned integer size for the solidity ABI.
    #[must_use]
    pub fn get_i256(&self, index: usize) -> Option<BigInt> {
        self.get_bytes32(index).map(|it| BigInt::from_signed_bytes_be(it))
    }
}

impl FromProtobuf<services::ContractFunctionResult> for ContractFunctionResult {
    fn from_protobuf(pb: services::ContractFunctionResult) -> crate::Result<Self>
    where
        Self: Sized,
    {
        let contract_id = pb_getf!(pb, contract_id)?;
        let contract_id = ContractId::from_protobuf(contract_id)?;

        let sender_account_id = Option::from_protobuf(pb.sender_id)?;

        let evm_address =
            pb.evm_address.and_then(|address| <[u8; 20]>::try_from(address).ok()).map(|address| {
                ContractId::from_evm_address_bytes(contract_id.shard, contract_id.realm, address)
            });

        let error_message = if pb.error_message.is_empty() { None } else { Some(pb.error_message) };

        // if an exception was thrown, the call result is encoded like the params
        // for a function `Error(string)`
        // https://solidity.readthedocs.io/en/v0.6.2/control-structures.html#revert
        // `map_or` wouldn't actually work here, because `contract_call_result
        #[allow(clippy::map_unwrap_or)]
        let bytes = if error_message.is_some() {
            pb.contract_call_result
                .strip_prefix(&[0x08, 0xc3, 0x79, 0xa0])
                .map(<[u8]>::to_vec)
                .unwrap_or(pb.contract_call_result)
        } else {
            pb.contract_call_result
        };

        let signer_nonce = pb.signer_nonce.map(|it| it as u64);

        Ok(Self {
            contract_id,
            bytes,
            error_message,
            bloom: pb.bloom,
            gas_used: pb.gas_used,
            gas: pb.gas as u64,
            hbar_amount: pb.amount as u64,
            contract_function_parameters_bytes: pb.function_parameters,
            sender_account_id,
            evm_address,
            logs: Vec::from_protobuf(pb.log_info)?,
            contract_nonces: Vec::from_protobuf(pb.contract_nonces)?,
            signer_nonce,
        })
    }
}

impl FromProtobuf<services::response::Response> for ContractFunctionResult {
    fn from_protobuf(pb: services::response::Response) -> crate::Result<Self>
    where
        Self: Sized,
    {
        let pb = pb_getv!(pb, ContractCallLocal, services::response::Response);

        let result = pb_getf!(pb, function_result)?;
        let result = ContractFunctionResult::from_protobuf(result)?;

        Ok(result)
    }
}

impl ToProtobuf for ContractFunctionResult {
    type Protobuf = services::ContractFunctionResult;

    fn to_protobuf(&self) -> Self::Protobuf {
        #[allow(deprecated)]
        services::ContractFunctionResult {
            contract_id: Some(self.contract_id.to_protobuf()),
            contract_call_result: self.bytes.clone(),
            error_message: self.error_message.clone().unwrap_or_default(),
            bloom: self.bloom.clone(),
            gas_used: self.gas,
            log_info: self.logs.to_protobuf(),
            created_contract_i_ds: Vec::new(),
            evm_address: self.evm_address.and_then(|it| it.evm_address.map(|it| it.to_vec())),
            gas: self.gas as i64,
            amount: self.hbar_amount as i64,
            function_parameters: self.contract_function_parameters_bytes.clone(),
            sender_id: self.sender_account_id.to_protobuf(),
            contract_nonces: self.contract_nonces.to_protobuf(),
            signer_nonce: self.signer_nonce.map(|it| it as i64),
        }
    }
}

#[cfg(test)]
mod tests {
    use hedera_proto::services;
    use hex_literal::hex;
    use num_bigint::{
        BigInt,
        BigUint,
    };

    use crate::protobuf::{
        FromProtobuf,
        ToProtobuf,
    };
    use crate::{
        AccountId,
        ContractFunctionResult,
        ContractId,
        ContractNonceInfo,
    };

    const CALL_RESULT: [u8; 320] = hex!(
        "00000000000000000000000000000000000000000000000000000000ffffffff"
        "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
        "00000000000000000000000011223344556677889900aabbccddeeff00112233"
        "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
        "00000000000000000000000000000000000000000000000000000000000000c0"
        "0000000000000000000000000000000000000000000000000000000000000100"
        "000000000000000000000000000000000000000000000000000000000000000d"
        "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
        "0000000000000000000000000000000000000000000000000000000000000014"
        "48656c6c6f2c20776f726c642c20616761696e21000000000000000000000000"
    );

    const STRING_ARRAY_RESULT: [u8; 256] = hex!(
        "0000000000000000000000000000000000000000000000000000000000000020"
        "0000000000000000000000000000000000000000000000000000000000000002"
        "0000000000000000000000000000000000000000000000000000000000000040"
        "0000000000000000000000000000000000000000000000000000000000000080"
        "000000000000000000000000000000000000000000000000000000000000000C"
        "72616E646F6D2062797465730000000000000000000000000000000000000000"
        "000000000000000000000000000000000000000000000000000000000000000E"
        "72616E646F6D2062797465732032000000000000000000000000000000000000"
    );

    // previous one, just offset by a bit, to ensure the logic works.
    // notes below, where `slot` is just an offset at a multiple of 32 bytes.
    const STRING_ARRAY_RESULT_2: [u8; 320] = hex!(
        // empty value at slot 0
        "0000000000000000000000000000000000000000000000000000000000000000"
        // reference to slot 3 at slot 1
        // this is interpreted as a string[]
        "0000000000000000000000000000000000000000000000000000000000000060"
        // empty value at slot 2
        "0000000000000000000000000000000000000000000000000000000000000000"
        // length of string (2 items) at slot 3
        "0000000000000000000000000000000000000000000000000000000000000002"
        // relative offset of strings[0] (2 slots) at slot 4
        "0000000000000000000000000000000000000000000000000000000000000040"
        // relative offset of strings[1] (4 slots) at slot 5
        "0000000000000000000000000000000000000000000000000000000000000080"
        // length of strings[0] (12 bytes) at slot 6
        "000000000000000000000000000000000000000000000000000000000000000c"
        // first 12 bytes: value of strings[0], rest is filler, at slot 7
        "72616e646f6d206279746573000000000000000000000000c0ffee0000000000"
        // length of strings[1] (14 bytes) at slot 8
        "000000000000000000000000000000000000000000000000000000000000000e"
        // first 14 bytes: value of strings[0], rest is filler, at slot 7
        "72616E646F6D2062797465732032000000000000decaff000000000000000000"
    );

    #[test]
    fn evm_address() {
        const EVM_ADDRESS: [u8; 20] = hex!("98329e006610472e6b372c080833f6d79ed833cf");
        let result = services::ContractFunctionResult {
            contract_id: Some(ContractId::new(3, 7, 13).to_protobuf()),
            evm_address: Some(EVM_ADDRESS.to_vec()),
            ..Default::default()
        };

        let result = ContractFunctionResult::from_protobuf(result).unwrap();

        assert_eq!(result.contract_id, ContractId::new(3, 7, 13));

        // ensure that we follow *Java* behavior (every SDK has different behavior here)
        assert_eq!(result.evm_address, Some(ContractId::from_evm_address_bytes(3, 7, EVM_ADDRESS)));
    }

    #[test]
    #[allow(deprecated)]
    fn provides_results() {
        let result = services::ContractFunctionResult {
            contract_id: Some(ContractId::from(3).to_protobuf()),
            contract_call_result: CALL_RESULT.to_vec(),
            sender_id: Some(
                AccountId {
                    shard: 31,
                    realm: 41,
                    num: 65,
                    alias: None,
                    evm_address: None,
                    checksum: None,
                }
                .to_protobuf(),
            ),
            contract_nonces: vec![services::ContractNonceInfo {
                contract_id: Some(services::ContractId {
                    shard_num: 1,
                    realm_num: 2,
                    contract: Some(services::contract_id::Contract::ContractNum(3)),
                }),
                nonce: 10,
            }],
            ..Default::default()
        };

        let result = ContractFunctionResult::from_protobuf(result).unwrap();

        assert_eq!(result.get_bool(0).unwrap(), true);
        assert_eq!(result.get_i32(0).unwrap(), -1);
        assert_eq!(result.get_i64(0).unwrap(), u32::MAX as u64 as i64);
        assert_eq!(result.get_i256(0).unwrap(), BigInt::from(u32::MAX));
        assert_eq!(result.get_i256(1).unwrap(), (BigInt::from(1) << 255) - 1);
        assert_eq!(&result.get_address(2).unwrap(), "11223344556677889900aabbccddeeff00112233");
        assert_eq!(result.get_u32(3).unwrap(), u32::MAX);
        assert_eq!(result.get_u64(3).unwrap(), u64::MAX);
        // BigInteger can represent the full range and so should be 2^256 - 1
        assert_eq!(result.get_u256(3).unwrap(), (BigUint::from(1_u8) << 256) - 1_u32);

        assert_eq!(result.get_str(4).unwrap(), "Hello, world!");
        assert_eq!(result.get_str(5).unwrap(), "Hello, world, again!");

        assert_eq!(
            result.sender_account_id,
            Some(AccountId {
                shard: 31,
                realm: 41,
                num: 65,
                alias: None,
                evm_address: None,
                checksum: None,
            })
        );

        assert_eq!(
            result.contract_nonces,
            [ContractNonceInfo {
                contract_id: ContractId {
                    shard: 1,
                    realm: 2,
                    num: 3,
                    checksum: None,
                    evm_address: None
                },
                nonce: 10
            }]
        )
    }

    #[test]
    fn str_array_results() {
        let result = services::ContractFunctionResult {
            contract_id: Some(ContractId::from(3).to_protobuf()),
            contract_call_result: STRING_ARRAY_RESULT.to_vec(),
            ..Default::default()
        };

        let result = ContractFunctionResult::from_protobuf(result).unwrap();

        let strings = result.get_str_array(0).unwrap();
        assert_eq!(strings[0], "random bytes");
        assert_eq!(strings[1], "random bytes 2")
    }

    // previous one, just offset by a bit, to ensure the logic works.
    #[test]
    fn str_array_results2() {
        let result = services::ContractFunctionResult {
            contract_id: Some(ContractId::from(3).to_protobuf()),
            contract_call_result: STRING_ARRAY_RESULT_2.to_vec(),
            ..Default::default()
        };

        let result = ContractFunctionResult::from_protobuf(result).unwrap();

        let strings = result.get_str_array(1).unwrap();
        assert_eq!(strings[0], "random bytes");
        assert_eq!(strings[1], "random bytes 2")
    }
}