Skip to content

Commit

Permalink
DotNetRGS: remove mut vars in serializeDeltaSet
Browse files Browse the repository at this point in the history
  • Loading branch information
aarani committed May 31, 2023
1 parent 8dd67c2 commit f5c09c8
Showing 1 changed file with 216 additions and 123 deletions.
339 changes: 216 additions & 123 deletions DotNetRGS/GossipSnapshotter.fs
Expand Up @@ -653,138 +653,231 @@ type GossipSnapshotter
ChainHash = Constants.ChainHash
}

let fullUpdateHistograms = FullUpdateValueHistograms.Default

let recordFullUpdateInHistograms(fullUpdate: UnsignedChannelUpdateMsg) =
fullUpdateHistograms.CLTVExpiryDelta <-
fullUpdateHistograms.CLTVExpiryDelta
|> Map.change
fullUpdate.CLTVExpiryDelta
(fun previousValue ->
Some((Option.defaultValue 0u previousValue) + 1u)
)

fullUpdateHistograms.HTLCMinimumMSat <-
fullUpdateHistograms.HTLCMinimumMSat
|> Map.change
fullUpdate.HTLCMinimumMSat
(fun previousValue ->
Some((Option.defaultValue 0u previousValue) + 1u)
)
let rec readChannelDelta
(channelDeltas: List<ShortChannelId * ChannelDelta>)
(serializationSet: SerializationSet)
(fullUpdateHistograms: FullUpdateValueHistograms)
=
match channelDeltas with
| (_scId, channelDelta) :: tail ->
let channelAnnouncementDelta =
UnwrapOption
channelDelta.Announcement
"channelDelta.Announcement is none, did you forget to run filterDeltaSet?"

let currentAnnouncementSeen = channelAnnouncementDelta.Seen

let isNewAnnouncement =
currentAnnouncementSeen >= lastSyncTimestamp

let isNewlyUpdatedAnnouncement =
match channelDelta.FirstUpdateSeen with
| Some firstUpdateSeen ->
firstUpdateSeen >= lastSyncTimestamp
| None -> false

let sendAnnouncement =
isNewAnnouncement || isNewlyUpdatedAnnouncement

let serializationSet =
if sendAnnouncement then
{ serializationSet with
LatestSeen =
max
serializationSet.LatestSeen
currentAnnouncementSeen
Announcements =
channelAnnouncementDelta.Announcement
:: serializationSet.Announcements
}
else
serializationSet

let directionAUpdates, directionBUpdates = channelDelta.Updates

let categorizeDirectedUpdateSerialization
(directedUpdates: Option<DirectedUpdateDelta>)
(serializationSet: SerializationSet)
(fullUpdateHistograms: FullUpdateValueHistograms)
=
let recordFullUpdateInHistograms
(fullUpdate: UnsignedChannelUpdateMsg)
(fullUpdateHistograms: FullUpdateValueHistograms)
=
let fullUpdateHistograms =
{ fullUpdateHistograms with
CLTVExpiryDelta =
fullUpdateHistograms.CLTVExpiryDelta
|> Map.change
fullUpdate.CLTVExpiryDelta
(fun previousValue ->
Some(
(Option.defaultValue
0u
previousValue)
+ 1u
)
)
}

fullUpdateHistograms.FeeBaseMSat <-
fullUpdateHistograms.FeeBaseMSat
|> Map.change
fullUpdate.FeeBaseMSat
(fun previousValue ->
Some((Option.defaultValue 0u previousValue) + 1u)
)
let fullUpdateHistograms =
{ fullUpdateHistograms with
HTLCMinimumMSat =
fullUpdateHistograms.HTLCMinimumMSat
|> Map.change
fullUpdate.HTLCMinimumMSat
(fun previousValue ->
Some(
(Option.defaultValue
0u
previousValue)
+ 1u
)
)
}

fullUpdateHistograms.FeeProportionalMillionths <-
fullUpdateHistograms.FeeProportionalMillionths
|> Map.change
fullUpdate.FeeProportionalMillionths
(fun previousValue ->
Some((Option.defaultValue 0u previousValue) + 1u)
)
let fullUpdateHistograms =
{ fullUpdateHistograms with
FeeBaseMSat =
fullUpdateHistograms.FeeBaseMSat
|> Map.change
fullUpdate.FeeBaseMSat
(fun previousValue ->
Some(
(Option.defaultValue
0u
previousValue)
+ 1u
)
)
}

fullUpdateHistograms.HTLCMaximumMSat <-
fullUpdateHistograms.HTLCMaximumMSat
|> Map.change
fullUpdate.HTLCMaximumMSat.Value
(fun previousValue ->
Some((Option.defaultValue 0u previousValue) + 1u)
)
let fullUpdateHistograms =
{ fullUpdateHistograms with
FeeProportionalMillionths =
fullUpdateHistograms.FeeProportionalMillionths
|> Map.change
fullUpdate.FeeProportionalMillionths
(fun previousValue ->
Some(
(Option.defaultValue
0u
previousValue)
+ 1u
)
)
}

for (_scId, channelDelta) in deltaSet |> Map.toSeq do
let channelAnnouncementDelta =
UnwrapOption
channelDelta.Announcement
"channelDelta.Announcement is none, did you forget to run filterDeltaSet?"

