Skip to content

Commit

Permalink
Upload snapshots into memory
Browse files Browse the repository at this point in the history
  • Loading branch information
esuwu committed Dec 27, 2023
1 parent 42af476 commit 478498e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
51 changes: 41 additions & 10 deletions cmd/snapshotsreader/snapshots_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,58 @@ package main

import (
"encoding/binary"
"errors"
"flag"
"fmt"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/logging"
"github.com/wavesplatform/gowaves/pkg/settings"
"log"
"os"

"github.com/wavesplatform/gowaves/pkg/proto"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func main() {
snapshotsBody, err := os.Open("/home/alex/Documents/snapshots-1834298")
const (
defaultBlocksNumber = 1000
)
var (
logLevel = zap.LevelFlag("log-level", zapcore.InfoLevel,
"Logging level. Supported levels: DEBUG, INFO, WARN, ERROR, FATAL. Default logging level INFO.")
blockchainType = flag.String("blockchain-type", "mainnet",
"Blockchain type. Allowed values: mainnet/testnet/stagenet/custom. Default is 'mainnet'.")
snapshotsPath = flag.String("snapshots-path", "", "Path to binary blockchain file.")
nBlocks = flag.Int("blocks-number", defaultBlocksNumber, "Number of blocks to import.")
)
flag.Parse()

logger := logging.SetupSimpleLogger(*logLevel)
defer func() {
err := logger.Sync()
if err != nil && errors.Is(err, os.ErrInvalid) {
panic(fmt.Sprintf("failed to close logging subsystem: %v\n", err))
}
}()
if *snapshotsPath == "" {
zap.S().Fatalf("You must specify snapshots-path option.")
}

ss, err := settings.BlockchainSettingsByTypeName(*blockchainType)
if err != nil {
zap.S().Fatalf("failed to load blockchain settings: %v", err)
}

snapshotsBody, err := os.Open(*snapshotsPath)
if err != nil {
log.Fatalf("unable to read file: %v", err)
zap.S().Fatalf("failed to open snapshots file, %v", err)
}

var nBlocks uint64 = 1000000
snapshotsSizeBytes := make([]byte, 4)
readPos := int64(0)
var blocksSnapshots []proto.BlockSnapshot
for height := uint64(1); height <= nBlocks; height++ {
for height := uint64(1); height <= uint64(*nBlocks); height++ {
if _, err := snapshotsBody.ReadAt(snapshotsSizeBytes, readPos); err != nil {
log.Fatal(err)
}
Expand All @@ -34,16 +69,12 @@ func main() {
if _, err := snapshotsBody.ReadAt(snapshots, readPos); err != nil {
log.Fatal(err)
}
err := snapshotsInBlock.UnmarshalBinaryImport(snapshots, proto.StageNetScheme)
err := snapshotsInBlock.UnmarshalBinaryImport(snapshots, ss.AddressSchemeCharacter)
if err != nil {
return
}
blocksSnapshots = append(blocksSnapshots, snapshotsInBlock)
readPos += int64(snapshotsSize) + 4

if height == 63397 || height == 63398 {
fmt.Println("here")
}
}
}
}
33 changes: 33 additions & 0 deletions pkg/proto/block_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,36 @@ func (bs *BlockSnapshot) UnmarshalBinary(data []byte, scheme Scheme) error {
bs.TxSnapshots = txSnapshots
return nil
}

func (bs *BlockSnapshot) UnmarshalBinaryImport(data []byte, scheme Scheme) error {
if len(data) < uint32Size {
return errors.Errorf("BlockSnapshot UnmarshallBinary: invalid data size")
}
snapshotsBytesSize := binary.BigEndian.Uint32(data[0:uint32Size])
data = data[uint32Size:] // skip size
var txSnapshots [][]AtomicSnapshot
for i := uint32(0); snapshotsBytesSize > 0; i++ {
if len(data) < uint32Size {
return errors.Errorf("BlockSnapshot UnmarshallBinary: invalid data size")
}
oneSnapshotSize := binary.BigEndian.Uint32(data[0:uint32Size])
var tsProto g.TransactionStateSnapshot
data = data[uint32Size:] // skip size
if uint32(len(data)) < oneSnapshotSize {
return errors.Errorf("BlockSnapshot UnmarshallBinary: invalid snapshot size")
}
err := tsProto.UnmarshalVT(data[0:oneSnapshotSize])
if err != nil {
return err
}
atomicTS, err := TxSnapshotsFromProtobuf(scheme, &tsProto)
if err != nil {
return err
}
txSnapshots = append(txSnapshots, atomicTS)
data = data[oneSnapshotSize:]
snapshotsBytesSize = snapshotsBytesSize - oneSnapshotSize - uint32Size
}
bs.TxSnapshots = txSnapshots
return nil
}

0 comments on commit 478498e

Please sign in to comment.