From 36ca617f2d89f2d366b212c62a58a5216398a441 Mon Sep 17 00:00:00 2001 From: evlekht <> Date: Tue, 14 Nov 2023 18:04:06 +0400 Subject: [PATCH] [PVM, DAC] Extracts proposalState types codec registration from txs codec to their own dac codec --- vms/platformvm/dac/camino_codec.go | 38 ++++++++++++++++++++ vms/platformvm/state/camino_proposal.go | 7 ++-- vms/platformvm/state/camino_proposal_test.go | 6 ++-- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 vms/platformvm/dac/camino_codec.go diff --git a/vms/platformvm/dac/camino_codec.go b/vms/platformvm/dac/camino_codec.go new file mode 100644 index 000000000000..cc0774749e6d --- /dev/null +++ b/vms/platformvm/dac/camino_codec.go @@ -0,0 +1,38 @@ +// Copyright (C) 2023, Chain4Travel AG. All rights reserved. +// See the file LICENSE for licensing terms. + +package dac + +import ( + "math" + + "github.com/ava-labs/avalanchego/codec" + "github.com/ava-labs/avalanchego/codec/linearcodec" + "github.com/ava-labs/avalanchego/utils/wrappers" +) + +// Version is the current default codec version +const Version = 0 + +var Codec codec.Manager + +func init() { + c := linearcodec.NewCaminoDefault() + Codec = codec.NewDefaultManager() + gc := linearcodec.NewCaminoCustomMaxLength(math.MaxInt32) + + errs := wrappers.Errs{} + for _, c := range []linearcodec.CaminoCodec{c, gc} { + errs.Add( + c.RegisterCustomType(&BaseFeeProposalState{}), + c.RegisterCustomType(&AddMemberProposalState{}), + c.RegisterCustomType(&ExcludeMemberProposalState{}), + ) + } + errs.Add( + Codec.RegisterCodec(Version, c), + ) + if errs.Errored() { + panic(errs.Err) + } +} diff --git a/vms/platformvm/state/camino_proposal.go b/vms/platformvm/state/camino_proposal.go index b7792c03ec27..2fa37fa1b0d3 100644 --- a/vms/platformvm/state/camino_proposal.go +++ b/vms/platformvm/state/camino_proposal.go @@ -16,7 +16,6 @@ import ( "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/vms/platformvm/blocks" "github.com/ava-labs/avalanchego/vms/platformvm/dac" - "github.com/ava-labs/avalanchego/vms/platformvm/txs" ) type proposalStateWrapper struct { @@ -66,7 +65,7 @@ func (cs *caminoState) GetProposal(proposalID ids.ID) (dac.ProposalState, error) } proposal := &proposalStateWrapper{} - if _, err := txs.Codec.Unmarshal(proposalBytes, proposal); err != nil { + if _, err := dac.Codec.Unmarshal(proposalBytes, proposal); err != nil { return nil, err } @@ -199,7 +198,7 @@ func (cs *caminoState) writeProposals() error { return err } } else { - proposalBytes, err := txs.Codec.Marshal(blocks.Version, &proposalStateWrapper{ProposalState: proposalDiff.Proposal}) + proposalBytes, err := dac.Codec.Marshal(blocks.Version, &proposalStateWrapper{ProposalState: proposalDiff.Proposal}) if err != nil { return fmt.Errorf("failed to serialize deposit: %w", err) } @@ -386,7 +385,7 @@ func (it *proposalsIterator) Value() (dac.ProposalState, error) { } proposal := &proposalStateWrapper{} - if _, err := txs.Codec.Unmarshal(it.dbIterator.Value(), proposal); err != nil { + if _, err := dac.Codec.Unmarshal(it.dbIterator.Value(), proposal); err != nil { return nil, err } diff --git a/vms/platformvm/state/camino_proposal_test.go b/vms/platformvm/state/camino_proposal_test.go index 38a18bc35043..c4df9b7d6969 100644 --- a/vms/platformvm/state/camino_proposal_test.go +++ b/vms/platformvm/state/camino_proposal_test.go @@ -32,7 +32,7 @@ func TestGetProposal(t *testing.T) { TotalAllowedVoters: 5, }, } - proposalBytes, err := blocks.GenesisCodec.Marshal(blocks.Version, wrapper) + proposalBytes, err := dac.Codec.Marshal(blocks.Version, wrapper) require.NoError(t, err) testError := errors.New("test error") @@ -854,9 +854,9 @@ func TestWriteProposals(t *testing.T) { }} proposalEndtime := proposalWrapper2.EndTime() - proposal1Bytes, err := blocks.GenesisCodec.Marshal(blocks.Version, proposalWrapper1) + proposal1Bytes, err := dac.Codec.Marshal(blocks.Version, proposalWrapper1) require.NoError(t, err) - proposal2Bytes, err := blocks.GenesisCodec.Marshal(blocks.Version, proposalWrapper2) + proposal2Bytes, err := dac.Codec.Marshal(blocks.Version, proposalWrapper2) require.NoError(t, err) tests := map[string]struct {