Skip to content

Commit 53f09ce

Browse files
committed
refactor(heightindexdb): remove safety of byte slice modifications in Put and Get
1 parent 2a10c52 commit 53f09ce

File tree

5 files changed

+7
-61
lines changed

5 files changed

+7
-61
lines changed

database/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ type HeightIndex interface {
102102
// If value is nil or an empty slice, then when it's retrieved it may be nil
103103
// or an empty slice.
104104
//
105-
// value is safe to read and modify after calling Put.
105+
// The provided value is not safe to modify after calling Put.
106106
Put(height uint64, value []byte) error
107107

108108
// Get retrieves a value by its height.
109109
// Returns [ErrNotFound] if the key is not present in the database.
110110
//
111-
// Returned []byte is safe to read and modify after calling Get.
111+
// The returned byte slice is not safe to modify.
112112
Get(height uint64) ([]byte, error)
113113

114114
// Has checks if a value exists at the given height.

database/heightindexdb/dbtest/dbtest.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,10 @@ func TestPutGet(t *testing.T, newDB func() database.HeightIndex) {
9999
require.NoError(t, db.Put(write.height, write.data))
100100
}
101101

102-
// modify the original value of the put data to ensure the saved
103-
// value won't be changed after Get
104-
if len(tt.puts) > int(tt.queryHeight) && tt.puts[tt.queryHeight].data != nil {
105-
copy(tt.puts[tt.queryHeight].data, []byte("modified data"))
106-
}
107-
108102
// Query the specific height
109103
retrievedData, err := db.Get(tt.queryHeight)
110104
require.ErrorIs(t, err, tt.wantErr)
111105
require.True(t, bytes.Equal(tt.want, retrievedData))
112-
113-
// modify the data returned from Get and ensure it won't change the
114-
// data from a second Get
115-
copy(retrievedData, []byte("modified data"))
116-
newData, err := db.Get(tt.queryHeight)
117-
require.ErrorIs(t, err, tt.wantErr)
118-
require.True(t, bytes.Equal(tt.want, newData))
119106
})
120107
}
121108
}

database/heightindexdb/memdb/database.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package memdb
55

66
import (
7-
"slices"
87
"sync"
98

109
"github.com/ava-labs/avalanchego/database"
@@ -31,7 +30,7 @@ func (db *Database) Put(height uint64, data []byte) error {
3130
db.data = make(map[uint64][]byte)
3231
}
3332

34-
db.data[height] = slices.Clone(data)
33+
db.data[height] = data
3534
return nil
3635
}
3736

@@ -48,7 +47,7 @@ func (db *Database) Get(height uint64) ([]byte, error) {
4847
return nil, database.ErrNotFound
4948
}
5049

51-
return slices.Clone(data), nil
50+
return data, nil
5251
}
5352

5453
func (db *Database) Has(height uint64) (bool, error) {

x/blockdb/cache_db.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package blockdb
55

66
import (
7-
"slices"
87
"sync"
98

109
"go.uber.org/zap"
@@ -46,13 +45,13 @@ func (c *cacheDB) Get(height BlockHeight) (BlockData, error) {
4645
}
4746

4847
if cached, ok := c.cache.Get(height); ok {
49-
return slices.Clone(cached), nil
48+
return cached, nil
5049
}
5150
data, err := c.db.Get(height)
5251
if err != nil {
5352
return nil, err
5453
}
55-
c.cache.Put(height, slices.Clone(data))
54+
c.cache.Put(height, data)
5655
return data, nil
5756
}
5857

@@ -69,7 +68,7 @@ func (c *cacheDB) Put(height BlockHeight, data BlockData) error {
6968
return err
7069
}
7170

72-
c.cache.Put(height, slices.Clone(data))
71+
c.cache.Put(height, data)
7372
return nil
7473
}
7574

x/blockdb/cache_db_test.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package blockdb
55

66
import (
7-
"slices"
87
"testing"
98

109
"github.com/stretchr/testify/require"
@@ -57,44 +56,6 @@ func TestCacheHas(t *testing.T) {
5756
require.True(t, has)
5857
}
5958

60-
func TestCachePutStoresClone(t *testing.T) {
61-
db := newCacheDatabase(t, DefaultConfig())
62-
height := uint64(40)
63-
block := randomBlock(t)
64-
clone := slices.Clone(block)
65-
require.NoError(t, db.Put(height, clone))
66-
67-
// Modify the original block after Put
68-
clone[0] = 99
69-
70-
// Cache should have the original unmodified data
71-
cached, ok := db.cache.Get(height)
72-
require.True(t, ok)
73-
require.Equal(t, block, cached)
74-
}
75-
76-
func TestCacheGetReturnsClone(t *testing.T) {
77-
db := newCacheDatabase(t, DefaultConfig())
78-
height := uint64(50)
79-
block := randomBlock(t)
80-
require.NoError(t, db.Put(height, block))
81-
82-
// Get the block and modify the returned data
83-
data, err := db.Get(height)
84-
require.NoError(t, err)
85-
data[0] = 99
86-
87-
// Cache should still have the original unmodified data
88-
cached, ok := db.cache.Get(height)
89-
require.True(t, ok)
90-
require.Equal(t, block, cached)
91-
92-
// Second Get should also return original data
93-
data, err = db.Get(height)
94-
require.NoError(t, err)
95-
require.Equal(t, block, data)
96-
}
97-
9859
func TestCachePutOverridesSameHeight(t *testing.T) {
9960
db := newCacheDatabase(t, DefaultConfig())
10061
height := uint64(60)

0 commit comments

Comments
 (0)