github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/txs/payload/payload.go (about)

     1  package payload
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  /*
     8  Payload (Transaction) is an atomic operation on the ledger state.
     9  
    10  Account Txs:
    11   - SendTx         Send coins to address
    12   - CallTx         Send a msg to a contract that runs in the vm
    13   - NameTx	  Store some value under a name in the global namereg
    14  
    15  Validation Txs:
    16   - BondTx         New validator posts a bond
    17   - UnbondTx       Validator leaves
    18  
    19  Admin Txs:
    20   - PermsTx
    21  */
    22  
    23  type Type uint32
    24  
    25  // Types of Payload implementations
    26  const (
    27  	TypeUnknown = Type(0x00)
    28  	// Account transactions
    29  	TypeSend  = Type(0x01)
    30  	TypeCall  = Type(0x02)
    31  	TypeName  = Type(0x03)
    32  	TypeBatch = Type(0x04)
    33  
    34  	// Validation transactions
    35  	TypeBond   = Type(0x11)
    36  	TypeUnbond = Type(0x12)
    37  
    38  	// Admin transactions
    39  	TypePermissions = Type(0x21)
    40  	TypeGovernance  = Type(0x22)
    41  	TypeProposal    = Type(0x23)
    42  	TypeIdentify    = Type(0x24)
    43  )
    44  
    45  type Payload interface {
    46  	String() string
    47  	GetInputs() []*TxInput
    48  	Type() Type
    49  	Any() *Any
    50  	// The serialised size in bytes
    51  	Size() int
    52  }
    53  
    54  var nameFromType = map[Type]string{
    55  	TypeUnknown:     "UnknownTx",
    56  	TypeSend:        "SendTx",
    57  	TypeCall:        "CallTx",
    58  	TypeName:        "NameTx",
    59  	TypeBatch:       "BatchTx",
    60  	TypePermissions: "PermsTx",
    61  	TypeGovernance:  "GovTx",
    62  	TypeProposal:    "ProposalTx",
    63  	TypeBond:        "BondTx",
    64  	TypeUnbond:      "UnbondTx",
    65  	TypeIdentify:    "IdentifyTx",
    66  }
    67  
    68  var typeFromName = make(map[string]Type)
    69  
    70  func init() {
    71  	for t, n := range nameFromType {
    72  		typeFromName[n] = t
    73  	}
    74  }
    75  
    76  func TxTypeFromString(name string) Type {
    77  	return typeFromName[name]
    78  }
    79  
    80  func (typ Type) String() string {
    81  	name, ok := nameFromType[typ]
    82  	if ok {
    83  		return name
    84  	}
    85  	return "UnknownTx"
    86  }
    87  
    88  func (typ Type) MarshalText() ([]byte, error) {
    89  	return []byte(typ.String()), nil
    90  }
    91  
    92  func (typ *Type) UnmarshalText(data []byte) error {
    93  	*typ = TxTypeFromString(string(data))
    94  	return nil
    95  }
    96  
    97  // Protobuf support
    98  func (typ Type) Marshal() ([]byte, error) {
    99  	return typ.MarshalText()
   100  }
   101  
   102  func (typ *Type) Unmarshal(data []byte) error {
   103  	return typ.UnmarshalText(data)
   104  }
   105  
   106  //func (tx *CallTx) ProtoReflect() protoreflect.Message {
   107  //	fd, _ := descriptor.ForMessage(tx)
   108  //	ggfd, _ := descriptor.ForMessage()
   109  //	protodesc.NewFiles(&descriptorpb.FileDescriptorSet{File: })
   110  //	f, err := protodesc.NewFile(fd, nil)
   111  //	if err != nil {
   112  //		panic(err)
   113  //	}
   114  //	mi := &protoimpl.MessageInfo{
   115  //		GoReflectType: reflect.TypeOf(tx),
   116  //		Desc:          f.Messages().Get(0),
   117  //	}
   118  //	m := mi.MessageOf(tx)
   119  //	fmt.Sprint(fd)
   120  //	return m
   121  //}
   122  //
   123  func New(txType Type) (Payload, error) {
   124  	switch txType {
   125  	case TypeSend:
   126  		return &SendTx{}, nil
   127  	case TypeCall:
   128  		return &CallTx{}, nil
   129  	case TypeName:
   130  		return &NameTx{}, nil
   131  	case TypeBatch:
   132  		return &BatchTx{}, nil
   133  	case TypePermissions:
   134  		return &PermsTx{}, nil
   135  	case TypeGovernance:
   136  		return &GovTx{}, nil
   137  	case TypeBond:
   138  		return &BondTx{}, nil
   139  	case TypeUnbond:
   140  		return &UnbondTx{}, nil
   141  	case TypeProposal:
   142  		return &ProposalTx{}, nil
   143  	case TypeIdentify:
   144  		return &IdentifyTx{}, nil
   145  	}
   146  	return nil, fmt.Errorf("unknown payload type: %d", txType)
   147  }