Reference¶
Information about methods and functions
bmt_py
¶
Binary Merkle Tree operations on data
Chunk
¶
Bases: BaseModel
- General chunk class for Swarm
It stores the serialized data and provides functions to access the fields of a chunk.
It also provides an address function to calculate the address of the chunk that is required for the Chunk API.
Attributes:
Name | Type | Description |
---|---|---|
payload |
bytes
|
FlexBytes |
max_payload_length |
int
|
int |
span_length |
int
|
int |
data |
Callable[[], bytes]
|
ChunkData |
span |
Callable[[], bytes]
|
Bytes |
address |
Callable[[], bytes]
|
ChunkAddress |
inclusion_proof |
Callable[[int], list[bytes]]
|
List[Any] |
bmt |
Callable[[], bytes]
|
List[Any] |
Notes
This class extends Pydantic's :class:BaseModel
.
The inputs for missing attributes will be set to None if not provided during object creation.
ChunkInclusionProof
¶
Bases: BaseModel
Represents the inclusion proof for a chunk, including the span and sister segments.
ChunkedFile
¶
Bases: BaseModel
- ChunkedFile class for Swarm
It stores the serialized data and provides functions to access the fields of a chunked file.
It also provides functions to calculate the BMT and inclusion proof for a given chunk and its index.
Attributes:
Name | Type | Description |
---|---|---|
leaf_chunks |
Callable[[], list]
|
list[Chunk] |
root_chunk |
Callable
|
Chunk |
payload |
bytes
|
bytes |
address |
Callable[[], ChunkAddress]
|
ChunkAddress |
span |
Callable[[], bytes]
|
bytes |
bmt |
Callable[[], list[list[Chunk]]]
|
list[list[Chunk]] |
Notes
This class extends Pydantic's :class:BaseModel
.
The inputs for missing attributes will be set to None if not provided during object creation.
FlexBytes
¶
Bases: BaseModel
, Generic[Min, Max]
Helper type for dealing with flexible sized byte arrays.
The actual min and and max values are not stored in runtime, they are only there to differentiate the type from the Uint8Array at compile time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Min |
int
|
The minimum length of the byte array. |
required |
Max |
int
|
The maximum length of the byte array. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the length of the byte array is not within the specified range. |
assert_flex_bytes(b, min_length, max_length)
¶
Asserts that b
is a valid FlexBytes with minimum length min_length
and maximum length max_length
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b |
Any
|
The value to check. |
required |
min_length |
int
|
The minimum length. |
required |
max_length |
int
|
The maximum length. |
required |
Returns:
boolean: True if b
is a valid FlexBytes
Raises:
TypeError: If b
is not a valid FlexBytes.
bytes_equal(a, b)
¶
bytes_to_hex(inp, length=None)
¶
Converts a byte array to a hexadecimal string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
Union[bytes, str, bytearray]
|
The byte array to convert. |
required |
length |
Optional[int]
|
The length of the resulting hex string in bytes. |
None
|
Returns:
Type | Description |
---|---|
str
|
A hexadecimal string representing the byte array. |
Raises:
Type | Description |
---|---|
ValueError
|
If the length of the resulting hex string does not match the specified length. |
file_address_from_inclusion_proof(prove_chunks, prove_segment, prove_segment_index, max_chunk_payload_byte_length=4096)
¶
Calculates the file address based on the inclusion proof segments and the proved segment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prove_chunks |
list[ChunkInclusionProof]
|
A list of chunk inclusion proofs |
required |
prove_segment |
bytes
|
A bytes representing the proved segment |
required |
prove_segment_index |
int
|
The segment index of the proved segment |
required |
max_chunk_payload_byte_length |
int
|
Maximum byte length of a chunk. Default is 4096 |
4096
|
Returns: The calculated file address
file_inclusion_proof_bottom_up(chunked_file, segment_index)
¶
Gives back required sister segments of a given payload segment index for inclusion proof.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunked_file |
ChunkedFile
|
The chunked file object |
required |
segment_index |
int
|
The segment index of the payload |
required |
Returns: A list of chunk inclusion proofs
get_bmt_index_of_segment(segment_index, last_chunk_index, max_chunk_payload_byte_length=4096)
¶
Get the chunk's position of a given payload segment index in the BMT tree. Args: segment_index: The segment index of the payload last_chunk_index: The last chunk index on the BMT level of the segment max_chunk_payload_byte_length: Maximum byte length of a chunk. Default is 4096 Returns: A tuple of 'level' and 'chunk_index'
get_span_value(span)
¶
is_flex_bytes(b, flex_bytes)
¶
keccak256_hash(*messages)
¶
make_chunked_file(payload, options=None)
¶
make_span(value, length=DEFAULT_SPAN_SIZE)
¶
Creates a span for storing the length of a chunk.
The length is encoded in 64-bit little endian format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
int
|
Value of the span |
required |
length |
int
|
The length of the chunk. |
DEFAULT_SPAN_SIZE
|
Returns:
Name | Type | Description |
---|---|---|
bytes |
bytes
|
The span representing the chunk length. |
root_hash_from_inclusion_proof(proof_segments, prove_segment, prove_segment_index, options=None)
¶
Calculates the BMT root hash from the provided inclusion proof segments and its corresponding segment index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proof_segments |
list
|
List of inclusion proof segments as bytes objects |
required |
prove_segment |
bytes
|
The segment to be proven as bytes |
required |
prove_segment_index |
int
|
The index of the segment to be proven |
required |
options |
Optional[dict]
|
Function configurations, including a custom hash function |
None
|
Returns:
Type | Description |
---|---|
bytes
|
The calculated BMT root hash as bytes |