-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services: add new service for fetching blocks from NeoFS
Close #3496 Signed-off-by: Ekaterina Pavlova <[email protected]>
- Loading branch information
1 parent
dc6c195
commit 2c6f8ca
Showing
12 changed files
with
691 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/nspcc-dev/neo-go/cli/cmdargs" | ||
"github.com/nspcc-dev/neo-go/cli/options" | ||
"github.com/nspcc-dev/neo-go/pkg/core/block" | ||
"github.com/nspcc-dev/neo-go/pkg/io" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func fetchBin(ctx *cli.Context) error { | ||
var err error | ||
if err := cmdargs.EnsureNone(ctx); err != nil { | ||
return err | ||
} | ||
cfg, err := options.GetConfigFromContext(ctx) | ||
if err != nil { | ||
return cli.Exit(err, 1) | ||
} | ||
log, _, logCloser, err := options.HandleLoggingParams(ctx.Bool("debug"), cfg.ApplicationConfiguration) | ||
if err != nil { | ||
return cli.Exit(err, 1) | ||
} | ||
if logCloser != nil { | ||
defer func() { _ = logCloser() }() | ||
} | ||
count := uint32(ctx.Uint("count")) | ||
start := uint32(ctx.Uint("start")) | ||
|
||
chain, prometheus, pprof, err := initBCWithMetrics(cfg, log) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
pprof.ShutDown() | ||
prometheus.ShutDown() | ||
chain.Close() | ||
}() | ||
|
||
chainCount := chain.BlockHeight() + 1 | ||
if start+count > chainCount { | ||
return cli.Exit(fmt.Errorf("chain is not that high (%d) to dump %d blocks starting from %d", chainCount-1, count, start), 1) | ||
} | ||
if count == 0 { | ||
count = chainCount - start | ||
} | ||
|
||
testDir := ctx.String("out") | ||
if _, err = os.Stat(testDir); os.IsNotExist(err) { | ||
if err = os.MkdirAll(testDir, 0755); err != nil { | ||
return cli.Exit(fmt.Sprintf("failed to create directory %s: %v", testDir, err), 1) | ||
} | ||
} | ||
|
||
for i := start; i < start+count; i++ { | ||
bh := chain.GetHeaderHash(i) | ||
blk, err2 := chain.GetBlock(bh) | ||
if err2 != nil { | ||
return cli.Exit(fmt.Sprintf("failed to get block %d: %v", i, err), 1) | ||
} | ||
filePath := filepath.Join(testDir, fmt.Sprintf("block-%d.bin", i)) | ||
if err = saveBlockToFile(blk, filePath); err != nil { | ||
return cli.Exit(fmt.Sprintf("failed to save block %d to file: %v", i, err), 1) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func saveBlockToFile(blk *block.Block, filePath string) error { | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
writer := io.NewBinWriterFromIO(file) | ||
|
||
var buf = io.NewBufBinWriter() | ||
blk.EncodeBinary(buf.BinWriter) | ||
bytes := buf.Bytes() | ||
|
||
writer.WriteU32LE(uint32(len(bytes))) | ||
blk.EncodeBinary(writer) | ||
if writer.Err != nil { | ||
return writer.Err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" | ||
) | ||
|
||
// NeoFSBlockFetcher represents the configuration for the blockfetcher service. | ||
type ( | ||
NeoFSBlockFetcher struct { | ||
Timeout time.Duration `yaml:"Timeout"` | ||
ContainerID string `yaml:"ContainerID"` | ||
Mode string `yaml:"Mode"` | ||
BasicService `yaml:",inline"` | ||
} | ||
) | ||
|
||
func (f NeoFSBlockFetcher) Validate() error { | ||
if f.Timeout == 0 { | ||
return errors.New("timeout is not set") | ||
} | ||
if f.ContainerID == "" { | ||
return errors.New("container ID is not set") | ||
} | ||
var containerID cid.ID | ||
err := containerID.DecodeString(f.ContainerID) | ||
if err != nil { | ||
return errors.New("invalid container ID") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.