accounts/abi: add more testcase for PendingStateReader

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-10-30 17:25:33 +08:00
parent 83152bb60d
commit a64f62dee0

View file

@ -58,6 +58,11 @@ func TestSimulatedBackend(t *testing.T) {
t.Fatalf("err should be `ethereum.NotFound` but received %v", err) t.Fatalf("err should be `ethereum.NotFound` but received %v", err)
} }
pendingCount, _ := sim.PendingTransactionCount(context.Background())
if pendingCount != 0 {
t.Errorf("expected pending tx count of 0 got %v", pendingCount)
}
// generate a transaction and confirm you can retrieve it // generate a transaction and confirm you can retrieve it
head, _ := sim.HeaderByNumber(context.Background(), nil) // Should be child's, good enough head, _ := sim.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1)) gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
@ -72,6 +77,11 @@ func TestSimulatedBackend(t *testing.T) {
t.Fatal("error sending transaction") t.Fatal("error sending transaction")
} }
pendingCount, _ = sim.PendingTransactionCount(context.Background())
if pendingCount != 1 {
t.Errorf("expected pending tx count of 1 got %v", pendingCount)
}
txHash = tx.Hash() txHash = tx.Hash()
_, isPending, err = sim.TransactionByHash(context.Background(), txHash) _, isPending, err = sim.TransactionByHash(context.Background(), txHash)
if err != nil { if err != nil {
@ -89,6 +99,25 @@ func TestSimulatedBackend(t *testing.T) {
if isPending { if isPending {
t.Fatal("transaction should not have pending status") t.Fatal("transaction should not have pending status")
} }
to := crypto.PubkeyToAddress(testKey.PublicKey)
tx = types.NewTransaction(1, to, big.NewInt(1024), params.TxGas, gasPrice, nil)
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key)
if err != nil {
t.Errorf("could not sign tx: %v", err)
}
err = sim.SendTransaction(context.Background(), signedTx)
if err != nil {
t.Errorf("could not add tx to pending block: %v", err)
}
balance, err := sim.PendingBalanceAt(context.Background(), to)
if err != nil {
t.Errorf("could not get pending balance: %v", err)
}
if balance.Cmp(big.NewInt(1024)) != 0 {
t.Errorf("pendingBalance not match, want: 1024 have: %d", balance.Int64())
}
} }
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")