include blob txs in the unit test
This commit is contained in:
parent
768a29e3a4
commit
1103cb388b
|
@ -37,8 +37,10 @@ import (
|
||||||
var _ bind.ContractBackend = (Client)(nil)
|
var _ bind.ContractBackend = (Client)(nil)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
||||||
|
testKey2, _ = crypto.HexToECDSA("7ee346e3f7efc685250053bfbafbfc880d58dc6145247053d4fb3cb0f66dfcb2")
|
||||||
|
testAddr2 = crypto.PubkeyToAddress(testKey2.PublicKey)
|
||||||
)
|
)
|
||||||
|
|
||||||
func simTestBackend(testAddr common.Address) *Backend {
|
func simTestBackend(testAddr common.Address) *Backend {
|
||||||
|
|
|
@ -2,39 +2,50 @@ package simulated
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestTransactionRollbackBehavior verifies the behavior of transactions
|
// TestTransactionRollbackBehavior tests that calling Rollback on the simulated backend doesn't prevent subsequent
|
||||||
// in the simulated backend after rollback operations.
|
// addition of new transactions
|
||||||
//
|
|
||||||
// The test demonstrates that after a rollback:
|
|
||||||
// 1. The first test shows normal transaction processing without rollback
|
|
||||||
// 2. The second test shows that transactions immediately after rollback fail
|
|
||||||
// 3. The third test shows a workaround: committing an empty block after rollback
|
|
||||||
// makes subsequent transactions succeed
|
|
||||||
func TestTransactionRollbackBehavior(t *testing.T) {
|
func TestTransactionRollbackBehavior(t *testing.T) {
|
||||||
sim := simTestBackend(testAddr)
|
sim := NewBackend(
|
||||||
|
types.GenesisAlloc{
|
||||||
|
testAddr: {Balance: big.NewInt(10000000000000000)},
|
||||||
|
testAddr2: {Balance: big.NewInt(10000000000000000)},
|
||||||
|
},
|
||||||
|
)
|
||||||
defer sim.Close()
|
defer sim.Close()
|
||||||
client := sim.Client()
|
client := sim.Client()
|
||||||
|
|
||||||
// First transaction gets rolled back
|
btx0 := testSendSignedTx(t, testKey, sim, true)
|
||||||
_ = testSendSignedTx(t, sim, true)
|
tx0 := testSendSignedTx(t, testKey2, sim, false)
|
||||||
|
tx1 := testSendSignedTx(t, testKey2, sim, false)
|
||||||
|
|
||||||
sim.Rollback()
|
sim.Rollback()
|
||||||
|
|
||||||
// Attempting to process a new transaction immediately after rollback
|
if pendingStateHasTx(client, btx0) || pendingStateHasTx(client, tx0) || pendingStateHasTx(client, tx1) {
|
||||||
// Currently, this case fails to get a valid receipt
|
t.Fatalf("all transactions were not rolled back")
|
||||||
tx := testSendSignedTx(t, sim, true)
|
}
|
||||||
|
|
||||||
|
btx2 := testSendSignedTx(t, testKey, sim, true)
|
||||||
|
tx2 := testSendSignedTx(t, testKey2, sim, false)
|
||||||
|
tx3 := testSendSignedTx(t, testKey2, sim, false)
|
||||||
|
|
||||||
sim.Commit()
|
sim.Commit()
|
||||||
assertSuccessfulReceipt(t, client, tx)
|
|
||||||
|
if !pendingStateHasTx(client, btx2) || !pendingStateHasTx(client, tx2) || !pendingStateHasTx(client, tx3) {
|
||||||
|
t.Fatalf("all post-rollback transactions were not included")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// testSendSignedTx sends a signed transaction to the simulated backend.
|
// testSendSignedTx sends a signed transaction to the simulated backend.
|
||||||
// It does not commit the block.
|
// It does not commit the block.
|
||||||
func testSendSignedTx(t *testing.T, sim *Backend, isBlobTx bool) *types.Transaction {
|
func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobTx bool) *types.Transaction {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
client := sim.Client()
|
client := sim.Client()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
@ -44,9 +55,9 @@ func testSendSignedTx(t *testing.T, sim *Backend, isBlobTx bool) *types.Transact
|
||||||
signedTx *types.Transaction
|
signedTx *types.Transaction
|
||||||
)
|
)
|
||||||
if isBlobTx {
|
if isBlobTx {
|
||||||
signedTx, err = newBlobTx(sim, testKey)
|
signedTx, err = newBlobTx(sim, key)
|
||||||
} else {
|
} else {
|
||||||
signedTx, err = newTx(sim, testKey)
|
signedTx, err = newTx(sim, key)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create transaction: %v", err)
|
t.Fatalf("failed to create transaction: %v", err)
|
||||||
|
@ -61,8 +72,7 @@ func testSendSignedTx(t *testing.T, sim *Backend, isBlobTx bool) *types.Transact
|
||||||
|
|
||||||
// assertSuccessfulReceipt verifies that a transaction was successfully included as of the pending state
|
// assertSuccessfulReceipt verifies that a transaction was successfully included as of the pending state
|
||||||
// by checking its receipt status.
|
// by checking its receipt status.
|
||||||
func assertSuccessfulReceipt(t *testing.T, client Client, tx *types.Transaction) {
|
func pendingStateHasTx(client Client, tx *types.Transaction) bool {
|
||||||
t.Helper()
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -81,12 +91,13 @@ func assertSuccessfulReceipt(t *testing.T, client Client, tx *types.Transaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to get transaction receipt: %v", err)
|
return false
|
||||||
}
|
}
|
||||||
if receipt == nil {
|
if receipt == nil {
|
||||||
t.Fatal("transaction receipt is nil")
|
return false
|
||||||
}
|
}
|
||||||
if receipt.Status != types.ReceiptStatusSuccessful {
|
if receipt.Status != types.ReceiptStatusSuccessful {
|
||||||
t.Fatalf("transaction failed with status: %v", receipt.Status)
|
return false
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue