2222from hathorlib .headers .types import VertexHeaderId
2323from hathorlib .nanocontracts import DeprecatedNanoContract
2424from hathorlib .nanocontracts .types import NCActionType
25- from hathorlib .utils import int_to_bytes , unpack , unpack_len
25+ from hathorlib .utils import decode_unsigned , encode_unsigned , int_to_bytes , unpack , unpack_len
2626
2727if TYPE_CHECKING :
2828 from hathorlib .base_transaction import BaseTransaction
2929
30- NC_VERSION = 1
3130NC_INITIALIZE_METHOD = 'initialize'
3231ADDRESS_LEN_BYTES = 25
32+ ADDRESS_SEQNUM_SIZE : int = 8 # bytes
33+ _NC_SCRIPT_LEN_MAX_BYTES : int = 2
3334
3435
3536@dataclass (frozen = True )
@@ -43,6 +44,9 @@ class NanoHeaderAction:
4344class NanoHeader (VertexBaseHeader ):
4445 tx : BaseTransaction
4546
47+ # Sequence number for the caller.
48+ nc_seqnum : int
49+
4650 # nc_id equals to the blueprint_id when a Nano Contract is being created.
4751 # nc_id equals to the nanocontract_id when a method is being called.
4852 nc_id : bytes
@@ -59,8 +63,6 @@ class NanoHeader(VertexBaseHeader):
5963 nc_address : bytes
6064 nc_script : bytes
6165
62- nc_version : int = NC_VERSION
63-
6466 @classmethod
6567 def _deserialize_action (cls , buf : bytes ) -> tuple [NanoHeaderAction , bytes ]:
6668 from hathorlib .base_transaction import bytes_to_output_value
@@ -78,11 +80,9 @@ def _deserialize_action(cls, buf: bytes) -> tuple[NanoHeaderAction, bytes]:
7880 def deserialize (cls , tx : BaseTransaction , buf : bytes ) -> tuple [NanoHeader , bytes ]:
7981 header_id , buf = buf [:1 ], buf [1 :]
8082 assert header_id == VertexHeaderId .NANO_HEADER .value
81- (nc_version ,), buf = unpack ('!B' , buf )
82- if nc_version != NC_VERSION :
83- raise ValueError ('unknown nanocontract version: {}' .format (nc_version ))
8483
8584 nc_id , buf = unpack_len (32 , buf )
85+ nc_seqnum , buf = decode_unsigned (buf , max_bytes = ADDRESS_SEQNUM_SIZE )
8686 (nc_method_len ,), buf = unpack ('!B' , buf )
8787 nc_method , buf = unpack_len (nc_method_len , buf )
8888 (nc_args_bytes_len ,), buf = unpack ('!H' , buf )
@@ -96,14 +96,14 @@ def deserialize(cls, tx: BaseTransaction, buf: bytes) -> tuple[NanoHeader, bytes
9696 nc_actions .append (action )
9797
9898 nc_address , buf = unpack_len (ADDRESS_LEN_BYTES , buf )
99- ( nc_script_len ,), buf = unpack ( '!H' , buf )
99+ nc_script_len , buf = decode_unsigned ( buf , max_bytes = _NC_SCRIPT_LEN_MAX_BYTES )
100100 nc_script , buf = unpack_len (nc_script_len , buf )
101101
102102 decoded_nc_method = nc_method .decode ('ascii' )
103103
104104 return cls (
105105 tx = tx ,
106- nc_version = nc_version ,
106+ nc_seqnum = nc_seqnum ,
107107 nc_id = nc_id ,
108108 nc_method = decoded_nc_method ,
109109 nc_args_bytes = nc_args_bytes ,
@@ -127,8 +127,8 @@ def _serialize_without_header_id(self, *, skip_signature: bool) -> deque[bytes]:
127127 encoded_method = self .nc_method .encode ('ascii' )
128128
129129 ret : deque [bytes ] = deque ()
130- ret .append (int_to_bytes (NC_VERSION , 1 ))
131130 ret .append (self .nc_id )
131+ ret .append (encode_unsigned (self .nc_seqnum , max_bytes = ADDRESS_SEQNUM_SIZE ))
132132 ret .append (int_to_bytes (len (encoded_method ), 1 ))
133133 ret .append (encoded_method )
134134 ret .append (int_to_bytes (len (self .nc_args_bytes ), 2 ))
@@ -141,10 +141,10 @@ def _serialize_without_header_id(self, *, skip_signature: bool) -> deque[bytes]:
141141
142142 ret .append (self .nc_address )
143143 if not skip_signature :
144- ret .append (int_to_bytes (len (self .nc_script ), 2 ))
144+ ret .append (encode_unsigned (len (self .nc_script ), max_bytes = _NC_SCRIPT_LEN_MAX_BYTES ))
145145 ret .append (self .nc_script )
146146 else :
147- ret .append (int_to_bytes (0 , 2 ))
147+ ret .append (encode_unsigned (0 , max_bytes = _NC_SCRIPT_LEN_MAX_BYTES ))
148148 return ret
149149
150150 def serialize (self ) -> bytes :
0 commit comments