MCPcopy Create free account
hub / github.com/ElementsProject/lightning / bitcoin_block_from_hex

Function bitcoin_block_from_hex

bitcoin/block.c:138–223  ·  view source on GitHub ↗

Encoding is ... */

Source from the content-addressed store, hash-verified

136
137/* Encoding is <blockhdr> <varint-num-txs> <tx>... */
138struct bitcoin_block *
139bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
140 const char *hex, size_t hexlen)
141{
142 struct bitcoin_block *b;
143 u8 *linear_tx;
144 const u8 *p;
145 size_t len, i, num, templen;
146 struct sha256_ctx shactx;
147 bool is_dynafed;
148 u32 height;
149
150 if (hexlen && hex[hexlen-1] == '\n')
151 hexlen--;
152
153 /* Set up the block for success. */
154 b = tal(ctx, struct bitcoin_block);
155
156 /* De-hex the array. */
157 len = hex_data_size(hexlen);
158 p = linear_tx = tal_arr(ctx, u8, len);
159 if (!hex_decode(hex, hexlen, linear_tx, len))
160 return tal_free(b);
161
162 sha256_init(&shactx);
163
164 b->hdr.version = pull_le32(&p, &len);
165 sha256_le32(&shactx, b->hdr.version);
166
167 pull(&p, &len, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
168 sha256_update(&shactx, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
169
170 pull(&p, &len, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
171 sha256_update(&shactx, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
172
173 b->hdr.timestamp = pull_le32(&p, &len);
174 sha256_le32(&shactx, b->hdr.timestamp);
175
176 if (is_elements(chainparams)) {
177 /* A dynafed block is signalled by setting the MSB of the version. */
178 is_dynafed = (b->hdr.version >> 31 == 1);
179
180 /* elements_header.height */
181 height = pull_le32(&p, &len);
182 sha256_le32(&shactx, height);
183
184 if (is_dynafed) {
185 bitcoin_block_pull_dynafed_details(&p, &len, &shactx);
186 } else {
187 /* elemens_header.challenge */
188 templen = pull_varint(&p, &len);
189 sha256_varint(&shactx, templen);
190 sha256_update(&shactx, p, templen);
191 pull(&p, &len, NULL, templen);
192
193 /* elements_header.solution. Not hashed since it'd be
194 * a circular dependency. */
195 templen = pull_varint(&p, &len);

Callers 3

mainFunction · 0.85
block_from_responseFunction · 0.85

Calls 15

hex_data_sizeFunction · 0.85
hex_decodeFunction · 0.85
tal_freeFunction · 0.85
sha256_initFunction · 0.85
pull_le32Function · 0.85
sha256_le32Function · 0.85
pullFunction · 0.85
sha256_updateFunction · 0.85
is_elementsFunction · 0.85
pull_varintFunction · 0.85
sha256_varintFunction · 0.85

Tested by 1

mainFunction · 0.68