Browse Source

store transactions info (from, to, amount, txid)

master
arnaucode 6 years ago
parent
commit
826ec89716
6 changed files with 81 additions and 9 deletions
  1. +3
    -2
      .gitignore
  2. +40
    -6
      exploreBlockchain.go
  3. +1
    -1
      instructions.md
  4. +4
    -0
      main.go
  5. +23
    -0
      mongoModels.go
  6. +10
    -0
      mongoOperations.go

+ 3
- 2
.gitignore

@ -14,5 +14,6 @@
.glide/
*.conf
*.json
/*.conf
/*.json
web

+ 40
- 6
exploreBlockchain.go

@ -16,18 +16,33 @@ func explore(client *btcrpcclient.Client, blockHash string) {
block, err := client.GetBlockVerbose(bh)
check(err)
var newBlock BlockModel
newBlock.Hash = block.Hash
newBlock.Height = block.Height
newBlock.Confirmations = block.Confirmations
//get Fee value
th, err := chainhash.NewHashFromStr(block.Tx[0])
check(err)
tx, err := client.GetRawTransactionVerbose(th)
check(err)
var totalFee float64
for _, Vo := range tx.Vout {
totalFee = totalFee + Vo.Value
}
newBlock.Fee = totalFee
//for each Tx, get the Tx value
var totalAmount float64
/*inside each block, there are []Tx
inside each Tx, if is the Tx[0], is the Fees
in the Tx[n] where n>0, each Tx is independent,
and inside each Tx there are []Vout.
Usually Vout[0] is the real Tx value
and the Vout[1] is the rest of the amount in the original wallet.
Each Tx moves all the wallet amount, and the realTx amount is sent to the destination
and the rest of the wallet amount, is send to another owned wallet
*/
for k, txHash := range block.Tx {
//first Tx is the Fee
//after first Tx is the Sent Amount
@ -36,21 +51,37 @@ func explore(client *btcrpcclient.Client, blockHash string) {
check(err)
tx, err := client.GetRawTransactionVerbose(th)
check(err)
var originAddress string
for _, Vi := range tx.Vin {
th, err := chainhash.NewHashFromStr(Vi.Txid)
check(err)
txVi, err := client.GetRawTransactionVerbose(th)
check(err)
if len(txVi.Vout[0].ScriptPubKey.Addresses) > 0 {
originAddress = txVi.Vout[0].ScriptPubKey.Addresses[0]
}
}
for _, Vo := range tx.Vout {
totalAmount = totalAmount + Vo.Value
var blockTx TxModel
blockTx.Txid = tx.Txid
blockTx.Amount = Vo.Value
blockTx.From = originAddress
blockTx.To = Vo.ScriptPubKey.Addresses[0]
newBlock.Tx = append(newBlock.Tx, blockTx)
}
}
}
if totalAmount > 0 {
var newBlock BlockModel
newBlock.Hash = block.Hash
newBlock.Height = block.Height
newBlock.Confirmations = block.Confirmations
newBlock.Amount = totalAmount
newBlock.Fee = totalFee
saveBlock(blockCollection, newBlock)
fmt.Print("Height: ")
fmt.Println(newBlock.Height)
fmt.Print("Amount: ")
fmt.Println(newBlock.Amount)
fmt.Print("Fee: ")
fmt.Println(newBlock.Fee)
fmt.Println("-----")
realBlocks++
@ -58,6 +89,9 @@ func explore(client *btcrpcclient.Client, blockHash string) {
//set the next block
blockHash = block.NextHash
//analyze block creator
}
fmt.Print("realBlocks (blocks with Fee and Amount values): ")
fmt.Println(realBlocks)

+ 1
- 1
instructions.md

@ -16,7 +16,7 @@ rpcallowip=127.0.0.1
```
execute wallet:
./FairCoind -txindex -reindex-chainstate
./faircoind -txindex -reindex-chainstate
### Configure

+ 4
- 0
main.go

@ -9,6 +9,8 @@ import (
)
var blockCollection *mgo.Collection
var nodeCollection *mgo.Collection
var edgeCollection *mgo.Collection
func main() {
//read goBlockchainDataAbalysis config
@ -19,6 +21,8 @@ func main() {
session, err := getSession()
check(err)
blockCollection = getCollection(session, "blocks")
nodeCollection = getCollection(session, "nodes")
edgeCollection = getCollection(session, "edges")
// create new client instance
client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{

+ 23
- 0
mongoModels.go

@ -1,9 +1,32 @@
package main
type TxModel struct {
Txid string
From string
To string
Amount float64
}
type BlockModel struct {
Hash string
Height int64
Confirmations uint64
Amount float64
Fee float64
Tx []TxModel
}
type NodeModel struct {
Id string
Label string
Title string
Group string
Value int
Shape string
}
type EdgeModel struct {
From string
To string
Label string
Arrows string
}

+ 10
- 0
mongoOperations.go

@ -63,3 +63,13 @@ func saveBlock(c *mgo.Collection, block BlockModel) {
}
}
func saveNode(c *mgo.Collection, block BlockModel) {
var node NodeModel
node.Id = block.Hash
node.Label = block.Hash
node.Title = block.Hash
node.Value = 1
node.Shape = "dot"
}
func saveEdge(c *mgo.Collection, block BlockModel) {
}

Loading…
Cancel
Save