mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-23 17:51:39 +00:00
add json2wal & fix wal2json (#2196)
* add json2wal & fix wal2json * fix bug * Update main.go * add wal2JsonTest file * Delete wal2JsonTest
This commit is contained in:
committed by
Anton Kaliaev
parent
5a8fe61200
commit
785786bec4
@ -164,7 +164,7 @@ $EDITOR /tmp/corrupted_wal
|
|||||||
5. After editing, convert this file back into binary form by running:
|
5. After editing, convert this file back into binary form by running:
|
||||||
|
|
||||||
```
|
```
|
||||||
./scripts/json2wal/json2wal /tmp/corrupted_wal > "$TMHOME/data/cs.wal/wal"
|
./scripts/json2wal/json2wal /tmp/corrupted_wal $TMHOME/data/cs.wal/wal
|
||||||
```
|
```
|
||||||
|
|
||||||
## Hardware
|
## Hardware
|
||||||
|
75
scripts/json2wal/main.go
Normal file
75
scripts/json2wal/main.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
json2wal converts JSON file to binary WAL file.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
json2wal <path-to-JSON> <path-to-wal>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/tendermint/go-amino"
|
||||||
|
"github.com/tendermint/tendermint/types"
|
||||||
|
cs "github.com/tendermint/tendermint/consensus"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"io"
|
||||||
|
"bufio"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cdc = amino.NewCodec()
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cs.RegisterConsensusMessages(cdc)
|
||||||
|
cs.RegisterWALMessages(cdc)
|
||||||
|
types.RegisterBlockAmino(cdc)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
br := bufio.NewReader(f)
|
||||||
|
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 = cdc.UnmarshalJSON(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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,14 +8,23 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
cs "github.com/tendermint/tendermint/consensus"
|
cs "github.com/tendermint/tendermint/consensus"
|
||||||
|
"github.com/tendermint/go-amino"
|
||||||
|
"github.com/tendermint/tendermint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var cdc = amino.NewCodec()
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cs.RegisterConsensusMessages(cdc)
|
||||||
|
cs.RegisterWALMessages(cdc)
|
||||||
|
types.RegisterBlockAmino(cdc)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
fmt.Println("missing one argument: <path-to-wal>")
|
fmt.Println("missing one argument: <path-to-wal>")
|
||||||
@ -37,7 +46,7 @@ func main() {
|
|||||||
panic(fmt.Errorf("failed to decode msg: %v", err))
|
panic(fmt.Errorf("failed to decode msg: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
json, err := json.Marshal(msg)
|
json, err := cdc.MarshalJSON(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("failed to marshal msg: %v", err))
|
panic(fmt.Errorf("failed to marshal msg: %v", err))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user