@@ -86,6 +86,22 @@ func (f *chainFreezer) Close() error {
86
86
return f .AncientStore .Close ()
87
87
}
88
88
89
+ // readHeadNumber returns the number of chain head block. 0 is returned if the
90
+ // block is unknown or not available yet.
91
+ func (f * chainFreezer ) readHeadNumber (db ethdb.KeyValueReader ) uint64 {
92
+ hash := ReadHeadBlockHash (db )
93
+ if hash == (common.Hash {}) {
94
+ log .Error ("Head block is not reachable" )
95
+ return 0
96
+ }
97
+ number := ReadHeaderNumber (db , hash )
98
+ if number == nil {
99
+ log .Error ("Number of head block is missing" )
100
+ return 0
101
+ }
102
+ return * number
103
+ }
104
+
89
105
// readFinalizedNumber returns the number of finalized block. 0 is returned
90
106
// if the block is unknown or not available yet.
91
107
func (f * chainFreezer ) readFinalizedNumber (db ethdb.KeyValueReader ) uint64 {
@@ -103,10 +119,20 @@ func (f *chainFreezer) readFinalizedNumber(db ethdb.KeyValueReader) uint64 {
103
119
104
120
// freezeThreshold returns the threshold for chain freezing.
105
121
func (f * chainFreezer ) freezeThreshold (db ethdb.KeyValueReader ) (uint64 , error ) {
106
- final := f .readFinalizedNumber (db )
107
- if final == 0 {
122
+ var (
123
+ head = f .readHeadNumber (db )
124
+ final = f .readFinalizedNumber (db )
125
+ )
126
+
127
+ if final == 0 || (head == final && final == 1 ) {
108
128
return 0 , errors .New ("freezing threshold is not available" )
109
129
}
130
+
131
+ // avoids freezing the head of the chain, since freezing removes the block from the active database
132
+ if head == final {
133
+ final --
134
+ }
135
+
110
136
return final , nil
111
137
}
112
138
0 commit comments