2021-07-14 13:33:17 +00:00
|
|
|
package rawdb
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-18 17:35:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/plugins"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-08-22 18:48:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-10-18 22:06:16 +00:00
|
|
|
"sync"
|
2021-07-14 13:33:17 +00:00
|
|
|
)
|
|
|
|
|
2021-10-18 17:35:56 +00:00
|
|
|
var (
|
|
|
|
freezerUpdates map[uint64]map[string]interface{}
|
2021-10-18 22:06:16 +00:00
|
|
|
lock sync.Mutex
|
2021-10-18 17:35:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func PluginTrackUpdate(num uint64, kind string, value interface{}) {
|
2021-10-18 22:06:16 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2021-10-18 20:10:13 +00:00
|
|
|
if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }
|
2021-10-18 17:35:56 +00:00
|
|
|
update, ok := freezerUpdates[num]
|
|
|
|
if !ok {
|
|
|
|
update = make(map[string]interface{})
|
|
|
|
freezerUpdates[num] = update
|
|
|
|
}
|
|
|
|
update[kind] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func pluginCommitUpdate(num uint64) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting CommitUpdate, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginCommitUpdate(plugins.DefaultPluginLoader, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) {
|
2021-10-18 22:06:16 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2021-10-18 20:10:13 +00:00
|
|
|
if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }
|
2021-10-29 01:41:32 +00:00
|
|
|
min := ^uint64(0)
|
|
|
|
for i := range freezerUpdates{
|
2022-01-07 17:36:30 +00:00
|
|
|
if min > i { min = i }
|
2021-10-18 17:35:56 +00:00
|
|
|
}
|
2021-10-29 01:41:32 +00:00
|
|
|
for i := min ; i < num; i++ {
|
|
|
|
update, ok := freezerUpdates[i]
|
2022-01-07 19:54:47 +00:00
|
|
|
defer func(i uint64) { delete(freezerUpdates, i) }(i)
|
2021-10-29 01:41:32 +00:00
|
|
|
if !ok {
|
|
|
|
log.Warn("Attempting to commit untracked block", "num", i)
|
|
|
|
continue
|
2021-10-18 17:35:56 +00:00
|
|
|
}
|
2021-10-29 01:41:32 +00:00
|
|
|
fnList := pl.Lookup("ModifyAncients", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(uint64, map[string]interface{}))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func(uint64, map[string]interface{})); ok {
|
|
|
|
fn(i, update)
|
2021-10-18 17:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-22 18:48:26 +00:00
|
|
|
appendAncientFnList := pl.Lookup("AppendAncient", func(item interface{}) bool {
|
2021-10-29 01:41:32 +00:00
|
|
|
_, ok := item.(func(number uint64, hash, header, body, receipts, td []byte))
|
2022-08-22 18:48:26 +00:00
|
|
|
if ok { log.Warn("PluGeth's AppendAncient is deprecated. Please update to ModifyAncients.") }
|
2021-10-29 01:41:32 +00:00
|
|
|
return ok
|
|
|
|
})
|
2022-08-22 18:48:26 +00:00
|
|
|
if len(appendAncientFnList) > 0 {
|
|
|
|
var (
|
|
|
|
hash []byte
|
|
|
|
header []byte
|
|
|
|
body []byte
|
|
|
|
receipts []byte
|
|
|
|
td []byte
|
|
|
|
)
|
2023-02-22 18:01:43 +00:00
|
|
|
if hashi, ok := update[ChainFreezerHashTable]; ok {
|
2022-08-22 18:48:26 +00:00
|
|
|
switch v := hashi.(type) {
|
|
|
|
case []byte:
|
|
|
|
hash = v
|
|
|
|
default:
|
|
|
|
hash, _ = rlp.EncodeToBytes(v)
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 18:01:43 +00:00
|
|
|
if headeri, ok := update[ChainFreezerHeaderTable]; ok {
|
2022-08-22 18:48:26 +00:00
|
|
|
switch v := headeri.(type) {
|
|
|
|
case []byte:
|
|
|
|
header = v
|
|
|
|
default:
|
|
|
|
header, _ = rlp.EncodeToBytes(v)
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 18:01:43 +00:00
|
|
|
if bodyi, ok := update[ChainFreezerBodiesTable]; ok {
|
2022-08-22 18:48:26 +00:00
|
|
|
switch v := bodyi.(type) {
|
|
|
|
case []byte:
|
|
|
|
body = v
|
|
|
|
default:
|
|
|
|
body, _ = rlp.EncodeToBytes(v)
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 18:01:43 +00:00
|
|
|
if receiptsi, ok := update[ChainFreezerReceiptTable]; ok {
|
2022-08-22 18:48:26 +00:00
|
|
|
switch v := receiptsi.(type) {
|
|
|
|
case []byte:
|
|
|
|
receipts = v
|
|
|
|
default:
|
|
|
|
receipts, _ = rlp.EncodeToBytes(v)
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 18:01:43 +00:00
|
|
|
if tdi, ok := update[ChainFreezerDifficultyTable]; ok {
|
2022-08-22 18:48:26 +00:00
|
|
|
switch v := tdi.(type) {
|
|
|
|
case []byte:
|
|
|
|
td = v
|
|
|
|
default:
|
|
|
|
td, _ = rlp.EncodeToBytes(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fni := range appendAncientFnList {
|
|
|
|
if fn, ok := fni.(func(number uint64, hash, header, body, receipts, td []byte)); ok {
|
|
|
|
fn(i, hash, header, body, receipts, td)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 17:35:56 +00:00
|
|
|
}
|
2021-07-14 13:33:17 +00:00
|
|
|
}
|