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

     1  package textui
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/piotrnar/gocoin/client/common"
     8  	"github.com/piotrnar/gocoin/client/network"
     9  	"github.com/piotrnar/gocoin/lib/btc"
    10  )
    11  
    12  func get_total_block_fees(txs []*network.OneTxToSend) (totfees uint64, totwgh, tcnt int) {
    13  	var quiet bool
    14  	already_in := make(map[[32]byte]bool)
    15  	for _, tx := range txs {
    16  		wght := tx.Weight()
    17  		if totwgh+wght > 4e6 {
    18  			break
    19  		}
    20  		tcnt++
    21  		totfees += tx.Fee
    22  		totwgh += wght
    23  		if quiet {
    24  			continue
    25  		}
    26  		for _, txinp := range tx.TxIn {
    27  			inp := &txinp.Input
    28  			tout := common.BlockChain.Unspent.UnspentGet(inp)
    29  			if tout == nil && !already_in[inp.Hash] {
    30  				println(" *** block invalid - tx", tx.Hash.String(), "at offs", tcnt, "needs", btc.NewUint256(inp.Hash[:]).String())
    31  				/*
    32  					println("writing txs.txt")
    33  					if f, _ := os.Create("txs.txt"); f != nil {
    34  						for _, tt := range txs {
    35  							fmt.Fprintln(f, tt.Hash.String())
    36  						}
    37  						f.Close()
    38  					}
    39  				*/
    40  				quiet = true
    41  			}
    42  		}
    43  		already_in[tx.Hash.Hash] = true
    44  	}
    45  	return
    46  }
    47  
    48  func new_block(par string) {
    49  	sta := time.Now()
    50  	txs := network.GetSortedMempool()
    51  	println(len(txs), "OLD tx_sort got in", time.Since(sta).String())
    52  
    53  	sta = time.Now()
    54  	cpfp := network.GetSortedMempoolNew()
    55  	println(len(cpfp), "NEW tx_sort got in", time.Since(sta).String())
    56  
    57  	var totwgh, tcnt int
    58  	var totfees, totfees2 uint64
    59  	totfees, totwgh, tcnt = get_total_block_fees(txs)
    60  	println("Fees from OLD sorting:", btc.UintToBtc(totfees), totwgh, tcnt)
    61  
    62  	totfees2, totwgh, tcnt = get_total_block_fees(cpfp)
    63  	fmt.Println("Fees from NEW sorting:", btc.UintToBtc(totfees2), totwgh, tcnt)
    64  
    65  	if totfees2 > totfees {
    66  		fmt.Printf("New method profit: %.3f%%\n", 100.0*float64(totfees2-totfees)/float64(totfees))
    67  	} else {
    68  		fmt.Printf("New method -LOSE-: %.3f%%\n", 100.0*float64(totfees-totfees2)/float64(totfees))
    69  	}
    70  }
    71  
    72  func gettxchildren(par string) {
    73  	txid := btc.NewUint256FromString(par)
    74  	if txid == nil {
    75  		println("Specify valid txid")
    76  		return
    77  	}
    78  	bidx := txid.BIdx()
    79  	t2s := network.TransactionsToSend[bidx]
    80  	if t2s == nil {
    81  		println(txid.String(), "not im mempool")
    82  		return
    83  	}
    84  	chlds := t2s.GetAllChildren()
    85  	println("has", len(chlds), "all children")
    86  	var tot_wg, tot_fee uint64
    87  	for _, tx := range chlds {
    88  		println(" -", tx.Hash.String(), len(tx.GetChildren()), tx.SPB(), "@", tx.Weight())
    89  		tot_wg += uint64(tx.Weight())
    90  		tot_fee += tx.Fee
    91  		//gettxchildren(tx.Hash.String())
    92  	}
    93  	println("Groups SPB:", float64(tot_fee)/float64(tot_wg)*4.0)
    94  }
    95  
    96  func init() {
    97  	newUi("newblock nb", true, new_block, "build a new block")
    98  	newUi("txchild ch", true, gettxchildren, "show all the children fo the given tx")
    99  }