github.com/evdatsion/aphelion-dpos-bft@v0.32.1/scripts/wal2json/main.go (about)

     1  /*
     2  	wal2json converts binary WAL file to JSON.
     3  
     4  	Usage:
     5  			wal2json <path-to-wal>
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"io"
    13  	"os"
    14  
    15  	amino "github.com/evdatsion/go-amino"
    16  	cs "github.com/evdatsion/aphelion-dpos-bft/consensus"
    17  	"github.com/evdatsion/aphelion-dpos-bft/types"
    18  )
    19  
    20  var cdc = amino.NewCodec()
    21  
    22  func init() {
    23  	cs.RegisterConsensusMessages(cdc)
    24  	cs.RegisterWALMessages(cdc)
    25  	types.RegisterBlockAmino(cdc)
    26  }
    27  
    28  func main() {
    29  	if len(os.Args) < 2 {
    30  		fmt.Println("missing one argument: <path-to-wal>")
    31  		os.Exit(1)
    32  	}
    33  
    34  	f, err := os.Open(os.Args[1])
    35  	if err != nil {
    36  		panic(fmt.Errorf("failed to open WAL file: %v", err))
    37  	}
    38  	defer f.Close()
    39  
    40  	dec := cs.NewWALDecoder(f)
    41  	for {
    42  		msg, err := dec.Decode()
    43  		if err == io.EOF {
    44  			break
    45  		} else if err != nil {
    46  			panic(fmt.Errorf("failed to decode msg: %v", err))
    47  		}
    48  
    49  		json, err := cdc.MarshalJSON(msg)
    50  		if err != nil {
    51  			panic(fmt.Errorf("failed to marshal msg: %v", err))
    52  		}
    53  
    54  		_, err = os.Stdout.Write(json)
    55  		if err == nil {
    56  			_, err = os.Stdout.Write([]byte("\n"))
    57  		}
    58  		if err == nil {
    59  			if end, ok := msg.Msg.(cs.EndHeightMessage); ok {
    60  				_, err = os.Stdout.Write([]byte(fmt.Sprintf("ENDHEIGHT %d\n", end.Height))) // nolint: errcheck, gas
    61  			}
    62  		}
    63  		if err != nil {
    64  			fmt.Println("Failed to write message", err)
    65  			os.Exit(1)
    66  		}
    67  	}
    68  }