go-ethereum/rlp
Arran Schlosberg eda3b59f67
feat(core/types): fine-grained Body RLP override (#109)
## Why this should be merged

Allows for modification of `types.Body` payload data + RLP encoding
without placing the entire RLP burden on the `libevm` user as we did
with `types.HeaderHooks`.

## How this works

RLP encoding of a struct is simply a concatenation of RLP encodings of
fields, encompassed by an RLP "list". The
`AppendRLPFields(rlp.EncoderBuffer, ...)` hook exploits this and plugs
in before all `rlp:"optional"`-tagged fields to allow for inclusion of
any new fields. The `EncoderBuffer` SHOULD be used as the `io.Writer`
passed when encoding each field: `rlp.Encode(buffer, fieldValue)`.

`Body` doesn't have `{En,De}codeRLP` methods so they are implemented to
identically replicate original behaviour when a no-op hook is present.

This pattern is sufficient for the `ava-labs/coreth` modifications of
`Body` but can be modified / extended for more complex scenarios, like
`Header`.

> [!NOTE]
> This PR does not include registration of the hooks as that was not the
initial goal and adding them would create too much PR bloat. There is a
placeholder `var todoRegisteredBodyHooks` global variable that can only
be set in tests.

## How this was tested

- Backwards compatibility: the new methods are fuzzed against a `type
withoutMethods Body` passed directly to `rlp.{En,De}code()`
- `coreth` compatibility: unit test of a local implementation of
`BodyHooks` demonstrating reproducibility of RLP encoding.

---------

Signed-off-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
Co-authored-by: Quentin McGaw <quentin.mcgaw@avalabs.org>
2025-02-05 10:52:28 +00:00
..
internal/rlpstruct all: fix docstrings 2022-10-04 09:18:02 +02:00
rlpgen feat: internalise command (#90) 2024-12-17 17:20:02 +01:00
decode.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
decode_tail_test.go all: update license information 2016-04-15 09:48:05 +02:00
decode_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
doc.go build: upgrade to go 1.19 (#25726) 2022-09-10 13:25:40 +02:00
encbuffer.go rlp: use identical receiver names for encBuffer methods (#27430) 2023-06-07 12:40:06 +02:00
encbuffer_example_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
encode.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
encode_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
encoder_example_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
iterator.go all: add whitespace linter (#25312) 2022-07-25 13:14:03 +03:00
iterator_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
list.libevm.go feat(core/types): fine-grained Body RLP override (#109) 2025-02-05 10:52:28 +00:00
list.libevm_test.go feat(core/types): fine-grained Body RLP override (#109) 2025-02-05 10:52:28 +00:00
raw.go p2p/discover: improve discv5 NODES response packing (#26033) 2022-11-07 19:19:02 +01:00
raw_test.go rlp: improve tests for Split functions (#26200) 2022-11-17 14:19:03 +01:00
safe.go rlp: minor optimizations for slice/array encoding (#23467) 2021-08-25 19:01:10 +02:00
typecache.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
unsafe.go rlp: minor optimizations for slice/array encoding (#23467) 2021-08-25 19:01:10 +02:00