diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index bb80da8e6..c5dac62e2 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -45,9 +45,6 @@ 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) } @@ -56,9 +53,6 @@ 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_injection_test.go b/core/rawdb/plugeth_injection_test.go index 4fb6d1560..7ab47f9eb 100644 --- a/core/rawdb/plugeth_injection_test.go +++ b/core/rawdb/plugeth_injection_test.go @@ -3,48 +3,48 @@ package rawdb import ( "fmt" - "os" "testing" + "math/big" + "github.com/ethereum/go-ethereum/ethdb" ) +func TestPlugethInjections(t *testing.T) { + var valuesRaw [][]byte + var valuesRLP []*big.Int + for x := 0; x < 100; x++ { + v := getChunk(256, x) + valuesRaw = append(valuesRaw, v) + iv := big.NewInt(int64(x)) + iv = iv.Exp(iv, iv, nil) + valuesRLP = append(valuesRLP, iv) + } + tables := map[string]bool{"raw": true, "rlp": false} + f, _ := newFreezerForTesting(t, tables) - -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) { + t.Run(fmt.Sprintf("test plugeth injections"), func(t *testing.T) { called := false - injectionCalled = &called - _, _ = f.ModifyAncients(func (ethdb.AncientWriteOp) error {return nil}) - if *injectionCalled != true { + modifyAncientsInjection = &called + + _, _ = f.ModifyAncients(func(op ethdb.AncientWriteOp) error { + + appendRawInjection = &called + _ = op.AppendRaw("raw", uint64(0), valuesRaw[0]) + if *appendRawInjection != true { + t.Fatalf("pluginTrackUpdate injection in AppendRaw not called") + } + + appendInjection = &called + _ = op.Append("rlp", uint64(0), valuesRaw[0]) + if *appendInjection != true { + t.Fatalf("pluginTrackUpdate injection in Append not called") + } + + return nil + }) + if *modifyAncientsInjection != 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 83a7aa9a6..e4635e4a6 100644 --- a/core/rawdb/plugin_hooks.go +++ b/core/rawdb/plugin_hooks.go @@ -2,23 +2,31 @@ package rawdb import ( + "sync" + "github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" - "sync" ) var ( freezerUpdates map[uint64]map[string]interface{} lock sync.Mutex - injectionCalled *bool + modifyAncientsInjection *bool + appendRawInjection *bool + appendInjection *bool ) func PluginTrackUpdate(num uint64, kind string, value interface{}) { - if injectionCalled != nil { + if appendRawInjection != nil { called := true - injectionCalled = &called + appendRawInjection = &called + } + + if appendInjection != nil { + called := true + appendInjection = &called } lock.Lock() @@ -33,10 +41,10 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) { } func pluginCommitUpdate(num uint64) { - - if injectionCalled != nil { + + if modifyAncientsInjection != nil { called := true - injectionCalled = &called + modifyAncientsInjection = &called } if plugins.DefaultPluginLoader == nil { @@ -47,6 +55,7 @@ func pluginCommitUpdate(num uint64) { } func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) { + lock.Lock() defer lock.Unlock() if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }