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

     1  package textui
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  	"regexp"
     7  	"strconv"
     8  	"github.com/piotrnar/gocoin/lib/btc"
     9  	"github.com/piotrnar/gocoin/client/common"
    10  )
    11  
    12  
    13  func do_mining(s string) {
    14  	var totbtc, hrs, segwit_cnt uint64
    15  	if s != "" {
    16  		hrs, _ = strconv.ParseUint(s, 10, 64)
    17  	}
    18  	if hrs == 0 {
    19  		hrs = uint64(common.CFG.Stat.MiningHrs)
    20  	}
    21  	fmt.Println("Looking back", hrs, "hours...")
    22  	lim := uint32(time.Now().Add(-time.Hour*time.Duration(hrs)).Unix())
    23  	common.Last.Mutex.Lock()
    24  	bte := common.Last.Block
    25  	end := bte
    26  	common.Last.Mutex.Unlock()
    27  	cnt, diff := 0, float64(0)
    28  	tot_blocks, tot_blocks_len := 0, 0
    29  
    30  	bip100_voting := make(map[string]uint)
    31  	bip100x := regexp.MustCompile("/BV{0,1}[0-9]+[M]{0,1}/")
    32  
    33  	eb_ad_voting := make(map[string]uint)
    34  	eb_ad_x := regexp.MustCompile("/EB[0-9]+/AD[0-9]+/")
    35  
    36  	for end.Timestamp() >= lim {
    37  		bl, _, e := common.BlockChain.Blocks.BlockGet(end.BlockHash)
    38  		if e != nil {
    39  			println(cnt, e.Error())
    40  			return
    41  		}
    42  		block, e := btc.NewBlock(bl)
    43  		if e!=nil {
    44  			println("btc.NewBlock failed", e.Error())
    45  			return
    46  		}
    47  
    48  		bt, _ := btc.NewBlock(bl)
    49  		cbasetx, _ := btc.NewTx(bl[bt.TxOffset:])
    50  
    51  		tot_blocks++
    52  		tot_blocks_len += len(bl)
    53  		diff += btc.GetDifficulty(block.Bits())
    54  
    55  		if (block.Version()&0x20000002) == 0x20000002 {
    56  			segwit_cnt++
    57  		}
    58  
    59  		res := bip100x.Find(cbasetx.TxIn[0].ScriptSig)
    60  		if res!=nil {
    61  			bip100_voting[string(res)]++
    62  			nimer, _ := common.TxMiner(cbasetx)
    63  			fmt.Println("      block", end.Height, "by", nimer, "BIP100 voting", string(res), " total:", bip100_voting[string(res)])
    64  		}
    65  
    66  		res = eb_ad_x.Find(cbasetx.TxIn[0].ScriptSig)
    67  		if res!=nil {
    68  			eb_ad_voting[string(res)]++
    69  		}
    70  
    71  		end = end.Parent
    72  	}
    73  	if tot_blocks == 0 {
    74  		fmt.Println("There are no blocks from the last", hrs, "hour(s)")
    75  		return
    76  	}
    77  	diff /= float64(tot_blocks)
    78  	if cnt > 0 {
    79  		fmt.Printf("Projected weekly income : %.0f BTC,  estimated hashrate : %s\n",
    80  			7*24*float64(totbtc)/float64(hrs)/1e8,
    81  			common.HashrateToString(float64(cnt)/float64(6*hrs) * diff * 7158278.826667))
    82  	}
    83  	bph := float64(tot_blocks)/float64(hrs)
    84  	fmt.Printf("Total network hashrate : %s @ average diff %.0f  (%.2f bph)\n",
    85  		common.HashrateToString(bph/6 * diff * 7158278.826667), diff, bph)
    86  	fmt.Printf("%d blocks in %d hours. Average size %.1f KB,  next diff in %d blocks\n",
    87  		tot_blocks, hrs, float64(tot_blocks_len/tot_blocks)/1e3, 2016-bte.Height%2016)
    88  
    89  	fmt.Printf("\nSegWit Voting: %d (%.1f%%)\n", segwit_cnt, float64(segwit_cnt)*100/float64(tot_blocks))
    90  	fmt.Println()
    91  	fmt.Println("BU Voting")
    92  	for k, v := range eb_ad_voting {
    93  		fmt.Printf(" %s \t %d \t %.1f%%\n", k, v, float64(v)*100/float64(tot_blocks))
    94  	}
    95  }
    96  
    97  
    98  func init() {
    99  	newUi("minerstat m", false, do_mining, "Look for the miner ID in recent blocks (optionally specify number of hours)")
   100  }