Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix] fix getReceipts rpc issue #4511

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions p2p/stream/protocols/sync/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ func (ch *chainHelperImpl) getNodeData(hs []common.Hash) ([][]byte, error) {

// getReceipts assembles the response to a receipt query.
func (ch *chainHelperImpl) getReceipts(hs []common.Hash) ([]types.Receipts, error) {
var receipts []types.Receipts

receipts := make([]types.Receipts, len(hs))
for i, hash := range hs {
// Retrieve the requested block's receipts
results := ch.chain.GetReceiptsByHash(hash)
if results == nil {
if header := ch.chain.GetHeaderByHash(hash); header == nil || header.ReceiptHash() != types.EmptyRootHash {
continue
}
return nil, errors.New("invalid hashes to get receipts")
}
receipts[i] = append(receipts[i], results...)
receipts[i] = results
}
return receipts, nil
}
17 changes: 16 additions & 1 deletion p2p/stream/protocols/sync/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,28 @@ func (tch *testChainHelper) getNodeData(hs []common.Hash) ([][]byte, error) {

func (tch *testChainHelper) getReceipts(hs []common.Hash) ([]types.Receipts, error) {
testReceipts := makeTestReceipts(len(hs), 3)
receipts := make([]types.Receipts, len(hs)*3)
receipts := make([]types.Receipts, len(hs))
for i, _ := range hs {
receipts[i] = testReceipts
}
return receipts, nil
}

func checkGetReceiptsResult(b []byte, hs []common.Hash) error {
var msg = &syncpb.Message{}
if err := protobuf.Unmarshal(b, msg); err != nil {
return err
}
bhResp, err := msg.GetReceiptsResponse()
if err != nil {
return err
}
if len(hs) != len(bhResp.Receipts) {
return errors.New("unexpected size")
}
return nil
}

func numberToHash(bn uint64) common.Hash {
var h common.Hash
binary.LittleEndian.PutUint64(h[:], bn)
Expand Down
16 changes: 16 additions & 0 deletions p2p/stream/protocols/sync/message/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,19 @@ func (msg *Message) GetBlocksByHashesResponse() (*GetBlocksByHashesResponse, err
}
return gbResp, nil
}

// GetReceiptsResponse parse the message to GetReceiptsResponse
func (msg *Message) GetReceiptsResponse() (*GetReceiptsResponse, error) {
resp := msg.GetResp()
if resp == nil {
return nil, errors.New("not response message")
}
if errResp := resp.GetErrorResponse(); errResp != nil {
return nil, &ResponseError{errResp.Error}
}
grResp := resp.GetGetReceiptsResponse()
if grResp == nil {
return nil, errors.New("not GetGetReceiptsResponse")
}
return grResp, nil
}
31 changes: 31 additions & 0 deletions p2p/stream/protocols/sync/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ var (
}
testGetBlocksByHashesRequest = syncpb.MakeGetBlocksByHashesRequest(testGetBlockByHashes)
testGetBlocksByHashesRequestMsg = syncpb.MakeMessageFromRequest(testGetBlocksByHashesRequest)

testGetReceipts = []common.Hash{
numberToHash(1),
numberToHash(2),
numberToHash(3),
numberToHash(4),
numberToHash(5),
}
testGetReceiptsRequest = syncpb.MakeGetReceiptsRequest(testGetReceipts)
testGetReceiptsRequestMsg = syncpb.MakeMessageFromRequest(testGetReceiptsRequest)
)

func TestSyncStream_HandleGetBlocksByRequest(t *testing.T) {
Expand Down Expand Up @@ -126,6 +136,27 @@ func TestSyncStream_HandleGetBlocksByHashes(t *testing.T) {
}
}

func TestSyncStream_HandleGetReceipts(t *testing.T) {
st, remoteSt := makeTestSyncStream()

go st.run()
defer close(st.closeC)

req := testGetReceiptsRequestMsg
b, _ := protobuf.Marshal(req)
err := remoteSt.WriteBytes(b)
if err != nil {
t.Fatal(err)
}

time.Sleep(200 * time.Millisecond)
receivedBytes, _ := remoteSt.ReadBytes()

if err := checkGetReceiptsResult(receivedBytes, testGetBlockByHashes); err != nil {
t.Fatal(err)
}
}

func makeTestSyncStream() (*syncStream, *testRemoteBaseStream) {
localRaw, remoteRaw := makePairP2PStreams()
remote := newTestRemoteBaseStream(remoteRaw)
Expand Down