项目作者: consulthys

项目描述 :
A Logstash input plugin for querying a blockchain
高级语言: Ruby
项目地址: git://github.com/consulthys/logstash-input-blockchain.git
创建时间: 2017-01-28T08:09:25Z
项目社区:https://github.com/consulthys/logstash-input-blockchain

开源协议:Other

下载


Logstash Blockchain Input Plugin

This Logstash input plugin queries your blockchain via the RPC API and generates
events for each block and/or transaction that it encounters. At the moment, both the Bitcoin and the
Ethereum blockchains are supported and other ones may be added in the future.

Prerequisite

Bitcoin blockchain

You need a fully synced bitcoin-core client.
Obviously, it will also work with partially synced clients but you’ll not be able to pull recent blocks.

Also make sure to enable the RPC server to serve requests in your bitcoin.conf configuration file:

  1. # JSON-RPC options (for controlling a running Bitcoin/bitcoind process)
  2. # server=1 tells Bitcoin-Qt and bitcoind to accept JSON-RPC commands
  3. server=1
  4. # You must set rpcuser and rpcpassword to secure the JSON-RPC api
  5. rpcuser=test
  6. rpcpassword=test
  7. # server=1 tells Bitcoin-Qt to accept JSON-RPC commands.
  8. # it is also read by bitcoind to determine if RPC should be enabled
  9. rpcallowip=127.0.0.1
  10. # Listen for RPC connections on this TCP port:
  11. rpcport=8332

Ethereum blockchain

You need a fully synced Ethereum client.
Obviously, it will also work with partially synced clients but you’ll not be able to pull recent blocks.

Also make sure to start your client with RPC enabled,
which can be done differently depending on the client you are using. If you use geth then you can start your client
like this:

  1. geth --rpc --rpcaddr 127.0.0.1 --rpcport 8545

Documentation

Configuration

The following list enumerates all configuration parameters of the blockchain input:

  • protocol: the underlying blockchain protocol: bitcoin (default) or ethereum
  • host: the host or ip of the bitcoin RPC endpoint to bind to (see Prerequisite above) (default: localhost)
  • port: the TCP port of the bitcoin RPC endpoint to bind to (see Prerequisite above) (default: 8332)
  • user: the username for basic authentication to the bitcoin RPC endpoint (see Prerequisite above)
  • password: the password for basic authentication to the bitcoin RPC endpoint (see Prerequisite above)
  • start_height: (optional) the height of the first block to fetch (default: 0, i.e. starts at the genesis block)
  • granularity: (optional) the granularity of the events to produce (possible values are block, transaction) (default: block)
    • block: one event will be created for each retrieved block
    • transaction: one event will be created for each transaction of each retrieved block
  • interval: set how frequently blocks should be retrieved:
    • 1 means retrieve one block per second
    • 0 means retrieve the next block immediately

Sample configurations

The following configuration will start pulling blocks from the beginning of the Bitcoin blockchain and produce one event per block

  1. input {
  2. blockchain {
  3. protocol => "bitcoin"
  4. host => "localhost"
  5. port => 8332
  6. user => "test"
  7. password => "test"
  8. }
  9. }
  10. output {
  11. stdout {
  12. codec => json
  13. }
  14. }

The following configuration will start pulling block 100000 from the Ethereum blockchain and create one event for each transaction with the retrieved blocks:

  1. input {
  2. blockchain {
  3. protocol => "ethereum"
  4. host => "localhost"
  5. port => 8545
  6. start_height => 100000
  7. granularity => "transaction"
  8. }
  9. }
  10. output {
  11. stdout {
  12. codec => json
  13. }
  14. }

Sample Bitcoin events

Here is a how a sample Bitcoin block event will look like:

  1. {
  2. "@timestamp": "2010-12-29T11:57:43.000Z",
  3. "@version": "1",
  4. "time": 1293623863,
  5. "height": 100000,
  6. "hash": "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506",
  7. "mediantime": 1293622620,
  8. "previousblockhash": "000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250",
  9. "bits": "1b04864c",
  10. "weight": 3828,
  11. "versionHex": "00000001",
  12. "confirmations": 109057,
  13. "version": 1,
  14. "nonce": 274148111,
  15. "nextblockhash": "00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090",
  16. "difficulty": 14484.1623612254,
  17. "chainwork": "0000000000000000000000000000000000000000000000000644cb7f5234089e",
  18. "size": 957,
  19. "merkleroot": "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",
  20. "strippedsize": 957,
  21. "tx_count": 1,
  22. "tx": [
  23. "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
  24. "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4",
  25. "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4",
  26. "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d"
  27. ],
  28. "tx_info": [
  29. {
  30. "vsize": 135,
  31. "size": 135,
  32. "locktime": 0,
  33. "txid": "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
  34. "vin": [
  35. {
  36. "sequence": 4294967295,
  37. "coinbase": "044c86041b020602"
  38. }
  39. ],
  40. "version": 1,
  41. "hash": "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
  42. "vout": [
  43. {
  44. "scriptPubKey": {
  45. "addresses": [
  46. "1HWqMzw1jfpXb3xyuUZ4uWXY4tqL2cW47J"
  47. ],
  48. "asm": "041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84 OP_CHECKSIG",
  49. "hex": "41041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac",
  50. "type": "pubkey",
  51. "reqSigs": 1
  52. },
  53. "value": 50,
  54. "n": 0
  55. }
  56. ]
  57. }
  58. ]
  59. }

