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
use cfx_parameters::internal_contract_addresses::ADMIN_CONTROL_CONTRACT_ADDRESS;
use cfx_types::{AddressSpaceUtil, AddressWithSpace};
use cfx_vm_types::Spec;
use std::collections::HashMap;

#[derive(Debug)]
pub struct CallStackInfo {
    call_stack_recipient_addresses: Vec<(AddressWithSpace, bool)>,
    address_counter: HashMap<AddressWithSpace, u32>,
    first_reentrancy_depth: Option<usize>,
}

impl CallStackInfo {
    pub fn new() -> Self {
        CallStackInfo {
            call_stack_recipient_addresses: Vec::default(),
            address_counter: HashMap::default(),
            first_reentrancy_depth: None,
        }
    }

    pub fn push(&mut self, address: AddressWithSpace, is_create: bool) {
        // We should still use the correct behaviour to check if reentrancy
        // happens.
        if self.last() != Some(&address) && self.contains_key(&address) {
            self.first_reentrancy_depth
                .get_or_insert(self.call_stack_recipient_addresses.len());
        }

        self.call_stack_recipient_addresses
            .push((address.clone(), is_create));
        *self.address_counter.entry(address).or_insert(0) += 1;
    }

    pub fn pop(&mut self) -> Option<(AddressWithSpace, bool)> {
        let maybe_address = self.call_stack_recipient_addresses.pop();
        if let Some((address, _is_create)) = &maybe_address {
            let poped_address_cnt = self
                .address_counter
                .get_mut(address)
                .expect("The lookup table should consistent with call stack");
            *poped_address_cnt -= 1;
            if *poped_address_cnt == 0 {
                self.address_counter.remove(address);
            }
            if self.first_reentrancy_depth
                == Some(self.call_stack_recipient_addresses.len())
            {
                self.first_reentrancy_depth = None
            }
        }
        maybe_address
    }

    pub fn last(&self) -> Option<&AddressWithSpace> {
        self.call_stack_recipient_addresses
            .last()
            .map(|(address, _is_create)| address)
    }

    pub fn contains_key(&self, key: &AddressWithSpace) -> bool {
        self.address_counter.contains_key(key)
    }

    pub fn in_reentrancy(&self, spec: &Spec) -> bool {
        if spec.cip71 {
            // After CIP-71, anti-reentrancy will closed.
            false
        } else {
            // Consistent with old behaviour
            // The old (unexpected) behaviour is equivalent to the top element
            // is lost.
            self.first_reentrancy_depth.map_or(false, |depth| {
                (depth as isize)
                    < self.call_stack_recipient_addresses.len() as isize - 1
            })
        }
    }

    pub fn contract_in_creation(&self) -> Option<&AddressWithSpace> {
        if let [.., second_last, last] =
            self.call_stack_recipient_addresses.as_slice()
        {
            if last.0 == ADMIN_CONTROL_CONTRACT_ADDRESS.with_native_space()
                && second_last.1
            {
                Some(&second_last.0)
            } else {
                None
            }
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::CallStackInfo;
    use cfx_types::{Address, AddressSpaceUtil, AddressWithSpace};

    fn get_test_address_raw(n: u8) -> Address { Address::from([n; 20]) }

    fn get_test_address(n: u8) -> AddressWithSpace {
        get_test_address_raw(n).with_native_space()
    }

    #[test]
    fn test_callstack_info() {
        let mut call_stack = CallStackInfo::new();
        call_stack.push(get_test_address(1), false);
        call_stack.push(get_test_address(2), false);
        assert_eq!(call_stack.pop(), Some((get_test_address(2), false)));
        assert_eq!(call_stack.contains_key(&get_test_address(2)), false);

        call_stack.push(get_test_address(3), true);
        call_stack.push(get_test_address(4), false);
        call_stack.push(get_test_address(3), false);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(3));

        assert_eq!(call_stack.pop(), Some((get_test_address(3), false)));
        assert_eq!(call_stack.contains_key(&get_test_address(3)), true);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(4));

        assert_eq!(call_stack.pop(), Some((get_test_address(4), false)));
        assert_eq!(call_stack.contains_key(&get_test_address(4)), false);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(3));

        assert_eq!(call_stack.pop(), Some((get_test_address(3), true)));
        assert_eq!(call_stack.contains_key(&get_test_address(3)), false);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(1));

        call_stack.push(get_test_address(3), true);
        call_stack.push(get_test_address(4), false);
        call_stack.push(get_test_address(3), false);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(3));

        assert_eq!(call_stack.pop(), Some((get_test_address(3), false)));
        assert_eq!(call_stack.contains_key(&get_test_address(3)), true);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(4));

        assert_eq!(call_stack.pop(), Some((get_test_address(4), false)));
        assert_eq!(call_stack.contains_key(&get_test_address(4)), false);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(3));

        assert_eq!(call_stack.pop(), Some((get_test_address(3), true)));
        assert_eq!(call_stack.contains_key(&get_test_address(3)), false);
        assert_eq!(call_stack.last().unwrap().clone(), get_test_address(1));

        assert_eq!(call_stack.pop(), Some((get_test_address(1), false)));
        assert_eq!(call_stack.pop(), None);
        assert_eq!(call_stack.last(), None);
    }
}