Skip to content

Commit

Permalink
feat(scripts): retrieve blocks script (#3945)
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior authored May 13, 2024
1 parent d0ce33d commit 6dcc6bb
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dot/network/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *testStreamHandler) writeToStream(stream libp2pnetwork.Stream, msg Messa
}

msgLen := uint64(len(encMsg))
lenBytes := uint64ToLEB128(msgLen)
lenBytes := Uint64ToLEB128(msgLen)
encMsg = append(lenBytes, encMsg...)

_, err = stream.Write(encMsg)
Expand Down
2 changes: 1 addition & 1 deletion dot/network/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (h *host) writeToStream(s network.Stream, msg Message) error {
}

msgLen := uint64(len(encMsg))
lenBytes := uint64ToLEB128(msgLen)
lenBytes := Uint64ToLEB128(msgLen)
encMsg = append(lenBytes, encMsg...)

sent, err := s.Write(encMsg)
Expand Down
6 changes: 3 additions & 3 deletions dot/network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func saveKey(priv crypto.PrivKey, fp string) (err error) {
return f.Close()
}

func uint64ToLEB128(in uint64) []byte {
func Uint64ToLEB128(in uint64) []byte {
var out []byte
for {
b := uint8(in & 0x7f)
Expand All @@ -149,7 +149,7 @@ func uint64ToLEB128(in uint64) []byte {
return out
}

func readLEB128ToUint64(r io.Reader) (uint64, int, error) {
func ReadLEB128ToUint64(r io.Reader) (uint64, int, error) {
var out uint64
var shift uint

Expand Down Expand Up @@ -188,7 +188,7 @@ func readStream(stream libp2pnetwork.Stream, bufPointer *[]byte, maxSize uint64)
return 0, ErrNilStream
}

length, bytesRead, err := readLEB128ToUint64(stream)
length, bytesRead, err := ReadLEB128ToUint64(stream)
if err != nil {
return bytesRead, fmt.Errorf("failed to read length: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions dot/network/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestReadLEB128ToUint64(t *testing.T) {
_, err := buf.Write(tc.input)
require.NoError(t, err)

ret, _, err := readLEB128ToUint64(buf)
ret, _, err := ReadLEB128ToUint64(buf)
require.NoError(t, err)
require.Equal(t, tc.output, ret)
}
Expand All @@ -150,7 +150,7 @@ func TestInvalidLeb128(t *testing.T) {
_, err := buf.Write(input)
require.NoError(t, err)

_, _, err = readLEB128ToUint64(buf)
_, _, err = ReadLEB128ToUint64(buf)
require.Error(t, err)
}

Expand Down
50 changes: 50 additions & 0 deletions scripts/retrieve_block/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## retrieve_block

The script is used to retrieve blocks from the network

### Usage

#### To retrieve a single block

```sh
go run retrieve_block.go [number or hash] [network chain spec] [output file]

# requesting a single block
go run retrieve_block.go 0x9b0211aadcef4bb65e69346cfd256ddd2abcb674271326b08f0975dac7c17bc7 ./westend.json file.out
```

#### To retrieve a chain of blocks (ascending or descending)

> _note that the arguments to request a chain of blocks are separated by comma_
> _the max numbers of blocks you can retrieve is 128. Given network limitations in the implementation the peer might not respond the full chain you've requested_
```sh
go run retrieve_block.go [number or hash],[direction],[number of blocks] [network chain spec] [output file]

# requesting from a chain of blocks from 10 to 12
go run retrieve_block.go 10,asc,3 ./westend.json file.out

# requesting from a chain of blocks from 50 to 30
go run retrieve_block.go 50,desc,21 ./westend.json file.out
```

The block response is written to the output file in the raw form, so you can use your own language to parse the block response to your own structure, here is an example in Go:

```go
rawBlockResponse, err := os.ReadFile(output_file)
if err != nil {
panic(err)
}

blockResponse := &network.BlockResponseMessage{}
err := blockResponse.Decode(rawBlockResponse)
if err != nil {
panic(err)
}

// iterate over the blocks
for _, b := range blockResponse.BlockData {

}
```
Loading

0 comments on commit 6dcc6bb

Please sign in to comment.