And here is a how a sample Bitcoin transaction event will look like (note that the block information is inside the block property):

  1. {
  2. "vsize": 135,
  3. "@timestamp": "2010-12-29T11:57:43.000Z",
  4. "@version": "1",
  5. "version": 1,
  6. "hash": "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
  7. "txid": "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
  8. "size": 135,
  9. "locktime": 0,
  10. "block": {
  11. "mediantime": 1293622620,
  12. "previousblockhash": "000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250",
  13. "bits": "1b04864c",
  14. "weight": 3828,
  15. "versionHex": "00000001",
  16. "confirmations": 109633,
  17. "version": 1,
  18. "nonce": 274148111,
  19. "nextblockhash": "00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090",
  20. "difficulty": 14484.1623612254,
  21. "chainwork": "0000000000000000000000000000000000000000000000000644cb7f5234089e",
  22. "size": 957,
  23. "merkleroot": "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",
  24. "strippedsize": 957,
  25. "time": 1293623863,
  26. "hash": "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506",
  27. "height": 100000,
  28. "tx_count": 1,
  29. "tx": [
  30. "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
  31. "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4",
  32. "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4",
  33. "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d"
  34. ]
  35. },
  36. "vin": [
  37. {
  38. "sequence": 4294967295,
  39. "coinbase": "044c86041b020602"
  40. }
  41. ],
  42. "vout": [
  43. {
  44. "scriptPubKey": {
  45. "addresses": [
  46. "1HWqMzw1jfpXb3xyuUZ4uWXY4tqL2cW47J"
  47. ],
  48. "asm": "041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84 OP_CHECKSIG",
  49. "hex": "41041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac",
  50. "type": "pubkey",
  51. "reqSigs": 1
  52. },
  53. "value": 50,
  54. "n": 0
  55. }
  56. ]
  57. }

Sample Ethereum events

Here is a how a sample Ethereum block event will look like:

  1. {
  2. "@timestamp": "2015-08-17T08:15:53.000Z",
  3. "@version": "1",
  4. "timestamp": 1439799353,
  5. "number": 100014,
  6. "hash": "660cc186e2c5386fdecd98a65fd87ab132c5d64c181b7375323b90567a011fe8",
  7. "logsBloom": "0",
  8. "totalDifficulty": 169479946288717920,
  9. "receiptsRoot": "d988c84f68a0eec17e038d15883c4cff0577a8cd766e7d59156a6ebc2234596d",
  10. "tx_count": 1,
  11. "extraData": "476574682f76312e302e312f77696e646f77732f676f312e342e32",
  12. "nonce": "7a7798c8d78b985b",
  13. "miner": "8b454d830fef179e66206840f8f3d1d83bc32b17",
  14. "difficulty": 3853051700593,
  15. "gasLimit": 3141592,
  16. "gasUsed": 21000,
  17. "uncles": [],
  18. "sha3Uncles": "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  19. "size": 658,
  20. "transactionsRoot": "761dfca816db7eb8e1c76b332ab692ee198af989ffd0b33a96a7a455fbde0f37",
  21. "stateRoot": "d1ab073392d6c161f77e9f0dd1a84136ca82c1d66d891d5ef730f031cdc5af9",
  22. "mixHash": "5688e9d618d1464fbd2c51ff085b17530c189c5c84291435c4ba1e6d96bc5479",
  23. "parentHash": "3ffbb968107fca6052ff609b6a8478ae9b915a88a3a8f406600aab793cf54d20",
  24. "tx_info": [
  25. {
  26. "blockHash": "660cc186e2c5386fdecd98a65fd87ab132c5d64c181b7375323b90567a011fe8",
  27. "transactionIndex": 0,
  28. "nonce": 157,
  29. "input": "0",
  30. "r": "cc4386a09f05b2b798fa8a4eee869d35a3298e12aa6599bfadad6828ce3a7338",
  31. "s": "ca85d381477bcae6fd253161515aeeb104b8fc31e9ad9cb1b9bdeb78784f8f",
  32. "v": "1b",
  33. "blockNumber": 100014,
  34. "gas": 90000,
  35. "from": "7f7f58d3eb5b7510a301ecc749fc1fcddbe14d",
  36. "to": "b1abce2918e21ddb93aa452731a12672a3d9f75a",
  37. "value": 5000000000000000000,
  38. "hash": "9e5c90e71421b732e5984f6baddab2e9de977147730707cc10a48eaa73fbfcf3",
  39. "gasPrice": 60347544134
  40. }
  41. ]
  42. }

