From 679d02848440c87fd2e27a75c71ababef1a86cfa Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 1 Jun 2023 13:14:08 -0700 Subject: [PATCH] Functional test for injections in core/rawdb --- core/rawdb/freezer_batch.go | 6 ++++ core/rawdb/plugeth_hook_test.go | 50 +++++++++++++++++++++++++++++++++ core/rawdb/plugin_hooks.go | 13 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 core/rawdb/plugeth_hook_test.go diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index 99d226e9a..8189cecff 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -46,6 +46,9 @@ func newFreezerBatch(f *Freezer) *freezerBatch { func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error { // begin PluGeth injection PluginTrackUpdate(num, kind, item) + if injectionCalled != nil { + return nil + } // end PluGeth injection return batch.tables[kind].Append(num, item) } @@ -54,6 +57,9 @@ func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) err func (batch *freezerBatch) AppendRaw(kind string, num uint64, item []byte) error { // being PluGeth injection PluginTrackUpdate(num, kind, item) + if injectionCalled != nil { + return nil + } // end PluGeth injection return batch.tables[kind].AppendRaw(num, item) } diff --git a/core/rawdb/plugeth_hook_test.go b/core/rawdb/plugeth_hook_test.go new file mode 100644 index 000000000..4fb6d1560 --- /dev/null +++ b/core/rawdb/plugeth_hook_test.go @@ -0,0 +1,50 @@ +package rawdb + + +import ( + "fmt" + "os" + "testing" + "github.com/ethereum/go-ethereum/ethdb" +) + + + + +func TestAncientsInjections(t *testing.T) { + + test_dir_path := "./injection_test_dir" + f, _ := NewFreezer(test_dir_path, "plugeth hook test", false, uint32(0), map[string]bool{"test": false}) + + t.Run(fmt.Sprintf("test ModifyAncients"), func(t *testing.T) { + called := false + injectionCalled = &called + _, _ = f.ModifyAncients(func (ethdb.AncientWriteOp) error {return nil}) + if *injectionCalled != true { + t.Fatalf("pluginCommitUpdate injection in ModifyAncients not called") + } + }) + + os.RemoveAll(test_dir_path) + + fb := newFreezerBatch(f) + + t.Run(fmt.Sprintf("test Append"), func(t *testing.T) { + var item interface{} + called := false + injectionCalled = &called + _ = fb.Append("kind", uint64(0), item) + if *injectionCalled != true { + t.Fatalf("PluginTrackUpdate injection in Append not called") + } + }) + + t.Run(fmt.Sprintf("test AppendRaw"), func(t *testing.T) { + called := false + injectionCalled = &called + _ = fb.AppendRaw("kind", uint64(100), []byte{}) + if *injectionCalled != true { + t.Fatalf("PluginTrackUpdate injection in AppendRaw not called") + } + }) +} \ No newline at end of file diff --git a/core/rawdb/plugin_hooks.go b/core/rawdb/plugin_hooks.go index 98fd27a2e..83a7aa9a6 100644 --- a/core/rawdb/plugin_hooks.go +++ b/core/rawdb/plugin_hooks.go @@ -11,9 +11,16 @@ import ( var ( freezerUpdates map[uint64]map[string]interface{} lock sync.Mutex + injectionCalled *bool ) func PluginTrackUpdate(num uint64, kind string, value interface{}) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + lock.Lock() defer lock.Unlock() if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) } @@ -26,6 +33,12 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) { } func pluginCommitUpdate(num uint64) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + if plugins.DefaultPluginLoader == nil { log.Warn("Attempting CommitUpdate, but default PluginLoader has not been initialized") return