Skip to content

Conversation

@gzliudan
Copy link
Collaborator

Proposed changes

This adds a new type wrapper that decodes as a list, but does not actually decode the contents of the list. The type parameter exists as a marker, and enables decoding the elements lazily. RawList can also be used for building a list incrementally.

Ref: ethereum#33755

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

This adds a new type wrapper that decodes as a list, but does not
actually decode the contents of the list. The type parameter exists as a
marker, and enables decoding the elements lazily. RawList can also be
used for building a list incrementally.
Copilot AI review requested due to automatic review settings February 10, 2026 07:59
@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a RawList[T] wrapper to represent RLP lists in encoded form (allowing lazy element decoding and incremental construction), plus iterator enhancements needed to support raw list iteration with offsets.

Changes:

  • Introduce RawList[T] with list-level encode/decode, content access, iteration, and append support.
  • Add EncodeToRawList helper for producing RawList[T] from a typed slice.
  • Replace the old list iterator type with an exported Iterator that tracks Offset() and supports Count().

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
rlp/raw.go Adds RawList[T] type and core behaviors (decode/encode, Content/Bytes, Len/Size, Items, iterator, Append).
rlp/encode.go Adds EncodeToRawList[T] helper to build a RawList[T] from a slice.
rlp/iterator.go Exports iterator as Iterator, adds offset tracking and a remaining-count helper.
rlp/raw_test.go Adds comprehensive tests for RawList decoding/encoding, iteration, optional pointers, and append.
rlp/iterator_test.go Extends iterator test coverage to validate Offset() behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +67 to +74
// Count returns the remaining number of items.
// Note this is O(n) and the result may be incorrect if the list data is invalid.
// The returned count is always an upper bound on the remaining items
// that will be visited by the iterator.
func (it *Iterator) Count() int {
count, _ := CountValues(it.data)
return count
}
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterator.Count's doc claims the returned value is "always an upper bound" even when list data is invalid, but the implementation ignores CountValues errors. Since CountValues returns 0 on parse error, Count can return 0 even when there are still decodable items remaining, violating the stated guarantee. Either relax the doc to remove the upper-bound guarantee or compute an actual upper bound even in the presence of invalid data.

Copilot uses AI. Check for mistakes.
Comment on lines +96 to +101
items := make([]T, r.Len())
it := r.ContentIterator()
for i := 0; it.Next(); i++ {
if err := DecodeBytes(it.Value(), &items[i]); err != nil {
return items[:i], err
}
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RawList.Items preallocates the result slice using r.Len(), but Len ignores CountValues errors (CountValues returns 0 on parse error). For partially-invalid content (some valid items followed by invalid bytes), Len will be 0 and Items will panic when indexing items[i] on the first successful iteration. Consider building the slice incrementally (append per decoded item) and returning iterator/parse errors instead of relying on Len for sizing.

Suggested change
items := make([]T, r.Len())
it := r.ContentIterator()
for i := 0; it.Next(); i++ {
if err := DecodeBytes(it.Value(), &items[i]); err != nil {
return items[:i], err
}
it := r.ContentIterator()
var items []T
for it.Next() {
var v T
if err := DecodeBytes(it.Value(), &v); err != nil {
return items, err
}
items = append(items, v)

Copilot uses AI. Check for mistakes.
Comment on lines +105 to +126
// EncodeToRawList encodes val as an RLP list and returns it as a RawList.
func EncodeToRawList[T any](val []T) (RawList[T], error) {
if len(val) == 0 {
return RawList[T]{}, nil
}

// Encode the value to an internal buffer.
buf := getEncBuffer()
defer encBufferPool.Put(buf)
if err := buf.encode(val); err != nil {
return RawList[T]{}, err
}

// Create the RawList. RawList assumes the initial list header is padded
// 9 bytes, so we have to determine the offset where the value should be
// placed.
contentSize := buf.lheads[0].size
bytes := make([]byte, contentSize+9)
offset := 9 - headsize(uint64(contentSize))
buf.copyTo(bytes[offset:])
return RawList[T]{enc: bytes}, nil
}
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EncodeToRawList assumes encoding val produces an outer list header in buf.lheads[0]. This is false for some []T inputs (e.g., T==byte/uint8, where RLP encodes []byte as a string), which will leave lheads empty and cause an index-out-of-range panic. Please add a guard (e.g., verify the encoded kind is List / len(lheads)>0) and return a descriptive error when the input encodes as a string rather than a list.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants