internal/ethapi: fix test

This commit is contained in:
Gary Rong 2025-08-25 15:15:13 +08:00
parent b4bf2ae708
commit 3e4ed1cdc0
3 changed files with 59 additions and 24 deletions

View file

@ -1519,17 +1519,18 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction
// processing (signing + broadcast). // processing (signing + broadcast).
func (api *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) { func (api *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
config := sidecarConfig{ sidecarVersion := types.BlobSidecarVersion0
blobSidecarAllowed: true,
blobSidecarVersion: types.BlobSidecarVersion0,
}
if len(args.Blobs) > 0 { if len(args.Blobs) > 0 {
chainHead := api.b.CurrentHeader() chainHead := api.b.CurrentHeader()
isOsaka := api.b.ChainConfig().IsOsaka(chainHead.Number, chainHead.Time) isOsaka := api.b.ChainConfig().IsOsaka(chainHead.Number, chainHead.Time)
if isOsaka { if isOsaka {
config.blobSidecarVersion = types.BlobSidecarVersion1 sidecarVersion = types.BlobSidecarVersion1
} }
} }
config := sidecarConfig{
blobSidecarAllowed: true,
blobSidecarVersion: sidecarVersion,
}
if err := args.setDefaults(ctx, api.b, config); err != nil { if err := args.setDefaults(ctx, api.b, config); err != nil {
return nil, err return nil, err
} }

View file

@ -34,11 +34,9 @@ import (
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/internal/ethapi/override"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -56,6 +54,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/blocktest" "github.com/ethereum/go-ethereum/internal/blocktest"
"github.com/ethereum/go-ethereum/internal/ethapi/override"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/holiman/uint256" "github.com/holiman/uint256"
@ -2661,19 +2660,53 @@ func TestSendBlobTransaction(t *testing.T) {
func TestFillBlobTransaction(t *testing.T) { func TestFillBlobTransaction(t *testing.T) {
t.Parallel() t.Parallel()
testFillBlobTransaction(t, false)
testFillBlobTransaction(t, true)
}
func testFillBlobTransaction(t *testing.T, osaka bool) {
// Initialize test accounts // Initialize test accounts
config := *params.MergedTestChainConfig
if !osaka {
config.OsakaTime = nil
}
var ( var (
key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
to = crypto.PubkeyToAddress(key.PublicKey) to = crypto.PubkeyToAddress(key.PublicKey)
genesis = &core.Genesis{ genesis = &core.Genesis{
Config: params.MergedTestChainConfig, Config: &config,
Alloc: types.GenesisAlloc{}, Alloc: types.GenesisAlloc{},
} }
emptyBlob = new(kzg4844.Blob) emptyBlob = new(kzg4844.Blob)
emptyBlobs = []kzg4844.Blob{*emptyBlob} emptyBlobs = []kzg4844.Blob{*emptyBlob}
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob) emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit) emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
emptyBlobHash common.Hash = kzg4844.CalcBlobHashV1(sha256.New(), &emptyBlobCommit) emptyBlobCellProofs, _ = kzg4844.ComputeCellProofs(emptyBlob)
emptyBlobHash common.Hash = kzg4844.CalcBlobHashV1(sha256.New(), &emptyBlobCommit)
fillEmptyKZGProofs = func(blobs int) []kzg4844.Proof {
if osaka {
return make([]kzg4844.Proof, blobs*kzg4844.CellProofsPerBlob)
}
return make([]kzg4844.Proof, blobs)
}
expectSidecar = func() *types.BlobTxSidecar {
if osaka {
return types.NewBlobTxSidecar(
types.BlobSidecarVersion1,
emptyBlobs,
[]kzg4844.Commitment{emptyBlobCommit},
emptyBlobCellProofs,
)
}
return types.NewBlobTxSidecar(
types.BlobSidecarVersion0,
emptyBlobs,
[]kzg4844.Commitment{emptyBlobCommit},
[]kzg4844.Proof{emptyBlobProof},
)
}
) )
b := newTestBackend(t, 1, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) { b := newTestBackend(t, 1, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {
b.SetPoS() b.SetPoS()
@ -2733,7 +2766,7 @@ func TestFillBlobTransaction(t *testing.T) {
Commitments: []kzg4844.Commitment{{}, {}}, Commitments: []kzg4844.Commitment{{}, {}},
Proofs: []kzg4844.Proof{{}}, Proofs: []kzg4844.Proof{{}},
}, },
err: `number of blobs and proofs mismatch (have=1, want=2)`, err: fmt.Sprintf(`number of blobs and proofs mismatch (have=1, want=%d)`, len(fillEmptyKZGProofs(2))),
}, },
{ {
name: "TestInvalidProofVerification", name: "TestInvalidProofVerification",
@ -2743,7 +2776,7 @@ func TestFillBlobTransaction(t *testing.T) {
Value: (*hexutil.Big)(big.NewInt(1)), Value: (*hexutil.Big)(big.NewInt(1)),
Blobs: []kzg4844.Blob{{}, {}}, Blobs: []kzg4844.Blob{{}, {}},
Commitments: []kzg4844.Commitment{{}, {}}, Commitments: []kzg4844.Commitment{{}, {}},
Proofs: []kzg4844.Proof{{}, {}}, Proofs: fillEmptyKZGProofs(2),
}, },
err: `failed to verify blob proof: short buffer`, err: `failed to verify blob proof: short buffer`,
}, },
@ -2759,7 +2792,7 @@ func TestFillBlobTransaction(t *testing.T) {
}, },
want: &result{ want: &result{
Hashes: []common.Hash{emptyBlobHash}, Hashes: []common.Hash{emptyBlobHash},
Sidecar: types.NewBlobTxSidecar(types.BlobSidecarVersion0, emptyBlobs, []kzg4844.Commitment{emptyBlobCommit}, []kzg4844.Proof{emptyBlobProof}), Sidecar: expectSidecar(),
}, },
}, },
{ {
@ -2775,7 +2808,7 @@ func TestFillBlobTransaction(t *testing.T) {
}, },
want: &result{ want: &result{
Hashes: []common.Hash{emptyBlobHash}, Hashes: []common.Hash{emptyBlobHash},
Sidecar: types.NewBlobTxSidecar(types.BlobSidecarVersion0, emptyBlobs, []kzg4844.Commitment{emptyBlobCommit}, []kzg4844.Proof{emptyBlobProof}), Sidecar: expectSidecar(),
}, },
}, },
{ {
@ -2801,7 +2834,7 @@ func TestFillBlobTransaction(t *testing.T) {
}, },
want: &result{ want: &result{
Hashes: []common.Hash{emptyBlobHash}, Hashes: []common.Hash{emptyBlobHash},
Sidecar: types.NewBlobTxSidecar(types.BlobSidecarVersion0, emptyBlobs, []kzg4844.Commitment{emptyBlobCommit}, []kzg4844.Proof{emptyBlobProof}), Sidecar: expectSidecar(),
}, },
}, },
} }

View file

@ -306,9 +306,10 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sideca
if args.Commitments != nil && len(args.Commitments) != n { if args.Commitments != nil && len(args.Commitments) != n {
return fmt.Errorf("number of blobs and commitments mismatch (have=%d, want=%d)", len(args.Commitments), n) return fmt.Errorf("number of blobs and commitments mismatch (have=%d, want=%d)", len(args.Commitments), n)
} }
proofLen := n
// if V0: len(blobs) == len(proofs) // if V0: len(blobs) == len(proofs)
// if V1: len(blobs) == len(proofs) * 128 // if V1: len(blobs) == len(proofs) * 128
proofLen := n
if config.blobSidecarVersion == types.BlobSidecarVersion1 { if config.blobSidecarVersion == types.BlobSidecarVersion1 {
proofLen = n * kzg4844.CellProofsPerBlob proofLen = n * kzg4844.CellProofsPerBlob
} }
@ -326,7 +327,7 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sideca
if args.Commitments == nil { if args.Commitments == nil {
var ( var (
commitments = make([]kzg4844.Commitment, n) commitments = make([]kzg4844.Commitment, n)
proofs = make([]kzg4844.Proof, proofLen) proofs = make([]kzg4844.Proof, 0, proofLen)
) )
for i, b := range args.Blobs { for i, b := range args.Blobs {
c, err := kzg4844.BlobToCommitment(&b) c, err := kzg4844.BlobToCommitment(&b)
@ -341,11 +342,11 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sideca
if err != nil { if err != nil {
return fmt.Errorf("blobs[%d]: error computing proof: %v", i, err) return fmt.Errorf("blobs[%d]: error computing proof: %v", i, err)
} }
proofs[i] = p proofs = append(proofs, p)
case types.BlobSidecarVersion1: case types.BlobSidecarVersion1:
ps, err := kzg4844.ComputeCellProofs(&b) ps, err := kzg4844.ComputeCellProofs(&b)
if err != nil { if err != nil {
return fmt.Errorf("blobs[%d]: error computing cell proof: %v", i, err) return fmt.Errorf("blobs[%d]: error computing proof: %v", i, err)
} }
proofs = append(proofs, ps...) proofs = append(proofs, ps...)
} }
@ -362,7 +363,7 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sideca
} }
case types.BlobSidecarVersion1: case types.BlobSidecarVersion1:
if err := kzg4844.VerifyCellProofs(args.Blobs, args.Commitments, args.Proofs); err != nil { if err := kzg4844.VerifyCellProofs(args.Blobs, args.Commitments, args.Proofs); err != nil {
return fmt.Errorf("failed to verify blob cell proof: %v", err) return fmt.Errorf("failed to verify blob proof: %v", err)
} }
} }
} }