mukan-consensus/scripts/json2wal/main.go
Mukan Erkin Törük ef24c0b67e
Some checks are pending
docker-build-cometbft / vars (push) Waiting to run
docker-build-cometbft / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-cometbft / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-cometbft / merge-images (push) Blocked by required conditions
docker-build-e2e-node / vars (push) Waiting to run
docker-build-e2e-node / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-e2e-node / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-e2e-node / merge-images (push) Blocked by required conditions
initial: sovereign Mukan Network fork
2026-05-11 03:18:27 +03:00

69 lines
1.5 KiB
Go

/*
json2wal converts JSON file to binary WAL file.
Usage:
json2wal <path-to-JSON> <path-to-wal>
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
cs "github.com/cometbft/cometbft/consensus"
cmtjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/types"
)
func main() {
if len(os.Args) < 3 {
fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON> <path-to-wal>")
os.Exit(1)
}
f, err := os.Open(os.Args[1])
if err != nil {
panic(fmt.Errorf("failed to open WAL file: %v", err))
}
defer f.Close()
walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(fmt.Errorf("failed to open WAL file: %v", err))
}
defer walFile.Close()
// the length of wal/MsgInfo in the wal.json may exceed the defaultBufSize(4096) of bufio
// because of the byte array in BlockPart
// leading to unmarshal error: unexpected end of JSON input
br := bufio.NewReaderSize(f, int(2*types.BlockPartSizeBytes))
dec := cs.NewWALEncoder(walFile)
for {
msgJSON, _, err := br.ReadLine()
if err == io.EOF {
break
} else if err != nil {
panic(fmt.Errorf("failed to read file: %v", err))
}
// ignore the ENDHEIGHT in json.File
if strings.HasPrefix(string(msgJSON), "ENDHEIGHT") {
continue
}
var msg cs.TimedWALMessage
err = cmtjson.Unmarshal(msgJSON, &msg)
if err != nil {
panic(fmt.Errorf("failed to unmarshal json: %v", err))
}
err = dec.Encode(&msg)
if err != nil {
panic(fmt.Errorf("failed to encode msg: %v", err))
}
}
}