From 826ec89716254153da103dbd98784a940bead041 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sun, 23 Jul 2017 23:34:28 +0200 Subject: [PATCH] store transactions info (from, to, amount, txid) --- .gitignore | 5 +++-- exploreBlockchain.go | 46 ++++++++++++++++++++++++++++++++++++++------ instructions.md | 2 +- main.go | 4 ++++ mongoModels.go | 23 ++++++++++++++++++++++ mongoOperations.go | 10 ++++++++++ 6 files changed, 81 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index db726fa..a908ee5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,6 @@ .glide/ -*.conf -*.json +/*.conf +/*.json +web diff --git a/exploreBlockchain.go b/exploreBlockchain.go index 9d793df..7cfe019 100644 --- a/exploreBlockchain.go +++ b/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) diff --git a/instructions.md b/instructions.md index 4271b5f..395f7f0 100644 --- a/instructions.md +++ b/instructions.md @@ -16,7 +16,7 @@ rpcallowip=127.0.0.1 ``` execute wallet: -./FairCoind -txindex -reindex-chainstate +./faircoind -txindex -reindex-chainstate ### Configure diff --git a/main.go b/main.go index 60e3a12..a444924 100644 --- a/main.go +++ b/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{ diff --git a/mongoModels.go b/mongoModels.go index becee4f..3d8d419 100644 --- a/mongoModels.go +++ b/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 } diff --git a/mongoOperations.go b/mongoOperations.go index f03f67b..71b9def 100644 --- a/mongoOperations.go +++ b/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) { +}