doc.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. /*
  17. Package rlp implements the RLP serialization format.
  18. The purpose of RLP (Recursive Linear Prefix) is to encode arbitrarily nested arrays of
  19. binary data, and RLP is the main encoding method used to serialize objects in Ethereum.
  20. The only purpose of RLP is to encode structure; encoding specific atomic data types (eg.
  21. strings, ints, floats) is left up to higher-order protocols. In Ethereum integers must be
  22. represented in big endian binary form with no leading zeroes (thus making the integer
  23. value zero equivalent to the empty string).
  24. RLP values are distinguished by a type tag. The type tag precedes the value in the input
  25. stream and defines the size and kind of the bytes that follow.
  26. Encoding Rules
  27. Package rlp uses reflection and encodes RLP based on the Go type of the value.
  28. If the type implements the Encoder interface, Encode calls EncodeRLP. It does not
  29. call EncodeRLP on nil pointer values.
  30. To encode a pointer, the value being pointed to is encoded. A nil pointer to a struct
  31. type, slice or array always encodes as an empty RLP list unless the slice or array has
  32. elememt type byte. A nil pointer to any other value encodes as the empty string.
  33. Struct values are encoded as an RLP list of all their encoded public fields. Recursive
  34. struct types are supported.
  35. To encode slices and arrays, the elements are encoded as an RLP list of the value's
  36. elements. Note that arrays and slices with element type uint8 or byte are always encoded
  37. as an RLP string.
  38. A Go string is encoded as an RLP string.
  39. An unsigned integer value is encoded as an RLP string. Zero always encodes as an empty RLP
  40. string. big.Int values are treated as integers. Signed integers (int, int8, int16, ...)
  41. are not supported and will return an error when encoding.
  42. Boolean values are encoded as the unsigned integers zero (false) and one (true).
  43. An interface value encodes as the value contained in the interface.
  44. Floating point numbers, maps, channels and functions are not supported.
  45. Decoding Rules
  46. Decoding uses the following type-dependent rules:
  47. If the type implements the Decoder interface, DecodeRLP is called.
  48. To decode into a pointer, the value will be decoded as the element type of the pointer. If
  49. the pointer is nil, a new value of the pointer's element type is allocated. If the pointer
  50. is non-nil, the existing value will be reused. Note that package rlp never leaves a
  51. pointer-type struct field as nil unless one of the "nil" struct tags is present.
  52. To decode into a struct, decoding expects the input to be an RLP list. The decoded
  53. elements of the list are assigned to each public field in the order given by the struct's
  54. definition. The input list must contain an element for each decoded field. Decoding
  55. returns an error if there are too few or too many elements for the struct.
  56. To decode into a slice, the input must be a list and the resulting slice will contain the
  57. input elements in order. For byte slices, the input must be an RLP string. Array types
  58. decode similarly, with the additional restriction that the number of input elements (or
  59. bytes) must match the array's defined length.
  60. To decode into a Go string, the input must be an RLP string. The input bytes are taken
  61. as-is and will not necessarily be valid UTF-8.
  62. To decode into an unsigned integer type, the input must also be an RLP string. The bytes
  63. are interpreted as a big endian representation of the integer. If the RLP string is larger
  64. than the bit size of the type, decoding will return an error. Decode also supports
  65. *big.Int. There is no size limit for big integers.
  66. To decode into a boolean, the input must contain an unsigned integer of value zero (false)
  67. or one (true).
  68. To decode into an interface value, one of these types is stored in the value:
  69. []interface{}, for RLP lists
  70. []byte, for RLP strings
  71. Non-empty interface types are not supported when decoding.
  72. Signed integers, floating point numbers, maps, channels and functions cannot be decoded into.
  73. Struct Tags
  74. Package rlp honours certain struct tags: "-", "tail", "nil", "nilList" and "nilString".
  75. The "-" tag ignores fields.
  76. The "tail" tag, which may only be used on the last exported struct field, allows slurping
  77. up any excess list elements into a slice. See examples for more details.
  78. The "nil" tag applies to pointer-typed fields and changes the decoding rules for the field
  79. such that input values of size zero decode as a nil pointer. This tag can be useful when
  80. decoding recursive types.
  81. type StructWithOptionalFoo struct {
  82. Foo *[20]byte `rlp:"nil"`
  83. }
  84. RLP supports two kinds of empty values: empty lists and empty strings. When using the
  85. "nil" tag, the kind of empty value allowed for a type is chosen automatically. A struct
  86. field whose Go type is a pointer to an unsigned integer, string, boolean or byte
  87. array/slice expects an empty RLP string. Any other pointer field type encodes/decodes as
  88. an empty RLP list.
  89. The choice of null value can be made explicit with the "nilList" and "nilString" struct
  90. tags. Using these tags encodes/decodes a Go nil pointer value as the kind of empty
  91. RLP value defined by the tag.
  92. */
  93. package rlp