Skip to content

Commit

Permalink
chainID (#24)
Browse files Browse the repository at this point in the history
* add chain id to operations

* comment pr

* move chainID to init op
  • Loading branch information
modship authored Dec 22, 2023
1 parent f9037c7 commit cb83cb2
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions massa_test_framework/massa_py/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import base58
import varint
from blake3 import blake3
import struct


class Serializable(ABC):
Expand Down Expand Up @@ -66,10 +67,11 @@ def serialize(self) -> bytes:


class Operation(Serializable):
def __init__(self, fee: int, expire_period: int, op: InnerOp):
def __init__(self, fee: int, expire_period: int, op: InnerOp, chainID: int):
self.fee = fee
self.expire_period = expire_period
self.op = op
self.chainID = chainID

def serialize(self) -> bytes:
enc_fee = varint.encode(int(self.fee))
Expand All @@ -78,10 +80,15 @@ def serialize(self) -> bytes:
enc_op = self.op.serialize()
return bytes(enc_fee + enc_expire_period + enc_type_id + enc_op)

# massa doc https://docs.massa.net/docs/learn/operation-format-execution
def sign(self, creator_public_key: str, sender_private_key: str) -> bytes:
enc_data = self.serialize()
enc_sender_pub_key = decode_pubkey_to_bytes(creator_public_key)
enc_data = enc_sender_pub_key + enc_data
enc_sender_pub_key = decode_pubkey_to_bytes(creator_public_key)
# doc https://docs.massa.net/docs/learn/operation-format-execution
# > -> big-endian
# Q -> unsigned long long
enc_data = struct.pack('>Q', self.chainID) + enc_sender_pub_key + enc_data

# Hash
enc_data = blake3(enc_data).digest()

Expand Down Expand Up @@ -196,8 +203,9 @@ def create_roll_buy(
fee: int,
expire_period: int,
roll_count: int,
chainID: int,
):
op = Operation(fee, expire_period, RollBuy(roll_count))
op = Operation(fee, expire_period, RollBuy(roll_count), chainID)
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__

Expand All @@ -208,8 +216,9 @@ def create_roll_sell(
fee: int,
expire_period: int,
roll_count: int,
chainID: int,
):
op = Operation(fee, expire_period, RollSell(roll_count))
op = Operation(fee, expire_period, RollSell(roll_count), chainID)
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__

Expand All @@ -221,8 +230,9 @@ def create_transaction(
expire_period: int,
recipient_address: str,
amount: int,
chainID: int,
):
op = Operation(fee, expire_period, Transaction(recipient_address, amount))
op = Operation(fee, expire_period, Transaction(recipient_address, amount), chainID)
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__

Expand All @@ -237,9 +247,10 @@ def create_call_sc(
param: bytes,
max_gas: int,
coins: int,
chainID: int,
):
op = Operation(
fee, expire_period, CallSC(target_address, target_func, param, max_gas, coins)
fee, expire_period, CallSC(target_address, target_func, param, max_gas, coins), chainID
)
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__
Expand All @@ -254,7 +265,8 @@ def create_execute_sc(
max_gas: int,
max_coins: int,
datastore: Datastore,
chainID: int,
):
op = Operation(fee, expire_period, ExecuteSC(data, max_gas, max_coins, datastore))
op = Operation(fee, expire_period, ExecuteSC(data, max_gas, max_coins, datastore), chainID)
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__

0 comments on commit cb83cb2

Please sign in to comment.