2020-06-24 13:11:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-12 16:24:11 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2020-06-24 13:11:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2021-03-29 12:49:46 +00:00
|
|
|
var ledgerContractID = -4
|
2021-02-12 16:24:11 +00:00
|
|
|
|
2020-06-24 13:11:34 +00:00
|
|
|
type dump []blockDump
|
|
|
|
|
|
|
|
type blockDump struct {
|
|
|
|
Block uint32 `json:"block"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Storage []storageOp `json:"storage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type storageOp struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(path string) (dump, error) {
|
2022-02-22 16:27:32 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2020-06-24 13:11:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d := make(dump, 0)
|
|
|
|
if err := json.Unmarshal(data, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dump) normalize() {
|
2021-02-12 16:24:11 +00:00
|
|
|
ledgerIDBytes := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(ledgerIDBytes, uint32(ledgerContractID))
|
2020-06-24 13:11:34 +00:00
|
|
|
for i := range d {
|
2021-02-12 16:24:11 +00:00
|
|
|
var newStorage []storageOp
|
2020-06-24 13:11:34 +00:00
|
|
|
for j := range d[i].Storage {
|
2021-02-12 16:24:11 +00:00
|
|
|
keyBytes, err := base64.StdEncoding.DecodeString(d[i].Storage[j].Key)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("invalid key encoding: %w", err))
|
|
|
|
}
|
|
|
|
if bytes.HasPrefix(keyBytes, ledgerIDBytes) {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-24 13:11:34 +00:00
|
|
|
if d[i].Storage[j].State == "Changed" {
|
|
|
|
d[i].Storage[j].State = "Added"
|
|
|
|
}
|
2021-02-12 16:24:11 +00:00
|
|
|
newStorage = append(newStorage, d[i].Storage[j])
|
2020-06-24 13:11:34 +00:00
|
|
|
}
|
2021-02-12 16:24:11 +00:00
|
|
|
sort.Slice(newStorage, func(k, l int) bool {
|
|
|
|
return newStorage[k].Key < newStorage[l].Key
|
2020-06-24 13:11:34 +00:00
|
|
|
})
|
2021-02-12 16:24:11 +00:00
|
|
|
d[i].Storage = newStorage
|
2020-06-24 13:11:34 +00:00
|
|
|
}
|
|
|
|
// assume that d is already sorted by Block
|
|
|
|
}
|
|
|
|
|
|
|
|
func compare(a, b string) error {
|
|
|
|
dumpA, err := readFile(a)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("reading file %s: %w", a, err)
|
2020-06-24 13:11:34 +00:00
|
|
|
}
|
|
|
|
dumpB, err := readFile(b)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("reading file %s: %w", b, err)
|
2020-06-24 13:11:34 +00:00
|
|
|
}
|
|
|
|
dumpA.normalize()
|
|
|
|
dumpB.normalize()
|
|
|
|
if len(dumpA) != len(dumpB) {
|
|
|
|
return fmt.Errorf("dump files differ in size: %d vs %d", len(dumpA), len(dumpB))
|
|
|
|
}
|
|
|
|
for i := range dumpA {
|
|
|
|
blockA := &dumpA[i]
|
|
|
|
blockB := &dumpB[i]
|
|
|
|
if blockA.Block != blockB.Block {
|
|
|
|
return fmt.Errorf("block number mismatch: %d vs %d", blockA.Block, blockB.Block)
|
|
|
|
}
|
|
|
|
if len(blockA.Storage) != len(blockB.Storage) {
|
|
|
|
return fmt.Errorf("block %d, changes length mismatch: %d vs %d", blockA.Block, len(blockA.Storage), len(blockB.Storage))
|
|
|
|
}
|
|
|
|
fail := false
|
|
|
|
for j := range blockA.Storage {
|
|
|
|
if blockA.Storage[j].Key != blockB.Storage[j].Key {
|
|
|
|
return fmt.Errorf("block %d: key mismatch: %s vs %s", blockA.Block, blockA.Storage[j].Key, blockB.Storage[j].Key)
|
|
|
|
}
|
|
|
|
if blockA.Storage[j].State != blockB.Storage[j].State {
|
|
|
|
return fmt.Errorf("block %d: state mismatch for key %s: %s vs %s", blockA.Block, blockA.Storage[j].Key, blockA.Storage[j].State, blockB.Storage[j].State)
|
|
|
|
}
|
|
|
|
if blockA.Storage[j].Value != blockB.Storage[j].Value {
|
|
|
|
fail = true
|
|
|
|
fmt.Printf("block %d: value mismatch for key %s: %s vs %s\n", blockA.Block, blockA.Storage[j].Key, blockA.Storage[j].Value, blockB.Storage[j].Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fail {
|
|
|
|
return errors.New("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cliMain(c *cli.Context) error {
|
|
|
|
a := c.Args().Get(0)
|
|
|
|
b := c.Args().Get(1)
|
|
|
|
if a == "" {
|
|
|
|
return errors.New("no arguments given")
|
|
|
|
}
|
|
|
|
if b == "" {
|
|
|
|
return errors.New("missing second argument")
|
|
|
|
}
|
|
|
|
fa, err := os.Open(a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fa.Close()
|
|
|
|
fb, err := os.Open(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fb.Close()
|
|
|
|
|
|
|
|
astat, err := fa.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bstat, err := fb.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if astat.Mode().IsRegular() && bstat.Mode().IsRegular() {
|
|
|
|
return compare(a, b)
|
|
|
|
}
|
|
|
|
if astat.Mode().IsDir() && bstat.Mode().IsDir() {
|
|
|
|
for i := 0; i <= 6000000; i += 100000 {
|
|
|
|
dir := fmt.Sprintf("BlockStorage_%d", i)
|
|
|
|
fmt.Println("Processing directory", dir)
|
|
|
|
for j := i - 99000; j <= i; j += 1000 {
|
|
|
|
if j < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fname := fmt.Sprintf("%s/dump-block-%d.json", dir, j)
|
|
|
|
|
|
|
|
aname := filepath.Join(a, fname)
|
|
|
|
bname := filepath.Join(b, fname)
|
|
|
|
err := compare(aname, bname)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("file %s: %w", fname, err)
|
2020-06-24 13:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("both parameters must be either dump files or directories")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctl := cli.NewApp()
|
|
|
|
ctl.Name = "compare-dumps"
|
|
|
|
ctl.Version = "1.0"
|
|
|
|
ctl.Usage = "compare-dumps dumpDirA dumpDirB"
|
|
|
|
ctl.Action = cliMain
|
|
|
|
|
|
|
|
if err := ctl.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
fmt.Fprintln(os.Stderr, ctl.Usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|