@@ -83,8 +83,30 @@ func newLookup(head layer, descendant func(state common.Hash, ancestor common.Ha
83
83
// layer specified by the stateID: fallback to the disk layer for data retrieval,
84
84
// (b) or the layer specified by the stateID is stale: reject the data retrieval.
85
85
func (l * lookup ) accountTip (accountHash common.Hash , stateID common.Hash , base common.Hash ) common.Hash {
86
+ // Traverse the mutation history from latest to oldest one. Several
87
+ // scenarios are possible:
88
+ //
89
+ // Chain:
90
+ // D->C1->C2->C3->C4 (HEAD)
91
+ // ->C1'->C2'->C3'
92
+ // State:
93
+ // x: [C1, C1', C3', C3]
94
+ // y: []
95
+ //
96
+ // - (x, C4) => C3
97
+ // - (x, C3) => C3
98
+ // - (x, C2) => C1
99
+ // - (x, C3') => C3'
100
+ // - (x, C2') => C1'
101
+ // - (y, C4) => D
102
+ // - (y, C3') => D
103
+ // - (y, C0) => null
86
104
list := l .accounts [accountHash ]
87
105
for i := len (list ) - 1 ; i >= 0 ; i -- {
106
+ // If the current state matches the stateID, or the requested state is a
107
+ // descendant of it, return the current state as the most recent one
108
+ // containing the modified data. Otherwise, the current state may be ahead
109
+ // of the requested one or belong to a different branch.
88
110
if list [i ] == stateID || l .descendant (stateID , list [i ]) {
89
111
return list [i ]
90
112
}
@@ -115,6 +137,10 @@ func (l *lookup) storageTip(accountHash common.Hash, slotHash common.Hash, state
115
137
if exists {
116
138
list := subset [slotHash ]
117
139
for i := len (list ) - 1 ; i >= 0 ; i -- {
140
+ // If the current state matches the stateID, or the requested state is a
141
+ // descendant of it, return the current state as the most recent one
142
+ // containing the modified data. Otherwise, the current state may be ahead
143
+ // of the requested one or belong to a different branch.
118
144
if list [i ] == stateID || l .descendant (stateID , list [i ]) {
119
145
return list [i ]
120
146
}
0 commit comments