mirror of https://github.com/ethereum/go-ethereum
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way of keeping the blob sidecar within types.Transaction. It's better this way because most code in go-ethereum does not care about blob transactions, and probably never will. This will start mattering especially on the client side of RPC, where all APIs are based on types.Transaction. Users need to be able to use the same signing flows they already have. However, since blobs are only allowed in some places but not others, we will now need to add checks to avoid creating invalid blocks. I'm still trying to figure out the best place to do some of these. The way I have it currently is as follows: - In block validation (import), txs are verified not to have a blob sidecar. - In miner, we strip off the sidecar when committing the transaction into the block. - In TxPool validation, txs must have a sidecar to be added into the blobpool. - Note there is a special case here: when transactions are re-added because of a chain reorg, we cannot use the transactions gathered from the old chain blocks as-is, because they will be missing their blobs. This was previously handled by storing the blobs into the 'blobpool limbo'. The code has now changed to store the full transaction in the limbo instead, but it might be confusing for code readers why we're not simply adding the types.Transaction we already have. Code changes summary: - txpool.Transaction removed and all uses replaced by types.Transaction again - blobpool now stores types.Transaction instead of defining its own blobTx format for storage - the blobpool limbo now stores types.Transaction instead of storing only the blobs - checks to validate the presence/absence of the blob sidecar added in certain critical placespull/27899/head^2
parent
68860063fb
commit
2a6beb6a39
@ -0,0 +1,90 @@ |
||||
package types |
||||
|
||||
import ( |
||||
"crypto/ecdsa" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/crypto/kzg4844" |
||||
"github.com/holiman/uint256" |
||||
) |
||||
|
||||
// This test verifies that tx.Hash() is not affected by presence of a BlobTxSidecar.
|
||||
func TestBlobTxHashing(t *testing.T) { |
||||
key, _ := crypto.GenerateKey() |
||||
withBlobs := createEmptyBlobTx(key, true) |
||||
withBlobsStripped := withBlobs.WithoutBlobTxSidecar() |
||||
withoutBlobs := createEmptyBlobTx(key, false) |
||||
|
||||
hash := withBlobs.Hash() |
||||
t.Log("tx hash:", hash) |
||||
|
||||
if h := withBlobsStripped.Hash(); h != hash { |
||||
t.Fatal("wrong tx hash after WithoutBlobTxSidecar:", h) |
||||
} |
||||
if h := withoutBlobs.Hash(); h != hash { |
||||
t.Fatal("wrong tx hash on tx created without sidecar:", h) |
||||
} |
||||
} |
||||
|
||||
// This test verifies that tx.Size() takes BlobTxSidecar into account.
|
||||
func TestBlobTxSize(t *testing.T) { |
||||
key, _ := crypto.GenerateKey() |
||||
withBlobs := createEmptyBlobTx(key, true) |
||||
withBlobsStripped := withBlobs.WithoutBlobTxSidecar() |
||||
withoutBlobs := createEmptyBlobTx(key, false) |
||||
|
||||
withBlobsEnc, _ := withBlobs.MarshalBinary() |
||||
withoutBlobsEnc, _ := withoutBlobs.MarshalBinary() |
||||
|
||||
size := withBlobs.Size() |
||||
t.Log("size with blobs:", size) |
||||
|
||||
sizeNoBlobs := withoutBlobs.Size() |
||||
t.Log("size without blobs:", sizeNoBlobs) |
||||
|
||||
if size != uint64(len(withBlobsEnc)) { |
||||
t.Error("wrong size with blobs:", size, "encoded length:", len(withBlobsEnc)) |
||||
} |
||||
if sizeNoBlobs != uint64(len(withoutBlobsEnc)) { |
||||
t.Error("wrong size without blobs:", sizeNoBlobs, "encoded length:", len(withoutBlobsEnc)) |
||||
} |
||||
if sizeNoBlobs >= size { |
||||
t.Error("size without blobs >= size with blobs") |
||||
} |
||||
if sz := withBlobsStripped.Size(); sz != sizeNoBlobs { |
||||
t.Fatal("wrong size on tx after WithoutBlobTxSidecar:", sz) |
||||
} |
||||
} |
||||
|
||||
var ( |
||||
emptyBlob = kzg4844.Blob{} |
||||
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob) |
||||
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit) |
||||
) |
||||
|
||||
func createEmptyBlobTx(key *ecdsa.PrivateKey, withSidecar bool) *Transaction { |
||||
sidecar := &BlobTxSidecar{ |
||||
Blobs: []kzg4844.Blob{emptyBlob}, |
||||
Commitments: []kzg4844.Commitment{emptyBlobCommit}, |
||||
Proofs: []kzg4844.Proof{emptyBlobProof}, |
||||
} |
||||
blobtx := &BlobTx{ |
||||
ChainID: uint256.NewInt(1), |
||||
Nonce: 5, |
||||
GasTipCap: uint256.NewInt(22), |
||||
GasFeeCap: uint256.NewInt(5), |
||||
Gas: 25000, |
||||
To: common.Address{0x03, 0x04, 0x05}, |
||||
Value: uint256.NewInt(99), |
||||
Data: make([]byte, 50), |
||||
BlobFeeCap: uint256.NewInt(15), |
||||
BlobHashes: sidecar.BlobHashes(), |
||||
} |
||||
if withSidecar { |
||||
blobtx.Sidecar = sidecar |
||||
} |
||||
signer := NewCancunSigner(blobtx.ChainID.ToBig()) |
||||
return MustSignNewTx(key, signer, blobtx) |
||||
} |
Loading…
Reference in new issue