Skip to content

Commit 76e3a36

Browse files
committed
core/txpool/blobpool: add test
1 parent d75e28e commit 76e3a36

File tree

1 file changed

+91
-10
lines changed

1 file changed

+91
-10
lines changed

core/txpool/blobpool/blobpool_test.go

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"path/filepath"
2929
"reflect"
30+
"slices"
3031
"sync"
3132
"testing"
3233

@@ -47,11 +48,12 @@ import (
4748
)
4849

4950
var (
50-
testBlobs []*kzg4844.Blob
51-
testBlobCommits []kzg4844.Commitment
52-
testBlobProofs []kzg4844.Proof
53-
testBlobVHashes [][32]byte
54-
testBlobIndices = make(map[[32]byte]int)
51+
testBlobs []*kzg4844.Blob
52+
testBlobCommits []kzg4844.Commitment
53+
testBlobProofs []kzg4844.Proof
54+
testBlobCellProofs [][]kzg4844.Proof
55+
testBlobVHashes [][32]byte
56+
testBlobIndices = make(map[[32]byte]int)
5557
)
5658

5759
const testMaxBlobsPerBlock = 6
@@ -67,6 +69,9 @@ func init() {
6769
testBlobProof, _ := kzg4844.ComputeBlobProof(testBlob, testBlobCommit)
6870
testBlobProofs = append(testBlobProofs, testBlobProof)
6971

72+
testBlobCellProof, _ := kzg4844.ComputeCellProofs(testBlob)
73+
testBlobCellProofs = append(testBlobCellProofs, testBlobCellProof)
74+
7075
testBlobVHash := kzg4844.CalcBlobHashV1(sha256.New(), &testBlobCommit)
7176
testBlobIndices[testBlobVHash] = len(testBlobVHashes)
7277
testBlobVHashes = append(testBlobVHashes, testBlobVHash)
@@ -416,24 +421,40 @@ func verifyBlobRetrievals(t *testing.T, pool *BlobPool) {
416421
hashes = append(hashes, tx.vhashes...)
417422
}
418423
}
419-
blobs, _, proofs, err := pool.GetBlobs(hashes, types.BlobSidecarVersion0)
424+
blobs1, _, proofs1, err := pool.GetBlobs(hashes, types.BlobSidecarVersion0)
425+
if err != nil {
426+
t.Fatal(err)
427+
}
428+
blobs2, _, proofs2, err := pool.GetBlobs(hashes, types.BlobSidecarVersion1)
420429
if err != nil {
421430
t.Fatal(err)
422431
}
423432
// Cross validate what we received vs what we wanted
424-
if len(blobs) != len(hashes) || len(proofs) != len(hashes) {
425-
t.Errorf("retrieved blobs/proofs size mismatch: have %d/%d, want %d", len(blobs), len(proofs), len(hashes))
433+
if len(blobs1) != len(hashes) || len(proofs1) != len(hashes) {
434+
t.Errorf("retrieved blobs/proofs size mismatch: have %d/%d, want %d", len(blobs1), len(proofs1), len(hashes))
435+
return
436+
}
437+
if len(blobs2) != len(hashes) || len(proofs2) != len(hashes) {
438+
t.Errorf("retrieved blobs/proofs size mismatch: have %d/%d, want blobs %d, want proofs: %d", len(blobs2), len(proofs2), len(hashes), len(hashes))
426439
return
427440
}
428441
for i, hash := range hashes {
429442
// If an item is missing, but shouldn't, error
430-
if blobs[i] == nil || proofs[i] == nil {
443+
if blobs1[i] == nil || proofs1[i] == nil {
444+
t.Errorf("tracked blob retrieval failed: item %d, hash %x", i, hash)
445+
continue
446+
}
447+
if blobs2[i] == nil || proofs2[i] == nil {
431448
t.Errorf("tracked blob retrieval failed: item %d, hash %x", i, hash)
432449
continue
433450
}
434451
// Item retrieved, make sure it matches the expectation
435452
index := testBlobIndices[hash]
436-
if *blobs[i] != *testBlobs[index] || proofs[i][0] != testBlobProofs[index] {
453+
if *blobs1[i] != *testBlobs[index] || proofs1[i][0] != testBlobProofs[index] {
454+
t.Errorf("retrieved blob or proof mismatch: item %d, hash %x", i, hash)
455+
continue
456+
}
457+
if *blobs2[i] != *testBlobs[index] || !slices.Equal(proofs2[i], testBlobCellProofs[index]) {
437458
t.Errorf("retrieved blob or proof mismatch: item %d, hash %x", i, hash)
438459
continue
439460
}
@@ -1668,6 +1689,66 @@ func TestAdd(t *testing.T) {
16681689
}
16691690
}
16701691

1692+
// Tests that adding the transactions with legacy sidecar and expect them to
1693+
// be converted to new format correctly.
1694+
func TestAddLegacyBlobTx(t *testing.T) {
1695+
var (
1696+
key1, _ = crypto.GenerateKey()
1697+
key2, _ = crypto.GenerateKey()
1698+
1699+
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
1700+
addr2 = crypto.PubkeyToAddress(key2.PublicKey)
1701+
)
1702+
1703+
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
1704+
statedb.AddBalance(addr1, uint256.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified)
1705+
statedb.AddBalance(addr2, uint256.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified)
1706+
statedb.Commit(0, true, false)
1707+
1708+
// Make Prague-enabled custom chain config.
1709+
cancunTime := uint64(0)
1710+
pragueTime := uint64(0)
1711+
osakaTime := uint64(0)
1712+
config := &params.ChainConfig{
1713+
ChainID: big.NewInt(1),
1714+
LondonBlock: big.NewInt(0),
1715+
BerlinBlock: big.NewInt(0),
1716+
CancunTime: &cancunTime,
1717+
PragueTime: &pragueTime,
1718+
OsakaTime: &osakaTime,
1719+
BlobScheduleConfig: &params.BlobScheduleConfig{
1720+
Cancun: params.DefaultCancunBlobConfig,
1721+
Prague: params.DefaultPragueBlobConfig,
1722+
Osaka: params.DefaultOsakaBlobConfig,
1723+
},
1724+
}
1725+
chain := &testBlockChain{
1726+
config: config,
1727+
basefee: uint256.NewInt(1050),
1728+
blobfee: uint256.NewInt(105),
1729+
statedb: statedb,
1730+
}
1731+
pool := New(Config{Datadir: t.TempDir()}, chain, nil)
1732+
if err := pool.Init(1, chain.CurrentBlock(), newReserver()); err != nil {
1733+
t.Fatalf("failed to create blob pool: %v", err)
1734+
}
1735+
1736+
// Attempt to add legacy blob transactions.
1737+
var (
1738+
tx1 = makeMultiBlobTx(0, 1, 1000, 100, 6, 0, key1, types.BlobSidecarVersion0)
1739+
tx2 = makeMultiBlobTx(0, 1, 800, 70, 6, 6, key2, types.BlobSidecarVersion0)
1740+
tx3 = makeMultiBlobTx(1, 1, 800, 70, 6, 12, key2, types.BlobSidecarVersion1)
1741+
)
1742+
errs := pool.Add([]*types.Transaction{tx1, tx2, tx3}, true)
1743+
for _, err := range errs {
1744+
if err != nil {
1745+
t.Fatalf("failed to add tx: %v", err)
1746+
}
1747+
}
1748+
verifyPoolInternals(t, pool)
1749+
pool.Close()
1750+
}
1751+
16711752
func TestGetBlobs(t *testing.T) {
16721753
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
16731754

0 commit comments

Comments
 (0)