forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blob encode/decode API, tests, plus placeholder implementation (ether…
- Loading branch information
Roberto Bayardo
authored
Jan 3, 2024
1 parent
68c6550
commit 78ecdf5
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package eth | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestBlobEncodeDecode(t *testing.T) { | ||
cases := []string{ | ||
"this is a test of blob encoding/decoding", | ||
"short", | ||
"\x00", | ||
"\x00\x01\x00", | ||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", | ||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", | ||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", | ||
"", | ||
} | ||
|
||
var b Blob | ||
for _, c := range cases { | ||
data := Data(c) | ||
if err := b.FromData(data); err != nil { | ||
t.Fatalf("failed to encode bytes: %v", err) | ||
} | ||
decoded, err := b.ToData() | ||
if err != nil { | ||
t.Fatalf("failed to decode blob: %v", err) | ||
} | ||
if string(decoded) != c { | ||
t.Errorf("decoded != input. got: %v, want: %v", decoded, Data(c)) | ||
} | ||
} | ||
} | ||
|
||
func TestBigBlobEncoding(t *testing.T) { | ||
bigData := Data(make([]byte, MaxBlobDataSize)) | ||
bigData[MaxBlobDataSize-1] = 0xFF | ||
var b Blob | ||
if err := b.FromData(bigData); err != nil { | ||
t.Fatalf("failed to encode bytes: %v", err) | ||
} | ||
decoded, err := b.ToData() | ||
if err != nil { | ||
t.Fatalf("failed to decode blob: %v", err) | ||
} | ||
if string(decoded) != string(bigData) { | ||
t.Errorf("decoded blob != big blob input") | ||
} | ||
} | ||
|
||
func TestInvalidBlobDecoding(t *testing.T) { | ||
data := Data("this is a test of invalid blob decoding") | ||
var b Blob | ||
if err := b.FromData(data); err != nil { | ||
t.Fatalf("failed to encode bytes: %v", err) | ||
} | ||
b[32] = 0x80 // field elements should never have their highest order bit set | ||
if _, err := b.ToData(); err == nil { | ||
t.Errorf("expected error, got none") | ||
} | ||
|
||
b[32] = 0x00 | ||
b[4] = 0xFF // encode an invalid (much too long) length prefix | ||
if _, err := b.ToData(); err == nil { | ||
t.Errorf("expected error, got none") | ||
} | ||
} | ||
|
||
func TestTooLongDataEncoding(t *testing.T) { | ||
// should never be able to encode data that has size the same as that of the blob due to < 256 | ||
// bit precision of each field element | ||
data := Data(make([]byte, BlobSize)) | ||
var b Blob | ||
err := b.FromData(data) | ||
if err == nil { | ||
t.Errorf("expected error, got none") | ||
} | ||
} |