diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index 762fa8f25f19..563d8442c95d 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -115,6 +115,11 @@ func (batch *freezerTableBatch) reset() { // precautionary parameter to ensure data correctness, but the table will reject already // existing data. func (batch *freezerTableBatch) Append(item uint64, data interface{}) error { + // Sanity check. In principle this method shouldn't be called at all + // in readonly mode. + if batch.t.readonly { + return fmt.Errorf("permission error: table in readonly mode") + } if item != batch.curItem { return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem) } @@ -135,6 +140,11 @@ func (batch *freezerTableBatch) Append(item uint64, data interface{}) error { // precautionary parameter to ensure data correctness, but the table will reject already // existing data. func (batch *freezerTableBatch) AppendRaw(item uint64, blob []byte) error { + // Sanity check. In principle this method shouldn't be called at all + // in readonly mode. + if batch.t.readonly { + return fmt.Errorf("permission error: table in readonly mode") + } if item != batch.curItem { return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem) } diff --git a/core/rawdb/freezer_table_test.go b/core/rawdb/freezer_table_test.go index 6f1885583aad..925b33884ba8 100644 --- a/core/rawdb/freezer_table_test.go +++ b/core/rawdb/freezer_table_test.go @@ -903,13 +903,12 @@ func TestFreezerReadonlyBasics(t *testing.T) { if !bytes.Equal(v, exp) { t.Errorf("retrieved value is incorrect") } - // Now write some data. This should fail either during AppendRaw or Commit + // Now write some data. Append/AppendRaw should fail. batch := f.newBatch() - writeErr := batch.AppendRaw(32, make([]byte, 1)) - if writeErr == nil { - writeErr = batch.commit() + if err := batch.AppendRaw(32, make([]byte, 1)); err == nil { + t.Errorf("Writing to readonly table should fail") } - if writeErr == nil { - t.Fatalf("Writing to readonly table should fail") + if err := batch.Append(0, make([]byte, 1)); err == nil { + t.Errorf("Writing to readonly table should fail") } }