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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use better_any::{Tid, TidAble};
use move_deps::{
    move_binary_format::errors::PartialVMResult,
    move_core_types::account_address::AccountAddress,
    move_vm_runtime::{
        native_functions,
        native_functions::{NativeContext, NativeFunctionTable},
    },
    move_vm_types::{
        gas_schedule::NativeCostIndex,
        loaded_data::runtime_types::Type,
        natives::function::{native_gas, NativeResult},
        values::Value,
    },
};
use smallvec::smallvec;
use std::collections::VecDeque;

/// The native transaction context extension. This needs to be attached to the
/// NativeContextExtensions value which is passed into session functions, so its accessible from
/// natives of this extension.
#[derive(Tid)]
pub struct NativeTransactionContext {
    script_hash: Vec<u8>,
}

impl NativeTransactionContext {
    /// Create a new instance of a native transaction context. This must be passed in via an
    /// extension into VM session functions.
    pub fn new(script_hash: Vec<u8>) -> Self {
        Self { script_hash }
    }
}

/// Returns all natives for transaction context.
pub fn transaction_context_natives(table_addr: AccountAddress) -> NativeFunctionTable {
    native_functions::make_table(
        table_addr,
        &[(
            "transaction_context",
            "get_script_hash",
            native_get_script_hash,
        )],
    )
}

fn native_get_script_hash(
    context: &mut NativeContext,
    mut _ty_args: Vec<Type>,
    _args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    let transaction_context = context.extensions().get::<NativeTransactionContext>();
    let cost = native_gas(context.cost_table(), NativeCostIndex::SHA3_256, 0);

    Ok(NativeResult::ok(
        cost,
        smallvec![Value::vector_u8(transaction_context.script_hash.clone())],
    ))
}

pub fn test_transaction_context_natives(table_addr: AccountAddress) -> NativeFunctionTable {
    native_functions::make_table(
        table_addr,
        &[(
            "transaction_context",
            "get_script_hash",
            test_native_get_script_hash,
        )],
    )
}

fn test_native_get_script_hash(
    context: &mut NativeContext,
    mut _ty_args: Vec<Type>,
    _args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    let cost = native_gas(context.cost_table(), NativeCostIndex::SHA3_256, 0);
    Ok(NativeResult::ok(cost, smallvec![Value::vector_u8(vec![])]))
}