@@ -7,8 +7,11 @@ import (
7
7
"io"
8
8
9
9
"github.com/mit-dci/utreexo/accumulator"
10
+ "github.com/mit-dci/utreexo/common"
10
11
)
11
12
13
+ // UData is all the data needed to verify the utreexo accumulator proof
14
+ // for a given block
12
15
type UData struct {
13
16
Height int32
14
17
AccProof accumulator.BatchProof
@@ -52,22 +55,20 @@ func (ud *UData) ProofSanity(nl uint64, h uint8) bool {
52
55
return false
53
56
}
54
57
}
55
- // return to presorted target list
56
- // ud.AccProof.Targets = presort
58
+
57
59
return true
58
60
}
59
61
60
62
// on disk
61
63
// aaff aaff 0000 0014 0000 0001 0000 0001 0000 0000 0000 0000 0000 0000
62
64
// magic | size | height | numttls | ttl0 | numTgts | ????
63
65
64
- // ToBytes serializes UData into bytes.
66
+ // Serialize serializes UData into bytes.
65
67
// First, height, 4 bytes.
66
68
// Then, number of TTL values (4 bytes, even though we only need 2)
67
- // Then a bunch of TTL values, (4B each) one for each txo in the associated block
68
- // batch proof
69
- // Bunch of LeafDatas
70
-
69
+ // Then a bunch of TTL values, (4B each) one for each txo in the
70
+ // associated block batch proof
71
+ // And the rest is a bunch of LeafDatas
71
72
func (ud * UData ) Serialize (w io.Writer ) (err error ) {
72
73
err = binary .Write (w , binary .BigEndian , ud .Height )
73
74
if err != nil { // ^ 4B block height
@@ -89,43 +90,28 @@ func (ud *UData) Serialize(w io.Writer) (err error) {
89
90
return
90
91
}
91
92
92
- // fmt.Printf("accproof %d bytes\n", ud.AccProof.SerializeSize())
93
-
94
93
// write all the leafdatas
95
94
for _ , ld := range ud .Stxos {
96
- // fmt.Printf("writing ld %d %s\n", i, ld.ToString())
97
95
err = ld .Serialize (w )
98
96
if err != nil {
99
97
return
100
98
}
101
- // fmt.Printf("h %d leaf %d %s len %d\n",
102
- // ud.Height, i, ld.Outpoint.String(), len(ld.PkScript))
103
99
}
104
100
105
101
return
106
102
}
107
103
108
- //
104
+ // SerializeSize outputs the size of the udata when it is serialized
109
105
func (ud * UData ) SerializeSize () int {
110
106
var ldsize int
111
- var b bytes.Buffer
112
-
113
- // TODO this is slow, can remove double checking once it works reliably
114
- for _ , l := range ud .Stxos {
115
- ldsize += l .SerializeSize ()
116
- b .Reset ()
117
- l .Serialize (& b )
118
- if b .Len () != l .SerializeSize () {
119
- fmt .Printf (" b.Len() %d, l.SerializeSize() %d\n " ,
120
- b .Len (), l .SerializeSize ())
121
- }
122
- }
107
+ buf := common .NewFreeBytes ()
108
+ bufWriter := bytes .NewBuffer (buf .Bytes )
123
109
124
- b .Reset ()
125
- ud .AccProof .Serialize (& b )
126
- if b .Len () != ud .AccProof .SerializeSize () {
110
+ bufWriter .Reset ()
111
+ ud .AccProof .Serialize (bufWriter )
112
+ if bufWriter .Len () != ud .AccProof .SerializeSize () {
127
113
fmt .Printf (" b.Len() %d, AccProof.SerializeSize() %d\n " ,
128
- b .Len (), ud .AccProof .SerializeSize ())
114
+ bufWriter .Len (), ud .AccProof .SerializeSize ())
129
115
}
130
116
131
117
guess := 8 + (4 * len (ud .TxoTTLs )) + ud .AccProof .SerializeSize () + ldsize
@@ -134,23 +120,20 @@ func (ud *UData) SerializeSize() int {
134
120
return guess
135
121
}
136
122
123
+ // Deserialize reads from the reader and deserializes the udata
137
124
func (ud * UData ) Deserialize (r io.Reader ) (err error ) {
138
-
139
125
err = binary .Read (r , binary .BigEndian , & ud .Height )
140
126
if err != nil { // ^ 4B block height
141
127
fmt .Printf ("ud deser Height err %s\n " , err .Error ())
142
128
return
143
129
}
144
- // fmt.Printf("read height %d\n", ud.Height)
145
130
146
131
var numTTLs uint32
147
132
err = binary .Read (r , binary .BigEndian , & numTTLs )
148
133
if err != nil { // ^ 4B num ttls
149
134
fmt .Printf ("ud deser numTTLs err %s\n " , err .Error ())
150
135
return
151
136
}
152
- // fmt.Printf("read ttls %d\n", numTTLs)
153
- // fmt.Printf("UData deser read h %d - %d ttls ", ud.Height, numTTLs)
154
137
155
138
ud .TxoTTLs = make ([]int32 , numTTLs )
156
139
for i , _ := range ud .TxoTTLs { // write all ttls
@@ -159,7 +142,6 @@ func (ud *UData) Deserialize(r io.Reader) (err error) {
159
142
fmt .Printf ("ud deser LeafTTLs[%d] err %s\n " , i , err .Error ())
160
143
return
161
144
}
162
- // fmt.Printf("read ttl[%d] %d\n", i, ud.TxoTTLs[i])
163
145
}
164
146
165
147
err = ud .AccProof .Deserialize (r )
@@ -168,8 +150,6 @@ func (ud *UData) Deserialize(r io.Reader) (err error) {
168
150
return
169
151
}
170
152
171
- // fmt.Printf("%d byte accproof, read %d targets\n",
172
- // ud.AccProof.SerializeSize(), len(ud.AccProof.Targets))
173
153
// we've already gotten targets. 1 leafdata per target
174
154
ud .Stxos = make ([]LeafData , len (ud .AccProof .Targets ))
175
155
for i , _ := range ud .Stxos {
@@ -180,9 +160,6 @@ func (ud *UData) Deserialize(r io.Reader) (err error) {
180
160
ud .Height , numTTLs , len (ud .AccProof .Targets ), i , err .Error ())
181
161
return
182
162
}
183
- // fmt.Printf("h %d leaf %d %s len %d\n",
184
- // ud.Height, i, ud.Stxos[i].Outpoint.String(), len(ud.Stxos[i].PkScript))
185
-
186
163
}
187
164
188
165
return
@@ -212,6 +189,7 @@ func GenUData(delLeaves []LeafData, forest *accumulator.Forest, height int32) (
212
189
213
190
ud .Height = height
214
191
ud .Stxos = delLeaves
192
+
215
193
// make slice of hashes from leafdata
216
194
delHashes := make ([]accumulator.Hash , len (ud .Stxos ))
217
195
for i , _ := range ud .Stxos {
@@ -233,6 +211,5 @@ func GenUData(delLeaves []LeafData, forest *accumulator.Forest, height int32) (
233
211
return
234
212
}
235
213
236
- // fmt.Printf(ud.AccProof.ToString())
237
214
return
238
215
}
0 commit comments