Skip to content

Commit

Permalink
Merge branch 'development' into ramiroJCB/fix/epochDataNotInDisk
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroJCB authored Sep 4, 2024
2 parents 97ddc43 + e7dc9f7 commit ab635e0
Show file tree
Hide file tree
Showing 5 changed files with 318 additions and 17 deletions.
46 changes: 46 additions & 0 deletions dot/network/messages/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

pb "github.com/ChainSafe/gossamer/dot/network/proto"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/trie"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -52,3 +53,48 @@ func (s *StateRequest) Decode(in []byte) error {
s.NoProof = message.NoProof
return nil
}

type StateResponse struct {
Entries []KeyValueStateEntry
Proof []byte
}

type KeyValueStateEntry struct {
StateRoot common.Hash
StateEntries trie.Entries
Complete bool
}

func (s *StateResponse) Decode(in []byte) error {
decodedResponse := &pb.StateResponse{}
err := proto.Unmarshal(in, decodedResponse)
if err != nil {
return err
}

s.Proof = make([]byte, len(decodedResponse.Proof))
copy(s.Proof, decodedResponse.Proof)

s.Entries = make([]KeyValueStateEntry, len(decodedResponse.Entries))
for idx, entry := range decodedResponse.Entries {
s.Entries[idx] = KeyValueStateEntry{
Complete: entry.Complete,
StateRoot: common.BytesToHash(entry.StateRoot),
}

trieFragment := make(trie.Entries, len(entry.Entries))
for stateEntryIdx, stateEntry := range entry.Entries {
trieFragment[stateEntryIdx] = trie.Entry{
Key: make([]byte, len(stateEntry.Key)),
Value: make([]byte, len(stateEntry.Value)),
}

copy(trieFragment[stateEntryIdx].Key, stateEntry.Key)
copy(trieFragment[stateEntryIdx].Value, stateEntry.Value)
}

s.Entries[idx].StateEntries = trieFragment
}

return nil
}
25 changes: 21 additions & 4 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
emptyByteVectorEncoded []byte = scale.MustMarshal([]byte{})
noneEncoded []byte = []byte{0x00}
allZeroesBytes = [32]byte{}

childStorageKeyPrefix = []byte(":child_storage:")
)

const (
Expand Down Expand Up @@ -2065,10 +2067,15 @@ func ext_storage_clear_prefix_version_1(ctx context.Context, m api.Module, prefi
prefix := read(m, prefixSpan)
logger.Debugf("prefix: 0x%x", prefix)

err := storage.ClearPrefix(prefix)
if err != nil {
panic(err)
if !bytes.HasPrefix(prefix, childStorageKeyPrefix) {
err := storage.ClearPrefix(prefix)
if err != nil {
panic(err)
}
} else {
logger.Warnf("cannot clear prefix that is part of or contains a child storage key")
}

}

// toKillStorageResultEnum encodes the `allRemoved` flag and
Expand Down Expand Up @@ -2117,6 +2124,17 @@ func ext_storage_clear_prefix_version_2(ctx context.Context, m api.Module, prefi
limitPtr = &maxLimit
}

if bytes.HasPrefix(prefix, childStorageKeyPrefix) {
logger.Warnf("cannot clear child prefix: 0x%x", prefix)
encBytes, err := toKillStorageResultEnum(false, 0)
if err != nil {
panic(err)
}

valueSpan := mustWrite(m, rtCtx.Allocator, encBytes)
return valueSpan
}

numRemoved, all, err := storage.ClearPrefixLimit(prefix, *limitPtr)
if err != nil {
logger.Errorf("failed to clear prefix limit: %s", err)
Expand All @@ -2134,7 +2152,6 @@ func ext_storage_clear_prefix_version_2(ctx context.Context, m api.Module, prefi
logger.Errorf("failed to allocate: %s", err)
panic(err)
}

return valueSpan
}

Expand Down
25 changes: 15 additions & 10 deletions scripts/p2p/common_p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package p2p

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -117,20 +118,22 @@ func parsePeerAddress(arg string) peer.AddrInfo {
return *p
}

func ReadStream(stream lip2pnetwork.Stream) []byte {
var errZeroLength = errors.New("zero length")

func ReadStream(stream lip2pnetwork.Stream) ([]byte, error) {
responseBuf := make([]byte, network.MaxBlockResponseSize)

length, _, err := network.ReadLEB128ToUint64(stream)
if err != nil {
log.Fatalf("reading response length: %s", err.Error())
return nil, fmt.Errorf("reading leb128: %w", err)
}

if length == 0 {
return nil
return nil, errZeroLength
}

if length > network.MaxBlockResponseSize {
log.Fatalf("%s: max %d, got %d", network.ErrGreaterThanMaxSize, network.MaxBlockResponseSize, length)
return nil, fmt.Errorf("%w: max %d, got %d", network.ErrGreaterThanMaxSize, network.MaxBlockResponseSize, length)
}

if length > uint64(len(responseBuf)) {
Expand All @@ -142,22 +145,22 @@ func ReadStream(stream lip2pnetwork.Stream) []byte {
for tot < int(length) {
n, err := stream.Read(responseBuf[tot:])
if err != nil {
log.Fatalf("reading stream: %s", err.Error())
return nil, fmt.Errorf("reading stream: %w", err)
}
tot += n
}

if tot != int(length) {
log.Fatalf("%s: expected %d bytes, received %d bytes", network.ErrFailedToReadEntireMessage, length, tot)
return nil, fmt.Errorf("%w: expected %d bytes, received %d bytes", network.ErrFailedToReadEntireMessage, length, tot)
}

return responseBuf[:tot]
return responseBuf[:tot], nil
}

func WriteStream(msg *messages.BlockRequestMessage, stream lip2pnetwork.Stream) {
func WriteStream(msg messages.P2PMessage, stream lip2pnetwork.Stream) error {
encMsg, err := msg.Encode()
if err != nil {
log.Fatalf("encoding message: %s", err.Error())
return fmt.Errorf("encoding message: %w", err)
}

msgLen := uint64(len(encMsg))
Expand All @@ -166,6 +169,8 @@ func WriteStream(msg *messages.BlockRequestMessage, stream lip2pnetwork.Stream)

_, err = stream.Write(encMsg)
if err != nil {
log.Fatalf("writing message: %s", err.Error())
return fmt.Errorf("writing message: %w", err)
}

return nil
}
16 changes: 13 additions & 3 deletions scripts/retrieve_block/retrieve_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ func parseTargetBlock(arg string) variadic.Uint32OrHash {
}

func waitAndStoreResponse(stream lip2pnetwork.Stream, outputFile string) bool {
output := p2p.ReadStream(stream)
output, err := p2p.ReadStream(stream)
if len(output) == 0 {
return false
}

if err != nil {
log.Println(err.Error())
return false
}

blockResponse := &messages.BlockResponseMessage{}
err := blockResponse.Decode(output)
err = blockResponse.Decode(output)
if err != nil {
log.Fatalf("could not decode block response message: %s", err.Error())
}
Expand Down Expand Up @@ -125,7 +130,12 @@ func main() {
}

defer stream.Close() //nolint:errcheck
p2p.WriteStream(requestMessage, stream)
err = p2p.WriteStream(requestMessage, stream)
if err != nil {
log.Println(err.Error())
continue
}

if !waitAndStoreResponse(stream, os.Args[3]) {
continue
}
Expand Down
Loading

0 comments on commit ab635e0

Please sign in to comment.