Fix tracking of ancients

We had been assuming that the `item` returned from batch.commit()
was the item committed, but it's actually the next item to be added
to the freezer, and multiple items can be committed in a single batch.

This commit finds the smallest item in the freezer and iterates from
that to the number returned by commit(), passing any tracked blocks
in that range to plugins.
This commit is contained in:
Austin Roberts 2021-10-28 20:41:32 -05:00
parent 4acf8d22df
commit 80ae5b46d4

View File

@ -37,11 +37,16 @@ func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) } if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }
defer func() { delete(freezerUpdates, num) }() min := ^uint64(0)
update, ok := freezerUpdates[num] for i := range freezerUpdates{
if min < i { min = i }
}
for i := min ; i < num; i++ {
update, ok := freezerUpdates[i]
defer func() { delete(freezerUpdates, i) }()
if !ok { if !ok {
log.Warn("Attempting to commit untracked block", "num", num) log.Warn("Attempting to commit untracked block", "num", i)
return continue
} }
fnList := pl.Lookup("ModifyAncients", func(item interface{}) bool { fnList := pl.Lookup("ModifyAncients", func(item interface{}) bool {
_, ok := item.(func(uint64, map[string]interface{})) _, ok := item.(func(uint64, map[string]interface{}))
@ -49,7 +54,7 @@ func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) {
}) })
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func(uint64, map[string]interface{})); ok { if fn, ok := fni.(func(uint64, map[string]interface{})); ok {
fn(num, update) fn(i, update)
} }
} }
appendAncientFnList := pl.Lookup("AppendAncient", func(item interface{}) bool { appendAncientFnList := pl.Lookup("AppendAncient", func(item interface{}) bool {
@ -107,7 +112,8 @@ func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) {
} }
for _, fni := range appendAncientFnList { for _, fni := range appendAncientFnList {
if fn, ok := fni.(func(number uint64, hash, header, body, receipts, td []byte)); ok { if fn, ok := fni.(func(number uint64, hash, header, body, receipts, td []byte)); ok {
fn(num, hash, header, body, receipts, td) fn(i, hash, header, body, receipts, td)
}
} }
} }
} }