H13: Say which part of the world differs when a soak hash does
When a client's world hash differs from the server's, the soak report now names the part that differs (tide, props, pieces or loot), the client's ClientWorld tick, and each piece whose entry differs on the two sides, so the next transient mismatch can be traced. - NetWorld.world_detail(): part hashes and a piece list. world_hash() is unchanged: the same bytes, hashed in four parts. - The server's world_hash log lines and the soak clients' hash lines carry the detail. A full soak with this (24 clients, 2 matches) compared 3,743 hashes with 0 differing, so the mismatch didn't come back to trace yet. Refs #80 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+63
-25
@@ -257,36 +257,74 @@ static func apply_own(player: SimEntity, own: Dictionary) -> void:
|
||||
## up to some tick, and brought its pieces' building up to that tick, agrees with the
|
||||
## server's simulation at that tick.
|
||||
static func world_hash(sim: Simulation) -> String:
|
||||
var buffer: StreamPeerBuffer = StreamPeerBuffer.new()
|
||||
buffer.big_endian = false
|
||||
buffer.put_64(sim.tide.started_tick if sim.tide != null else -1)
|
||||
if sim.tide != null:
|
||||
buffer.put_data(sim.tide.levels.to_byte_array())
|
||||
buffer.put_64(sim.props.count())
|
||||
buffer.put_data(sim.props.health.to_byte_array())
|
||||
var piece_ids: Array = sim.structures.pieces.keys()
|
||||
piece_ids.sort()
|
||||
buffer.put_64(piece_ids.size())
|
||||
for piece_id: int in piece_ids:
|
||||
var piece: StructureSet.Piece = sim.structures.pieces[piece_id]
|
||||
buffer.put_64(piece.id)
|
||||
buffer.put_u8(piece.kind)
|
||||
buffer.put_u8(piece.material)
|
||||
buffer.put_u8(piece.facing)
|
||||
for value: int in [piece.slot.x, piece.slot.y, piece.slot.z, piece.slot.w]:
|
||||
buffer.put_32(value)
|
||||
buffer.put_64(piece.owner)
|
||||
buffer.put_32(piece.health)
|
||||
buffer.put_32(piece.build_ticks_left)
|
||||
buffer.put_u8(1 if piece.grounded else 0)
|
||||
buffer.put_64(piece.placed_tick)
|
||||
sim.loot.write_state(buffer)
|
||||
var hasher: HashingContext = HashingContext.new()
|
||||
hasher.start(HashingContext.HASH_SHA256)
|
||||
hasher.update(buffer.data_array)
|
||||
for part: PackedByteArray in _world_parts(sim):
|
||||
hasher.update(part)
|
||||
return hasher.finish().hex_encode()
|
||||
|
||||
|
||||
## The same state as world_hash(), hashed part by part ("tide", "props", "pieces",
|
||||
## "loot"), with "piece_list": every piece as [id, health, build_ticks_left, grounded,
|
||||
## placed_tick, kind, owner], in ID order. For telling where a client and the server
|
||||
## disagree.
|
||||
static func world_detail(sim: Simulation) -> Dictionary:
|
||||
var parts: Array[PackedByteArray] = _world_parts(sim)
|
||||
var detail: Dictionary = {}
|
||||
var names: Array[String] = ["tide", "props", "pieces", "loot"]
|
||||
for index: int in names.size():
|
||||
var hasher: HashingContext = HashingContext.new()
|
||||
hasher.start(HashingContext.HASH_SHA256)
|
||||
hasher.update(parts[index])
|
||||
detail[names[index]] = hasher.finish().hex_encode().substr(0, 12)
|
||||
var piece_list: Array = []
|
||||
var piece_ids: Array = sim.structures.pieces.keys()
|
||||
piece_ids.sort()
|
||||
for piece_id: int in piece_ids:
|
||||
var piece: StructureSet.Piece = sim.structures.pieces[piece_id]
|
||||
piece_list.append([piece.id, piece.health, piece.build_ticks_left, 1 if piece.grounded else 0, piece.placed_tick, piece.kind, piece.owner])
|
||||
detail["piece_list"] = piece_list
|
||||
return detail
|
||||
|
||||
|
||||
## The bytes world_hash() hashes, in four parts: tide, props, pieces and loot. Their
|
||||
## concatenation is the same buffer the hash has always covered.
|
||||
static func _world_parts(sim: Simulation) -> Array[PackedByteArray]:
|
||||
var tide: StreamPeerBuffer = _buffer()
|
||||
tide.put_64(sim.tide.started_tick if sim.tide != null else -1)
|
||||
if sim.tide != null:
|
||||
tide.put_data(sim.tide.levels.to_byte_array())
|
||||
var props: StreamPeerBuffer = _buffer()
|
||||
props.put_64(sim.props.count())
|
||||
props.put_data(sim.props.health.to_byte_array())
|
||||
var pieces: StreamPeerBuffer = _buffer()
|
||||
var piece_ids: Array = sim.structures.pieces.keys()
|
||||
piece_ids.sort()
|
||||
pieces.put_64(piece_ids.size())
|
||||
for piece_id: int in piece_ids:
|
||||
var piece: StructureSet.Piece = sim.structures.pieces[piece_id]
|
||||
pieces.put_64(piece.id)
|
||||
pieces.put_u8(piece.kind)
|
||||
pieces.put_u8(piece.material)
|
||||
pieces.put_u8(piece.facing)
|
||||
for value: int in [piece.slot.x, piece.slot.y, piece.slot.z, piece.slot.w]:
|
||||
pieces.put_32(value)
|
||||
pieces.put_64(piece.owner)
|
||||
pieces.put_32(piece.health)
|
||||
pieces.put_32(piece.build_ticks_left)
|
||||
pieces.put_u8(1 if piece.grounded else 0)
|
||||
pieces.put_64(piece.placed_tick)
|
||||
var loot: StreamPeerBuffer = _buffer()
|
||||
sim.loot.write_state(loot)
|
||||
return [tide.data_array, props.data_array, pieces.data_array, loot.data_array]
|
||||
|
||||
|
||||
static func _buffer() -> StreamPeerBuffer:
|
||||
var buffer: StreamPeerBuffer = StreamPeerBuffer.new()
|
||||
buffer.big_endian = false
|
||||
return buffer
|
||||
|
||||
|
||||
static func _put_varint(out: StreamPeerBuffer, value: int) -> void:
|
||||
value &= 0xFFFFFFFF
|
||||
while value >= 0x80:
|
||||
|
||||
Reference in New Issue
Block a user