Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACP-77: Add caching to SoV DB helpers #3516

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 39 additions & 6 deletions vms/platformvm/state/subnet_only_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

"github.com/google/btree"

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/maybe"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
)

Expand All @@ -21,6 +23,8 @@ var (
_ utils.Sortable[SubnetOnlyValidator] = SubnetOnlyValidator{}

ErrMutatedSubnetOnlyValidator = errors.New("subnet only validator contains mutated constant fields")

emptySoVCache cache.Cacher[ids.ID, maybe.Maybe[SubnetOnlyValidator]] = &cache.Empty[ids.ID, maybe.Maybe[SubnetOnlyValidator]]{}
marun marked this conversation as resolved.
Show resolved Hide resolved
)

// SubnetOnlyValidator defines an ACP-77 validator. For a given ValidationID, it
Expand Down Expand Up @@ -103,7 +107,18 @@ func (v SubnetOnlyValidator) immutableFieldsAreUnmodified(o SubnetOnlyValidator)
v.StartTime == o.StartTime
}

func getSubnetOnlyValidator(db database.KeyValueReader, validationID ids.ID) (SubnetOnlyValidator, error) {
func getSubnetOnlyValidator(
cache cache.Cacher[ids.ID, maybe.Maybe[SubnetOnlyValidator]],
db database.KeyValueReader,
validationID ids.ID,
) (SubnetOnlyValidator, error) {
if maybeSOV, ok := cache.Get(validationID); ok {
if maybeSOV.IsNothing() {
return SubnetOnlyValidator{}, database.ErrNotFound
}
return maybeSOV.Value(), nil
}

StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
bytes, err := db.Get(validationID[:])
if err != nil {
return SubnetOnlyValidator{}, err
Expand All @@ -118,14 +133,32 @@ func getSubnetOnlyValidator(db database.KeyValueReader, validationID ids.ID) (Su
return vdr, nil
}

func putSubnetOnlyValidator(db database.KeyValueWriter, vdr SubnetOnlyValidator) error {
bytes, err := block.GenesisCodec.Marshal(block.CodecVersion, vdr)
func putSubnetOnlyValidator(
db database.KeyValueWriter,
cache cache.Cacher[ids.ID, maybe.Maybe[SubnetOnlyValidator]],
sov SubnetOnlyValidator,
) error {
bytes, err := block.GenesisCodec.Marshal(block.CodecVersion, sov)
if err != nil {
return fmt.Errorf("failed to marshal SubnetOnlyValidator: %w", err)
}
return db.Put(vdr.ValidationID[:], bytes)
if err := db.Put(sov.ValidationID[:], bytes); err != nil {
return err
}

cache.Put(sov.ValidationID, maybe.Some(sov))
return nil
}

func deleteSubnetOnlyValidator(db database.KeyValueDeleter, validationID ids.ID) error {
return db.Delete(validationID[:])
func deleteSubnetOnlyValidator(
db database.KeyValueDeleter,
cache cache.Cacher[ids.ID, maybe.Maybe[SubnetOnlyValidator]],
validationID ids.ID,
) error {
if err := db.Delete(validationID[:]); err != nil {
return err
}

cache.Put(validationID, maybe.Nothing[SubnetOnlyValidator]())
marun marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
111 changes: 89 additions & 22 deletions vms/platformvm/state/subnet_only_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/maybe"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand Down Expand Up @@ -139,11 +141,8 @@ func TestSubnetOnlyValidator_immutableFieldsAreUnmodified(t *testing.T) {
}

func TestSubnetOnlyValidator_DatabaseHelpers(t *testing.T) {
require := require.New(t)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
db := memdb.New()

sk, err := bls.NewSecretKey()
require.NoError(err)
require.NoError(t, err)
pk := bls.PublicFromSecretKey(sk)
pkBytes := bls.PublicKeyToUncompressedBytes(pk)

Expand All @@ -154,7 +153,7 @@ func TestSubnetOnlyValidator_DatabaseHelpers(t *testing.T) {
},
}
remainingBalanceOwnerBytes, err := block.GenesisCodec.Marshal(block.CodecVersion, &remainingBalanceOwner)
require.NoError(err)
require.NoError(t, err)

var deactivationOwner fx.Owner = &secp256k1fx.OutputOwners{
Threshold: 1,
Expand All @@ -163,9 +162,9 @@ func TestSubnetOnlyValidator_DatabaseHelpers(t *testing.T) {
},
}
deactivationOwnerBytes, err := block.GenesisCodec.Marshal(block.CodecVersion, &deactivationOwner)
require.NoError(err)
require.NoError(t, err)

vdr := SubnetOnlyValidator{
sov := SubnetOnlyValidator{
ValidationID: ids.GenerateTestID(),
SubnetID: ids.GenerateTestID(),
NodeID: ids.GenerateTestNodeID(),
Expand All @@ -178,24 +177,92 @@ func TestSubnetOnlyValidator_DatabaseHelpers(t *testing.T) {
EndAccumulatedFee: rand.Uint64(), // #nosec G404
}

// Validator hasn't been put on disk yet
gotVdr, err := getSubnetOnlyValidator(db, vdr.ValidationID)
require.ErrorIs(err, database.ErrNotFound)
require.Zero(gotVdr)
var (
addedDB = memdb.New()
removedDB = memdb.New()
addedAndRemovedDB = memdb.New()
addedAndRemovedAndAddedDB = memdb.New()

addedCache = &cache.LRU[ids.ID, maybe.Maybe[SubnetOnlyValidator]]{Size: 10}
removedCache = &cache.LRU[ids.ID, maybe.Maybe[SubnetOnlyValidator]]{Size: 10}
addedAndRemovedCache = &cache.LRU[ids.ID, maybe.Maybe[SubnetOnlyValidator]]{Size: 10}
addedAndRemovedAndAddedCache = &cache.LRU[ids.ID, maybe.Maybe[SubnetOnlyValidator]]{Size: 10}
)

// Place the validator on disk
require.NoError(putSubnetOnlyValidator(db, vdr))
require.NoError(t, putSubnetOnlyValidator(addedDB, addedCache, sov))
require.NoError(t, putSubnetOnlyValidator(addedAndRemovedDB, addedAndRemovedCache, sov))
require.NoError(t, putSubnetOnlyValidator(addedAndRemovedAndAddedDB, addedAndRemovedAndAddedCache, sov))

// Verify that the validator can be fetched from disk
gotVdr, err = getSubnetOnlyValidator(db, vdr.ValidationID)
require.NoError(err)
require.Equal(vdr, gotVdr)
// Remove the validator on disk
require.NoError(t, deleteSubnetOnlyValidator(removedDB, removedCache, sov.ValidationID))
require.NoError(t, deleteSubnetOnlyValidator(addedAndRemovedDB, addedAndRemovedCache, sov.ValidationID))
require.NoError(t, deleteSubnetOnlyValidator(addedAndRemovedAndAddedDB, addedAndRemovedAndAddedCache, sov.ValidationID))

// Remove the validator from disk
require.NoError(deleteSubnetOnlyValidator(db, vdr.ValidationID))
// Reintroduce the validator to disk
require.NoError(t, putSubnetOnlyValidator(addedAndRemovedAndAddedDB, addedAndRemovedAndAddedCache, sov))

// Verify that the validator has been removed from disk
gotVdr, err = getSubnetOnlyValidator(db, vdr.ValidationID)
require.ErrorIs(err, database.ErrNotFound)
require.Zero(gotVdr)
addedTests := []struct {
name string
db database.Database
cache cache.Cacher[ids.ID, maybe.Maybe[SubnetOnlyValidator]]
}{
{
name: "added in cache",
db: memdb.New(),
cache: addedCache,
},
{
name: "added on disk",
db: addedDB,
cache: emptySoVCache,
},
{
name: "added and removed and added in cache",
db: memdb.New(),
cache: addedAndRemovedAndAddedCache,
},
{
name: "added and removed and added on disk",
db: addedAndRemovedAndAddedDB,
cache: emptySoVCache,
},
}
for _, test := range addedTests {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

gotSOV, err := getSubnetOnlyValidator(test.cache, test.db, sov.ValidationID)
require.NoError(err)
require.Equal(sov, gotSOV)
})
}

removedTests := []struct {
name string
db database.Database
cache cache.Cacher[ids.ID, maybe.Maybe[SubnetOnlyValidator]]
}{
{
name: "empty",
db: memdb.New(),
cache: emptySoVCache,
},
{
name: "removed from cache",
db: addedDB,
cache: removedCache,
},
{
name: "removed from disk",
db: removedDB,
cache: emptySoVCache,
},
}
for _, test := range removedTests {
t.Run(test.name, func(t *testing.T) {
_, err := getSubnetOnlyValidator(test.cache, test.db, sov.ValidationID)
require.ErrorIs(t, err, database.ErrNotFound)
})
}
}
Loading