[DEPRECATED] Go implementation of plcbundle

fix did audit log (show nullified)

Changed files
+34 -164
bundle
cmd
plcbundle
commands
internal
didindex
server
+21 -38
bundle/manager.go
··· 1077 1077 } 1078 1078 1079 1079 // GetDIDOperations retrieves all operations for a DID (bundles + mempool combined) 1080 - func (m *Manager) GetDIDOperations(ctx context.Context, did string, verbose bool) ([]plcclient.PLCOperation, error) { 1080 + // Returns: operations only, operations with locations, error 1081 + func (m *Manager) GetDIDOperations(ctx context.Context, did string, verbose bool) ([]plcclient.PLCOperation, []PLCOperationWithLocation, error) { 1081 1082 if err := plcclient.ValidateDIDFormat(did); err != nil { 1082 - return nil, err 1083 + return nil, nil, err 1083 1084 } 1084 1085 1085 1086 // Set verbose mode ··· 1087 1088 m.didIndex.SetVerbose(verbose) 1088 1089 } 1089 1090 1090 - // Get bundled operations from DID index 1091 - bundledOps, err := m.didIndex.GetDIDOperations(ctx, did, m) 1091 + // Get bundled operations from DID index (includes nullified) 1092 + bundledOpsWithLoc, err := m.didIndex.GetDIDOperations(ctx, did, m) 1092 1093 if err != nil { 1093 - return nil, err 1094 + return nil, nil, err 1095 + } 1096 + 1097 + // Convert to bundle types 1098 + opsWithLoc := make([]PLCOperationWithLocation, len(bundledOpsWithLoc)) 1099 + bundledOps := make([]plcclient.PLCOperation, len(bundledOpsWithLoc)) 1100 + for i, r := range bundledOpsWithLoc { 1101 + opsWithLoc[i] = PLCOperationWithLocation{ 1102 + Operation: r.Operation, 1103 + Bundle: r.Bundle, 1104 + Position: r.Position, 1105 + } 1106 + bundledOps[i] = r.Operation 1094 1107 } 1095 1108 1096 1109 // Get mempool operations 1097 1110 mempoolOps, err := m.GetDIDOperationsFromMempool(did) 1098 1111 if err != nil { 1099 - return nil, err 1112 + return nil, nil, err 1100 1113 } 1101 1114 1102 1115 if len(mempoolOps) > 0 && verbose { 1103 1116 m.logger.Printf("DEBUG: Found %d operations in mempool", len(mempoolOps)) 1104 1117 } 1105 1118 1106 - // Combine and sort 1119 + // Combine operations (for the slice return) 1107 1120 allOps := append(bundledOps, mempoolOps...) 1108 1121 1109 1122 sort.Slice(allOps, func(i, j int) bool { 1110 1123 return allOps[i].CreatedAt.Before(allOps[j].CreatedAt) 1111 1124 }) 1112 1125 1113 - return allOps, nil 1126 + return allOps, opsWithLoc, nil 1114 1127 } 1115 1128 1116 1129 // GetDIDOperationsFromMempool retrieves operations for a DID from mempool only ··· 1141 1154 1142 1155 // Delegate to DID index for bundled operations 1143 1156 return m.didIndex.GetLatestDIDOperation(ctx, did, m) 1144 - } 1145 - 1146 - // GetDIDOperationsWithLocations returns operations along with their bundle/position info 1147 - func (m *Manager) GetDIDOperationsWithLocations(ctx context.Context, did string, verbose bool) ([]PLCOperationWithLocation, error) { 1148 - if err := plcclient.ValidateDIDFormat(did); err != nil { 1149 - return nil, err 1150 - } 1151 - 1152 - // Set verbose mode 1153 - if m.didIndex != nil { 1154 - m.didIndex.SetVerbose(verbose) 1155 - } 1156 - 1157 - // Delegate to DID index 1158 - results, err := m.didIndex.GetDIDOperationsWithLocations(ctx, did, m) 1159 - if err != nil { 1160 - return nil, err 1161 - } 1162 - 1163 - // Convert to bundle's type 1164 - bundleResults := make([]PLCOperationWithLocation, len(results)) 1165 - for i, r := range results { 1166 - bundleResults[i] = PLCOperationWithLocation{ 1167 - Operation: r.Operation, 1168 - Bundle: r.Bundle, 1169 - Position: r.Position, 1170 - } 1171 - } 1172 - 1173 - return bundleResults, nil 1174 1157 } 1175 1158 1176 1159 // VerifyChain verifies the entire bundle chain
+1 -2
cmd/plcbundle/commands/common.go
··· 36 36 GetDIDIndexStats() map[string]interface{} 37 37 GetDIDIndex() *didindex.Manager 38 38 BuildDIDIndex(ctx context.Context, progress func(int, int)) error 39 - GetDIDOperations(ctx context.Context, did string, verbose bool) ([]plcclient.PLCOperation, error) 40 - GetDIDOperationsWithLocations(ctx context.Context, did string, verbose bool) ([]bundle.PLCOperationWithLocation, error) 39 + GetDIDOperations(ctx context.Context, did string, verbose bool) ([]plcclient.PLCOperation, []PLCOperationWithLocation, error) 41 40 GetDIDOperationsFromMempool(did string) ([]plcclient.PLCOperation, error) 42 41 GetLatestDIDOperation(ctx context.Context, did string) (*plcclient.PLCOperation, error) 43 42 LoadOperation(ctx context.Context, bundleNum, position int) (*plcclient.PLCOperation, error)
+7 -8
cmd/plcbundle/commands/did.go
··· 73 73 • DID: did:plc:524tuhdhh3m7li5gycdn6boe 74 74 • Handle: tree.fail (resolves via configured resolver) 75 75 76 - Requires DID index to be built. If not available, will fall back to 77 - full scan (slow).`, 76 + Requires DID index to be built.`, 78 77 79 78 Example: ` # Lookup by DID 80 79 plcbundle did lookup did:plc:524tuhdhh3m7li5gycdn6boe 81 80 82 - # Lookup by handle (requires --resolver-url) 81 + # Lookup by handle 83 82 plcbundle did lookup tree.fail 84 83 plcbundle did lookup ngerakines.me 85 84 ··· 114 113 115 114 // Lookup operations 116 115 lookupStart := time.Now() 117 - opsWithLoc, err := mgr.GetDIDOperationsWithLocations(ctx, did, verbose) 116 + _, opsWithLoc, err := mgr.GetDIDOperations(ctx, did, verbose) 118 117 if err != nil { 119 118 return err 120 119 } ··· 298 297 ctx := context.Background() 299 298 300 299 // Get all operations with locations 301 - opsWithLoc, err := mgr.GetDIDOperationsWithLocations(ctx, did, verbose) 300 + _, opsWithLoc, err := mgr.GetDIDOperations(ctx, did, verbose) 302 301 if err != nil { 303 302 return err 304 303 } ··· 629 628 ctx := context.Background() 630 629 631 630 // Get operations 632 - opsWithLoc, err := mgr.GetDIDOperationsWithLocations(ctx, did, false) 631 + _, opsWithLoc, err := mgr.GetDIDOperations(ctx, did, false) 633 632 if err != nil { 634 633 return err 635 634 } ··· 822 821 errorCount := 0 823 822 824 823 for i, did := range dids { 825 - opsWithLoc, err := mgr.GetDIDOperationsWithLocations(ctx, did, false) 824 + _, opsWithLoc, err := mgr.GetDIDOperations(ctx, did, false) 826 825 if err != nil { 827 826 errorCount++ 828 827 fmt.Fprintf(output, "%s,error,0,0,0,0\n", did) ··· 1110 1109 defer writer.Flush() 1111 1110 1112 1111 for i, did := range dids { 1113 - opsWithLoc, err := mgr.GetDIDOperationsWithLocations(ctx, did, false) 1112 + _, opsWithLoc, err := mgr.GetDIDOperations(ctx, did, false) 1114 1113 if err != nil { 1115 1114 errorCount++ 1116 1115 if i < 10 { // Only log first few errors
+3 -113
internal/didindex/lookup.go
··· 9 9 "tangled.org/atscan.net/plcbundle/internal/plcclient" 10 10 ) 11 11 12 - // GetDIDOperations retrieves all operations for a DID from bundles 13 - func (dim *Manager) GetDIDOperations(ctx context.Context, did string, provider BundleProvider) ([]plcclient.PLCOperation, error) { 14 - if err := plcclient.ValidateDIDFormat(did); err != nil { 15 - return nil, err 16 - } 17 - 18 - if !dim.Exists() { 19 - return nil, fmt.Errorf("DID index not available - run 'plcbundle index build' to enable DID lookups") 20 - } 21 - 22 - if dim.verbose { 23 - dim.logger.Printf("DEBUG: Using DID index for lookup") 24 - } 25 - 26 - locations, err := dim.GetDIDLocations(did) 27 - if err != nil { 28 - return nil, err 29 - } 30 - 31 - if len(locations) == 0 { 32 - return []plcclient.PLCOperation{}, nil 33 - } 34 - 35 - // Filter nullified 36 - var validLocations []OpLocation 37 - for _, loc := range locations { 38 - if !loc.Nullified() { 39 - validLocations = append(validLocations, loc) 40 - } 41 - } 42 - 43 - if dim.verbose { 44 - dim.logger.Printf("DEBUG: Filtered %d valid locations (from %d total)", 45 - len(validLocations), len(locations)) 46 - } 47 - 48 - if len(validLocations) == 1 { 49 - loc := validLocations[0] 50 - op, err := provider.LoadOperation(ctx, loc.BundleInt(), loc.PositionInt()) 51 - if err != nil { 52 - return nil, err 53 - } 54 - return []plcclient.PLCOperation{*op}, nil 55 - } 56 - 57 - // For multiple operations: group by bundle to minimize bundle loads 58 - bundleMap := make(map[uint16][]uint16) 59 - for _, loc := range validLocations { 60 - bundleMap[loc.Bundle()] = append(bundleMap[loc.Bundle()], loc.Position()) 61 - } 62 - 63 - if dim.verbose { 64 - dim.logger.Printf("DEBUG: Loading from %d bundle(s)", len(bundleMap)) 65 - } 66 - 67 - // Load operations 68 - var allOps []plcclient.PLCOperation 69 - for bundleNum, positions := range bundleMap { 70 - // Optimization: If single position from bundle, use LoadOperation 71 - if len(positions) == 1 { 72 - op, err := provider.LoadOperation(ctx, int(bundleNum), int(positions[0])) 73 - if err != nil { 74 - dim.logger.Printf("Warning: failed to load operation at bundle %d position %d: %v", 75 - bundleNum, positions[0], err) 76 - continue 77 - } 78 - allOps = append(allOps, *op) 79 - } else { 80 - // Multiple positions: load full bundle 81 - bundle, err := provider.LoadBundleForDIDIndex(ctx, int(bundleNum)) 82 - if err != nil { 83 - dim.logger.Printf("Warning: failed to load bundle %d: %v", bundleNum, err) 84 - continue 85 - } 86 - 87 - for _, pos := range positions { 88 - if int(pos) < len(bundle.Operations) { 89 - allOps = append(allOps, bundle.Operations[pos]) 90 - } 91 - } 92 - } 93 - } 94 - 95 - if dim.verbose { 96 - dim.logger.Printf("DEBUG: Loaded %d total operations", len(allOps)) 97 - } 98 - 99 - // Sort by time 100 - sort.Slice(allOps, func(i, j int) bool { 101 - return allOps[i].CreatedAt.Before(allOps[j].CreatedAt) 102 - }) 103 - 104 - return allOps, nil 105 - } 106 - 107 - // GetDIDOperationsWithLocations returns operations with their bundle/position metadata 108 - func (dim *Manager) GetDIDOperationsWithLocations(ctx context.Context, did string, provider BundleProvider) ([]OpLocationWithOperation, error) { 12 + // GetDIDOperations retrieves all operations for a DID WITH location metadata 13 + // Returns operations with bundle/position info (includes nullified operations) 14 + func (dim *Manager) GetDIDOperations(ctx context.Context, did string, provider BundleProvider) ([]OpLocationWithOperation, error) { 109 15 if err := plcclient.ValidateDIDFormat(did); err != nil { 110 16 return nil, err 111 17 } ··· 114 20 return nil, fmt.Errorf("DID index not available - run 'plcbundle index build' to enable DID lookups") 115 21 } 116 22 117 - if dim.verbose { 118 - dim.logger.Printf("DEBUG: Using DID index for lookup with locations") 119 - } 120 - 121 23 locations, err := dim.GetDIDLocations(did) 122 24 if err != nil { 123 25 return nil, err ··· 127 29 return []OpLocationWithOperation{}, nil 128 30 } 129 31 130 - if dim.verbose { 131 - dim.logger.Printf("DEBUG: Found %d locations in index", len(locations)) 132 - } 133 - 134 32 // Group by bundle 135 33 bundleMap := make(map[uint16][]OpLocation) 136 34 for _, loc := range locations { 137 35 bundleMap[loc.Bundle()] = append(bundleMap[loc.Bundle()], loc) 138 - } 139 - 140 - if dim.verbose { 141 - dim.logger.Printf("DEBUG: Loading from %d bundle(s)", len(bundleMap)) 142 36 } 143 37 144 38 var results []OpLocationWithOperation ··· 167 61 sort.Slice(results, func(i, j int) bool { 168 62 return results[i].Operation.CreatedAt.Before(results[j].Operation.CreatedAt) 169 63 }) 170 - 171 - if dim.verbose { 172 - dim.logger.Printf("DEBUG: Loaded %d total operations", len(results)) 173 - } 174 64 175 65 return results, nil 176 66 }
+2 -3
server/handlers.go
··· 815 815 return 816 816 } 817 817 818 - operations, err := s.manager.GetDIDOperations(context.Background(), did, false) 818 + operations, _, err := s.manager.GetDIDOperations(context.Background(), did, false) 819 819 if err != nil { 820 820 sendJSON(w, 500, map[string]string{"error": err.Error()}) 821 821 return ··· 842 842 843 843 func (s *Server) handleDIDAuditLog(input string) http.HandlerFunc { 844 844 return func(w http.ResponseWriter, r *http.Request) { 845 - // Resolve handle to DID 846 845 did, _, err := s.manager.ResolveHandleOrDID(r.Context(), input) 847 846 if err != nil { 848 847 sendJSON(w, 400, map[string]string{"error": err.Error()}) 849 848 return 850 849 } 851 850 852 - operations, err := s.manager.GetDIDOperations(context.Background(), did, false) 851 + operations, _, err := s.manager.GetDIDOperations(context.Background(), did, false) 853 852 if err != nil { 854 853 sendJSON(w, 500, map[string]string{"error": err.Error()}) 855 854 return