Skip to content

Commit 2c0a426

Browse files
committed
some progress bah
1 parent ea8eb47 commit 2c0a426

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/discof/forest/fd_forest.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,17 +1086,29 @@ fd_forest_evict( fd_forest_t * forest, ulong new_slot, ulong parent_slot ) {
10861086
ulong null = fd_forest_pool_idx_null( pool );
10871087

10881088

1089-
int has_orphans = !fd_forest_subtlist_is_empty( subtlist, pool );
1089+
int has_orphans = !fd_forest_subtlist_is_empty( subtlist, pool );
10901090
int parent_exists = fd_forest_query( forest, parent_slot )!=NULL;
10911091

10921092
/* During regular operation there's usually no orphans, but if eviction
10931093
is called then it's likely we have orphans. */
10941094
if( FD_LIKELY( has_orphans ) ) {
10951095
/* We'll keep it if it's older than all of our orphans, or if it's
10961096
parent is part of the main tree. */
1097+
1098+
ulong evict_orphan_slot = ULONG_MAX; /* best orphan candidate for eviction is the newest one. */
1099+
for( fd_forest_subtlist_iter_t iter = fd_forest_subtlist_iter_fwd_init( subtlist, pool );
1100+
!fd_forest_subtlist_iter_done( iter, subtlist, pool );
1101+
iter = fd_forest_subtlist_iter_fwd_next( iter, subtlist, pool ) ) {
1102+
fd_forest_blk_t * ele = fd_forest_subtlist_iter_ele( iter, subtlist, pool );
1103+
if( FD_LIKELY( evict_orphan_slot == ULONG_MAX ) ) evict_orphan_slot = ele->slot;
1104+
else evict_orphan_slot = fd_ulong_max( evict_orphan_slot, ele->slot );
1105+
1106+
}
1107+
/// TODO: idk should we evict the whole subtree??? or just like the leafy
10971108
if( fd_forest_ancestry_ele_query( ancestry, &parent_slot, NULL, pool ) ||
10981109
fd_forest_frontier_ele_query( frontier, &parent_slot, NULL, pool ) ) {
10991110
// choose something to evict from orphans
1111+
11001112
return SLOT_INSERT;
11011113
}
11021114

@@ -1117,22 +1129,38 @@ fd_forest_evict( fd_forest_t * forest, ulong new_slot, ulong parent_slot ) {
11171129
if( FD_LIKELY( parent_exists ) ) {
11181130
/* Parent exists. This slot chains to the main tree. From oldest to newest,
11191131
choose an unconfirmed leaf to evict. */
1120-
ulong unconfirmed_leaves[128];
1121-
uint cnt = 0;
1132+
ulong evict_slot = ULONG_MAX;
1133+
uint fork_cnt = 0;
11221134
for( fd_forest_frontier_iter_t iter = fd_forest_frontier_iter_init( frontier, pool );
11231135
!fd_forest_frontier_iter_done( iter, frontier, pool );
11241136
iter = fd_forest_frontier_iter_next( iter, frontier, pool ) ) {
11251137
fd_forest_blk_t * ele = fd_forest_frontier_iter_ele( iter, frontier, pool );
11261138
if( !ele->confirmed ) {
1127-
unconfirmed_leaves[cnt++] = ele->slot;
1128-
if( FD_UNLIKELY( cnt == 128 ) ) break; // It doesn't matter if we don't get all the unconfirmed leaves, we'll just evict the oldest from what we've seen.
1139+
if( FD_UNLIKELY( evict_slot == ULONG_MAX ) ) evict_slot = ele->slot;
1140+
else evict_slot = fd_ulong_min( evict_slot, ele->slot );
11291141
}
1142+
fork_cnt++;
11301143
}
11311144

1132-
/* Take the oldest unconfirmed leaf */
1133-
1134-
1145+
if( FD_UNLIKELY( evict_slot == ULONG_MAX ) ) {
1146+
/* It should be impossible to confirm more than
1147+
one fork (one leaf) at a time. If there is NO leaf that is
1148+
unconfirmed, then that means there's either multiple forks
1149+
that have been confirmed ( in this case its JOVER. For
1150+
EVERYONE!), or that we have only one fork, that has been
1151+
confirmed, but it hasn't been rooted in SLOT_MAX. Now it's
1152+
possible that the leaf of our one fork has been confirmed,
1153+
but we're still in the process of confirming all of it's
1154+
ancestors. In that case... we can't evict anything, but our
1155+
state is POTENTIALLY valid (but unlikely) */
1156+
FD_TEST( fork_cnt >= 1 );
1157+
if( FD_UNLIKELY( fork_cnt > 1 )) FD_LOG_CRIT(( "Multiple forks confirmed. Ignore? Crit? Pick..." ));
1158+
else return SLOT_IGNORE;
1159+
} else {
1160+
/* evict slot found. EVICT IT. */
11351161

1162+
return SLOT_INSERT;
1163+
}
11361164
} else {
11371165
/* Parent does not exist. This new slot would've been added as an orphan. */
11381166
return SLOT_IGNORE;

0 commit comments

Comments
 (0)