Skip to content

[WIP] Tx module #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions director.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions tx/client.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions tx/handler.go
Original file line number Diff line number Diff line change
@@ -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) {

}
23 changes: 23 additions & 0 deletions tx/log.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 61 additions & 0 deletions tx/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tx

import (
"context"
"net/http"

"github.com/go-chi/chi"
"github.com/planetdecred/pdanalytics/web"
)

type Tx struct {
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,
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
}

// 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))
})
}
9 changes: 9 additions & 0 deletions tx/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tx

type contexKey int

const (
ctxTxHash contexKey = iota
ctxTxInOut
ctxTxInOutId
)