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
use rlp::{self, Rlp};

use crate::{
    merkle::{
        empty_nodes,
        nibble::{self, NibbleVec},
        MerkleNode, MerkleValue,
    },
    Change, Database,
};

fn value_and_leaf_branch<'a>(
    anibble: NibbleVec,
    avalue: MerkleValue<'a>,
    bnibble: NibbleVec,
    bvalue: &'a [u8],
) -> (MerkleNode<'a>, Change) {
    debug_assert!(!anibble.is_empty());

    let mut change = Change::default();
    let mut additional = None;
    let mut nodes: [MerkleValue<'_>; 16] = empty_nodes();

    let ai: usize = anibble[0].into();
    let asub: NibbleVec = anibble[1..].into();

    if !asub.is_empty() {
        let ext_value = change.add_value(&MerkleNode::Extension(asub, avalue));
        nodes[ai] = ext_value;
    } else {
        nodes[ai] = avalue;
    }

    if bnibble.is_empty() {
        additional = Some(bvalue);
    } else {
        let bi: usize = bnibble[0].into();
        debug_assert!(ai != bi);

        let bsub = bnibble[1..].into();
        let bvalue = change.add_value(&MerkleNode::Leaf(bsub, bvalue));

        nodes[bi] = bvalue;
    }

    (MerkleNode::Branch(nodes, additional), change)
}

fn two_leaf_branch<'a>(
    anibble: NibbleVec,
    avalue: &'a [u8],
    bnibble: NibbleVec,
    bvalue: &'a [u8],
) -> (MerkleNode<'a>, Change) {
    debug_assert!(bnibble.is_empty() || !anibble.starts_with(&bnibble));
    debug_assert!(anibble.is_empty() || !bnibble.starts_with(&anibble));

    let mut change = Change::default();
    let mut additional = None;
    let mut nodes: [MerkleValue<'_>; 16] = empty_nodes();

    if anibble.is_empty() {
        additional = Some(avalue);
    } else {
        let ai: usize = anibble[0].into();
        let asub: NibbleVec = anibble[1..].into();
        let avalue = change.add_value(&MerkleNode::Leaf(asub, avalue));
        nodes[ai] = avalue;
    }

    if bnibble.is_empty() {
        additional = Some(bvalue);
    } else {
        let bi: usize = bnibble[0].into();
        let bsub: NibbleVec = bnibble[1..].into();
        let bvalue = change.add_value(&MerkleNode::Leaf(bsub, bvalue));
        nodes[bi] = bvalue;
    }

    (MerkleNode::Branch(nodes, additional), change)
}

pub fn insert_by_value<'a, D: Database>(
    merkle: MerkleValue<'a>,
    nibble: NibbleVec,
    value: &'a [u8],
    database: &'a D,
) -> (MerkleValue<'a>, Change) {
    let mut change = Change::default();

    let new = match merkle {
        MerkleValue::Empty => change.add_value(&MerkleNode::Leaf(nibble, value)),
        MerkleValue::Full(ref sub_node) => {
            let (new_node, subchange) =
                insert_by_node(sub_node.as_ref().clone(), nibble, value, database);
            change.merge(&subchange);
            change.add_value(&new_node)
        }
        MerkleValue::Hash(h) => {
            let sub_node = MerkleNode::decode(&Rlp::new(database.get(h)))
                .expect("Unable to decide Node value");
            change.remove_raw(h);
            let (new_node, subchange) = insert_by_node(sub_node, nibble, value, database);
            change.merge(&subchange);
            change.add_value(&new_node)
        }
    };

    (new, change)
}

pub fn insert_by_node<'a, D: Database>(
    node: MerkleNode<'a>,
    nibble: NibbleVec,
    value: &'a [u8],
    database: &'a D,
) -> (MerkleNode<'a>, Change) {
    let mut change = Change::default();

    let new = match node {
        MerkleNode::Leaf(ref node_nibble, ref node_value) => {
            if node_nibble == &nibble {
                MerkleNode::Leaf(nibble, value)
            } else {
                let (common, nibble_sub, node_nibble_sub) =
                    nibble::common_with_sub(&nibble, &node_nibble);

                let (branch, subchange) =
                    two_leaf_branch(node_nibble_sub, node_value, nibble_sub, value);
                change.merge(&subchange);
                if !common.is_empty() {
                    MerkleNode::Extension(common.into(), change.add_value(&branch))
                } else {
                    branch
                }
            }
        }
        MerkleNode::Extension(ref node_nibble, ref node_value) => {
            if nibble.starts_with(node_nibble) {
                let (subvalue, subchange) = insert_by_value(
                    node_value.clone(),
                    nibble[node_nibble.len()..].into(),
                    value,
                    database,
                );
                change.merge(&subchange);

                MerkleNode::Extension(node_nibble.clone(), subvalue)
            } else {
                let (common, nibble_sub, node_nibble_sub) =
                    nibble::common_with_sub(&nibble, &node_nibble);

                let (branch, subchange) =
                    value_and_leaf_branch(node_nibble_sub, node_value.clone(), nibble_sub, value);
                change.merge(&subchange);
                if !common.is_empty() {
                    MerkleNode::Extension(common.into(), change.add_value(&branch))
                } else {
                    branch
                }
            }
        }
        MerkleNode::Branch(ref node_nodes, ref node_additional) => {
            let mut nodes = node_nodes.clone();
            if nibble.is_empty() {
                MerkleNode::Branch(nodes, Some(value))
            } else {
                let ni: usize = nibble[0].into();
                let prev = nodes[ni].clone();
                let (new, subchange) = insert_by_value(prev, nibble[1..].into(), value, database);
                change.merge(&subchange);

                nodes[ni] = new;
                MerkleNode::Branch(nodes, *node_additional)
            }
        }
    };

    (new, change)
}

pub fn insert_by_empty(nibble: NibbleVec, value: &[u8]) -> (MerkleNode<'_>, Change) {
    let new = MerkleNode::Leaf(nibble, value);
    (new, Change::default())
}