github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/cmd/geth/build_atxi_cmd.go (about)

     1  package main
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/ethereumproject/go-ethereum/core"
     7  	"github.com/ethereumproject/go-ethereum/ethdb"
     8  	"github.com/ethereumproject/go-ethereum/logger/glog"
     9  	"gopkg.in/urfave/cli.v1"
    10  )
    11  
    12  var buildAddrTxIndexCommand = cli.Command{
    13  	Action: buildAddrTxIndexCmd,
    14  	Name:   "atxi-build",
    15  	Usage:  "Generate index for transactions by address",
    16  	Description: `
    17  	Builds an index for transactions by address. 
    18  	The command is idempotent; it will not hurt to run multiple times on the same range.
    19  	If run without --start flag, the command makes use of a persistent placeholder, so you can
    20  	run the command on multiple occasions and pick up indexing progress where the last session
    21  	left off.
    22  	To enable address-transaction indexing during block sync and import, use the '--atxi' flag.
    23  			`,
    24  	Flags: []cli.Flag{
    25  		cli.IntFlag{
    26  			Name:  "start",
    27  			Usage: "Block number at which to begin building index",
    28  		},
    29  		cli.IntFlag{
    30  			Name:  "stop",
    31  			Usage: "Block number at which to stop building index",
    32  		},
    33  		cli.IntFlag{
    34  			Name:  "step",
    35  			Usage: "Step increment for batching. Higher number requires more mem, but may be faster",
    36  			Value: 10000,
    37  		},
    38  	},
    39  }
    40  
    41  func buildAddrTxIndexCmd(ctx *cli.Context) error {
    42  	// Divide global cache availability equally between chaindata (pre-existing blockdata) and
    43  	// address-transaction database. This ratio is arbitrary and could potentially be optimized or delegated to be user configurable.
    44  	ethdb.SetCacheRatio("chaindata", 0.5)
    45  	ethdb.SetHandleRatio("chaindata", 1)
    46  	ethdb.SetCacheRatio("indexes", 0.5)
    47  	ethdb.SetHandleRatio("indexes", 1)
    48  
    49  	var startIndex uint64 = math.MaxUint64
    50  	if ctx.IsSet("start") {
    51  		startIndex = uint64(ctx.Int("start"))
    52  	}
    53  	stopIndex := uint64(ctx.Int("stop"))
    54  	step := uint64(ctx.Int("step"))
    55  
    56  	indexDB := MakeIndexDatabase(ctx)
    57  	if indexDB == nil {
    58  		glog.Fatalln("can't open index database")
    59  	}
    60  	defer indexDB.Close()
    61  
    62  	bc, chainDB := MakeChain(ctx)
    63  	if bc == nil || chainDB == nil {
    64  		glog.Fatalln("can't open chain database")
    65  	}
    66  	defer chainDB.Close()
    67  
    68  	bc.SetAtxi(&core.AtxiT{Db: indexDB, AutoMode: false, Progress: &core.AtxiProgressT{}})
    69  	return core.BuildAddrTxIndex(bc, chainDB, indexDB, startIndex, stopIndex, step)
    70  }