MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / raw_iter

Method raw_iter

test/functional/test_framework/script.py:480–534  ·  view source on GitHub ↗

Raw iteration Yields tuples of (opcode, data, sop_idx) so that the different possible PUSHDATA encodings can be accurately distinguished, as well as determining the exact opcode byte indexes. (sop_idx)

(self)

Source from the content-addressed store, hash-verified

478 return super().__new__(cls, b''.join(coerce_iterable(value)))
479
480 def raw_iter(self):
481 """Raw iteration
482
483 Yields tuples of (opcode, data, sop_idx) so that the different possible
484 PUSHDATA encodings can be accurately distinguished, as well as
485 determining the exact opcode byte indexes. (sop_idx)
486 """
487 i = 0
488 while i < len(self):
489 sop_idx = i
490 opcode = CScriptOp(self[i])
491 i += 1
492
493 if opcode > OP_PUSHDATA4:
494 yield (opcode, None, sop_idx)
495 else:
496 datasize = None
497 pushdata_type = None
498 if opcode < OP_PUSHDATA1:
499 pushdata_type = 'PUSHDATA(%d)' % opcode
500 datasize = opcode
501
502 elif opcode == OP_PUSHDATA1:
503 pushdata_type = 'PUSHDATA1'
504 if i >= len(self):
505 raise CScriptInvalidError('PUSHDATA1: missing data length')
506 datasize = self[i]
507 i += 1
508
509 elif opcode == OP_PUSHDATA2:
510 pushdata_type = 'PUSHDATA2'
511 if i + 1 >= len(self):
512 raise CScriptInvalidError('PUSHDATA2: missing data length')
513 datasize = self[i] + (self[i + 1] << 8)
514 i += 2
515
516 elif opcode == OP_PUSHDATA4:
517 pushdata_type = 'PUSHDATA4'
518 if i + 3 >= len(self):
519 raise CScriptInvalidError('PUSHDATA4: missing data length')
520 datasize = self[i] + (self[i + 1] << 8) + (self[i + 2] << 16) + (self[i + 3] << 24)
521 i += 4
522
523 else:
524 assert False # shouldn't happen
525
526 data = bytes(self[i:i + datasize])
527
528 # Check for truncation
529 if len(data) < datasize:
530 raise CScriptTruncatedPushDataError('%s: truncated data' % pushdata_type, data)
531
532 i += datasize
533
534 yield (opcode, data, sop_idx)
535
536 def __iter__(self):
537 """'Cooked' iteration

Callers 4

__iter__Method · 0.95
GetSigOpCountMethod · 0.95
FindAndDeleteFunction · 0.80

Calls 3

CScriptOpClass · 0.85
CScriptInvalidErrorClass · 0.85

Tested by

no test coverage detected