Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions internal/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blockchain

import (
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -17,21 +18,29 @@ type Event interface {
}

var (
events = make([]common.Hash, 0)
contracts = make([]common.Address, 0)
EventMap = make(map[common.Hash][]Event, 0)
EIP1155 = make([]common.Address, 0)
events = make([]common.Hash, 0)
contracts = make([]common.Address, 0)
contractsMu sync.RWMutex // Protects concurrent access to contracts slice
initOnce sync.Once // Ensures initialization only runs once
EventMap = make(map[common.Hash][]Event, 0)
EIP1155 = make([]common.Address, 0)
)

func init() {
Register(&event.DisputeGameCreated{})
Register(&event.DisputeGameMove{})
Register(&event.DisputeGameResolved{})
cfg := config.GetConfig()
disputeGameProxys := strings.Split(cfg.DisputeGameProxyContract, ",")
for _, one := range disputeGameProxys {
AddContract(one)
}
}

// InitContracts initializes contract addresses (runs only once)
func InitContracts() {
initOnce.Do(func() {
cfg := config.GetConfig()
disputeGameProxys := strings.Split(cfg.DisputeGameProxyContract, ",")
for _, one := range disputeGameProxys {
AddContract(one)
}
})
}

func Register(event Event) {
Expand All @@ -42,19 +51,40 @@ func Register(event Event) {
}

func AddContract(contract string) {
contracts = append(contracts, common.HexToAddress(contract))
contractsMu.Lock()
defer contractsMu.Unlock()

addr := common.HexToAddress(contract)
// Check if already exists to prevent duplicate additions
for _, existing := range contracts {
if existing == addr {
return // Already exists, return directly
}
}
contracts = append(contracts, addr)
}

func RemoveContract(contract string) {
for index, ct := range contracts {
if ct == common.HexToAddress(contract) {
contracts = append(contracts[:index], contracts[index+1:]...)
contractsMu.Lock()
defer contractsMu.Unlock()

addr := common.HexToAddress(contract)
// Use reverse iteration to safely remove all matching contracts
for i := len(contracts) - 1; i >= 0; i-- {
if contracts[i] == addr {
contracts = append(contracts[:i], contracts[i+1:]...)
}
}
}

func GetContracts() []common.Address {
return contracts
contractsMu.RLock()
defer contractsMu.RUnlock()

// Return a copy to prevent external modifications
result := make([]common.Address, len(contracts))
copy(result, contracts)
return result
}

func GetEvents() []common.Hash {
Expand Down
3 changes: 3 additions & 0 deletions internal/handler/syncEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (

func SyncEvent(ctx *svc.ServiceContext) {
log.Infof("Initializes the monitored contract address...\n")
// Ensure basic contract initialization
blockchain.InitContracts()
// Initialize monitored contracts
initMonitoredContract(ctx)
for {
var blocks []schema.SyncBlock
Expand Down
Loading