Skip to content

Commit

Permalink
Use custom codec for validator metadata (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 authored Oct 23, 2023
1 parent 3a1dcca commit d3287dd
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/keystore/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
maxPackerSize = 1 * units.GiB // max size, in bytes, of something being marshalled by Marshal()
maxSliceLength = 256 * 1024
maxSliceLength = linearcodec.DefaultMaxSliceLength

codecVersion = 0
)
Expand Down
4 changes: 2 additions & 2 deletions codec/linearcodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const (
// default max length of a slice being marshalled by Marshal(). Should be <= math.MaxUint32.
defaultMaxSliceLength = 256 * 1024
DefaultMaxSliceLength = 256 * 1024
)

var (
Expand Down Expand Up @@ -56,7 +56,7 @@ func New(tagNames []string, maxSliceLen uint32) Codec {

// NewDefault is a convenience constructor; it returns a new codec with reasonable default values
func NewDefault() Codec {
return New([]string{reflectcodec.DefaultTagName}, defaultMaxSliceLength)
return New([]string{reflectcodec.DefaultTagName}, DefaultMaxSliceLength)
}

// NewCustomMaxLength is a convenience constructor; it returns a new codec with custom max length and default tags
Expand Down
2 changes: 1 addition & 1 deletion codec/linearcodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestVectors(t *testing.T) {

func TestMultipleTags(t *testing.T) {
for _, test := range codec.MultipleTagsTests {
c := New([]string{"tag1", "tag2"}, defaultMaxSliceLength)
c := New([]string{"tag1", "tag2"}, DefaultMaxSliceLength)
test(c, t)
}
}
Expand Down
28 changes: 28 additions & 0 deletions vms/platformvm/state/metadata_codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package state

import (
"math"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
)

const (
v0tag = "v0"
v0 = uint16(0)
)

var metadataCodec codec.Manager

func init() {
c := linearcodec.New([]string{v0tag}, math.MaxInt32)
metadataCodec = codec.NewManager(math.MaxInt32)

err := metadataCodec.RegisterCodec(v0, c)
if err != nil {
panic(err)
}
}
25 changes: 25 additions & 0 deletions vms/platformvm/state/metadata_delegator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package state

import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
)

type delegatorMetadata struct {
PotentialReward uint64

txID ids.ID
}

func parseDelegatorMetadata(bytes []byte, metadata *delegatorMetadata) error {
var err error
metadata.PotentialReward, err = database.ParseUInt64(bytes)
return err
}

func writeDelegatorMetadata(db database.KeyValueWriter, metadata *delegatorMetadata) error {
return database.PutUInt64(db, metadata.txID[:], metadata.PotentialReward)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/platformvm/genesis"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)

// preDelegateeRewardSize is the size of codec marshalling
Expand All @@ -24,16 +22,16 @@ const preDelegateeRewardSize = wrappers.ShortLen + 3*wrappers.LongLen
var _ validatorState = (*metadata)(nil)

type preDelegateeRewardMetadata struct {
UpDuration time.Duration `serialize:"true"`
LastUpdated uint64 `serialize:"true"` // Unix time in seconds
PotentialReward uint64 `serialize:"true"`
UpDuration time.Duration `v0:"true"`
LastUpdated uint64 `v0:"true"` // Unix time in seconds
PotentialReward uint64 `v0:"true"`
}

type validatorMetadata struct {
UpDuration time.Duration `serialize:"true"`
LastUpdated uint64 `serialize:"true"` // Unix time in seconds
PotentialReward uint64 `serialize:"true"`
PotentialDelegateeReward uint64 `serialize:"true"`
UpDuration time.Duration `v0:"true"`
LastUpdated uint64 `v0:"true"` // Unix time in seconds
PotentialReward uint64 `v0:"true"`
PotentialDelegateeReward uint64 `v0:"true"`

txID ids.ID
lastUpdated time.Time
Expand All @@ -60,7 +58,7 @@ func parseValidatorMetadata(bytes []byte, metadata *validatorMetadata) error {
// potential reward and uptime was stored but potential delegatee reward
// was not
tmp := preDelegateeRewardMetadata{}
if _, err := txs.Codec.Unmarshal(bytes, &tmp); err != nil {
if _, err := metadataCodec.Unmarshal(bytes, &tmp); err != nil {
return err
}

Expand All @@ -69,7 +67,7 @@ func parseValidatorMetadata(bytes []byte, metadata *validatorMetadata) error {
metadata.PotentialReward = tmp.PotentialReward
default:
// everything was stored
if _, err := txs.Codec.Unmarshal(bytes, metadata); err != nil {
if _, err := metadataCodec.Unmarshal(bytes, metadata); err != nil {
return err
}
}
Expand Down Expand Up @@ -238,7 +236,7 @@ func (m *metadata) WriteValidatorMetadata(
metadata := m.metadata[vdrID][subnetID]
metadata.LastUpdated = uint64(metadata.lastUpdated.Unix())

metadataBytes, err := genesis.Codec.Marshal(txs.Version, metadata)
metadataBytes, err := metadataCodec.Marshal(v0, metadata)
if err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,10 @@ func (s *state) loadCurrentValidators() error {
return err
}

potentialRewardBytes := delegatorIt.Value()
potentialReward, err := database.ParseUInt64(potentialRewardBytes)
metadata := &delegatorMetadata{
txID: txID,
}
err = parseDelegatorMetadata(delegatorIt.Value(), metadata)
if err != nil {
return err
}
Expand All @@ -1579,7 +1581,7 @@ func (s *state) loadCurrentValidators() error {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

staker, err := NewCurrentStaker(txID, stakerTx, potentialReward)
staker, err := NewCurrentStaker(txID, stakerTx, metadata.PotentialReward)
if err != nil {
return err
}
Expand Down Expand Up @@ -2032,7 +2034,7 @@ func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error
PotentialDelegateeReward: 0,
}

metadataBytes, err := block.GenesisCodec.Marshal(block.Version, metadata)
metadataBytes, err := metadataCodec.Marshal(v0, metadata)
if err != nil {
return fmt.Errorf("failed to serialize current validator: %w", err)
}
Expand Down Expand Up @@ -2175,7 +2177,11 @@ func writeCurrentDelegatorDiff(
return fmt.Errorf("failed to increase node weight diff: %w", err)
}

if err := database.PutUInt64(currentDelegatorList, staker.TxID[:], staker.PotentialReward); err != nil {
metadata := &delegatorMetadata{
txID: staker.TxID,
PotentialReward: staker.PotentialReward,
}
if err := writeDelegatorMetadata(currentDelegatorList, metadata); err != nil {
return fmt.Errorf("failed to write current delegator to list: %w", err)
}
}
Expand Down

0 comments on commit d3287dd

Please sign in to comment.