-
Notifications
You must be signed in to change notification settings - Fork 721
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
Use custom codec for validator metadata #1510
Changes from all commits
e2e96fc
7b371af
c17a619
5a7eeaa
9b3b10e
f08f9e5
302e6ab
7c98dca
c5ec0db
d45a56f
e7989fc
f3c3106
8f450c8
400af13
c1c05f2
005ad9b
cad02b9
d6bc37d
940089b
73a8d63
f473bcd
91a867d
7f234c2
9fcff0f
46d8042
f04fbcc
1a477c6
32f846f
1e3858d
7389334
6aa2c75
616d69a
195d32c
11cf52d
4cf68cd
d85f475
64e4973
f9d9c8a
f8b959d
d518ac9
2b424cb
ef58ed1
73a880a
ea6abe8
65dad4e
2494246
68cde28
2cfb6cc
e049f41
dc241a9
e8c8425
e0f5e03
68fc5a0
42cb462
60c4bbf
d7698e6
f09d0b8
cbe1b4e
51119a4
ea9ca44
47301bc
ab086d3
276c1d2
3ce6889
ff63392
25db2fd
d6ef189
54a6eef
9bde97b
7d57392
dad193c
bb1ff16
28cb766
9311560
c01adef
b086883
e82e829
94fa5d5
23e4b34
a6ae416
884a347
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we initialize the metadata code similarly to what we do with txs.Codec |
||
c := linearcodec.New([]string{v0tag}, math.MaxInt32) | ||
metadataCodec = codec.NewManager(math.MaxInt32) | ||
|
||
err := metadataCodec.RegisterCodec(v0, c) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is currently redundant really, but when dropping stakers we'll extend it to store delegator StartTime. |
||
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main change in the PR: I need to marshal/unmarshal validator metadata with their own codec metadataCodec, to be free to change version without impacting txs.Codec |
||
return err | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
} | ||
|
@@ -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 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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 { | ||
Comment on lines
+2180
to
+2184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just encapsulating delegator metadata writing into delegatorMetadata object |
||
return fmt.Errorf("failed to write current delegator to list: %w", err) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just removed some code duplication, once
linearcodec.DefaultMaxSliceLength
is exported