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
/*
 Copyright (c) 2022 ParallelChain Lab

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use crate::Callback;
use crate::context::ContractCallData;
use crate::imports::*;
use borsh::BorshDeserialize;
use protocol_types::{Serializable, Deserializable};


/// smart_contract::Transaction is a handle containing the parameters of the smart contract invocation
/// (e.g., the 'args' string provided by the contract_caller, the previous block hash, etc.)
///
/// It also has methods attached ('set' & 'get') that allow smart contracts to maintain
/// persistent, blockchained state.
///
/// From this point on, smart_contract::Transaction can be interchangeably known as
/// "ParallelChain Mainnet Smart Contract Development Kit" or the "SDK" or "SC-SDK".
/// 
/// # Basic example 
/// ```no_run
/// let tx: smart_contract::Transaction = Transaction::new();
/// 
/// assert!(tx.get("hello").is_none());
/// 
/// tx.Set("hello", "world");
/// assert_eq!(tx.get("hello")?, "world");
///
/// tx.Set("hello", "");
/// assert_eq!(tx.get("hello")?, "");
/// ```
pub struct Transaction {
    /// Block Hash of this block
    pub this_block_number: u64,
    /// Block Hash of the previous block.
    pub prev_block_hash: protocol_types::crypto::Sha256Hash,
    /// Unix timestamp
    pub timestamp: u32,
    /// Reserved data
    pub random_bytes: protocol_types::crypto::Sha256Hash,
    /// Address of a recipient.
    pub to_address: protocol_types::crypto::PublicAddress,
    /// Address of a sender.
    pub from_address: protocol_types::crypto::PublicAddress,
    /// Amount of tokens to be sent to the smart contract address.
    pub value: u64,
    /// Hash of a transaction's signature. The transaction hashes of other transactions
    /// included in a block are used to obtain the Merkle root hash of a block.
    pub transaction_hash: protocol_types::crypto::Sha256Hash,
    /// Transaction data as arguments to this contract call
    pub arguments: Vec<u8>,
}

impl Transaction {
    /// Default constructor.
    /// 
    /// `new` should never fail if the ParallelChain Mainnet Fullnode
    /// is configured properly.
    /// 
    /// `new` expects arguments in the form of Vec<u8>.
    pub fn new() -> Self {

        let params_from_transaction = Self::parse_params_from_transaction();
        let params_from_blockchain = Self::parse_params_from_blockchain();
                
        Self {
            this_block_number: params_from_blockchain.this_block_number,
            prev_block_hash: params_from_blockchain.prev_block_hash,
            timestamp: params_from_blockchain.timestamp,
            random_bytes: params_from_blockchain.random_bytes,
            to_address: params_from_transaction.to_address,
            from_address: params_from_transaction.from_address,
            value: params_from_transaction.value,
            transaction_hash: params_from_transaction.transaction_hash,
            arguments: params_from_transaction.data,
        }
    }

    /// get returns Some(value) if a non-empty string is stored with key in the world state. 
    ///
    /// If get fails, the smart contract terminates and the sets this invocation made
    /// are not committed.
    pub fn get(key: &[u8]) -> Option<Vec<u8>> {

        let key_ptr = key.as_ptr();

        // `get` needs to get two things from `raw_get`:
        //   * A WASM linear memory offset pointing to where the host process
        //     wrote the UTF-8 encoded result of the DB get: `val_ptr`.
        //   * The byte-wise length of the gotten value: `val_len`.
        // 
        // `val_len` is returned directly, but the problem is that WASM does not
        // yet support multiple return values, so we can't return `val_ptr` alongside it.
        // Instead, we allocate a space in the heap for the host to write `val_ptr` in,
        // and then tell the host to write `val` there by passing it `val_ptr_ptr` through
        // `raw_get`.
        //
        // When val_ptr leaves this scope, it is deallocated (we have no further use
        // for it).
        let mut val_ptr: u32 = 0;
        let val_ptr_ptr = &mut val_ptr;

        let value;
        unsafe {
            let val_len = raw_get(key_ptr, key.len() as u32, val_ptr_ptr);

            // If module execution reaches this point, we can assume that the `get` has succeeded.
            //
            // This Vec<u8> takes ownership of the segment of memory, letting the Rust ownership
            // system to Drop it later.
            value = Vec::<u8>::from_raw_parts(val_ptr as *mut u8, val_len as usize, val_len as usize);
        }

        match value {
            vec if vec.is_empty() => {
                None
            },
            set_value => {
                Some(set_value)
            }
        }
    } 

    /// set binds key to value in the world state.
    pub fn set(key: &[u8], value: &[u8]) {
        let key_ptr = key.as_ptr();
        let val_ptr = value.as_ptr();
        unsafe {       
            raw_set(key_ptr, key.len() as u32, val_ptr, value.len() as u32);
        } 
    }

    /// `return_value` places `value` in the receipt of an `ExternalToContract` transaction.
    /// This method is not required when `contract_init` macro is being used on the actions()
    /// entrypoint function.
    pub fn return_value(value: Vec<u8>) {    
        let value_ptr = value.as_ptr();
        let value_len = value.len() as u32;
        unsafe {           
            raw_return(value_ptr, value_len);
        }
    }

    /// get input arguments for entrypoint
    pub fn get_arguments() -> Vec<u8> {
        let mut args_ptr: u32 = 0;
        let args_ptr_ptr = &mut args_ptr;

        let arguments;
        unsafe {
            let args_len = raw_get_arguments(args_ptr_ptr);
            arguments = Vec::<u8>::from_raw_parts(args_ptr as *mut u8,args_len as usize, args_len as usize);
        }
        arguments
    }

    pub fn emit_event(topic: &[u8], value: &[u8]) {
        let event = protocol_types::transaction::Event{ 
            topic: topic.to_vec(), 
            value: value.to_vec()
        };
        let serialized_event = protocol_types::transaction::Event::serialize(&event);

        let event_ptr= serialized_event.as_ptr();
        let event_len = serialized_event.len() as u32;

        unsafe {
            raw_emit(event_ptr, event_len);
        }
    }

    fn parse_params_from_transaction() -> protocol_types::sc_params::ParamsFromTransaction {
        let params_from_transaction_ptr: u32 = 0;
        let params_from_transaction_ptr_ptr: *const u32 = &params_from_transaction_ptr;

        let bytes;

        unsafe {
            let params_len = raw_get_params_from_transaction(params_from_transaction_ptr_ptr);
            bytes = Vec::from_raw_parts(params_from_transaction_ptr as *mut u8, params_len as usize, params_len as usize);
        }

        // SAFETY: this will not fail if fullnode serializes transaction correctly.
        let transaction = protocol_types::transaction::Transaction::deserialize(&bytes).unwrap();
        let params_from_transaction = protocol_types::sc_params::ParamsFromTransaction{
                                                            to_address: transaction.to_address,
                                                            from_address: transaction.from_address,
                                                            data: transaction.data,
                                                            value: transaction.value,
                                                            transaction_hash: transaction.hash,
                                                        };

        params_from_transaction
    }

    fn parse_params_from_blockchain() -> protocol_types::sc_params::ParamsFromBlockchain {
        let params_from_blockchain_ptr: u32 = 0;
        let params_from_blockchain_ptr_ptr: *const u32 = &params_from_blockchain_ptr;

        let bytes;

        unsafe {
            let params_len = raw_get_params_from_blockchain(params_from_blockchain_ptr_ptr);
            bytes = Vec::from_raw_parts(params_from_blockchain_ptr as *mut u8, params_len as usize, params_len as usize);
        }

        // SAFETY: this will not fail if fullnode serializes params_from_blockchain correctly.
        protocol_types::sc_params::ParamsFromBlockchain::deserialize(&bytes).unwrap()
    }

    /// calling function which handled by blockchain executor
    /// It returns Option of Vec of bytes. Interpretation on the bytes depends on caller
    pub fn call_contract(contract_address : protocol_types::PublicAddress, method_name:&str, arguments :Vec<u8>, value :u64, gas :u64) -> Option<Vec<u8>> {
        let contract_address_ptr : *const u8 = contract_address.as_ptr();

        let value_bs = value.to_le_bytes().to_vec();
        let value_ptr :*const u8 = value_bs.as_ptr();
        let gas_bs = gas.to_le_bytes().to_vec();
        let gas_ptr :*const u8 = gas_bs.as_ptr();

        let mut val_ptr: u32 = 0;
        let val_ptr_ptr = &mut val_ptr;

        let is_multiple_methods_contract = method_name.len() > 0;

        let call_data = ContractCallData::to_raw_call_data(method_name, arguments.clone());
        let call_data_ptr :*const u8 = call_data.as_ptr();
        let call_data_len = call_data.len();

        let value;
        unsafe {
            let val_len = raw_call(contract_address_ptr, call_data_ptr, call_data_len as u32, value_ptr as *const u8, gas_ptr as *const u8, val_ptr_ptr);

            // This Vec<u8> takes ownership of the segment of memory, letting the Rust ownership
            // system to Drop it later.
            value = Vec::<u8>::from_raw_parts(val_ptr as *mut u8, val_len as usize, val_len as usize);
        }

        match value {
            vec if vec.is_empty() => {
                None
            },
            return_value => {
                // Raw call function should return the values set by tx.return_value
                // If mutiple methods is called, the return value type should be Callback
                if is_multiple_methods_contract {
                    Callback::from_callback(return_value)
                } else { // Otherwise, raw bytes should be returned (e.g. for those contracts using contract_init)
                    Some(return_value)
                }
            }
        }
    }

    /// view contract by accessing view entrypoint of the contract
    pub fn view_contract(contract_address : protocol_types::PublicAddress, method_name:&str, arguments :Vec<u8>) -> Option<Vec<u8>> {
        let contract_address_ptr : *const u8 = contract_address.as_ptr();
        let mut val_ptr: u32 = 0;
        let val_ptr_ptr = &mut val_ptr;

        let is_multiple_methods_contract = method_name.len() > 0;

        let call_data = ContractCallData::to_raw_call_data(method_name, arguments.clone());
        let call_data_ptr :*const u8 = call_data.as_ptr();
        let call_data_len = call_data.len();

        let value;
        unsafe {
            let val_len = raw_view(contract_address_ptr, call_data_ptr, call_data_len as u32, val_ptr_ptr);

            // This Vec<u8> takes ownership of the segment of memory, letting the Rust ownership
            // system to Drop it later.
            value = Vec::<u8>::from_raw_parts(val_ptr as *mut u8, val_len as usize, val_len as usize);
        }

        match value {
            vec if vec.is_empty() => {
                None
            },
            return_value => {
                // Raw function should return the values set by tx.return_value
                // If mutiple methods is called, the return value type should be Callback
                if is_multiple_methods_contract {
                    Callback::from_callback(return_value)
                } else { // Otherwise, raw bytes should be returned (e.g. for those contracts using contract_init)
                    Some(return_value)
                }
            }
        }
    }

    /// A call to contract. The caller should already know the data type of return value from the function call
    /// It returns Option of T where T is return value from the function. 
    /// If data type T is different from the actual return value type of the function, None is returned.
    pub fn call<T: BorshDeserialize>(address : protocol_types::PublicAddress, method_name:&str, arguments :Vec<u8>, value :u64, gas :u64) -> Option<T> {
        if let Some(ret)= Self::call_contract(address, method_name, arguments, value, gas) {
            let mut ret = ret.as_slice();
            match BorshDeserialize::deserialize(&mut ret) {
                Ok(e) => {
                    return Some(e);
                }
                _=>{ return None;}
            }
        }
        None
    }

    /// pay() calls the raw_pay() that 
    /// runs a ctoe call to transfer credit to another address. 
    /// Return the remaining balance of the receiver's account
    pub fn pay(address : protocol_types::PublicAddress, value : u64) -> u64 {
        let contract_address_ptr : *const u8 = address.as_ptr();
        let value_vs = value.to_le_bytes().to_vec();
        let value_ptr :*const u8 = value_vs.as_ptr();
        unsafe {
            raw_pay(contract_address_ptr, value_ptr as *const u8)
        }
    }
}