github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/usif/webui/blocks.go (about)

     1  package webui
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/piotrnar/gocoin/client/common"
     9  	"github.com/piotrnar/gocoin/client/usif"
    10  	"github.com/piotrnar/gocoin/lib/btc"
    11  
    12  	"strconv"
    13  	"time"
    14  )
    15  
    16  func p_blocks(w http.ResponseWriter, r *http.Request) {
    17  	if !ipchecker(r) {
    18  		return
    19  	}
    20  
    21  	s := load_template("blocks.html")
    22  	fees_chart := load_template("fees_chart.html")
    23  	s = strings.Replace(s, "<!-- include fees_chart.html -->", fees_chart, 1)
    24  
    25  	write_html_head(w, r)
    26  	w.Write([]byte(s))
    27  	write_html_tail(w)
    28  }
    29  
    30  func json_blocks(w http.ResponseWriter, r *http.Request) {
    31  	if !ipchecker(r) {
    32  		return
    33  	}
    34  
    35  	type one_block struct {
    36  		Height    uint32
    37  		Timestamp uint32
    38  		Hash      string
    39  		TxCnt     int
    40  		Size      int
    41  		Weight    uint
    42  		Version   uint32
    43  		Reward    uint64
    44  		Miner     string
    45  		FeeSPB    float64
    46  
    47  		OrdCnt    uint
    48  		OrdSize   uint
    49  		OrdWeight uint
    50  
    51  		Received                          uint32
    52  		TimePre, TimeDl, TimeVer, TimeQue int
    53  		WasteCnt                          uint
    54  		MissedCnt                         int
    55  		FromConID                         uint32
    56  		Sigops                            int
    57  
    58  		NonWitnessSize int
    59  
    60  		HaveFeeStats bool
    61  
    62  		PaidTxVSize uint
    63  		TotalFees   uint64
    64  	}
    65  
    66  	var blks []*one_block
    67  
    68  	common.Last.Mutex.Lock()
    69  	end := common.Last.Block
    70  	common.Last.Mutex.Unlock()
    71  
    72  	for cnt := uint32(0); end != nil && cnt < common.GetUint32(&common.CFG.WebUI.ShowBlocks); cnt++ {
    73  		bl, _, e := common.BlockChain.Blocks.BlockGet(end.BlockHash)
    74  		if e != nil {
    75  			break
    76  		}
    77  		block, e := btc.NewBlockX(bl, end.BlockHash)
    78  		if e != nil {
    79  			break
    80  		}
    81  		common.BlockChain.BlockIndexAccess.Lock()
    82  		node := common.BlockChain.BlockIndex[end.BlockHash.BIdx()]
    83  		common.BlockChain.BlockIndexAccess.Unlock()
    84  
    85  		var cbasetx *btc.Tx
    86  
    87  		rb, cbasetx := usif.GetReceivedBlockX(block)
    88  
    89  		b := new(one_block)
    90  		b.Height = end.Height
    91  		b.Timestamp = block.BlockTime()
    92  		b.Hash = end.BlockHash.String()
    93  		b.TxCnt = block.TxCount
    94  		b.Size = len(bl)
    95  		b.Weight = rb.TheWeight
    96  		b.NonWitnessSize = rb.NonWitnessSize
    97  		b.Version = block.Version()
    98  
    99  		b.OrdCnt = rb.TheOrdCnt
   100  		b.OrdSize = rb.TheOrdSize
   101  		b.OrdWeight = rb.TheOrdWeight
   102  
   103  		for o := range cbasetx.TxOut {
   104  			b.Reward += cbasetx.TxOut[o].Value
   105  		}
   106  
   107  		b.Miner, _ = common.TxMiner(cbasetx)
   108  		b.PaidTxVSize = rb.ThePaidVSize
   109  		b.TotalFees = b.Reward - btc.GetBlockReward(end.Height)
   110  		if rb.ThePaidVSize > 0 {
   111  			b.FeeSPB = float64(b.TotalFees) / float64(rb.ThePaidVSize)
   112  		}
   113  
   114  		b.Received = uint32(rb.TmStart.Unix())
   115  		b.Sigops = int(node.SigopsCost)
   116  
   117  		if rb.TmPreproc.IsZero() {
   118  			b.TimePre = -1
   119  		} else {
   120  			b.TimePre = int(rb.TmPreproc.Sub(rb.TmStart) / time.Millisecond)
   121  		}
   122  
   123  		if rb.TmDownload.IsZero() {
   124  			b.TimeDl = -1
   125  		} else {
   126  			b.TimeDl = int(rb.TmDownload.Sub(rb.TmStart) / time.Millisecond)
   127  		}
   128  
   129  		if rb.TmQueue.IsZero() {
   130  			b.TimeQue = -1
   131  		} else {
   132  			b.TimeQue = int(rb.TmQueue.Sub(rb.TmStart) / time.Millisecond)
   133  		}
   134  
   135  		if rb.TmAccepted.IsZero() {
   136  			b.TimeVer = -1
   137  		} else {
   138  			b.TimeVer = int(rb.TmAccepted.Sub(rb.TmStart) / time.Millisecond)
   139  		}
   140  
   141  		b.WasteCnt = rb.Cnt
   142  		b.MissedCnt = rb.TxMissing
   143  		b.FromConID = rb.FromConID
   144  
   145  		usif.BlockFeesMutex.Lock()
   146  		_, b.HaveFeeStats = usif.BlockFees[end.Height]
   147  		usif.BlockFeesMutex.Unlock()
   148  
   149  		blks = append(blks, b)
   150  		end = end.Parent
   151  	}
   152  
   153  	bx, er := json.Marshal(blks)
   154  	if er == nil {
   155  		w.Header()["Content-Type"] = []string{"application/json"}
   156  		w.Write(bx)
   157  	} else {
   158  		println(er.Error())
   159  	}
   160  
   161  }
   162  
   163  func json_blfees(w http.ResponseWriter, r *http.Request) {
   164  	if !ipchecker(r) {
   165  		return
   166  	}
   167  
   168  	if len(r.Form["height"]) == 0 {
   169  		w.Write([]byte("No hash given"))
   170  		return
   171  	}
   172  
   173  	height, e := strconv.ParseUint(r.Form["height"][0], 10, 32)
   174  	if e != nil {
   175  		w.Write([]byte(e.Error()))
   176  		return
   177  	}
   178  
   179  	usif.BlockFeesMutex.Lock()
   180  	fees, ok := usif.BlockFees[uint32(height)]
   181  	usif.BlockFeesMutex.Unlock()
   182  
   183  	if !ok {
   184  		w.Write([]byte("File not found"))
   185  		return
   186  	}
   187  
   188  	bx, er := json.Marshal(fees)
   189  	if er == nil {
   190  		w.Header()["Content-Type"] = []string{"application/json"}
   191  		w.Write(bx)
   192  	} else {
   193  		println(er.Error())
   194  	}
   195  }