github.com/amazechain/amc@v0.1.3/internal/avm/rlp/doc.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  /*
    17  Package rlp implements the RLP serialization format.
    18  
    19  The purpose of RLP (Recursive Linear Prefix) is to encode arbitrarily nested arrays of
    20  binary data, and RLP is the main encoding method used to serialize objects in Ethereum.
    21  The only purpose of RLP is to encode structure; encoding specific atomic data types (eg.
    22  strings, ints, floats) is left up to higher-order protocols. In Ethereum integers must be
    23  represented in big endian binary form with no leading zeroes (thus making the integer
    24  value zero equivalent to the empty string).
    25  
    26  RLP values are distinguished by a type tag. The type tag precedes the value in the input
    27  stream and defines the size and kind of the bytes that follow.
    28  
    29  # Encoding Rules
    30  
    31  Package rlp uses reflection and encodes RLP based on the Go type of the value.
    32  
    33  If the type implements the Encoder interface, Encode calls EncodeRLP. It does not
    34  call EncodeRLP on nil pointer values.
    35  
    36  To encode a pointer, the value being pointed to is encoded. A nil pointer to a struct
    37  type, slice or array always encodes as an empty RLP list unless the slice or array has
    38  elememt type byte. A nil pointer to any other value encodes as the empty string.
    39  
    40  Struct values are encoded as an RLP list of all their encoded public fields. Recursive
    41  struct types are supported.
    42  
    43  To encode slices and arrays, the elements are encoded as an RLP list of the value's
    44  elements. Note that arrays and slices with element type uint8 or byte are always encoded
    45  as an RLP string.
    46  
    47  A Go string is encoded as an RLP string.
    48  
    49  An unsigned integer value is encoded as an RLP string. Zero always encodes as an empty RLP
    50  string. big.Int values are treated as integers. Signed integers (int, int8, int16, ...)
    51  are not supported and will return an error when encoding.
    52  
    53  Boolean values are encoded as the unsigned integers zero (false) and one (true).
    54  
    55  An interface value encodes as the value contained in the interface.
    56  
    57  Floating point numbers, maps, channels and functions are not supported.
    58  
    59  # Decoding Rules
    60  
    61  Decoding uses the following type-dependent rules:
    62  
    63  If the type implements the Decoder interface, DecodeRLP is called.
    64  
    65  To decode into a pointer, the value will be decoded as the element type of the pointer. If
    66  the pointer is nil, a new value of the pointer's element type is allocated. If the pointer
    67  is non-nil, the existing value will be reused. Note that package rlp never leaves a
    68  pointer-type struct field as nil unless one of the "nil" struct tags is present.
    69  
    70  To decode into a struct, decoding expects the input to be an RLP list. The decoded
    71  elements of the list are assigned to each public field in the order given by the struct's
    72  definition. The input list must contain an element for each decoded field. Decoding
    73  returns an error if there are too few or too many elements for the struct.
    74  
    75  To decode into a slice, the input must be a list and the resulting slice will contain the
    76  input elements in order. For byte slices, the input must be an RLP string. Array types
    77  decode similarly, with the additional restriction that the number of input elements (or
    78  bytes) must match the array's defined length.
    79  
    80  To decode into a Go string, the input must be an RLP string. The input bytes are taken
    81  as-is and will not necessarily be valid UTF-8.
    82  
    83  To decode into an unsigned integer type, the input must also be an RLP string. The bytes
    84  are interpreted as a big endian representation of the integer. If the RLP string is larger
    85  than the bit size of the type, decoding will return an error. Decode also supports
    86  *big.Int. There is no size limit for big integers.
    87  
    88  To decode into a boolean, the input must contain an unsigned integer of value zero (false)
    89  or one (true).
    90  
    91  To decode into an interface value, one of these types is stored in the value:
    92  
    93  	[]interface{}, for RLP lists
    94  	[]byte, for RLP strings
    95  
    96  Non-empty interface types are not supported when decoding.
    97  Signed integers, floating point numbers, maps, channels and functions cannot be decoded into.
    98  
    99  # Struct Tags
   100  
   101  As with other encoding packages, the "-" tag ignores fields.
   102  
   103  	type StructWithIgnoredField struct{
   104  	    Ignored uint `rlp:"-"`
   105  	    Field   uint
   106  	}
   107  
   108  Go struct values encode/decode as RLP lists. There are two ways of influencing the mapping
   109  of fields to list elements. The "tail" tag, which may only be used on the last exported
   110  struct field, allows slurping up any excess list elements into a slice.
   111  
   112  	type StructWithTail struct{
   113  	    Field   uint
   114  	    Tail    []string `rlp:"tail"`
   115  	}
   116  
   117  The "optional" tag says that the field may be omitted if it is zero-valued. If this tag is
   118  used on a struct field, all subsequent public fields must also be declared optional.
   119  
   120  When encoding a struct with optional fields, the output RLP list contains all values up to
   121  the last non-zero optional field.
   122  
   123  When decoding into a struct, optional fields may be omitted from the end of the input
   124  list. For the example below, this means input lists of one, two, or three elements are
   125  accepted.
   126  
   127  	type StructWithOptionalFields struct{
   128  	     Required  uint
   129  	     Optional1 uint `rlp:"optional"`
   130  	     Optional2 uint `rlp:"optional"`
   131  	}
   132  
   133  The "nil", "nilList" and "nilString" tags apply to pointer-typed fields only, and change
   134  the decoding rules for the field type. For regular pointer fields without the "nil" tag,
   135  input values must always match the required input length exactly and the decoder does not
   136  produce nil values. When the "nil" tag is set, input values of size zero decode as a nil
   137  pointer. This is especially useful for recursive types.
   138  
   139  	type StructWithNilField struct {
   140  	    Field *[3]byte `rlp:"nil"`
   141  	}
   142  
   143  In the example above, Field allows two possible input sizes. For input 0xC180 (a list
   144  containing an empty string) Field is set to nil after decoding. For input 0xC483000000 (a
   145  list containing a 3-byte string), Field is set to a non-nil array pointer.
   146  
   147  RLP supports two kinds of empty values: empty lists and empty strings. When using the
   148  "nil" tag, the kind of empty value allowed for a type is chosen automatically. A field
   149  whose Go type is a pointer to an unsigned integer, string, boolean or byte array/slice
   150  expects an empty RLP string. Any other pointer field type encodes/decodes as an empty RLP
   151  list.
   152  
   153  The choice of null value can be made explicit with the "nilList" and "nilString" struct
   154  tags. Using these tags encodes/decodes a Go nil pointer value as the empty RLP value kind
   155  defined by the tag.
   156  */
   157  package rlp