Skip to content

Commit 189b854

Browse files
committed
trimUnreferencedBumps
1 parent 7f4e2fc commit 189b854

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

transaction/beef.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,83 @@ func (b *Beef) TrimknownTxIDs(knownTxIDs []string) {
10211021
}
10221022
}
10231023
}
1024-
// TODO: bumps could be trimmed to eliminate unreferenced proofs.
1024+
1025+
// Trim unreferenced BUMP proofs
1026+
b.trimUnreferencedBumps()
1027+
}
1028+
1029+
// trimUnreferencedBumps removes BUMP proofs that are no longer referenced by any remaining transactions
1030+
func (b *Beef) trimUnreferencedBumps() {
1031+
if len(b.BUMPs) == 0 {
1032+
return
1033+
}
1034+
1035+
// Track which BUMP indices are still referenced by remaining transactions
1036+
usedBumpIndices := make(map[int]bool)
1037+
1038+
// Build a set of transaction IDs that need BUMPs
1039+
txidsNeedingBumps := make(map[string]bool)
1040+
1041+
for txid, tx := range b.Transactions {
1042+
switch tx.DataFormat {
1043+
case RawTxAndBumpIndex:
1044+
// Direct BUMP reference
1045+
usedBumpIndices[tx.BumpIndex] = true
1046+
case RawTx:
1047+
// Raw transaction without explicit BUMP - we need to check if any BUMP references this txid
1048+
txidsNeedingBumps[txid] = true
1049+
case TxIDOnly:
1050+
// Known transaction ID - we need to check if any BUMP references this txid
1051+
if tx.KnownTxID != nil {
1052+
txidsNeedingBumps[tx.KnownTxID.String()] = true
1053+
}
1054+
}
1055+
}
1056+
1057+
// Check each BUMP to see if it's needed for any of the txids
1058+
for bumpIndex, bump := range b.BUMPs {
1059+
if bump != nil && len(bump.Path) > 0 && len(bump.Path[0]) > 0 {
1060+
// Get the transaction ID from the first path element (leaf level)
1061+
for _, leaf := range bump.Path[0] {
1062+
if leaf.Hash != nil {
1063+
txidStr := leaf.Hash.String()
1064+
if txidsNeedingBumps[txidStr] {
1065+
usedBumpIndices[bumpIndex] = true
1066+
break
1067+
}
1068+
}
1069+
}
1070+
}
1071+
}
1072+
1073+
// If all BUMPs are still in use, no trimming needed
1074+
if len(usedBumpIndices) == len(b.BUMPs) {
1075+
return
1076+
}
1077+
1078+
// Build new BUMP slice with only referenced BUMPs
1079+
newBumps := make([]*MerklePath, 0, len(usedBumpIndices))
1080+
bumpIndexMapping := make(map[int]int) // old index -> new index
1081+
1082+
for oldIndex := 0; oldIndex < len(b.BUMPs); oldIndex++ {
1083+
if usedBumpIndices[oldIndex] {
1084+
newIndex := len(newBumps)
1085+
newBumps = append(newBumps, b.BUMPs[oldIndex])
1086+
bumpIndexMapping[oldIndex] = newIndex
1087+
}
1088+
}
1089+
1090+
// Update BUMP indices in remaining transactions
1091+
for _, tx := range b.Transactions {
1092+
if tx.DataFormat == RawTxAndBumpIndex {
1093+
if newIndex, exists := bumpIndexMapping[tx.BumpIndex]; exists {
1094+
tx.BumpIndex = newIndex
1095+
}
1096+
}
1097+
}
1098+
1099+
// Replace the BUMP slice
1100+
b.BUMPs = newBumps
10251101
}
10261102

10271103
func (b *Beef) GetValidTxids() []string {

0 commit comments

Comments
 (0)