In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to implement PHP Bitcoin utxo tracking". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement PHP bitcoin utxo tracking".
The UtxoScanner package is used to scan the UTXO that listens to the Bitcoin blockchain
1. Overview of the development package
The UtxoScanner development package has the following features:
Scan and listen for Utxo generation and consumption events at any Bitcoin address
Manage Utxo locally to facilitate query, naked transaction structure and balance statistics
Support custom business logic when Utxo events occur
UtxoScanner runs in PHP 7.1 + environment, and the main interfaces, classes, and interrelationships are shown in the following figure:
For a list of the main code files for UtxoScanner, see: http://sc.hubwiz.com/codebag/btx-utxo-scanner/
2. Basic usage
The UtxoScanner class is the entry to the development package, and you can track the utxo of the specified address (list) by calling its scan () method. For example, the following code creates an instance of UtxoScanner and scans the utxo at the specified address in the latest block:
Use BtcTool\ UtxoScanner;use BtcTool\ ChainRpc;use BtcTool\ UtxoStoreSqlite3;$scanner = new UtxoScanner (new ChainRpc ('http://user:123456@127.0.0.1:8332'), new UtxoStoreSqlite3 (' scanner.db')); $addressList = ['1GMsiunopL5sZuTMaPbCjfwXdJEvC62KSG']; $stats = $scanner- > scan ($addressList)
The scan () method returns the statistics object for this scan, which is structured as follows:
Block: total number of blocks scanned
Tx: total number of transactions scanned
AddEvents: the total number of new UTXO found in this scan
SpendEvents: the total number of consumer UTXO found in this scan
The prototype of the scan () method is as follows:
Function scan ($addressList,$startBlockRef='latest',$endBlockRef='latest')
Therefore, when the last two parameters are not specified, the scan () method scans only the latest chunks. When multiple blocks need to be scanned, you can specify the start and end block number. For example, the following code scans the 901 blocks of 100mm 1000:
$stats = $scanner- > scan ($addressList,100,1000)
The UtxoScanner instance records the scanned Utxo to the local sqlite3 database, and its path is specified when the UtxoStoreSqlite3 instance is created. You can access the library directly using SQL, or you can use UtxoStoreSqlite3 in the UtxoScanner package. For example, the following code extracts all available UTXO in the current library:
Use BtcTool\ UtxoStoreSqlite3;$store = new UtxoStoreSqlite3 ('scanner.db'); $utxos = $store- > fetch ([]); var_dump ($utxos)
When you call the fetch () method, you can pass in a list of target addresses, which returns a collection of available UTXO corresponding to those addresses. For example:
$addressList = ['1GMsiunopL5sZuTMaPbCjfwXdJEvC62KSG,' 3LiJoKm5e3wLbkaAtZ2E15eEVvkQxG9Z7q']; $utxos = $store- > fetch ($addressList); 3. Set up the Utxo database
UtxoScanner has two built-in Utxo databases:
UtxoStoreMemory: memory library
UtxoStoreSqlite3: using Sqlite3
If you want to store utxo in other ways, you can refer to the implementation code of the above class to extend it.
When creating a UtxoScanner instance, you can specify the store parameter as the desired IUtxoStore instance. For example, the following code uses MySQL to hold the UTXO (assuming the corresponding class is implemented):
Use BtcTool\ UtxoScanner;use BtcTool\ ChainRpc;use BtcTool\ UtxoStoreMySQL;$scanner = new UtxoScanner (new ChainRpc ('http://user:123456@127.0.0.1:8332'), new UtxoStoreMySQL (...)); 4. Listen for Utxo events
Using the addEventListener () method of the UtxoScanner instance, you can be notified when the utxo scanner discovers a new UTXO or consumes an existing UTXO, and you can use this method if you need additional processing when a UTXO event occurs.
First, you need to define a listener class that implements the IEventListener interface, and you only need to implement the handleEvent () method. For example, the following code outputs the contents of each monitored Utxo event on the screen:
Use BtcTool\ UtxoScanner;use BtcTool\ IEventListener;$scanner = new UtxoScanner (); $scanner- > addEventListener (new class implements IEventListener {function handleEvent ($event) {echo "event = >". $event- > type. PHP_EOL; var_dump ($event);}})
The parameter $event of the handleEvent () method is a StdClass object that includes a type field and other additional fields.
When the value of type is add, it indicates that this is a UTXO generation event, with the following additional fields:
Utxo:Utxo object, which is structured as follows:
Txid: transaction hash
Vout: transaction output serial number
Value: number of transactions (in btc)
Script: target public key script
Height: the height of the exchange in the block
When the value of type is spend, it indicates that this is a UTXO consumption event. The additional fields are as follows:
Txid: transaction hash
Vout: transaction output serial number
At this point, I believe you have a better understanding of "how to implement PHP Bitcoin utxo tracking". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.