Blockchain development (14) Discussion on the query transaction list of the Ethereum go-ethereum client

Bitcoin can query the historical transactions of the specified address through the api (listtransactions). But no similar query api is provided in eth. Hyperledger fabric also has a corresponding method for querying historical transaction records. The following functions are used to query historical data GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error). For the specific implementation, see my HyperLedger Fabric ChainCode development article. Today's blog will briefly introduce how to solve this problem.
Problem
The Ethereum go-ethereum client does not have an API to query the transaction list by address. However, we often use similar APIs in the process of use. There is already an active discussion on github about whether to add this API on the client side. Click on the URL to find out:
https://github.com/ethereum/go-ethereum/issues/1897Solution Since there is such a demand, even if there is no targeted API, it is necessary to find a corresponding solution
.
There are currently two effective solutions:
(1) Call the third-party API interface. There are many such interfaces, which can not only query block information on the official website, but also provide query APIs. For example: https://etherchain.org/apidoc . (I cannot open or debug this interface file at present.) Document introduction: The interface is simple, but there are restrictions on the frequency of visits. Unless there is a large amount of visits, this kind of interface can still be used. easy and convenient. (Specifically, you need to open the document partner verification. If it is a private chain established by yourself, how to implement the third-party interface needs to be verified.)
(2) Maintain data by yourself and realize this function by yourself. The basic idea of ​​this method is to query the specific transaction of each block, enter it into its own database, and then query its own data through SQL statements. This method requires a certain coding foundation and equipment investment. The existing API supports querying the transaction records in each block. Although it is relatively cumbersome, once the data is maintained by itself, how to query is only a different matter of SQL statements. (I recommend this method. The combination of centralized system and distributed system is implemented. We are not entangled in one technology, but to solve specific problems.)
But after each transaction is completed, we must obtain the Hash of the transaction. , record the transaction hash. Obtained as follows:

result = meta.sendTranscation(receiver, amount, {from: account});
var hashTx;
for (var i = 0; i < result.logs.length; i++) {
        var log = result.logs[i];
        if( log.event =="Transfer" ) {   
          hashTx = result.tx;
          break;
        }
      }

After getting it, save it in the database.
Can be queried via geth client or via web3.js

>eth.getTransaction("0xf197f83993742a4e422244fe91a1175513ad11da2bd1209906443dcc391279eb")
{
  blockHash: "0xf3ac98d6db31f72a586b0239eab43d40fb568668561769c2e60d4dd7082050d8",
  blockNumber: 40164,
  from: "0x95a171d45c7551474f3479bf006e2a9a3852bbd8",
  gas: 90000,
  gasPrice: 100000000000,
  hash: "0xf197f83993742a4e422244fe91a1175513ad11da2bd1209906443dcc391279eb",
  input: "0x90b98a11000000000000000000000000964f31c1c95eac140e9f1ef1e1a22a5f3cc7a90600000000000000000000000000000000000000000000000000000000000f4240",
  nonce: 73,
  r: "0x3480b8405e47e1b402039f813b966cfb894ff61f60d2c5301f85c889638dce1d",
  s: "0x6cc87ac31c610f78f258f36551e7a74833cb361afe596fc43bc9ca3e8a6e3ceb",
  to: "0x7673af653c4ea7dee50a10c50d1d62a20c63cd5c",
  transactionIndex: 0,
  v: "0x42",
  value: 0
}

The input field is the details of the transaction.
function signature:

0x90b98a11000000000000000000000000

The recipient address of the transaction (you need to add 0x)

964f31c1c95eac140e9f1ef1e1a22a5f3cc7a906

Transaction subject (amount) [Hexadecimal data]

00000000000000000000000000000000000000000000000000000000000f4240

Exceptions
If you want to query the transaction history of a contract, you can do this by filtering the contract log. This scenario is supported by the corresponding API. But for this kind of data, you must scrape data from all blocks and query your log data. (In Eth, Log and Event are the same thing. We often write the method to the blockchain by adding Event in front of it, so we can query transaction records through the filter log.) In this case, we will discuss it later Discuss in detail.
Compiled in Shenzhen on December 24, 2017.
Revised January 30, 2018.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325350116&siteId=291194637