github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/block/codec.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package block
     5  
     6  import (
     7  	"errors"
     8  	"math"
     9  
    10  	"github.com/ava-labs/avalanchego/codec"
    11  	"github.com/ava-labs/avalanchego/codec/linearcodec"
    12  	"github.com/ava-labs/avalanchego/utils/wrappers"
    13  	"github.com/ava-labs/avalanchego/vms/platformvm/txs"
    14  )
    15  
    16  const CodecVersion = txs.CodecVersion
    17  
    18  var (
    19  	// GenesisCodec allows blocks of larger than usual size to be parsed.
    20  	// While this gives flexibility in accommodating large genesis blocks
    21  	// it must not be used to parse new, unverified blocks which instead
    22  	// must be processed by Codec
    23  	GenesisCodec codec.Manager
    24  
    25  	Codec codec.Manager
    26  )
    27  
    28  func init() {
    29  	c := linearcodec.NewDefault()
    30  	gc := linearcodec.NewDefault()
    31  
    32  	errs := wrappers.Errs{}
    33  	for _, c := range []linearcodec.Codec{c, gc} {
    34  		errs.Add(
    35  			RegisterApricotTypes(c),
    36  			RegisterBanffTypes(c),
    37  			RegisterDurangoTypes(c),
    38  		)
    39  	}
    40  
    41  	Codec = codec.NewDefaultManager()
    42  	GenesisCodec = codec.NewManager(math.MaxInt32)
    43  	errs.Add(
    44  		Codec.RegisterCodec(CodecVersion, c),
    45  		GenesisCodec.RegisterCodec(CodecVersion, gc),
    46  	)
    47  	if errs.Errored() {
    48  		panic(errs.Err)
    49  	}
    50  }
    51  
    52  // RegisterApricotTypes registers the type information for blocks that were
    53  // valid during the Apricot series of upgrades.
    54  func RegisterApricotTypes(targetCodec linearcodec.Codec) error {
    55  	return errors.Join(
    56  		targetCodec.RegisterType(&ApricotProposalBlock{}),
    57  		targetCodec.RegisterType(&ApricotAbortBlock{}),
    58  		targetCodec.RegisterType(&ApricotCommitBlock{}),
    59  		targetCodec.RegisterType(&ApricotStandardBlock{}),
    60  		targetCodec.RegisterType(&ApricotAtomicBlock{}),
    61  		txs.RegisterApricotTypes(targetCodec),
    62  	)
    63  }
    64  
    65  // RegisterBanffTypes registers the type information for blocks that were valid
    66  // during the Banff series of upgrades.
    67  func RegisterBanffTypes(targetCodec linearcodec.Codec) error {
    68  	return errors.Join(
    69  		txs.RegisterBanffTypes(targetCodec),
    70  		targetCodec.RegisterType(&BanffProposalBlock{}),
    71  		targetCodec.RegisterType(&BanffAbortBlock{}),
    72  		targetCodec.RegisterType(&BanffCommitBlock{}),
    73  		targetCodec.RegisterType(&BanffStandardBlock{}),
    74  	)
    75  }
    76  
    77  // RegisterDurangoTypes registers the type information for blocks that were
    78  // valid during the Durango series of upgrades.
    79  func RegisterDurangoTypes(targetCodec linearcodec.Codec) error {
    80  	return txs.RegisterDurangoTypes(targetCodec)
    81  }