Skip to content

Commit

Permalink
block request response is not length prefixed array (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored Oct 25, 2024
1 parent 1d51337 commit 96aa905
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Codec/Sources/Codec/JamDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class JamDecoder {
)
}
}

public var isAtEnd: Bool {
input.isEmpty
}
}

protocol ArrayWrapper: Collection where Element: Decodable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ extension CERequest: RequestProtocol {
fatalError("unimplemented")
}
}

static func decodeResponseForBlockRequest(data: Data, config: ProtocolConfigRef) throws -> [BlockRef] {
let decoder = JamDecoder(data: data, config: config)
var resp = [BlockRef]()
while !decoder.isAtEnd {
try resp.append(decoder.decode(BlockRef.self))
}
return resp
}
}
9 changes: 5 additions & 4 deletions Node/Sources/Node/NetworkingProtocol/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TracingUtils
import Utils

public protocol NetworkProtocolHandler: Sendable {
func handle(ceRequest: CERequest) async throws -> (any Encodable)?
func handle(ceRequest: CERequest) async throws -> [any Encodable]
func handle(connection: some ConnectionInfoProtocol, upMessage: UPMessage) async throws

func handle(
Expand Down Expand Up @@ -142,10 +142,11 @@ struct EphemeralStreamHandlerImpl: EphemeralStreamHandler {

func handle(connection: any ConnectionInfoProtocol, request: Request) async throws -> Data {
impl.logger.trace("handling request: \(request) from \(connection.id)")
let encoder = JamEncoder()
let resp = try await impl.handler.handle(ceRequest: request)
if let resp {
return try JamEncoder.encode(resp)
for r in resp {
try encoder.encode(r)
}
return Data()
return encoder.data
}
}
6 changes: 3 additions & 3 deletions Node/Sources/Node/NetworkingProtocol/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct HandlerImpl: NetworkProtocolHandler {
let blockchain: Blockchain
let peerManager: PeerManager

func handle(ceRequest: CERequest) async throws -> (any Encodable)? {
func handle(ceRequest: CERequest) async throws -> [any Encodable] {
switch ceRequest {
case let .blockRequest(message):
let dataProvider = blockchain.dataProvider
Expand Down Expand Up @@ -178,7 +178,7 @@ struct HandlerImpl: NetworkProtocolHandler {
]
))
// TODO: rebroadcast to other peers after some time
return nil
return []
case let .safroleTicket2(message):
blockchain.publish(event: RuntimeEvents.SafroleTicketsReceived(
items: [
Expand All @@ -188,7 +188,7 @@ struct HandlerImpl: NetworkProtocolHandler {
),
]
))
return nil
return []
}
}

Expand Down
2 changes: 1 addition & 1 deletion Node/Sources/Node/NetworkingProtocol/SyncManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public actor SyncManager: Sendable {

Task {
let resp = try await network.send(to: addr, message: .blockRequest(request))
let decoded = try JamDecoder.decode([BlockRef].self, from: resp, withConfig: blockchain.config)
let decoded = try CERequest.decodeResponseForBlockRequest(data: resp, config: blockchain.config)
for block in decoded {
try await blockchain.importBlock(block)
}
Expand Down

0 comments on commit 96aa905

Please sign in to comment.