您正在查看: Ethereum-新手教程 分类下的文章

eth.pendingTransactions 不返回任何挂起的交易

在测试eth.pendingTransactions时,无法获取到数据,查看代码(Github Code)

// PendingTransactions returns the transactions that are in the transaction pool
// and have a from address that is one of the accounts this node manages.
func (s *PublicTransactionPoolAPI) PendingTransactions() ([]*RPCTransaction, error) {
    pending, err := s.b.GetPoolTransactions()
    if err != nil {
        return nil, err
    }
    accounts := make(map[common.Address]struct{})
    for _, wallet := range s.b.AccountManager().Wallets() {
        for _, account := range wallet.Accounts() {
            accounts[account.Address] = struct{}{}
        }
    }
    curHeader := s.b.CurrentHeader()
    transactions := make([]*RPCTransaction, 0, len(pending))
    for _, tx := range pending {
        from, _ := types.Sender(s.signer, tx)
        if _, exists := accounts[from]; exists {
            transactions = append(transactions, newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig()))
        }
    }
    return transactions, nil
}

原因

由于查询eth.pendingTransactions性能消耗较大,所以代码加了一层限制,必须查询当前交易的签名者地址加到当前节点地址中

./geth attach ipc:./data/geth.ipc
personal.importRawKey("e9bc9ae610535。。。。f4f49c025cc","passwrod..")

注意

由于当前私钥可能被外网猜测,所以当前查询节点不能对外提供RPC服务,只能内网被业务服务调用。并做好节点访问权限安全防范

参考

https://github.com/ethereum/go-ethereum/issues/21138

web3j 扩充 getPendingTransactions

由于web3j没有实现getPendingTransactions接口,业务又需要,所以补充下
参考下web3.js (Github Code)

 new Method({
     name: 'getPendingTransactions',
     call: 'eth_pendingTransactions',
     params: 0,
     outputFormatter: formatter.outputTransactionFormatter
 }),

通过查询(Github Code)

找到以下轮子 (Github Code

 @Override
    public List<Transaction> getPendingTransactions() throws EthereumException {
        // only works if a gexp node is used (pending transaction sent by one of the gexp accounts are returned)
        LOG.info("Get pending transactions sent by addr");
        JsonRpcRequest jsonRpcRequest = new JsonRpcRequest();
        jsonRpcRequest.setId(0);
        jsonRpcRequest.setMethod("eth_pendingTransactions");
        jsonRpcRequest.setJsonrpc("2.0");
        HttpEntity<JsonRpcRequest> request = new HttpEntity<>(jsonRpcRequest);
        PendingTransactionResponse response = doPOST(getNodeBaseUrl(), request, PendingTransactionResponse.class);

        List<Transaction> pendingTransactions = response
                .getPendingTransactions()
                .parallelStream()
                .map(DtoConverter::convert)
                .collect(Collectors.toList());

        LOG.info("Pending transactions found: " + pendingTransactions.size());
        return pendingTransactions;
    }

如果完整的,应该fork扩充到 (Github Code)

tx fee (1.30 ether) exceeds the configured cap (1.00 ether)

--rpc.txfeecap value 设置可以通过 RPC API 发送的交易费用上限(以以太为单位)(0 = 无上限)(默认值:1)

https://geth.ethereum.org/docs/interface/command-line-options

区块链浏览器上做合约验证,部署的时候用的多文件

多个文件验证不可控因素有点多,你用truffle-flatten做成一个文件,然后使用remix部署,然后再验证,就是单个文件验证,直接复制remix里的代码,比较顺利

How to check if Ethereum address is valid or not

web3.js

const address = "0x0089d53F703f7E0843953D48133f74cE247184c2"
let result = Web3.utils.isAddress(address)
console.log(result)  // => true

web3j

社区fork为其增加了isAddress commit
https://github.com/bcskill/web3j

package org.web3j.utils;

import org.junit.Test;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertTrue;

import static org.web3j.utils.Account.isAddress;

public class AccountTest {
    @Test
    public void verifyIsAddress() {
        String validLowerCaseAddress = "0x3de8c14c8e7a956f5cc4d82beff749ee65fdc358";
        assertTrue(isAddress(validLowerCaseAddress));
        String validChecksumAddress = "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359";
        assertTrue(isAddress(validChecksumAddress));
        String invalidLengthAddress = "0x3de8c14c8e7a956f5cc4d82beff749ee65bac35";
        assertFalse(isAddress(invalidLengthAddress));
        String invalidChecksumAddress = "0x3de8c14c8E7a956f5cc4d82beff749ee65fdc358";
        assertFalse(isAddress(invalidChecksumAddress));
    }
} 

参考

https://piyopiyo.medium.com/how-to-check-if-ethereum-address-is-valid-or-not-ef587b6c4819
https://github.com/assafY/web3j.git