Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unit tests for rlp decoding #53

Merged
merged 8 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions rlp/rlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,38 @@ type RLP struct {
// Decode decodes val into RLP and returns it.
func Decode(val []byte, block uint64) (*RLP, error) {
var (
substateRLP RLP
substateRLP = new(RLP)
err error
)

err = rlp.DecodeBytes(val, &substateRLP)
if err == nil {
return &substateRLP, nil
}
if IsLondonFork(block) {
err = rlp.DecodeBytes(val, substateRLP)
if err != nil {
return nil, err
}

return substateRLP, nil
} else if IsBerlinFork(block) {
var berlin berlinRLP
err = rlp.DecodeBytes(val, &berlin)
if err != nil {
return nil, err
}

var berlin berlinRLP
err = rlp.DecodeBytes(val, &berlin)
if err == nil {
return berlin.toLondon(), nil
}
} else {
var legacy legacyRLP
err = rlp.DecodeBytes(val, &legacy)
if err != nil {
return nil, err
}

var legacy legacyRLP
err = rlp.DecodeBytes(val, &legacy)
if err == nil {
return nil, err
return legacy.toLondon(), nil
}

return legacy.toLondon(), nil
}

// ToSubstate transforms every attribute of r from RLP to substate.Substate.
func (r RLP) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, error), block uint64, tx int) (*substate.Substate, error) {
func (r *RLP) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, error), block uint64, tx int) (*substate.Substate, error) {
msg, err := r.Message.ToSubstate(getHashFunc)
if err != nil {
return nil, err
Expand Down
117 changes: 117 additions & 0 deletions rlp/rlp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package rlp

import (
"bytes"
"math/big"
"testing"

"github.com/Fantom-foundation/Substate/geth/rlp"
)

func Test_DecodeLondon(t *testing.T) {
london := RLP{
Message: &Message{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &Env{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(london)
if err != nil {
t.Fatal(err)
}

res, err := Decode(b, londonBlock)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(res.Message.Data, []byte{1}) {
t.Fatal("incorrect data")
}
}

func Test_DecodeLondon_IncorrectDecoder(t *testing.T) {
london := RLP{
Message: &Message{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &Env{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(london)
if err != nil {
t.Fatal(err)
}

_, err = Decode(b, berlinBlock)
if err == nil {
t.Fatal(err)
}
}

func Test_DecodeBerlin(t *testing.T) {
berlin := berlinRLP{
Message: &berlinMessage{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &legacyEnv{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(berlin)
if err != nil {
t.Fatal(err)
}

res, err := Decode(b, berlinBlock)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(res.Message.Data, []byte{1}) {
t.Fatal("incorrect data")
}
}

func Test_DecodeBerlin_IncorrectDecoder(t *testing.T) {
berlin := berlinRLP{
Message: &berlinMessage{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &legacyEnv{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(berlin)
if err != nil {
t.Fatal(err)
}

_, err = Decode(b, londonBlock)
if err == nil {
t.Fatal(err)
}
}

func Test_DecodeLegacy(t *testing.T) {
legacy := legacyRLP{
Message: &legacyMessage{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &legacyEnv{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(legacy)
if err != nil {
t.Fatal(err)
}

res, err := Decode(b, berlinBlock-1)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(res.Message.Data, []byte{1}) {
t.Fatal("incorrect data")
}
}

func Test_DecodeLegacy_IncorrectDecoder(t *testing.T) {
legacy := legacyRLP{
Message: &legacyMessage{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &legacyEnv{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(legacy)
if err != nil {
t.Fatal(err)
}

_, err = Decode(b, londonBlock)
if err == nil {
t.Fatal(err)
}
}
54 changes: 54 additions & 0 deletions types/rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ var (
// Kind represents the kind of value contained in an RLP stream.
type Kind int8

func (k Kind) String() string {
switch k {
case Byte:
return "Byte"
case String:
return "String"
case List:
return "List"
default:
return fmt.Sprintf("Unknown(%d)", k)
}
}

// Decode parses RLP-encoded data from r and stores the result in the value pointed to by
// val. Please see package-level documentation for the decoding rules. Val must be a
// non-nil pointer.
//
// If r does not implement ByteReader, Decode will do its own buffering.
//
// Note that Decode does not set an input limit for all readers and may be vulnerable to
// panics cause by huge value sizes. If you need an input limit, use
//
// NewStream(r, limit).Decode(val)
func Decode(r io.Reader, val interface{}) error {
stream := streamPool.Get().(*Stream)
defer streamPool.Put(stream)

stream.Reset(r, 0)
return stream.Decode(val)
}

// DecodeBytes parses RLP data from b into val. Please see package-level documentation for
// the decoding rules. The input must contain exactly one value and no trailing data.
func DecodeBytes(b []byte, val interface{}) error {
Expand Down Expand Up @@ -100,6 +131,29 @@ type Stream struct {
limited bool // true if input limit is in effect
}

// NewStream creates a new decoding stream reading from r.
//
// If r implements the ByteReader interface, Stream will
// not introduce any buffering.
//
// For non-toplevel values, Stream returns ErrElemTooLarge
// for values that do not fit into the enclosing list.
//
// Stream supports an optional input limit. If a limit is set, the
// size of any toplevel value will be checked against the remaining
// input length. Stream operations that encounter a value exceeding
// the remaining input length will return ErrValueTooLarge. The limit
// can be set by passing a non-zero value for inputLimit.
//
// If r is a bytes.Reader or strings.Reader, the input limit is set to
// the length of r's underlying data unless an explicit limit is
// provided.
func NewStream(r io.Reader, inputLimit uint64) *Stream {
s := new(Stream)
s.Reset(r, inputLimit)
return s
}

// Decode decodes a value and stores the result in the value pointed
// to by val. Please see the documentation for the Decode function
// to learn about the decoding rules.
Expand Down
Loading