Skip to content

Commit

Permalink
https://github.com/neo-project/neo/pull/2101
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje committed Feb 8, 2021
1 parent c820281 commit 6cb6980
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions neo3/network/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MessageType(IntEnum):
TRANSACTION = 0x2b
BLOCK = 0x2c
CONSENSUS = 0x2d
EXTENSIBLE = 0x2e
REJECT = 0x2f

FILTERLOAD = 0x30
Expand Down
4 changes: 3 additions & 1 deletion neo3/network/payloads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
GetBlockByIndexPayload)
from .filter import FilterAddPayload, FilterLoadPayload
from .oracle import OracleReponseCode, OracleResponse
from .extensible import ExtensiblePayload

__all__ = ['EmptyPayload', 'InventoryPayload', 'InventoryType', 'VersionPayload', 'NetworkAddress',
'AddrPayload', 'PingPayload', 'Witness', 'WitnessScope', 'Header', 'Block', 'MerkleBlockPayload',
'HeadersPayload', 'ConsensusData', 'ConsensusPayload', 'Transaction', 'TransactionAttribute',
'TransactionAttributeType', 'Signer', 'GetBlocksPayload', 'GetBlockByIndexPayload', 'FilterAddPayload',
'FilterLoadPayload', 'TrimmedBlock', 'IVerifiable', 'OracleReponseCode', 'OracleReponseCode']
'FilterLoadPayload', 'TrimmedBlock', 'IVerifiable', 'OracleReponseCode', 'OracleReponseCode',
'ExtensiblePayload']
71 changes: 71 additions & 0 deletions neo3/network/payloads/extensible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from __future__ import annotations
import hashlib
from typing import List
from neo3 import storage, settings
from neo3.core import types, serialization, Size as s, utils
from neo3.network import payloads
from neo3.network.payloads import InventoryType


class ExtensiblePayload(payloads.IInventory):
def __init__(self,
category: str,
valid_block_start: int,
valid_block_end: int,
sender: types.UInt160,
data: bytes,
witness: payloads.Witness):
self.category = category
self.valid_block_start = valid_block_start
self.valid_block_end = valid_block_end
self.sender = sender
self.data = data
self.witness = witness

def __len__(self):
return (utils.get_var_size(self.category)
+ s.uint32
+ s.uint32
+ s.uint160
+ utils.get_var_size(self.data)
+ 1
+ len(self.witness))

def serialize(self, writer: serialization.BinaryWriter) -> None:
self.serialize_unsigned(writer)
writer.write_uint8(1)
writer.write_serializable(self.witness)

def serialize_unsigned(self, writer: serialization.BinaryWriter) -> None:
writer.write_var_string(self.category)
writer.write_uint32(self.valid_block_start)
writer.write_uint32(self.valid_block_end)
writer.write_serializable(self.sender)
writer.write_var_bytes(self.data)

def deserialize(self, reader: serialization.BinaryReader) -> None:
self.deserialize_unsigned(reader)
if reader.read_uint8() != 1:
raise ValueError("Deserialization error - check byte incorrect")
self.witness = reader.read_serializable(payloads.Witness)

def deserialize_unsigned(self, reader: serialization.BinaryReader) -> None:
self.category = reader.read_var_string(32)
self.valid_block_start = reader.read_uint32()
self.valid_block_end = reader.read_uint32()
if self.valid_block_start > self.valid_block_end:
raise ValueError("Deserialization error - valid_block_starts is bigger than valid_block_end")
self.sender = reader.read_serializable(types.UInt160)
self.data = reader.read_var_bytes(0xFFFF)

def hash(self) -> types.UInt256:
intermediate_data = hashlib.sha256(self.get_hash_data(settings.network.magic)).digest()
data = hashlib.sha256(intermediate_data).digest()
return types.UInt256(data)

@property
def inventory_type(self) -> InventoryType:
return InventoryType.EXTENSIBLE

def get_script_hashes_for_verifying(self, snapshot: storage.Snapshot) -> List[types.UInt160]:
return [self.sender]
1 change: 1 addition & 0 deletions neo3/network/payloads/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class InventoryType(IntEnum):
TX = 0x2b
BLOCK = 0x2c
CONSENSUS = 0x2d
EXTENSIBLE = 0x2e


class InventoryPayload(serialization.ISerializable):
Expand Down

0 comments on commit 6cb6980

Please sign in to comment.