let currentAnnouncementSeen = channelAnnouncementDelta.Seen
let isNewAnnouncement = currentAnnouncementSeen >= lastSyncTimestamp

let isNewlyUpdatedAnnouncement =
match channelDelta.FirstUpdateSeen with
| Some firstUpdateSeen -> firstUpdateSeen >= lastSyncTimestamp
| None -> false

let sendAnnouncement =
isNewAnnouncement || isNewlyUpdatedAnnouncement

let serializationSet =
if sendAnnouncement then
{ serializationSet with
LatestSeen =
max
serializationSet.LatestSeen
currentAnnouncementSeen
Announcements =
channelAnnouncementDelta.Announcement
:: serializationSet.Announcements
}
else
serializationSet
let fullUpdateHistograms =
{ fullUpdateHistograms with
HTLCMaximumMSat =
fullUpdateHistograms.HTLCMaximumMSat
|> Map.change
fullUpdate.HTLCMaximumMSat.Value
(fun previousValue ->
Some(
(Option.defaultValue
0u
previousValue)
+ 1u
)
)
}

let directionAUpdates, directionBUpdates = channelDelta.Updates
fullUpdateHistograms

let categorizeDirectedUpdateSerialization
(directedUpdates: Option<DirectedUpdateDelta>)
=
match directedUpdates with
| Some updates ->
match updates.LastUpdateAfterSeen with
| Some latestUpdateDelta ->
let latestUpdate = latestUpdateDelta.Update

serializationSet.LatestSeen <-
max
serializationSet.LatestSeen
latestUpdateDelta.Seen

if updates.LastUpdateBeforeSeen.IsSome then
let mutatedProperties = updates.MutatedProperties

if mutatedProperties.Length() = 5 then
// All five values have changed, it makes more sense to just
// serialize the update as a full update instead of as a change
// this way, the default values can be computed more efficiently
recordFullUpdateInHistograms latestUpdate

serializationSet.Updates <-
{
Update = latestUpdate
Mechanism =
UpdateSerializationMechanism.Full
}
:: serializationSet.Updates
elif mutatedProperties.Length() > 0
|| mutatedProperties.Flags then
// We don't count flags as mutated properties
serializationSet.Updates <-
{
Update = latestUpdate
Mechanism =
UpdateSerializationMechanism.Incremental
mutatedProperties
}
:: serializationSet.Updates
else
recordFullUpdateInHistograms latestUpdate
match directedUpdates with
| Some updates ->
match updates.LastUpdateAfterSeen with
| Some latestUpdateDelta ->
let latestUpdate = latestUpdateDelta.Update

serializationSet.Updates <-
{
Update = latestUpdate
Mechanism =
UpdateSerializationMechanism.Full
let serializationSet =
{ serializationSet with
LatestSeen =
max
serializationSet.LatestSeen
latestUpdateDelta.Seen
}
:: serializationSet.Updates
| None -> ()
| None -> ()

categorizeDirectedUpdateSerialization directionAUpdates
categorizeDirectedUpdateSerialization directionBUpdates
if updates.LastUpdateBeforeSeen.IsSome then
let mutatedProperties =
updates.MutatedProperties

if mutatedProperties.Length() = 5 then
// All five values have changed, it makes more sense to just
// serialize the update as a full update instead of as a change
// this way, the default values can be computed more efficiently
let fullUpdateHistograms =
recordFullUpdateInHistograms
latestUpdate
fullUpdateHistograms

{ serializationSet with
Updates =
{
Update = latestUpdate
Mechanism =
UpdateSerializationMechanism.Full
}
:: serializationSet.Updates
},
fullUpdateHistograms
elif mutatedProperties.Length() > 0
|| mutatedProperties.Flags then
// We don't count flags as mutated properties
{ serializationSet with
Updates =
{
Update = latestUpdate
Mechanism =
UpdateSerializationMechanism.Incremental
mutatedProperties
}
:: serializationSet.Updates
},
fullUpdateHistograms
else
serializationSet, fullUpdateHistograms
else
let fullUpdateHistograms =
recordFullUpdateInHistograms
latestUpdate
fullUpdateHistograms

{ serializationSet with
Updates =
{
Update = latestUpdate
Mechanism =
UpdateSerializationMechanism.Full
}
:: serializationSet.Updates
},
fullUpdateHistograms
| None -> serializationSet, fullUpdateHistograms
| None -> serializationSet, fullUpdateHistograms

let serializationSet, fullUpdateHistograms =
categorizeDirectedUpdateSerialization
directionAUpdates
serializationSet
fullUpdateHistograms

let serializationSet, fullUpdateHistograms =
categorizeDirectedUpdateSerialization
directionBUpdates
serializationSet
fullUpdateHistograms

readChannelDelta tail serializationSet fullUpdateHistograms
| [] -> serializationSet, fullUpdateHistograms

let serializationSet, fullUpdateHistograms =
readChannelDelta
(deltaSet |> Map.toList)
serializationSet
FullUpdateValueHistograms.Default

{ serializationSet with
FullUpdateDefaults =
Expand Down

0 comments on commit f5c09c8

Please sign in to comment.