github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/internal/cli/debug_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/mitchellh/cli"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/ethereum/go-ethereum/internal/cli/server"
    14  )
    15  
    16  var currentDir string
    17  
    18  func TestCommand_DebugBlock(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	// Start a blockchain in developer mode and get trace of block
    22  	config := server.DefaultConfig()
    23  
    24  	// enable developer mode
    25  	config.Developer.Enabled = true
    26  	config.Developer.Period = 2          // block time
    27  	config.Developer.GasLimit = 11500000 // initial block gaslimit
    28  
    29  	// enable archive mode for getting traces of ancient blocks
    30  	config.GcMode = "archive"
    31  
    32  	// start the mock server
    33  	srv, err := server.CreateMockServer(config)
    34  	require.NoError(t, err)
    35  
    36  	defer server.CloseMockServer(srv)
    37  
    38  	// get the grpc port
    39  	port := srv.GetGrpcAddr()
    40  
    41  	// wait for 4 seconds to mine a 2 blocks
    42  	time.Sleep(2 * time.Duration(config.Developer.Period) * time.Second)
    43  
    44  	// add prefix for debug trace
    45  	prefix := "bor-block-trace-"
    46  
    47  	// output dir
    48  	output := "debug_block_test"
    49  
    50  	// set current directory
    51  	currentDir, _ = os.Getwd()
    52  
    53  	// trace 1st block
    54  	start := time.Now()
    55  	dst1 := path.Join(output, prefix+time.Now().UTC().Format("2006-01-02-150405Z"), "block.json")
    56  	res := traceBlock(port, 1, output)
    57  	require.Equal(t, 0, res)
    58  	t.Logf("Completed trace of block %d in %d ms at %s", 1, time.Since(start).Milliseconds(), dst1)
    59  
    60  	// adding this to avoid debug directory name conflicts
    61  	time.Sleep(time.Second)
    62  
    63  	// trace last/recent block
    64  	start = time.Now()
    65  	latestBlock := srv.GetLatestBlockNumber().Int64()
    66  	dst2 := path.Join(output, prefix+time.Now().UTC().Format("2006-01-02-150405Z"), "block.json")
    67  	res = traceBlock(port, latestBlock, output)
    68  	require.Equal(t, 0, res)
    69  	t.Logf("Completed trace of block %d in %d ms at %s", latestBlock, time.Since(start).Milliseconds(), dst2)
    70  
    71  	// verify if the trace files are created
    72  	done := verify(dst1)
    73  	require.Equal(t, true, done)
    74  	done = verify(dst2)
    75  	require.Equal(t, true, done)
    76  
    77  	// delete the traces
    78  	deleteTraces(output)
    79  }
    80  
    81  // traceBlock calls the cli command to trace a block
    82  func traceBlock(port string, number int64, output string) int {
    83  	ui := cli.NewMockUi()
    84  	command := &DebugBlockCommand{
    85  		Meta2: &Meta2{
    86  			UI:   ui,
    87  			addr: "127.0.0.1:" + port,
    88  		},
    89  	}
    90  
    91  	// run trace (by explicitly passing the output directory and grpc address)
    92  	return command.Run([]string{strconv.FormatInt(number, 10), "--output", output, "--address", command.Meta2.addr})
    93  }
    94  
    95  // verify checks if the trace file is created at the destination
    96  // directory or not
    97  func verify(dst string) bool {
    98  	dst = path.Join(currentDir, dst)
    99  	if file, err := os.Stat(dst); err == nil {
   100  		// check if the file has content
   101  		if file.Size() > 0 {
   102  			return true
   103  		}
   104  	}
   105  
   106  	return false
   107  }
   108  
   109  // deleteTraces removes the traces created during the test
   110  func deleteTraces(dst string) {
   111  	dst = path.Join(currentDir, dst)
   112  	os.RemoveAll(dst)
   113  }