github.com/MetalBlockchain/metalgo@v1.11.9/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/MetalBlockchain/metalgo/codec" 11 "github.com/MetalBlockchain/metalgo/codec/linearcodec" 12 "github.com/MetalBlockchain/metalgo/utils/wrappers" 13 "github.com/MetalBlockchain/metalgo/vms/platformvm/signer" 14 "github.com/MetalBlockchain/metalgo/vms/platformvm/stakeable" 15 "github.com/MetalBlockchain/metalgo/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(RegisterUnsignedTxsTypes(c)) 42 43 c.SkipRegistrations(4) 44 45 errs.Add(RegisterDUnsignedTxsTypes(c)) 46 } 47 48 Codec = codec.NewDefaultManager() 49 GenesisCodec = codec.NewManager(math.MaxInt32) 50 errs.Add( 51 Codec.RegisterCodec(CodecVersion, c), 52 GenesisCodec.RegisterCodec(CodecVersion, gc), 53 ) 54 if errs.Errored() { 55 panic(errs.Err) 56 } 57 } 58 59 // RegisterUnsignedTxsTypes allows registering relevant type of unsigned package 60 // in the right sequence. Following repackaging of platformvm package, a few 61 // subpackage-level codecs were introduced, each handling serialization of 62 // specific types. 63 // 64 // RegisterUnsignedTxsTypes is made exportable so to guarantee that other codecs 65 // are coherent with components one. 66 func RegisterUnsignedTxsTypes(targetCodec linearcodec.Codec) error { 67 errs := wrappers.Errs{} 68 69 // The secp256k1fx is registered here because this is the same place it is 70 // registered in the AVM. This ensures that the typeIDs match up for utxos 71 // in shared memory. 72 errs.Add(targetCodec.RegisterType(&secp256k1fx.TransferInput{})) 73 targetCodec.SkipRegistrations(1) 74 errs.Add(targetCodec.RegisterType(&secp256k1fx.TransferOutput{})) 75 targetCodec.SkipRegistrations(1) 76 errs.Add( 77 targetCodec.RegisterType(&secp256k1fx.Credential{}), 78 targetCodec.RegisterType(&secp256k1fx.Input{}), 79 targetCodec.RegisterType(&secp256k1fx.OutputOwners{}), 80 81 targetCodec.RegisterType(&AddValidatorTx{}), 82 targetCodec.RegisterType(&AddSubnetValidatorTx{}), 83 targetCodec.RegisterType(&AddDelegatorTx{}), 84 targetCodec.RegisterType(&CreateChainTx{}), 85 targetCodec.RegisterType(&CreateSubnetTx{}), 86 targetCodec.RegisterType(&ImportTx{}), 87 targetCodec.RegisterType(&ExportTx{}), 88 targetCodec.RegisterType(&AdvanceTimeTx{}), 89 targetCodec.RegisterType(&RewardValidatorTx{}), 90 91 targetCodec.RegisterType(&stakeable.LockIn{}), 92 targetCodec.RegisterType(&stakeable.LockOut{}), 93 94 // Banff additions: 95 targetCodec.RegisterType(&RemoveSubnetValidatorTx{}), 96 targetCodec.RegisterType(&TransformSubnetTx{}), 97 targetCodec.RegisterType(&AddPermissionlessValidatorTx{}), 98 targetCodec.RegisterType(&AddPermissionlessDelegatorTx{}), 99 100 targetCodec.RegisterType(&signer.Empty{}), 101 targetCodec.RegisterType(&signer.ProofOfPossession{}), 102 ) 103 return errs.Err 104 } 105 106 func RegisterDUnsignedTxsTypes(targetCodec linearcodec.Codec) error { 107 return errors.Join( 108 targetCodec.RegisterType(&TransferSubnetOwnershipTx{}), 109 targetCodec.RegisterType(&BaseTx{}), 110 ) 111 }