MCPcopy Create free account
hub / github.com/ElementsProject/elements / raw_iter

Method raw_iter

test/functional/test_framework/script.py:563–617  ·  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

561 return super().__new__(cls, b''.join(coerce_iterable(value)))
562
563 def raw_iter(self):
564 """Raw iteration
565
566 Yields tuples of (opcode, data, sop_idx) so that the different possible
567 PUSHDATA encodings can be accurately distinguished, as well as
568 determining the exact opcode byte indexes. (sop_idx)
569 """
570 i = 0
571 while i < len(self):
572 sop_idx = i
573 opcode = self[i]
574 i += 1
575
576 if opcode > OP_PUSHDATA4:
577 yield (opcode, None, sop_idx)
578 else:
579 datasize = None
580 pushdata_type = None
581 if opcode < OP_PUSHDATA1:
582 pushdata_type = 'PUSHDATA(%d)' % opcode
583 datasize = opcode
584
585 elif opcode == OP_PUSHDATA1:
586 pushdata_type = 'PUSHDATA1'
587 if i >= len(self):
588 raise CScriptInvalidError('PUSHDATA1: missing data length')
589 datasize = self[i]
590 i += 1
591
592 elif opcode == OP_PUSHDATA2:
593 pushdata_type = 'PUSHDATA2'
594 if i + 1 >= len(self):
595 raise CScriptInvalidError('PUSHDATA2: missing data length')
596 datasize = self[i] + (self[i + 1] << 8)
597 i += 2
598
599 elif opcode == OP_PUSHDATA4:
600 pushdata_type = 'PUSHDATA4'
601 if i + 3 >= len(self):
602 raise CScriptInvalidError('PUSHDATA4: missing data length')
603 datasize = self[i] + (self[i + 1] << 8) + (self[i + 2] << 16) + (self[i + 3] << 24)
604 i += 4
605
606 else:
607 assert False # shouldn't happen
608
609 data = bytes(self[i:i + datasize])
610
611 # Check for truncation
612 if len(data) < datasize:
613 raise CScriptTruncatedPushDataError('%s: truncated data' % pushdata_type, data)
614
615 i += datasize
616
617 yield (opcode, data, sop_idx)
618
619 def __iter__(self):
620 """'Cooked' iteration

Callers 4

__iter__Method · 0.95
GetSigOpCountMethod · 0.95
FindAndDeleteFunction · 0.45

Calls 2

CScriptInvalidErrorClass · 0.70

Tested by

no test coverage detected