Modified injection test in /core/rawdb/

This commit is contained in:
philip-morlier 2023-06-22 13:00:05 -07:00
parent b3f1d35171
commit 4ca9210d59
3 changed files with 50 additions and 47 deletions

View File

@ -45,9 +45,6 @@ func newFreezerBatch(f *Freezer) *freezerBatch {
func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error { func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error {
// begin PluGeth injection // begin PluGeth injection
PluginTrackUpdate(num, kind, item) PluginTrackUpdate(num, kind, item)
if injectionCalled != nil {
return nil
}
// end PluGeth injection // end PluGeth injection
return batch.tables[kind].Append(num, item) 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 { func (batch *freezerBatch) AppendRaw(kind string, num uint64, item []byte) error {
// being PluGeth injection // being PluGeth injection
PluginTrackUpdate(num, kind, item) PluginTrackUpdate(num, kind, item)
if injectionCalled != nil {
return nil
}
// end PluGeth injection // end PluGeth injection
return batch.tables[kind].AppendRaw(num, item) return batch.tables[kind].AppendRaw(num, item)
} }

View File

@ -3,48 +3,48 @@ package rawdb
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"math/big"
"github.com/ethereum/go-ethereum/ethdb" "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)
t.Run(fmt.Sprintf("test plugeth injections"), func(t *testing.T) {
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 called := false
injectionCalled = &called modifyAncientsInjection = &called
_, _ = f.ModifyAncients(func (ethdb.AncientWriteOp) error {return nil})
if *injectionCalled != true { _, _ = 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") 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")
}
})
} }

View File

@ -2,23 +2,31 @@ package rawdb
import ( import (
"sync"
"github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"sync"
) )
var ( var (
freezerUpdates map[uint64]map[string]interface{} freezerUpdates map[uint64]map[string]interface{}
lock sync.Mutex lock sync.Mutex
injectionCalled *bool modifyAncientsInjection *bool
appendRawInjection *bool
appendInjection *bool
) )
func PluginTrackUpdate(num uint64, kind string, value interface{}) { func PluginTrackUpdate(num uint64, kind string, value interface{}) {
if injectionCalled != nil { if appendRawInjection != nil {
called := true called := true
injectionCalled = &called appendRawInjection = &called
}
if appendInjection != nil {
called := true
appendInjection = &called
} }
lock.Lock() lock.Lock()
@ -33,10 +41,10 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) {
} }
func pluginCommitUpdate(num uint64) { func pluginCommitUpdate(num uint64) {
if injectionCalled != nil { if modifyAncientsInjection != nil {
called := true called := true
injectionCalled = &called modifyAncientsInjection = &called
} }
if plugins.DefaultPluginLoader == nil { if plugins.DefaultPluginLoader == nil {
@ -47,6 +55,7 @@ func pluginCommitUpdate(num uint64) {
} }
func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) { 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{}) }