Skip to content

Commit 2baaa72

Browse files
committed
fix: make ConfigCompatError.Error more robust and add UT
1 parent a40e8e3 commit 2baaa72

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

params/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,11 @@ func (err *ConfigCompatError) Error() string {
891891
return fmt.Sprintf("mismatching %s in database (have block %d, want block %d, rewindto block %d)", err.What, err.StoredBlock, err.NewBlock, err.RewindToBlock)
892892
}
893893

894-
if err.StoredTime == nil {
894+
if err.StoredTime == nil && err.NewTime == nil {
895+
return ""
896+
} else if err.StoredTime == nil && err.NewTime != nil {
895897
return fmt.Sprintf("mismatching %s in database (have timestamp nil, want timestamp %d, rewindto timestamp %d)", err.What, *err.NewTime, err.RewindToTime)
896-
} else if err.NewTime == nil {
898+
} else if err.StoredTime != nil && err.NewTime == nil {
897899
return fmt.Sprintf("mismatching %s in database (have timestamp %d, want timestamp nil, rewindto timestamp %d)", err.What, *err.StoredTime, err.RewindToTime)
898900
}
899901
return fmt.Sprintf("mismatching %s in database (have timestamp %d, want timestamp %d, rewindto timestamp %d)", err.What, *err.StoredTime, *err.NewTime, err.RewindToTime)

params/config_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/ethereum/go-ethereum/common/math"
26+
"github.com/stretchr/testify/require"
2627
)
2728

2829
func TestCheckCompatible(t *testing.T) {
@@ -137,3 +138,20 @@ func TestConfigRules(t *testing.T) {
137138
t.Errorf("expected %v to be shanghai", stamp)
138139
}
139140
}
141+
142+
func TestTimestampCompatError(t *testing.T) {
143+
require.Equal(t, new(ConfigCompatError).Error(), "")
144+
145+
errWhat := "Shanghai fork timestamp"
146+
require.Equal(t, newTimestampCompatError(errWhat, nil, newUint64(1681338455)).Error(),
147+
"mismatching Shanghai fork timestamp in database (have timestamp nil, want timestamp 1681338455, rewindto timestamp 1681338454)")
148+
149+
require.Equal(t, newTimestampCompatError(errWhat, newUint64(1681338455), nil).Error(),
150+
"mismatching Shanghai fork timestamp in database (have timestamp 1681338455, want timestamp nil, rewindto timestamp 1681338454)")
151+
152+
require.Equal(t, newTimestampCompatError(errWhat, newUint64(1681338455), newUint64(600624000)).Error(),
153+
"mismatching Shanghai fork timestamp in database (have timestamp 1681338455, want timestamp 600624000, rewindto timestamp 600623999)")
154+
155+
require.Equal(t, newTimestampCompatError(errWhat, newUint64(0), newUint64(1681338455)).Error(),
156+
"mismatching Shanghai fork timestamp in database (have timestamp 0, want timestamp 1681338455, rewindto timestamp 0)")
157+
}

0 commit comments

Comments
 (0)