@@ -66,19 +66,6 @@ func (b *DagBuilder) GetLatestLabel() string {
6666 return result
6767}
6868
69- func (b * DagBuilder ) GetNextAvailableLabel () string {
70- latestLabel := b .GetLatestLabel ()
71-
72- number , err := strconv .ParseInt (latestLabel , 10 , 64 )
73- if err != nil {
74- fmt .Println ("Failed to parse label" )
75- }
76-
77- nextLabel := strconv .FormatInt (number + 1 , 10 )
78-
79- return nextLabel
80- }
81-
8269// CalculateTotalContentSize calculates the total size of actual content (not including metadata)
8370func (b * DagBuilder ) CalculateTotalContentSize () int64 {
8471 var totalSize int64
@@ -94,8 +81,50 @@ func (b *DagBuilder) CalculateTotalContentSize() int64 {
9481func (b * DagBuilder ) CalculateTotalDagSize () (int64 , error ) {
9582 var totalSize int64
9683 for _ , leaf := range b .Leafs {
97- serializable := leaf .ToSerializable ()
98- serialized , err := cbor .Marshal (serializable )
84+ bareHash := GetHash (leaf .Hash )
85+
86+ var linkHashes []string
87+ if len (leaf .Links ) > 0 {
88+ linkHashes = make ([]string , 0 , len (leaf .Links ))
89+ for _ , linkHash := range leaf .Links {
90+ linkHashes = append (linkHashes , GetHash (linkHash ))
91+ }
92+ sort .Strings (linkHashes )
93+ }
94+
95+ data := struct {
96+ Hash string
97+ ItemName string
98+ Type LeafType
99+ ContentHash []byte
100+ Content []byte
101+ ClassicMerkleRoot []byte
102+ CurrentLinkCount int
103+ LatestLabel string
104+ LeafCount int
105+ ContentSize int64
106+ DagSize int64
107+ Links []string
108+ AdditionalData map [string ]string
109+ StoredProofs map [string ]* ClassicTreeBranch
110+ }{
111+ Hash : bareHash ,
112+ ItemName : leaf .ItemName ,
113+ Type : leaf .Type ,
114+ ContentHash : leaf .ContentHash ,
115+ Content : leaf .Content ,
116+ ClassicMerkleRoot : leaf .ClassicMerkleRoot ,
117+ CurrentLinkCount : leaf .CurrentLinkCount ,
118+ LatestLabel : leaf .LatestLabel ,
119+ LeafCount : leaf .LeafCount ,
120+ ContentSize : leaf .ContentSize ,
121+ DagSize : leaf .DagSize ,
122+ Links : linkHashes ,
123+ AdditionalData : sortMapByKeys (leaf .AdditionalData ),
124+ StoredProofs : leaf .Proofs ,
125+ }
126+
127+ serialized , err := cbor .Marshal (data )
99128 if err != nil {
100129 return 0 , fmt .Errorf ("failed to serialize leaf %s: %w" , leaf .Hash , err )
101130 }
@@ -253,7 +282,7 @@ func (b *DagLeafBuilder) BuildRootLeaf(dag *DagBuilder, additionalData map[strin
253282 MerkleRoot : merkleRoot ,
254283 CurrentLinkCount : len (b .Links ),
255284 LatestLabel : latestLabel ,
256- LeafCount : len (dag .Leafs ),
285+ LeafCount : len (dag .Leafs ) + 1 ,
257286 ContentSize : contentSize ,
258287 DagSize : 0 ,
259288 ContentHash : nil ,
@@ -525,7 +554,7 @@ func (leaf *DagLeaf) VerifyLeaf() error {
525554func (leaf * DagLeaf ) VerifyRootLeaf (dag * Dag ) error {
526555 additionalData := sortMapByKeys (leaf .AdditionalData )
527556
528- if leaf . ClassicMerkleRoot == nil || len (leaf .ClassicMerkleRoot ) <= 0 {
557+ if len (leaf .ClassicMerkleRoot ) <= 0 {
529558 leaf .ClassicMerkleRoot = []byte {}
530559 }
531560
@@ -536,6 +565,16 @@ func (leaf *DagLeaf) VerifyRootLeaf(dag *Dag) error {
536565 len (dag .Leafs ) == leaf .LeafCount &&
537566 (leaf .ContentSize != 0 || leaf .DagSize != 0 || leaf .LeafCount == 1 )
538567
568+ // If any leaf has pruned links, this is a partial DAG
569+ if isFullDag && dag != nil {
570+ for _ , l := range dag .Leafs {
571+ if len (l .Links ) < l .CurrentLinkCount {
572+ isFullDag = false
573+ break
574+ }
575+ }
576+ }
577+
539578 if isFullDag {
540579 for _ , dagLeaf := range dag .Leafs {
541580 if dagLeaf .Content != nil {
@@ -550,8 +589,50 @@ func (leaf *DagLeaf) VerifyRootLeaf(dag *Dag) error {
550589 continue
551590 }
552591
553- serializable := dagLeaf .ToSerializable ()
554- serialized , err := cbor .Marshal (serializable )
592+ bareHash := GetHash (dagLeaf .Hash )
593+
594+ var linkHashes []string
595+ if len (dagLeaf .Links ) > 0 {
596+ linkHashes = make ([]string , 0 , len (dagLeaf .Links ))
597+ for _ , linkHash := range dagLeaf .Links {
598+ linkHashes = append (linkHashes , GetHash (linkHash ))
599+ }
600+ sort .Strings (linkHashes )
601+ }
602+
603+ data := struct {
604+ Hash string
605+ ItemName string
606+ Type LeafType
607+ ContentHash []byte
608+ Content []byte
609+ ClassicMerkleRoot []byte
610+ CurrentLinkCount int
611+ LatestLabel string
612+ LeafCount int
613+ ContentSize int64
614+ DagSize int64
615+ Links []string
616+ AdditionalData map [string ]string
617+ StoredProofs map [string ]* ClassicTreeBranch
618+ }{
619+ Hash : bareHash ,
620+ ItemName : dagLeaf .ItemName ,
621+ Type : dagLeaf .Type ,
622+ ContentHash : dagLeaf .ContentHash ,
623+ Content : dagLeaf .Content ,
624+ ClassicMerkleRoot : dagLeaf .ClassicMerkleRoot ,
625+ CurrentLinkCount : dagLeaf .CurrentLinkCount ,
626+ LatestLabel : dagLeaf .LatestLabel ,
627+ LeafCount : dagLeaf .LeafCount ,
628+ ContentSize : dagLeaf .ContentSize ,
629+ DagSize : dagLeaf .DagSize ,
630+ Links : linkHashes ,
631+ AdditionalData : sortMapByKeys (dagLeaf .AdditionalData ),
632+ StoredProofs : dagLeaf .Proofs ,
633+ }
634+
635+ serialized , err := cbor .Marshal (data )
555636 if err != nil {
556637 return fmt .Errorf ("failed to serialize leaf %s for size verification: %w" , dagLeaf .Hash , err )
557638 }
@@ -830,10 +911,10 @@ func GetLabel(hash string) string {
830911// StripLabel removes the "label:" prefix from a hash string
831912func StripLabel (hash string ) string {
832913 parts := strings .Split (hash , ":" )
833- if len (parts ) != 2 {
914+ if len (parts ) < 2 {
834915 return hash
835916 }
836- return parts [1 ]
917+ return strings . Join ( parts [1 :], ":" )
837918}
838919
839920// ReplaceLabelInLink replaces the label in a child's link hash
0 commit comments