Skip to content

Commit

Permalink
[PVM, DAC] Extracts proposalState types codec registration from txs c…
Browse files Browse the repository at this point in the history
…odec to their own dac codec
  • Loading branch information
evlekht committed Nov 14, 2023
1 parent c1422b7 commit 36ca617
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
38 changes: 38 additions & 0 deletions vms/platformvm/dac/camino_codec.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 3 additions & 4 deletions vms/platformvm/state/camino_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions vms/platformvm/state/camino_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 36ca617

Please sign in to comment.