ethclient: add tests for TransactionInBlock (#28283)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
hyunchel 2024-01-15 05:10:26 -05:00 committed by GitHub
parent 89ccc680da
commit 7596db5f48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -264,7 +264,7 @@ func TestEthClient(t *testing.T) {
func(t *testing.T) { testBalanceAt(t, client) },
},
"TxInBlockInterrupted": {
func(t *testing.T) { testTransactionInBlockInterrupted(t, client) },
func(t *testing.T) { testTransactionInBlock(t, client) },
},
"ChainID": {
func(t *testing.T) { testChainID(t, client) },
@ -329,7 +329,7 @@ func testHeader(t *testing.T, chain []*types.Block, client *rpc.Client) {
got.Number = big.NewInt(0) // hack to make DeepEqual work
}
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("HeaderByNumber(%v)\n = %v\nwant %v", tt.block, got, tt.want)
t.Fatalf("HeaderByNumber(%v) got = %v, want %v", tt.block, got, tt.want)
}
})
}
@ -381,7 +381,7 @@ func testBalanceAt(t *testing.T, client *rpc.Client) {
}
}
func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) {
func testTransactionInBlock(t *testing.T, client *rpc.Client) {
ec := NewClient(client)
// Get current block by number.
@ -390,22 +390,27 @@ func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) {
t.Fatalf("unexpected error: %v", err)
}
// Test tx in block interrupted.
ctx, cancel := context.WithCancel(context.Background())
cancel()
<-ctx.Done() // Ensure the close of the Done channel
tx, err := ec.TransactionInBlock(ctx, block.Hash(), 0)
if tx != nil {
t.Fatal("transaction should be nil")
}
if err == nil || err == ethereum.NotFound {
t.Fatal("error should not be nil/notfound")
}
// Test tx in block not found.
if _, err := ec.TransactionInBlock(context.Background(), block.Hash(), 20); err != ethereum.NotFound {
t.Fatal("error should be ethereum.NotFound")
}
// Test tx in block found.
tx, err := ec.TransactionInBlock(context.Background(), block.Hash(), 0)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tx.Hash() != testTx1.Hash() {
t.Fatalf("unexpected transaction: %v", tx)
}
tx, err = ec.TransactionInBlock(context.Background(), block.Hash(), 1)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tx.Hash() != testTx2.Hash() {
t.Fatalf("unexpected transaction: %v", tx)
}
}
func testChainID(t *testing.T, client *rpc.Client) {