Skip to content
Open
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
20 changes: 13 additions & 7 deletions erigon-lib/chain/hardfork_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chain
import (
"fmt"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/log/v3"
)

Expand All @@ -25,7 +26,7 @@ type XLayerForkConfig struct {

// Fork configurations
var ForkId13DencunConfig = XLayerForkConfig{
MainnetBlock: 41191800,
MainnetBlock: 1000000000000, // TODO, need to be updated
TestnetBlock: 7953000,
DevnetBlock: 30,
}
Expand All @@ -36,22 +37,27 @@ var forkConfigs = map[ForkId]XLayerForkConfig{
// Quickly add new fork configurations
}

// Network identification by zkevm address
var zkevmAddressNetworkMap = map[string]NetworkType{
"0x2B0ee28D4D51bC9aDde5E58E295873F61F4a0507": MainnetNetwork, // Mainnet
"0x7b1472be9a0115c3076b9f30e6bab91b13b3be6b": TestnetNetwork, // Testnet
"0xE45CCD0757670580a4a3600DE5cef1e45F0Ec2bd": LocalNetwork, // Local
// Network identification by zkevm address using common.Address type
var zkevmAddressNetworkMap = map[libcommon.Address]NetworkType{
libcommon.HexToAddress("0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507"): MainnetNetwork, // Mainnet
libcommon.HexToAddress("0x7b1472be9a0115c3076b9f30e6bab91b13b3be6b"): TestnetNetwork, // Testnet
libcommon.HexToAddress("0xe45ccd0757670580a4a3600de5cef1e45f0ec2bd"): LocalNetwork, // Local
}

// Global state
var currentNetwork NetworkType = UnknownNetwork

// InitializeNetworkByZkevmAddress sets the current network based on zkevm address
// Uses common.Address type for proper address comparison
func InitializeNetworkByZkevmAddress(zkevmAddr string) {
if network, exists := zkevmAddressNetworkMap[zkevmAddr]; exists {
// Parse string to Address type (handles case-insensitive comparison automatically)
addr := libcommon.HexToAddress(zkevmAddr)

if network, exists := zkevmAddressNetworkMap[addr]; exists {
currentNetwork = network
} else {
currentNetwork = UnknownNetwork
log.Error(fmt.Sprintf("Unknown network: %v", zkevmAddr))
}
log.Info(fmt.Sprintf("Current network: %v, zkevmAddr: %v", currentNetwork, zkevmAddr))
for key, _ := range forkConfigs {
Expand Down
72 changes: 53 additions & 19 deletions erigon-lib/chain/hardfork_xlayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chain

import (
"testing"

libcommon "github.com/ledgerwatch/erigon-lib/common"
)

// Test NetworkType constants
Expand Down Expand Up @@ -52,8 +54,8 @@ func TestForkId13DencunConfig(t *testing.T) {
if config.MainnetBlock != 1000000000000 {
t.Errorf("ForkId13DencunConfig.MainnetBlock = %d, want 1000000000000", config.MainnetBlock)
}
if config.TestnetBlock != 1000000000000 {
t.Errorf("ForkId13DencunConfig.TestnetBlock = %d, want 1000000000000", config.TestnetBlock)
if config.TestnetBlock != 7953000 {
t.Errorf("ForkId13DencunConfig.TestnetBlock = %d, want 7953000", config.TestnetBlock)
}
if config.DevnetBlock != 30 {
t.Errorf("ForkId13DencunConfig.DevnetBlock = %d, want 30", config.DevnetBlock)
Expand All @@ -72,8 +74,8 @@ func TestForkConfigsRegistry(t *testing.T) {
if config.MainnetBlock != 1000000000000 {
t.Errorf("forkConfigs[ForkId13Dencun].MainnetBlock = %d, want 1000000000000", config.MainnetBlock)
}
if config.TestnetBlock != 1000000000000 {
t.Errorf("forkConfigs[ForkId13Dencun].TestnetBlock = %d, want 1000000000000", config.TestnetBlock)
if config.TestnetBlock != 7953000 {
t.Errorf("forkConfigs[ForkId13Dencun].TestnetBlock = %d, want 7953000", config.TestnetBlock)
}
if config.DevnetBlock != 30 {
t.Errorf("forkConfigs[ForkId13Dencun].DevnetBlock = %d, want 30", config.DevnetBlock)
Expand All @@ -87,14 +89,15 @@ func TestZkevmAddressNetworkMap(t *testing.T) {
address string
network NetworkType
}{
{"Mainnet", "0x2B0ee28D4D51bC9aDde5E58E295873F61F4a0507", MainnetNetwork},
{"Mainnet", "0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507", MainnetNetwork},
{"Testnet", "0x7b1472be9a0115c3076b9f30e6bab91b13b3be6b", TestnetNetwork},
{"Local", "0xE45CCD0757670580a4a3600DE5cef1e45F0Ec2bd", LocalNetwork},
{"Local", "0xe45ccd0757670580a4a3600de5cef1e45f0ec2bd", LocalNetwork},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
network, exists := zkevmAddressNetworkMap[tt.address]
addr := libcommon.HexToAddress(tt.address)
network, exists := zkevmAddressNetworkMap[addr]
if !exists {
t.Errorf("Address %s should exist in zkevmAddressNetworkMap", tt.address)
}
Expand All @@ -105,6 +108,36 @@ func TestZkevmAddressNetworkMap(t *testing.T) {
}
}

// Test case-insensitive address comparison
func TestInitializeNetworkByZkevmAddressCaseInsensitive(t *testing.T) {
tests := []struct {
name string
inputAddress string
expected NetworkType
}{
{"Mainnet - lowercase", "0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507", MainnetNetwork},
{"Mainnet - uppercase", "0x2B0EE28D4D51BC9ADDE5E58E295873F61F4A0507", MainnetNetwork},
{"Mainnet - mixed case", "0x2B0ee28D4D51bC9aDde5E58E295873F61F4a0507", MainnetNetwork},
{"Testnet - lowercase", "0x7b1472be9a0115c3076b9f30e6bab91b13b3be6b", TestnetNetwork},
{"Testnet - uppercase", "0x7B1472BE9A0115C3076B9F30E6BAB91B13B3BE6B", TestnetNetwork},
{"Local - mixed case", "0xE45CCD0757670580a4a3600DE5cef1e45F0Ec2bd", LocalNetwork},
{"Unknown address", "0x1234567890123456789012345678901234567890", UnknownNetwork},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset currentNetwork before each test
currentNetwork = UnknownNetwork

InitializeNetworkByZkevmAddress(tt.inputAddress)

if currentNetwork != tt.expected {
t.Errorf("InitializeNetworkByZkevmAddress(%s) = %d, want %d", tt.inputAddress, currentNetwork, tt.expected)
}
})
}
}

// Test InitializeNetworkByZkevmAddress function
func TestInitializeNetworkByZkevmAddress(t *testing.T) {
// Save original state
Expand All @@ -118,9 +151,9 @@ func TestInitializeNetworkByZkevmAddress(t *testing.T) {
zkevmAddr string
expectedNetwork NetworkType
}{
{"Mainnet Address", "0x2B0ee28D4D51bC9aDde5E58E295873F61F4a0507", MainnetNetwork},
{"Mainnet Address", "0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507", MainnetNetwork},
{"Testnet Address", "0x7b1472be9a0115c3076b9f30e6bab91b13b3be6b", TestnetNetwork},
{"Local Address", "0xE45CCD0757670580a4a3600DE5cef1e45F0Ec2bd", LocalNetwork},
{"Local Address", "0xe45ccd0757670580a4a3600de5cef1e45f0ec2bd", LocalNetwork},
{"Unknown Address", "0x1234567890123456789012345678901234567890", UnknownNetwork},
{"Empty Address", "", UnknownNetwork},
{"Invalid Address", "invalid_address", UnknownNetwork},
Expand Down Expand Up @@ -152,7 +185,7 @@ func TestGetForkBlock(t *testing.T) {
expectedBlock uint64
}{
{"Mainnet ForkId13Dencun", MainnetNetwork, ForkId13Dencun, 1000000000000},
{"Testnet ForkId13Dencun", TestnetNetwork, ForkId13Dencun, 1000000000000},
{"Testnet ForkId13Dencun", TestnetNetwork, ForkId13Dencun, 7953000},
{"Local ForkId13Dencun", LocalNetwork, ForkId13Dencun, 30},
{"Unknown Network ForkId13Dencun", UnknownNetwork, ForkId13Dencun, 0},
}
Expand Down Expand Up @@ -241,9 +274,9 @@ func TestInitializeNetworkByZkevmAddressEdgeCases(t *testing.T) {
zkevmAddr string
expectedNetwork NetworkType
}{
{"Lowercase mainnet", "0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507", UnknownNetwork}, // Case sensitive
{"Uppercase mainnet", "0X2B0EE28D4D51BC9ADDE5E58E295873F61F4A0507", UnknownNetwork}, // Case sensitive
{"Without 0x prefix", "2B0ee28D4D51bC9aDde5E58E295873F61F4a0507", UnknownNetwork},
{"Lowercase mainnet", "0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507", MainnetNetwork}, // Case insensitive now
{"Uppercase mainnet", "0X2B0EE28D4D51BC9ADDE5E58E295873F61F4A0507", MainnetNetwork}, // Case insensitive now
{"Without 0x prefix", "2B0ee28D4D51bC9aDde5E58E295873F61F4a0507", MainnetNetwork}, // HexToAddress handles this
{"With extra characters", "0x2B0ee28D4D51bC9aDde5E58E295873F61F4a0507x", UnknownNetwork},
{"Short address", "0x2B0ee28D4D51bC9aDde5E58E295873F61F4a050", UnknownNetwork},
{"Long address", "0x2B0ee28D4D51bC9aDde5E58E295873F61F4a05077", UnknownNetwork},
Expand Down Expand Up @@ -280,7 +313,7 @@ func TestGetForkBlockAllNetworks(t *testing.T) {
case MainnetNetwork:
expectedBlock = 1000000000000
case TestnetNetwork:
expectedBlock = 1000000000000
expectedBlock = 7953000
case LocalNetwork:
expectedBlock = 30
default:
Expand Down Expand Up @@ -412,16 +445,17 @@ func TestZkevmAddressNetworkMapImmutability(t *testing.T) {
t.Errorf("zkevmAddressNetworkMap size = %d, want %d", len(zkevmAddressNetworkMap), expectedSize)
}

// Check all expected addresses exist
// Check all expected addresses exist (all in lowercase)
expectedAddresses := []string{
"0x2B0ee28D4D51bC9aDde5E58E295873F61F4a0507",
"0x2b0ee28d4d51bc9adde5e58e295873f61f4a0507",
"0x7b1472be9a0115c3076b9f30e6bab91b13b3be6b",
"0xE45CCD0757670580a4a3600DE5cef1e45F0Ec2bd",
"0xe45ccd0757670580a4a3600de5cef1e45f0ec2bd",
}

for _, addr := range expectedAddresses {
for _, addrStr := range expectedAddresses {
addr := libcommon.HexToAddress(addrStr)
if _, exists := zkevmAddressNetworkMap[addr]; !exists {
t.Errorf("Expected address %s not found in zkevmAddressNetworkMap", addr)
t.Errorf("Expected address %s not found in zkevmAddressNetworkMap", addrStr)
}
}
}
Expand Down
Loading