github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/txs/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 txs
     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/signer"
    14  	"github.com/ava-labs/avalanchego/vms/platformvm/stakeable"
    15  	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
    16  )
    17  
    18  const CodecVersion = 0
    19  
    20  var (
    21  	Codec codec.Manager
    22  
    23  	// GenesisCodec allows txs of larger than usual size to be parsed.
    24  	// While this gives flexibility in accommodating large genesis txs
    25  	// it must not be used to parse new, unverified txs which instead
    26  	// must be processed by Codec
    27  	GenesisCodec codec.Manager
    28  )
    29  
    30  func init() {
    31  	c := linearcodec.NewDefault()
    32  	gc := linearcodec.NewDefault()
    33  
    34  	errs := wrappers.Errs{}
    35  	for _, c := range []linearcodec.Codec{c, gc} {
    36  		// Order in which type are registered affect the byte representation
    37  		// generated by marshalling ops. To maintain codec type ordering,
    38  		// we skip positions for the blocks.
    39  		c.SkipRegistrations(5)
    40  
    41  		errs.Add(
    42  			RegisterApricotTypes(c),
    43  			RegisterBanffTypes(c),
    44  		)
    45  
    46  		c.SkipRegistrations(4)
    47  
    48  		errs.Add(RegisterDurangoTypes(c))
    49  	}
    50  
    51  	Codec = codec.NewDefaultManager()
    52  	GenesisCodec = codec.NewManager(math.MaxInt32)
    53  	errs.Add(
    54  		Codec.RegisterCodec(CodecVersion, c),
    55  		GenesisCodec.RegisterCodec(CodecVersion, gc),
    56  	)
    57  	if errs.Errored() {
    58  		panic(errs.Err)
    59  	}
    60  }
    61  
    62  // RegisterApricotTypes registers the type information for transactions that
    63  // were valid during the Apricot series of upgrades.
    64  func RegisterApricotTypes(targetCodec linearcodec.Codec) error {
    65  	errs := wrappers.Errs{}
    66  
    67  	// The secp256k1fx is registered here because this is the same place it is
    68  	// registered in the AVM. This ensures that the typeIDs match up for utxos
    69  	// in shared memory.
    70  	errs.Add(targetCodec.RegisterType(&secp256k1fx.TransferInput{}))
    71  	targetCodec.SkipRegistrations(1)
    72  	errs.Add(targetCodec.RegisterType(&secp256k1fx.TransferOutput{}))
    73  	targetCodec.SkipRegistrations(1)
    74  	errs.Add(
    75  		targetCodec.RegisterType(&secp256k1fx.Credential{}),
    76  		targetCodec.RegisterType(&secp256k1fx.Input{}),
    77  		targetCodec.RegisterType(&secp256k1fx.OutputOwners{}),
    78  
    79  		targetCodec.RegisterType(&AddValidatorTx{}),
    80  		targetCodec.RegisterType(&AddSubnetValidatorTx{}),
    81  		targetCodec.RegisterType(&AddDelegatorTx{}),
    82  		targetCodec.RegisterType(&CreateChainTx{}),
    83  		targetCodec.RegisterType(&CreateSubnetTx{}),
    84  		targetCodec.RegisterType(&ImportTx{}),
    85  		targetCodec.RegisterType(&ExportTx{}),
    86  		targetCodec.RegisterType(&AdvanceTimeTx{}),
    87  		targetCodec.RegisterType(&RewardValidatorTx{}),
    88  
    89  		targetCodec.RegisterType(&stakeable.LockIn{}),
    90  		targetCodec.RegisterType(&stakeable.LockOut{}),
    91  	)
    92  	return errs.Err
    93  }
    94  
    95  // RegisterBanffTypes registers the type information for transactions that were
    96  // valid during the Banff series of upgrades.
    97  func RegisterBanffTypes(targetCodec linearcodec.Codec) error {
    98  	return errors.Join(
    99  		targetCodec.RegisterType(&RemoveSubnetValidatorTx{}),
   100  		targetCodec.RegisterType(&TransformSubnetTx{}),
   101  		targetCodec.RegisterType(&AddPermissionlessValidatorTx{}),
   102  		targetCodec.RegisterType(&AddPermissionlessDelegatorTx{}),
   103  
   104  		targetCodec.RegisterType(&signer.Empty{}),
   105  		targetCodec.RegisterType(&signer.ProofOfPossession{}),
   106  	)
   107  }
   108  
   109  // RegisterDurangoTypes registers the type information for transactions that
   110  // were valid during the Durango series of upgrades.
   111  func RegisterDurangoTypes(targetCodec linearcodec.Codec) error {
   112  	return errors.Join(
   113  		targetCodec.RegisterType(&TransferSubnetOwnershipTx{}),
   114  		targetCodec.RegisterType(&BaseTx{}),
   115  	)
   116  }