And here is a how a sample Ethereum transaction event will look like (note that the block information is inside the block property):

  1. {
  2. "@timestamp": "2015-08-17T08:15:42.000Z",
  3. "@version": "1",
  4. "hash": "870cc7d27296419c9cf8f02a4c4c9a5dc7513a5d82c698d5026873d70fb0cc9d",
  5. "blockHash": "3ffbb968107fca6052ff609b6a8478ae9b915a88a3a8f406600aab793cf54d20",
  6. "blockNumber": 100013,
  7. "transactionIndex": 4,
  8. "nonce": 26,
  9. "input": "0",
  10. "r": "abf5727952e36e3f364237398b4e2af910dcc6e3c5ba84c6dbe1143223021b48",
  11. "s": "3d0ee86a353d7f480b1406b920c79251c393d30fb3fe573a01ff2ac7d63f8435",
  12. "v": "1c",
  13. "gas": 90000,
  14. "from": "cf00a85f3826941e7a25bfcf9aac575d40410852",
  15. "to": "d9666150a9da92d9108198a4072970805a8b3428",
  16. "value": 5000000000000000000,
  17. "gasPrice": 54588778004,
  18. "block": {
  19. "logsBloom": "0",
  20. "totalDifficulty": 169476093237017300,
  21. "receiptsRoot": "1b26dff83652d5e430c7e459fd8cfa61936ce9afbe4e3e65c185fd9ce0944a6c",
  22. "tx_count": 5,
  23. "extraData": "476574682f76312e302e312f77696e646f77732f676f312e342e32",
  24. "nonce": "af8b8913abb18374",
  25. "miner": "7f7f58d3eb5b7510a301ecc749fc1fcddbe14d",
  26. "difficulty": 3851171245884,
  27. "gasLimit": 3141592,
  28. "number": 100013,
  29. "gasUsed": 105000,
  30. "uncles": [],
  31. "sha3Uncles": "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  32. "size": 1102,
  33. "transactionsRoot": "80983f724b8e86a371d6807339ed6de46f3d2aad547f5df4bc4249fa379a493c",
  34. "stateRoot": "d06a9b2ecbc3b4f6096fbf956e92b955cf367976cec6ed29a682914e0e681ec6",
  35. "mixHash": "f09e3a5a47e376acbf71899c31dcc16c0d727b2eea59e5d1c00e068c8edb2a11",
  36. "parentHash": "c5f56dfc80b2ae51fcdc3f585f9b5ee5870ed28af34240dc7033fced51fb2034",
  37. "hash": "3ffbb968107fca6052ff609b6a8478ae9b915a88a3a8f406600aab793cf54d20",
  38. "timestamp": 1439799342
  39. }
  40. }

Need Help?

Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.

Developing

1. Plugin Development and Testing

Code

  • To get started, you’ll need JRuby with the Bundler gem installed.

  • Create a new plugin or clone and existing from the GitHub logstash-plugins organization. We also provide example plugins.

  • Install dependencies

    1. bundle install

Test

  • Update your dependencies
  1. bundle install
  • Run tests
  1. bundle exec rspec

2. Running your unpublished Plugin in Logstash

2.1 Run in a local Logstash clone

  • Edit Logstash Gemfile and add the local plugin path, for example:
    1. gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
  • Install plugin
    1. bin/logstash-plugin install --no-verify
  • Run Logstash with your plugin
    1. bin/logstash -e 'filter {awesome {}}'
    At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.

2.2 Run in an installed Logstash

You can use the same 2.1 method to run your plugin in an installed Logstash by editing its Gemfile and pointing the :path to your local plugin development directory or you can build the gem and install it using:

  • Build your plugin gem
    1. gem build logstash-filter-awesome.gemspec
  • Install the plugin from the Logstash home
    1. bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
  • Start Logstash and proceed to test the plugin

Contributing

All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.

Programming is not a required skill. Whatever you’ve seen about open source and maintainers or community members saying “send patches or die” - you will not see that here.

It is more important to the community that you are able to contribute.

For more information about contributing, see the CONTRIBUTING file.