Skip to content

Commit

Permalink
services: add new fetch
Browse files Browse the repository at this point in the history
Signed-off-by: Ekaterina Pavlova <[email protected]>
  • Loading branch information
AliceInHunterland committed Jul 16, 2024
1 parent c207b9b commit d4e0673
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 0 deletions.
75 changes: 75 additions & 0 deletions cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"

Expand Down Expand Up @@ -213,6 +214,80 @@ func dumpDB(ctx *cli.Context) error {
return nil
}

func dumpBin(ctx *cli.Context) error {

Check failure on line 217 in cli/server/server.go

View workflow job for this annotation

GitHub Actions / Lint

func `dumpBin` is unused (unused)
if err := cmdargs.EnsureNone(ctx); err != nil {
return err

Check warning on line 219 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L217-L219

Added lines #L217 - L219 were not covered by tests
}
cfg, err := options.GetConfigFromContext(ctx)
if err != nil {
return cli.NewExitError(err, 1)

Check failure on line 223 in cli/server/server.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: cli.NewExitError is deprecated: This function is a duplicate of Exit and will eventually be removed. (staticcheck)

Check warning on line 223 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L221-L223

Added lines #L221 - L223 were not covered by tests
}
log, _, logCloser, err := options.HandleLoggingParams(ctx.Bool("debug"), cfg.ApplicationConfiguration)
if err != nil {
return cli.NewExitError(err, 1)

Check failure on line 227 in cli/server/server.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: cli.NewExitError is deprecated: This function is a duplicate of Exit and will eventually be removed. (staticcheck)

Check warning on line 227 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L225-L227

Added lines #L225 - L227 were not covered by tests
}
if logCloser != nil {
defer func() { _ = logCloser() }()

Check warning on line 230 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L229-L230

Added lines #L229 - L230 were not covered by tests
}
count := uint32(ctx.Uint("count"))
start := uint32(ctx.Uint("start"))

Check warning on line 233 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L232-L233

Added lines #L232 - L233 were not covered by tests

chain, prometheus, pprof, err := initBCWithMetrics(cfg, log)
if err != nil {
return err

Check warning on line 237 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L235-L237

Added lines #L235 - L237 were not covered by tests
}
defer func() {
pprof.ShutDown()
prometheus.ShutDown()
chain.Close()
}()

Check warning on line 243 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L239-L243

Added lines #L239 - L243 were not covered by tests

chainCount := chain.BlockHeight() + 1
if start+count > chainCount {
return cli.NewExitError(fmt.Errorf("chain is not that high (%d) to dump %d blocks starting from %d", chainCount-1, count, start), 1)

Check failure on line 247 in cli/server/server.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: cli.NewExitError is deprecated: This function is a duplicate of Exit and will eventually be removed. (staticcheck)

Check warning on line 247 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L245-L247

Added lines #L245 - L247 were not covered by tests
}
if count == 0 {
count = chainCount - start

Check warning on line 250 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L249-L250

Added lines #L249 - L250 were not covered by tests
}

testDir := "./test/"
if _, err := os.Stat(testDir); os.IsNotExist(err) {
if err := os.MkdirAll(testDir, 0755); err != nil {
return cli.NewExitError(fmt.Sprintf("failed to create directory %s: %v", testDir, err), 1)

Check warning on line 256 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L253-L256

Added lines #L253 - L256 were not covered by tests
}
}

for i := start; i < start+count; i++ {
bh := chain.GetHeaderHash(i)
blk, err := chain.GetBlock(bh)
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to get block %d: %v", i, err), 1)

Check warning on line 264 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L260-L264

Added lines #L260 - L264 were not covered by tests
}
filePath := filepath.Join(testDir, fmt.Sprintf("block-%d.bin", i))
if err := saveBlockToFile(blk, filePath); err != nil {
return cli.NewExitError(fmt.Sprintf("failed to save block %d to file: %v", i, err), 1)

Check warning on line 268 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L266-L268

Added lines #L266 - L268 were not covered by tests
}
}

return nil

Check warning on line 272 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L272

Added line #L272 was not covered by tests
}

func saveBlockToFile(blk *block.Block, filePath string) error {

Check failure on line 275 in cli/server/server.go

View workflow job for this annotation

GitHub Actions / Lint

func `saveBlockToFile` is unused (unused)
file, err := os.Create(filePath)
if err != nil {
return err

Check warning on line 278 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L275-L278

Added lines #L275 - L278 were not covered by tests
}
defer file.Close()

Check warning on line 280 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L280

Added line #L280 was not covered by tests

writer := io.NewBinWriterFromIO(file)
blk.EncodeBinary(writer)
if writer.Err != nil {
return writer.Err

Check warning on line 285 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L282-L285

Added lines #L282 - L285 were not covered by tests
}

return nil

Check warning on line 288 in cli/server/server.go

View check run for this annotation

Codecov / codecov/patch

cli/server/server.go#L288

Added line #L288 was not covered by tests
}

func restoreDB(ctx *cli.Context) error {
if err := cmdargs.EnsureNone(ctx); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions pkg/network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ func (s *Server) Start() {
go s.relayBlocksLoop()
go s.bQueue.Run()
go s.bSyncQueue.Run()
//if s.chain.neofs.IsEnabled() {
// go s.neofs.Run()
// wait until end
//}
// tr.Accept() has PutBlock inside so here should be neofs block querying and then check tpcpeers transport.
for _, tr := range s.transports {
go tr.Accept()
}
Expand Down Expand Up @@ -785,6 +790,7 @@ func (s *Server) handlePing(p Peer, ping *payload.Ping) error {
return p.EnqueueP2PMessage(NewMessage(CMDPong, payload.NewPing(s.chain.BlockHeight(), s.id)))
}

// here requestBlocksOrHeaders is called

Check failure on line 793 in pkg/network/server.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
func (s *Server) requestBlocksOrHeaders(p Peer) error {
if s.stateSync.NeedHeaders() {
if s.chain.HeaderHeight() < p.LastBlockIndex() {
Expand Down Expand Up @@ -813,6 +819,7 @@ func (s *Server) requestBlocksOrHeaders(p Peer) error {
return nil
}

// here
// requestHeaders sends a CMDGetHeaders message to the peer to sync up in headers.
func (s *Server) requestHeaders(p Peer) error {
pl := getRequestBlocksPayload(p, s.chain.HeaderHeight(), &s.lastRequestedHeader)
Expand Down
Loading

0 comments on commit d4e0673

Please sign in to comment.