From 99af4a76fffe5d69f78fbd85a281aca701835e7b Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 29 Aug 2017 16:10:39 +0200 Subject: [PATCH] corrected tx model, added block view in frontend --- DevelopmentNotes.md | 2 - exploreBlockchain.go | 35 +++++++---- mongoModels.go | 23 +++++-- serverRoutes.go | 60 ++++++++++++++---- web/app.js | 3 +- web/css/bootstrapMaterialDarkOverwrite.css | 8 ++- web/index.html | 3 +- web/views/address/address.html | 44 +++++++++---- web/views/block/block.html | 73 ++++++++++++++++++++++ web/views/block/block.js | 21 +++++++ web/views/main/main.html | 68 +++++++++++--------- 11 files changed, 266 insertions(+), 74 deletions(-) create mode 100644 web/views/block/block.html create mode 100644 web/views/block/block.js diff --git a/DevelopmentNotes.md b/DevelopmentNotes.md index 37b41c5..3c0ef67 100644 --- a/DevelopmentNotes.md +++ b/DevelopmentNotes.md @@ -30,5 +30,3 @@ other - mantain connection with wallet using websockets - num address evolution throught time - -- Error in Tx model. exploreBlockchain.go line 174 diff --git a/exploreBlockchain.go b/exploreBlockchain.go index f6395d8..d5912f1 100644 --- a/exploreBlockchain.go +++ b/exploreBlockchain.go @@ -79,6 +79,17 @@ func explore(client *rpcclient.Client, blockHash string) { nodeTx.Type = "tx" saveNode(nodeCollection, nodeTx) + //Tx save + var newTx TxModel + newTx.Hex = blockTx.Hex + newTx.Txid = blockTx.Txid + newTx.Hash = blockTx.Hash + newTx.BlockHash = block.Hash + newTx.BlockHeight = strconv.FormatInt(block.Height, 10) + newTx.Time = blockTx.Time + newTx.DateT = unixTimeToTime(block.Time) + newTx.Date.Year, newTx.Date.Month, newTx.Date.Day, newTx.Date.Hour = decomposeDate(block.Time) + var originAddresses []string var outputAddresses []string var outputAmount []float64 @@ -101,6 +112,11 @@ func explore(client *rpcclient.Client, blockHash string) { addr.Hash = outputAddr addr.InBittrex = false saveAddress(addr) + + var newVout Vout + newVout.Value = Vo.Value + newVout.Address = outputAddr + newTx.Vout = append(newTx.Vout, newVout) } } for _, Vi := range blockTx.Vin { @@ -108,24 +124,20 @@ func explore(client *rpcclient.Client, blockHash string) { check(err) txVi, err := client.GetRawTransactionVerbose(th) check(err) + if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 { //add tx to newBlock newBlock.Tx = append(newBlock.Tx, blockTx.Txid) //Tx save - var newTx TxModel - newTx.Hex = blockTx.Hex - newTx.Txid = blockTx.Txid - newTx.Hash = blockTx.Hash - newTx.BlockHash = block.Hash - newTx.BlockHeight = strconv.FormatInt(block.Height, 10) - newTx.Time = blockTx.Time - newTx.DateT = unixTimeToTime(block.Time) - newTx.Date.Year, newTx.Date.Month, newTx.Date.Day, newTx.Date.Hour = decomposeDate(block.Time) for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses { originAddresses = append(originAddresses, originAddr) - newTx.From = originAddr + var newVin Vin + newVin.Txid = blockTx.Txid + newVin.Amount = txVi.Vout[Vi.Vout].Value + newVin.Address = originAddr + newTx.Vin = append(newTx.Vin, newVin) var n1 NodeModel n1.Id = originAddr @@ -167,11 +179,10 @@ func explore(client *rpcclient.Client, blockHash string) { //hour analysis hourAnalysis(eIn, blockTx.Time) - newTx.To = outputAddr + //newTx.To = outputAddr } } - //ERROR! need to make array with all Vin and array with Vout, with addresses and amount values saveTx(newTx) } else { originAddresses = append(originAddresses, "origin") diff --git a/mongoModels.go b/mongoModels.go index 594dc92..652a93f 100644 --- a/mongoModels.go +++ b/mongoModels.go @@ -18,12 +18,24 @@ type DateModel struct { BlockHash string `json:"blockhash"` BlockHeight string `json:"blockheight"`*/ } +type Vin struct { + Txid string `json:"txid"` + Vout uint32 `json:"vout"` + Amount float64 `json:"amount"` + Address string `json:"address"` +} +type Vout struct { + Value float64 `json:"value"` + Address string `json:"address"` +} type TxModel struct { - Hex string `json:"hex"` - Txid string `json:"txid"` - Hash string `json:"hash"` - From string `json:"from"` //hash of address - To string `json:"to"` //hash of address + Hex string `json:"hex"` + Txid string `json:"txid"` + Hash string `json:"hash"` + /*From string `json:"from"` //hash of address + To string `json:"to"` //hash of address*/ + Vin []Vin `json:"vin"` + Vout []Vout `json:"vout"` Amount float64 `json:"amount"` BlockHash string `json:"blockhash"` BlockHeight string `json:"blockheight"` @@ -39,6 +51,7 @@ type BlockModel struct { //Amount float64 `json:"amount"` //Fee float64 `json:"fee"` Tx []string `json:"txid"` //txid of the TxModel + Txs []TxModel `json:"txs"` PreviousHash string `json:"previoushash"` NextHash string `json:"nexthash"` Time int64 `json:"time"` diff --git a/serverRoutes.go b/serverRoutes.go index 2f59613..f874e76 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -47,10 +47,10 @@ var routes = Routes{ GetLastTx, }, Route{ - "AddressNetwork", + "Block", "GET", - "/address/network/{address}", - AddressNetwork, + "/block/{height}", + Block, }, Route{ "Address", @@ -58,6 +58,12 @@ var routes = Routes{ "/address/{hash}", Address, }, + Route{ + "AddressNetwork", + "GET", + "/address/network/{address}", + AddressNetwork, + }, Route{ "AddressSankey", "GET", @@ -167,22 +173,36 @@ func GetLastTx(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, string(jsonData)) } -func AddressNetwork(w http.ResponseWriter, r *http.Request) { +func Block(w http.ResponseWriter, r *http.Request) { ipFilter(w, r) vars := mux.Vars(r) - address := vars["address"] - if address == "undefined" { - fmt.Fprintln(w, "not valid address") + var heightString string + heightString = vars["height"] + height, err := strconv.ParseInt(heightString, 10, 64) + if err != nil { + fmt.Fprintln(w, "not valid height") } else { - network := addressTree(address) - network.Nodes[0].Shape = "triangle" + block := BlockModel{} + err := blockCollection.Find(bson.M{"height": height}).One(&block) + + txs := []TxModel{} + err = txCollection.Find(bson.M{"blockheight": heightString}).All(&txs) + block.Txs = txs + + /*for _, tx := range address.Txs { + blocks := []BlockModel{} + err = blockCollection.Find(bson.M{"blockheight": tx.BlockHash}).All(&blocks) + for _, block := range blocks { + address.Blocks = append(address.Blocks, block) + } + }*/ //convert []resp struct to json - jNetwork, err := json.Marshal(network) + jsonResp, err := json.Marshal(block) check(err) - fmt.Fprintln(w, string(jNetwork)) + fmt.Fprintln(w, string(jsonResp)) } } func Address(w http.ResponseWriter, r *http.Request) { @@ -215,6 +235,24 @@ func Address(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, string(jsonResp)) } } +func AddressNetwork(w http.ResponseWriter, r *http.Request) { + ipFilter(w, r) + + vars := mux.Vars(r) + address := vars["address"] + if address == "undefined" { + fmt.Fprintln(w, "not valid address") + } else { + network := addressTree(address) + network.Nodes[0].Shape = "triangle" + + //convert []resp struct to json + jNetwork, err := json.Marshal(network) + check(err) + + fmt.Fprintln(w, string(jNetwork)) + } +} func AddressSankey(w http.ResponseWriter, r *http.Request) { ipFilter(w, r) vars := mux.Vars(r) diff --git a/web/app.js b/web/app.js index 7594bae..258f655 100644 --- a/web/app.js +++ b/web/app.js @@ -11,9 +11,10 @@ angular.module('webApp', [ 'angular-svg-round-progressbar', 'app.navbar', 'app.main', + 'app.block', + 'app.address', 'app.network', 'app.addressNetwork', - 'app.address', 'app.sankey', 'app.dateAnalysis' ]). diff --git a/web/css/bootstrapMaterialDarkOverwrite.css b/web/css/bootstrapMaterialDarkOverwrite.css index 8709439..87f4e1f 100644 --- a/web/css/bootstrapMaterialDarkOverwrite.css +++ b/web/css/bootstrapMaterialDarkOverwrite.css @@ -21,9 +21,15 @@ body { .list-group-item:hover { background: rgba(255, 255, 255, 0.2)!important; } -.list-group-item p, a{ +.list-group-item p { color: #ffffff!important; } +.list-group-item a { + color: #512DA8!important; +} +.list-group-item-text { + color: #9575CD!important; +} .table-hover tbody tr:hover { background-color: rgba(245, 245, 245, 0.2)!important; } diff --git a/web/index.html b/web/index.html index 117c9eb..dd19dde 100644 --- a/web/index.html +++ b/web/index.html @@ -76,9 +76,10 @@ + + - diff --git a/web/views/address/address.html b/web/views/address/address.html index 4a836a2..f53502e 100644 --- a/web/views/address/address.html +++ b/web/views/address/address.html @@ -8,23 +8,29 @@ {{address.amount}} - + +

Blocks where address appears

-
- Block Height: {{block.height}} -
- Hash: {{block.Hash}} -
- {{block.datet}} -
- {{block.Size}} +
-
+
+ +
+

Tx where address appears

@@ -42,9 +48,21 @@ - {{tx.blockheight}} - {{tx.from}} - {{tx.to}} + + + {{tx.blockheight}} + + + + + {{tx.from}} + + + + + {{tx.to}} + + {{tx.amount}} View diff --git a/web/views/block/block.html b/web/views/block/block.html new file mode 100644 index 0000000..b3d5334 --- /dev/null +++ b/web/views/block/block.html @@ -0,0 +1,73 @@ +
+
+
+
+

Block Height {{block.height}}

+
+
+ Block Hash: +

+ {{block.hash}} +

+
+
+
+
+
+
+

Block evolution

+
+
+ +
+
+
+
+ +
+
+
+
+

Tx in block

+
+
+
+ + + + + + + + + + + + + + + +
TxidInputOutput
+ + {{tx.txid}} + + +
+ + {{vin.address}} + + :{{vin.amount}} +
+
+
+ + {{vout.address}} + + :{{vout.value}} +
+
+
+
+
+
+
diff --git a/web/views/block/block.js b/web/views/block/block.js new file mode 100644 index 0000000..ccec46b --- /dev/null +++ b/web/views/block/block.js @@ -0,0 +1,21 @@ +'use strict'; + +angular.module('app.block', ['ngRoute']) + + .config(['$routeProvider', function($routeProvider) { + $routeProvider.when('/block/:height', { + templateUrl: 'views/block/block.html', + controller: 'BlockCtrl' + }); + }]) + + .controller('BlockCtrl', function($scope, $http, $routeParams) { + $scope.block = {}; + $http.get(urlapi + 'block/' + $routeParams.height) + .then(function(data, status, headers, config) { + console.log(data); + $scope.block = data.data; + }, function(data, status, headers, config) { + console.log('data error'); + }); + }); diff --git a/web/views/main/main.html b/web/views/main/main.html index 29055a4..dff96eb 100644 --- a/web/views/main/main.html +++ b/web/views/main/main.html @@ -130,34 +130,46 @@

Last Tx with amount

- - - - - - - - - - - - - - - - - - - - -
BlockHeightFromToAmount
{{tx.blockheight}}{{tx.from}}{{tx.to}}{{tx.amount}}View
+ + + + + + + + + + + + + + + + + +
BlockHeightTxidInputOutput
+ + {{tx.blockheight}} + + + + {{tx.txid}} + + +
+ + {{vin.address}} + + :{{vin.amount}} +
+
+
+ + {{vout.address}} + + :{{vout.value}} +
+