From 27810f60adc25b872c2100cda9cf29c04114090c Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Wed, 8 Mar 2023 09:59:47 -0800 Subject: [PATCH] Added hook into trie.Database.Commit() in the trie package. The hook enables a pre and post commit plugin to report on the status of the commit and expose this information to plugins. --- trie/database.go | 9 +++++++++ trie/plugin_hooks.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 trie/plugin_hooks.go diff --git a/trie/database.go b/trie/database.go index 895ffdf89..5b55293b2 100644 --- a/trie/database.go +++ b/trie/database.go @@ -637,6 +637,11 @@ func (db *Database) Commit(node common.Hash, report bool) error { // outside code doesn't see an inconsistent state (referenced data removed from // memory cache during commit but not yet in persistent storage). This is ensured // by only uncaching existing data when the database write finalizes. + + // begin PluGeth injection + pluginPreTrieCommit(node) + // end PluGeth injection + start := time.Now() batch := db.diskdb.NewBatch() @@ -683,6 +688,10 @@ func (db *Database) Commit(node common.Hash, report bool) error { db.gcnodes, db.gcsize, db.gctime = 0, 0, 0 db.flushnodes, db.flushsize, db.flushtime = 0, 0, 0 + // begin PluGeth injection + pluginPostTrieCommit(node) + // end PluGeth injection + return nil } diff --git a/trie/plugin_hooks.go b/trie/plugin_hooks.go new file mode 100644 index 000000000..478b7f126 --- /dev/null +++ b/trie/plugin_hooks.go @@ -0,0 +1,46 @@ +package trie + +import ( + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/plugins" +) + +func PluginPreTrieCommit(pl *plugins.PluginLoader, node common.Hash) { + fnList := pl.Lookup("PreTrieCommit", func(item interface{}) bool { + _, ok := item.(func(common.Hash)) + return ok + }) + for _, fni := range fnList { + if fn, ok := fni.(func(common.Hash)); ok { + fn(node) + } + } +} + +func pluginPreTrieCommit(node common.Hash,) { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting PreTrieCommit, but default PluginLoader has not been initialized") + return + } + PluginPreTrieCommit(plugins.DefaultPluginLoader, node) +} + +func PluginPostTrieCommit(pl *plugins.PluginLoader, node common.Hash) { + fnList := pl.Lookup("PostTrieCommit", func(item interface{}) bool { + _, ok := item.(func(common.Hash)) + return ok + }) + for _, fni := range fnList { + if fn, ok := fni.(func(common.Hash)); ok { + fn(node) + } + } +} + +func pluginPostTrieCommit(node common.Hash,) { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting PostTrieCommit, but default PluginLoader has not been initialized") + return + } + PluginPostTrieCommit(plugins.DefaultPluginLoader, node) +} \ No newline at end of file