From 8516772c75f7bc23185c3aa5545c1bb933b4b4c0 Mon Sep 17 00:00:00 2001 From: "Amos .E" Date: Tue, 30 Nov 2021 12:47:39 +0100 Subject: [PATCH 1/3] Tx logging module --- log.go | 4 ++++ tx/log.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tx/log.go diff --git a/log.go b/log.go index 7e36bca..5e9b8de 100644 --- a/log.go +++ b/log.go @@ -23,6 +23,7 @@ import ( "github.com/planetdecred/pdanalytics/stakingreward" "github.com/planetdecred/pdanalytics/stats" "github.com/planetdecred/pdanalytics/treasury" + "github.com/planetdecred/pdanalytics/tx" "github.com/planetdecred/pdanalytics/vsp" "github.com/planetdecred/pdanalytics/web" ) @@ -74,6 +75,7 @@ var ( statsLog = backendLog.Logger("STAT") chartsLog = backendLog.Logger("CHRTS") treasuryLog = backendLog.Logger("TRS") + txLog = backendLog.Logger("TX") ) // Initialize package-global logger variables. @@ -96,6 +98,7 @@ func init() { stats.UseLogger(statsLog) charts.UseLogger(chartsLog) treasury.UseLogger(treasuryLog) + tx.UseLogger(txLog) } // subsystemLoggers maps each subsystem identifier to its associated logger. @@ -118,6 +121,7 @@ var subsystemLoggers = map[string]slog.Logger{ "STAT": statsLog, "CHRTS": chartsLog, "TRS": treasuryLog, + "TX": txLog, } // initLogRotator initializes the logging rotater to write logs to logFile and diff --git a/tx/log.go b/tx/log.go new file mode 100644 index 0000000..be1dc6b --- /dev/null +++ b/tx/log.go @@ -0,0 +1,23 @@ +// Copyright (c) 2013-2015 The btcsuite developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package tx + +import "github.com/decred/slog" + +// log is a logger that is initialized with no output filters. This +// means the package will not perform any logging by default until the caller +// requests it. +var log = slog.Disabled + +// DisableLog disables all library log output. Logging output is disabled +// by default until UseLogger is called. +func DisableLog() { + log = slog.Disabled +} + +// UseLogger uses a specified Logger to output package logging info. +func UseLogger(logger slog.Logger) { + log = logger +} From f9d5430f8c0dec6f7e0b1d221c7f4e82c20f7985 Mon Sep 17 00:00:00 2001 From: "Amos .E" Date: Tue, 30 Nov 2021 13:35:48 +0100 Subject: [PATCH 2/3] Implement Tx module activation logic --- director.go | 9 +++++++++ tx/handler.go | 10 ++++++++++ tx/tx.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ tx/types.go | 9 +++++++++ 4 files changed, 73 insertions(+) create mode 100644 tx/handler.go create mode 100644 tx/tx.go create mode 100644 tx/types.go diff --git a/director.go b/director.go index f495660..5f1c2cd 100644 --- a/director.go +++ b/director.go @@ -23,6 +23,7 @@ import ( "github.com/planetdecred/pdanalytics/stakingreward" "github.com/planetdecred/pdanalytics/stats" "github.com/planetdecred/pdanalytics/treasury" + "github.com/planetdecred/pdanalytics/tx" "github.com/planetdecred/pdanalytics/vsp" "github.com/planetdecred/pdanalytics/web" ) @@ -234,6 +235,14 @@ func setupModules(ctx context.Context, cfg *config, client *dcrd.Dcrd, server *w log.Info("Treasury chart activated") } + // Enable Tx module. + // This module is enabled irrespective of configuration. + // Since it is needed by other modules and activation only registers /tx route. + // It is more efficient to activate by default during startup. + if err := tx.Activate(server); err != nil { + return fmt.Errorf("Failed to ectivate the VSP modules, %s", err.Error()) + } + _, err = homepage.New(server, homepage.Mods{ Stk: stk, Prm: prms, diff --git a/tx/handler.go b/tx/handler.go new file mode 100644 index 0000000..c378f79 --- /dev/null +++ b/tx/handler.go @@ -0,0 +1,10 @@ +package tx + +import ( + "net/http" +) + +// TxPage is the page handler for the "/tx" path. +func (tx *Tx) TxPage(w http.ResponseWriter, r *http.Request) { + +} diff --git a/tx/tx.go b/tx/tx.go new file mode 100644 index 0000000..27bc71d --- /dev/null +++ b/tx/tx.go @@ -0,0 +1,45 @@ +package tx + +import ( + "context" + "net/http" + + "github.com/go-chi/chi" + "github.com/planetdecred/pdanalytics/web" +) + +type Tx struct { + server *web.Server +} + +// Activate activates the Transaction module. +func Activate(webServer *web.Server) error { + tx := &Tx{ + server: webServer, + } + + // Register routes. + tx.server.AddRoute("/tx/{txid}", web.GET, tx.TxPage, TransactionHashCtx) + tx.server.AddRoute("/tx/{txid}/{inout}/{inoutid}", web.GET, tx.TxPage, TransactionHashCtx, TransactionIoIndexCtx) + return nil +} + +// TransactionHashCtx embeds "txid" into the request context +func TransactionHashCtx(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + txid := chi.URLParam(r, "txid") + ctx := context.WithValue(r.Context(), ctxTxHash, txid) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +// TransactionIoIndexCtx embeds "inout" and "inoutid" into the request context +func TransactionIoIndexCtx(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + inout := chi.URLParam(r, "inout") + inoutid := chi.URLParam(r, "inoutid") + ctx := context.WithValue(r.Context(), ctxTxInOut, inout) + ctx = context.WithValue(ctx, ctxTxInOutId, inoutid) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} diff --git a/tx/types.go b/tx/types.go new file mode 100644 index 0000000..5cfbb9f --- /dev/null +++ b/tx/types.go @@ -0,0 +1,9 @@ +package tx + +type contexKey int + +const ( + ctxTxHash contexKey = iota + ctxTxInOut + ctxTxInOutId +) From 3b6f20346d8270192d186ed1c36e60b048079e65 Mon Sep 17 00:00:00 2001 From: "Amos .E" Date: Fri, 3 Dec 2021 19:30:20 +0100 Subject: [PATCH 3/3] Impliment http client for tx module --- go.mod | 4 ++-- go.sum | 8 ++++---- tx/client.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tx/tx.go | 20 ++++++++++++++++++-- 4 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 tx/client.go diff --git a/go.mod b/go.mod index 9db057a..eb64d6c 100644 --- a/go.mod +++ b/go.mod @@ -20,8 +20,8 @@ require ( github.com/decred/dcrdata/gov/v4 v4.0.0 github.com/decred/dcrdata/v5 v5.2.2 github.com/decred/dcrdata/v6 v6.0.0 - github.com/decred/dcrdata/v7 v7.0.0-20211005214036-90e57d284420 - github.com/decred/politeia v1.2.0 + github.com/decred/dcrdata/v7 v7.0.0-20211124233451-cdad9b4cb825 + github.com/decred/politeia v1.1.0 github.com/decred/slog v1.2.0 github.com/friendsofgo/errors v0.9.2 github.com/go-chi/chi v4.1.2+incompatible diff --git a/go.sum b/go.sum index 6e37e27..c53062b 100644 --- a/go.sum +++ b/go.sum @@ -432,8 +432,8 @@ github.com/decred/dcrdata/v5 v5.2.2/go.mod h1:q4KKI/dl3G9Dc9CE8e86B3Fa4cJQIKtIbn github.com/decred/dcrdata/v6 v6.0.0-20210510222533-6a2ca18d4382/go.mod h1:CWT5trkQ+8KUSBeyuI5/V1TQMleYyCh1vo/Ry6PiFWU= github.com/decred/dcrdata/v6 v6.0.0 h1:eNe2h7XCITLBM4FfBVd57VwqGYpCLJrnev38YzwJ9qw= github.com/decred/dcrdata/v6 v6.0.0/go.mod h1:3dzYsjv4i8/H7jAWRmIq3yXW5Wa/1Hv3xJV1oGDIgfk= -github.com/decred/dcrdata/v7 v7.0.0-20211005214036-90e57d284420 h1:iDWyHEw51GQ+uwYkdaSU9ApOtUabRvEq7+5pa4bSDXs= -github.com/decred/dcrdata/v7 v7.0.0-20211005214036-90e57d284420/go.mod h1:neDQncR16L03hwt4ZX41d8hno0RL3gVkNp+omLBoX9M= +github.com/decred/dcrdata/v7 v7.0.0-20211124233451-cdad9b4cb825 h1:tou5UWkavM6kdhX0gX4A4p6Cde34rirgTKUQpS4BZRc= +github.com/decred/dcrdata/v7 v7.0.0-20211124233451-cdad9b4cb825/go.mod h1:neDQncR16L03hwt4ZX41d8hno0RL3gVkNp+omLBoX9M= github.com/decred/dcrtime v0.0.0-20191018193024-8d8b4ef0458e h1:sNDR7vx6gaA3WD+WoEofTvtdjfwHAiogtjB3kt8iFco= github.com/decred/dcrtime v0.0.0-20191018193024-8d8b4ef0458e/go.mod h1:IyZnyBE3E6RBFsEjwEs21FrO/UsrLrL15hUnpZZQxpU= github.com/decred/dcrtime/api/v2 v2.0.0-20200912200806-b1e4dbc46be9/go.mod h1:JdIX208vnNj4TdU6hDRaN+ccxmxp1I1R6sWGZNK1BAQ= @@ -481,8 +481,8 @@ github.com/decred/go-socks v1.1.0 h1:dnENcc0KIqQo3HSXdgboXAHgqsCIutkqq6ntQjYtm2U github.com/decred/go-socks v1.1.0/go.mod h1:sDhHqkZH0X4JjSa02oYOGhcGHYp12FsY1jQ/meV8md0= github.com/decred/politeia v0.0.0-20191031182202-b33af07598f2/go.mod h1:biL6LECQ+6xwYHEJocudqMK+A1O0XbBshFkvFziFZgg= github.com/decred/politeia v1.0.1/go.mod h1:bWvCogzo8MzyPos4tX9V/5FXhVRUwcd6ycZhElcDLE0= -github.com/decred/politeia v1.2.0 h1:i8j+RhQSa88+qncwmP+hlUH8ja2tlMqfVIiPvL5Wevc= -github.com/decred/politeia v1.2.0/go.mod h1:IWytM75wWPGbQsEcQTEeHfX1KbcUt2dGDCtYbrrvqHI= +github.com/decred/politeia v1.1.0 h1:pWn6c8EGW90r9hLbGtOyQ3Kn8hAsXsIiXAJe+mLYL6Y= +github.com/decred/politeia v1.1.0/go.mod h1:IWytM75wWPGbQsEcQTEeHfX1KbcUt2dGDCtYbrrvqHI= github.com/decred/slog v1.0.0/go.mod h1:zR98rEZHSnbZ4WHZtO0iqmSZjDLKhkXfrPTZQKtAonQ= github.com/decred/slog v1.1.0/go.mod h1:kVXlGnt6DHy2fV5OjSeuvCJ0OmlmTF6LFpEPMu/fOY0= github.com/decred/slog v1.2.0 h1:soHAxV52B54Di3WtKLfPum9OFfWqwtf/ygf9njdfnPM= diff --git a/tx/client.go b/tx/client.go new file mode 100644 index 0000000..edea22a --- /dev/null +++ b/tx/client.go @@ -0,0 +1,48 @@ +package tx + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "strconv" + + apiTypes "github.com/decred/dcrdata/v7/api/types" +) + +type Client struct { + HttpClient *http.Client +} + +func NewClient() (c *Client) { + return &Client{ + HttpClient: &http.Client{}, + } +} + +func (client *Client) GetTransaction(apiBase string, txID string, spends bool) (*apiTypes.Tx, error) { + + tx := &apiTypes.Tx{} + + req, err := http.NewRequest("GET", apiBase+"tx/"+txID+"?spends="+strconv.FormatBool(spends), nil) + if err != nil { + return tx, err + } + + resp, err := client.HttpClient.Do(req) + if err != nil { + return tx, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return tx, err + } + + err = json.Unmarshal(body, &tx) + if err != nil { + return tx, err + } + + return tx, nil +} diff --git a/tx/tx.go b/tx/tx.go index 27bc71d..4daf9f2 100644 --- a/tx/tx.go +++ b/tx/tx.go @@ -9,18 +9,34 @@ import ( ) type Tx struct { - server *web.Server + server *web.Server + apiBase string + client Client } +const ( + apiURL = "http://143.110.159.142:7777/api/" +) + // Activate activates the Transaction module. func Activate(webServer *web.Server) error { + cl := NewClient() tx := &Tx{ - server: webServer, + server: webServer, + apiBase: apiURL, + client: cl, } // Register routes. tx.server.AddRoute("/tx/{txid}", web.GET, tx.TxPage, TransactionHashCtx) tx.server.AddRoute("/tx/{txid}/{inout}/{inoutid}", web.GET, tx.TxPage, TransactionHashCtx, TransactionIoIndexCtx) + + //client := NewClient() + //trx, err := client.GetTransaction(tx.apiBase, "bc202b8e511b5f57cccdd18c0713fdfbe9fbce79e59bf19e30fb50e489de22be", true) + //if err == nil { + // return err + //} + //log.Infof("Tx: %+v", trx) return nil }