Skip to content

Commit 16684f6

Browse files
authored
Merge pull request #2073 from Roasbeef/wire-opts
wire: only borrow/return binaryFreeList buffers at the message level
2 parents 790c570 + b0e9636 commit 16684f6

26 files changed

+1278
-359
lines changed

wire/bench_test.go

+469-15
Large diffs are not rendered by default.

wire/blockheader.go

+98-5
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,109 @@ func NewBlockHeader(version int32, prevHash, merkleRootHash *chainhash.Hash,
107107
// readBlockHeader reads a bitcoin block header from r. See Deserialize for
108108
// decoding block headers stored to disk, such as in a database, as opposed to
109109
// decoding from the wire.
110+
//
111+
// DEPRECATED: Use readBlockHeaderBuf instead.
110112
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
111-
return readElements(r, &bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
112-
(*uint32Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
113+
buf := binarySerializer.Borrow()
114+
err := readBlockHeaderBuf(r, pver, bh, buf)
115+
binarySerializer.Return(buf)
116+
return err
117+
}
118+
119+
// readBlockHeaderBuf reads a bitcoin block header from r. See Deserialize for
120+
// decoding block headers stored to disk, such as in a database, as opposed to
121+
// decoding from the wire.
122+
//
123+
// If b is non-nil, the provided buffer will be used for serializing small
124+
// values. Otherwise a buffer will be drawn from the binarySerializer's pool
125+
// and return when the method finishes.
126+
//
127+
// NOTE: b MUST either be nil or at least an 8-byte slice.
128+
func readBlockHeaderBuf(r io.Reader, pver uint32, bh *BlockHeader,
129+
buf []byte) error {
130+
131+
if _, err := io.ReadFull(r, buf[:4]); err != nil {
132+
return err
133+
}
134+
bh.Version = int32(littleEndian.Uint32(buf[:4]))
135+
136+
if _, err := io.ReadFull(r, bh.PrevBlock[:]); err != nil {
137+
return err
138+
}
139+
140+
if _, err := io.ReadFull(r, bh.MerkleRoot[:]); err != nil {
141+
return err
142+
}
143+
144+
if _, err := io.ReadFull(r, buf[:4]); err != nil {
145+
return err
146+
}
147+
bh.Timestamp = time.Unix(int64(littleEndian.Uint32(buf[:4])), 0)
148+
149+
if _, err := io.ReadFull(r, buf[:4]); err != nil {
150+
return err
151+
}
152+
bh.Bits = littleEndian.Uint32(buf[:4])
153+
154+
if _, err := io.ReadFull(r, buf[:4]); err != nil {
155+
return err
156+
}
157+
bh.Nonce = littleEndian.Uint32(buf[:4])
158+
159+
return nil
113160
}
114161

115162
// writeBlockHeader writes a bitcoin block header to w. See Serialize for
116163
// encoding block headers to be stored to disk, such as in a database, as
117164
// opposed to encoding for the wire.
165+
//
166+
// DEPRECATED: Use writeBlockHeaderBuf instead.
118167
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
119-
sec := uint32(bh.Timestamp.Unix())
120-
return writeElements(w, bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
121-
sec, bh.Bits, bh.Nonce)
168+
buf := binarySerializer.Borrow()
169+
err := writeBlockHeaderBuf(w, pver, bh, buf)
170+
binarySerializer.Return(buf)
171+
return err
172+
}
173+
174+
// writeBlockHeaderBuf writes a bitcoin block header to w. See Serialize for
175+
// encoding block headers to be stored to disk, such as in a database, as
176+
// opposed to encoding for the wire.
177+
//
178+
// If b is non-nil, the provided buffer will be used for serializing small
179+
// values. Otherwise a buffer will be drawn from the binarySerializer's pool
180+
// and return when the method finishes.
181+
//
182+
// NOTE: b MUST either be nil or at least an 8-byte slice.
183+
func writeBlockHeaderBuf(w io.Writer, pver uint32, bh *BlockHeader,
184+
buf []byte) error {
185+
186+
littleEndian.PutUint32(buf[:4], uint32(bh.Version))
187+
if _, err := w.Write(buf[:4]); err != nil {
188+
return err
189+
}
190+
191+
if _, err := w.Write(bh.PrevBlock[:]); err != nil {
192+
return err
193+
}
194+
195+
if _, err := w.Write(bh.MerkleRoot[:]); err != nil {
196+
return err
197+
}
198+
199+
littleEndian.PutUint32(buf[:4], uint32(bh.Timestamp.Unix()))
200+
if _, err := w.Write(buf[:4]); err != nil {
201+
return err
202+
}
203+
204+
littleEndian.PutUint32(buf[:4], bh.Bits)
205+
if _, err := w.Write(buf[:4]); err != nil {
206+
return err
207+
}
208+
209+
littleEndian.PutUint32(buf[:4], bh.Nonce)
210+
if _, err := w.Write(buf[:4]); err != nil {
211+
return err
212+
}
213+
214+
return nil
122215
}

0 commit comments

Comments
 (0)