Created
May 29, 2022 09:35
-
-
Save barnabemonnot/871aa56e7d28622b701bf0471603c86e to your computer and use it in GitHub Desktop.
7-block reorg diagram code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(jsonlite) | |
library(data.table) | |
library(gganimate) | |
library(magick) | |
pd <- lapply(readLines("~/Downloads/pb.log"), fromJSON) | |
pd_sample <- pd[[1]] | |
messages <- pd %>% | |
map(function(x) { | |
list(msg = x$msg) | |
}) %>% rbindlist() %>% unique() | |
block_received <- pd %>% | |
keep(function(x) { | |
x$msg == "Block received" && x$blck$slot %in% 3887073:3887083 | |
}) %>% | |
map(function(x) { | |
list( | |
slot = x$blck$slot, | |
parent_root = x$blck$parent_root, | |
beacon_block_root = x$blockRoot, | |
ts = x$ts | |
) | |
}) %>% rbindlist() %>% | |
add_column(line = c(2, 3, 1, 1, 1, 1, 1, 1, 1, 3, 3)) | |
block_received <- block_received %>% | |
left_join(block_received, by = c("parent_root" = "beacon_block_root")) %>% | |
select(slot = slot.x, line = line.x, parent_root, beacon_block_root, ts = ts.x, | |
parent_slot = slot.y, parent_line = line.y) | |
att_received <- pd %>% | |
keep(function(x) { | |
x$msg == "Attestation received" && x$attestation$data$slot %in% 3887073:3887083 | |
}) %>% map(function(x) { | |
list( | |
slot = x$attestation$data$slot, | |
beacon_block_root = x$attestation$data$beacon_block_root, | |
signature = x$attestation$signature, | |
ts = x$ts | |
) | |
}) %>% | |
rbindlist() %>% | |
unique() | |
att_received <- att_received %>% | |
left_join(block_received %>% select(beacon_block_root, block_slot = slot)) | |
filter_att_blocks <- function(ts_stop) { | |
return(list( | |
block_received = block_received %>% filter(ts <= ts_stop), | |
att_received = att_received %>% filter(ts <= ts_stop) | |
)) | |
} | |
make_plot <- function(df) { | |
p <- ggplot() + | |
geom_segment( | |
data = df$block_received %>% filter(!is.na(parent_slot)), | |
mapping = aes(x = slot, y = line, xend = parent_slot, yend = parent_line) | |
) + | |
geom_segment( | |
data = df$att_received %>% group_by(block_slot) %>% summarise(count = n()) %>% | |
left_join(df$block_received %>% select(block_slot = slot, line)), | |
mapping = aes(x = block_slot + 1/3, y = line + 3/4, xend = block_slot, yend = line), | |
color = "grey" | |
) + | |
geom_tile( | |
data = df$block_received %>% | |
mutate(line = factor(line, levels = c("1", "2", "3"))), | |
mapping = aes(x = slot, y = as.numeric(as.character(line)), width = 0.7, height = 0.2, fill = line) | |
) + | |
geom_text( | |
data = df$block_received, | |
aes(x = slot, y = line, label = str_trunc(slot, 2, side = "left", ellipsis = "")) | |
) + | |
geom_point( | |
data = df$att_received %>% group_by(block_slot) %>% summarise(count = n()) %>% | |
left_join(df$block_received %>% select(block_slot = slot, line)), | |
mapping = aes(x = block_slot + 1/3, y = line + 3/4, size = count), | |
color = "#F05431" | |
) + | |
scale_size_area( | |
max_size = 10, | |
limits = c(0, max(att_received %>% group_by(block_slot) %>% summarise(count = n()) %>% pull(count))) | |
) + | |
scale_fill_manual(breaks = c("1", "2", "3"), values = c("#FED152", "#45A9DE", "#BFCE80")) + | |
xlim(min(block_received$slot)-1, max(block_received$slot)+1) + | |
ylim(0.75, 4) + | |
guides( | |
fill = "none", | |
size = "none" | |
) + | |
theme( | |
axis.title.y = element_blank(), | |
axis.text.y = element_blank(), | |
axis.line.y = element_blank(), | |
axis.ticks.y = element_blank() | |
) | |
return(p) | |
} | |
save_plot <- function(p, index) { | |
ggsave( | |
str_c( | |
"plot_", | |
str_pad(index, 3, side = "left", pad = 0), | |
".png" | |
), | |
plot = p, | |
device = "png", | |
path = "~/Downloads/fcreorg/", | |
width = 2400, | |
height = 1600, | |
units = "px" | |
) | |
} | |
timesteps <- 100 | |
ts_lims <- range(c(block_received$ts)) | |
ls <- seq(from = as.POSIXct(ts_lims[1]), to = as.POSIXct(ts_lims[[2]]), length.out = timesteps) | |
for (i in 1:timesteps) { | |
print(i) | |
ts_stop <- ls[[i]] | |
df <- filter_att_blocks(ts_stop) | |
p <- make_plot(df) | |
save_plot(p, i) | |
} | |
images <- map(1:timesteps, function(index) { | |
path <- str_c("~/Downloads/fcreorg/plot_", str_pad(index, 3, side = "left", pad = 0), ".png") | |
image_read(path) | |
}) | |
images <- image_join(images) | |
animation <- image_animate(images, fps = timesteps / 10) | |
image_write(animation, "~/Downloads/fcreorg/anim.gif") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment