MCPcopy
hub / github.com/jimmysong/programmingbitcoin / read_varint

Function read_varint

code-ch08/helper.py:74–88  ·  view source on GitHub ↗

read_varint reads a variable integer from a stream

(s)

Source from the content-addressed store, hash-verified

72
73
74def read_varint(s):
75 '''read_varint reads a variable integer from a stream'''
76 i = s.read(1)[0]
77 if i == 0xfd:
78 # 0xfd means the next two bytes are the number
79 return little_endian_to_int(s.read(2))
80 elif i == 0xfe:
81 # 0xfe means the next four bytes are the number
82 return little_endian_to_int(s.read(4))
83 elif i == 0xff:
84 # 0xff means the next eight bytes are the number
85 return little_endian_to_int(s.read(8))
86 else:
87 # anything else is just the integer
88 return i
89
90
91def encode_varint(i):

Callers 2

parseMethod · 0.90
parseMethod · 0.90

Calls 2

little_endian_to_intFunction · 0.70
readMethod · 0.45

Tested by

no test coverage detected