Merge pull request #12 from openrelayxyz/updates/1.10.10-support

Use locks to prevent concurrent map accesses
This commit is contained in:
philip-morlier 2021-10-18 16:32:25 -07:00 committed by GitHub
commit df8b824e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,13 +5,17 @@ import (
"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
) )
func PluginTrackUpdate(num uint64, kind string, value interface{}) { func PluginTrackUpdate(num uint64, kind string, value interface{}) {
lock.Lock()
defer lock.Unlock()
if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) } if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }
update, ok := freezerUpdates[num] update, ok := freezerUpdates[num]
if !ok { if !ok {
@ -21,11 +25,6 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) {
update[kind] = value update[kind] = value
} }
func PluginResetUpdate(num uint64) {
delete(freezerUpdates, num)
}
func pluginCommitUpdate(num uint64) { func pluginCommitUpdate(num uint64) {
if plugins.DefaultPluginLoader == nil { if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting CommitUpdate, but default PluginLoader has not been initialized") log.Warn("Attempting CommitUpdate, but default PluginLoader has not been initialized")
@ -35,6 +34,8 @@ func pluginCommitUpdate(num uint64) {
} }
func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) { func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) {
lock.Lock()
defer lock.Unlock()
if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) } if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) }
defer func() { delete(freezerUpdates, num) }() defer func() { delete(freezerUpdates, num) }()
update, ok := freezerUpdates[num] update, ok := freezerUpdates[num]