hyperledger / web3j

Lightweight Java and Android library for integration with Ethereum clients
https://www.web3labs.com/web3j-sdk
Other
5.07k stars 1.67k forks source link

Block.transactions always returns empty array, i.e. = [{},...,{}] #1754

Open lukerQuant opened 2 years ago

lukerQuant commented 2 years ago

Block.transactions always returns empty array, i.e. = [{},...,{}]

When getting a block, the transactions parameter always has an array of empty transaction objects, i.e. block.transactions = [{},{},{},...{}]

Steps To Reproduce

use getBlock

Expected behavior

According to https://web3js.readthedocs.io/en/v1.7.5/web3-eth.html#getblock this function should return an array of transaction objects, or 32 Bytes transaction hashes depending on the returnTransactionObjects parameter.

Actual behavior

Empty array

Environment

Additional context

Possibly this is happening as web3j has transactions as a list of transactionResults https://github.com/web3j/web3j/blob/e7b7591b38e0632b65100a6b03ba134eedc94f3e/core/src/main/java/org/web3j/protocol/core/methods/response/EthBlock.java#L80 which is equal to transactionReceipts right? And web3js states this should be transaction objects (the non receipt version) or transaction hashes

calmacfadden commented 2 years ago

This issue is caused by the EthBlock.TransactionHash class which is an implementation of the EthBlock.TransactionResult interface.

On line 538 https://github.com/web3j/web3j/blob/v4.9.4/core/src/main/java/org/web3j/protocol/core/methods/response/EthBlock.java#L538

The default serializer doesn't like the default get() and fails to get the hash string. To get this to work it would need to be changed to getValue()

Below is a workaround. You can create a new class which overrides the getter and serialize this instead.

        class TransactionHashOverride extends EthBlock.TransactionHash {

            public  TransactionHashOverride(EthBlock.TransactionResult hash) {
                super.setValue(hash.get().toString());
            }
            public String getValue() {
                return super.get();
            }

        }
calmacfadden commented 2 years ago

PR: https://github.com/web3j/web3j/pull/1756