Skip to content

Commit

Permalink
Merge branch 'staging' into update/initial-state
Browse files Browse the repository at this point in the history
  • Loading branch information
Manali-Jain-squareops committed May 5, 2023
2 parents 3cd2d6d + 20f25f2 commit a2b9af4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions code/go/0chain.net/chaincore/chain/handler.go
Expand Up @@ -758,6 +758,11 @@ func DiagnosticsHomepageHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "</td>")
fmt.Fprintf(w, "<td valign='top'>")
fmt.Fprintf(w, "<li><a href='_chain_stats'>/_chain_stats</a></li>")

if node.NodeType(selfNodeType) == node.NodeTypeSharder {
fmt.Fprintf(w, "<li><a href='_transaction_errors'>/_transaction_errors</a></li>")
}

if node.NodeType(selfNodeType) == node.NodeTypeSharder {
fmt.Fprintf(w, "<li><a href='_healthcheck'>/_healthcheck</a></li>")
}
Expand Down
29 changes: 29 additions & 0 deletions code/go/0chain.net/sharder/handler.go
Expand Up @@ -29,6 +29,7 @@ func handlersMap() map[string]func(http.ResponseWriter, *http.Request) {
"/v1/sharder/get/stats": common.ToJSONResponse(SharderStatsHandler),
"/v1/state/nodes": common.ToJSONResponse(chain.StateNodesHandler),
"/v1/block/state_change": common.ToJSONResponse(BlockStateChangeHandler),
"/_transaction_errors": TransactionErrorWriter,
}

handlers := make(map[string]func(http.ResponseWriter, *http.Request))
Expand Down Expand Up @@ -149,6 +150,7 @@ func ChainStatsHandler(ctx context.Context, r *http.Request) (interface{}, error
func ChainStatsWriter(w http.ResponseWriter, r *http.Request) {
sc := GetSharderChain()
c := sc.Chain

w.Header().Set("Content-Type", "text/html")
chain.PrintCSS(w)
diagnostics.WriteStatisticsCSS(w)
Expand Down Expand Up @@ -269,3 +271,30 @@ func SharderStatsHandler(ctx context.Context, r *http.Request) (interface{}, err
MeanScanBlockStatsTime: cc.BlockSyncTimer.Mean() / 1000000.0,
}, nil
}

func TransactionErrorWriter(w http.ResponseWriter, r *http.Request) {

transactionErrors, err := GetSharderChain().Chain.GetEventDb().GetTransactionErrors()
if err != nil {
fmt.Fprintf(w, "Error getting transaction errors: %v", err)
return
}

w.Header().Set("Content-Type", "text/html")
chain.PrintCSS(w)
diagnostics.WriteStatisticsCSS(w)

fmt.Fprintf(w, "<h2>Transaction Output - Count</h2>")

fmt.Fprintf(w, "<br>")
fmt.Fprintf(w, "<table>")
fmt.Fprintf(w, "<tr><td>")
fmt.Fprintf(w, "<table width='100%%'>")

for _, transactionError := range transactionErrors {
fmt.Fprintf(w, "<tr><td class='tname'>%s</td><td>%d</td></tr>", transactionError.TransactionOutput, transactionError.Count)
}

fmt.Fprintf(w, "</td><td valign='top'>")
fmt.Fprintf(w, "</table>")
}
26 changes: 26 additions & 0 deletions code/go/0chain.net/sharder/worker.go
Expand Up @@ -42,6 +42,8 @@ func SetupWorkers(ctx context.Context) {
go sc.UpdateMagicBlockWorker(ctx)
go sc.RegisterSharderKeepWorker(ctx)
go sc.SharderHealthCheck(ctx)

go sc.TrackTransactionErrors(ctx)
}

/*BlockWorker - stores the blocks */
Expand Down Expand Up @@ -414,3 +416,27 @@ func (sc *Chain) SharderHealthCheck(ctx context.Context) {
time.Sleep(HEALTH_CHECK_TIMER)
}
}

func (sc *Chain) TrackTransactionErrors(ctx context.Context) {

var (
timer = time.NewTimer(10 * time.Minute)
)

edb := sc.GetQueryStateContext().GetEventDB()

for {
select {
case <-ctx.Done():
return
case <-timer.C:

err := edb.UpdateTransactionErrors()
if err != nil {
logging.Logger.Info("TrackTransactionErrors : ", zap.Error(err))
}

timer = time.NewTimer(10 * time.Minute)
}
}
}
40 changes: 40 additions & 0 deletions code/go/0chain.net/smartcontract/dbs/event/transaction.go
Expand Up @@ -4,7 +4,10 @@ import (
"0chain.net/smartcontract/common"
"0chain.net/smartcontract/dbs/model"
"github.com/0chain/common/core/currency"
"github.com/0chain/common/core/logging"
"go.uber.org/zap"
"gorm.io/gorm/clause"
"time"
)

// Transaction model to save the transaction data
Expand All @@ -29,6 +32,12 @@ type Transaction struct {
Status int `json:"status"`
}

type TransactionErrors struct {
TransactionOutput string `json:"transaction_output"`
OutputHash string `json:"output_hash"`
Count int `json:"count"`
}

func (edb *EventDb) addTransactions(txns []Transaction) error {
return edb.Store.Get().Create(&txns).Error
}
Expand Down Expand Up @@ -134,3 +143,34 @@ func (edb *EventDb) GetTransactionsForBlocks(blockStart, blockEnd int64) ([]Tran
Find(&tr)
return tr, res.Error
}

func (edb *EventDb) UpdateTransactionErrors() error {
db := edb.Get()

// created_at for last day from now
lastDay := time.Now().AddDate(0, 0, -1)
// convert to string
lastDayString := lastDay.Format("2006-01-02 15:04:05")

// clean up the transaction error table
err := db.Exec("TRUNCATE TABLE transaction_errors").Error
if err != nil {
return err
}

if dbTxn := db.Exec("INSERT INTO transaction_errors (transaction_output, output_hash, count) "+
"SELECT transaction_output, output_hash, count(*) as count FROM transactions WHERE status = ? and created_at > ? "+
"GROUP BY output_hash, transaction_output", 2, lastDayString); dbTxn.Error != nil {

logging.Logger.Error("Error while inserting transactions in transaction error table", zap.Any("error", dbTxn.Error))
return dbTxn.Error
}

return nil
}

func (edb *EventDb) GetTransactionErrors() ([]TransactionErrors, error) {
var transactions []TransactionErrors
err := edb.Get().Model(&TransactionErrors{}).Find(&transactions)
return transactions, err.Error
}
Expand Up @@ -1022,6 +1022,23 @@ ALTER TABLE public.transactions_id_seq OWNER TO zchain_user;

ALTER SEQUENCE public.transactions_id_seq OWNED BY public.transactions.id;



--
-- Name: transaction_errors; Type: TABLE; Schema: public; Owner: zchain_user
--

CREATE TABLE public.transaction_errors (
id SERIAL PRIMARY KEY,
created_at timestamp with time zone,
transaction_output text,
output_hash text,
count bigint
);


ALTER TABLE public.transaction_errors OWNER TO zchain_user;

--
-- Name: burn_tickets; Type: TABLE; Schema: public; Owner: zchain_user
--
Expand Down

0 comments on commit a2b9af4

Please sign in to comment.