Skip to content

Commit

Permalink
Fix address type for call_sc operations
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-François <[email protected]>
  • Loading branch information
bilboquet committed Nov 24, 2023
1 parent 30d9948 commit f0f8468
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
96 changes: 55 additions & 41 deletions massa_test_framework/massa_py/operations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from abc import ABC, abstractmethod
from typing import Dict

Expand All @@ -19,11 +18,11 @@ class Typed(ABC):
@classmethod
def type_id(cls) -> int:
TYPE_ID = {
'Transaction': 0,
'RollBuy': 1,
'RollSell': 2,
'ExecuteSC': 3,
'CallSC': 4
"Transaction": 0,
"RollBuy": 1,
"RollSell": 2,
"ExecuteSC": 3,
"CallSC": 4,
}
type_id = TYPE_ID.get(cls.__name__, -1)
if type_id == -1:
Expand All @@ -43,7 +42,7 @@ def push(self, key: bytes, value: bytes):
self.datastore[key] = value

def serialize(self) -> bytes:
enc_datastore = b''
enc_datastore = b""
# number of key-value pairs
enc_datastore += varint.encode(len(self.datastore))
for key, value in self.datastore.items():
Expand All @@ -67,7 +66,7 @@ def serialize(self) -> bytes:
enc_op = self.op.serialize()
return bytes(enc_fee + enc_expire_period + enc_type_id + enc_op)

def sign(self, creator_public_key, sender_private_key) -> bytes:
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
Expand All @@ -82,11 +81,14 @@ def sign(self, creator_public_key, sender_private_key) -> bytes:
return signature_b58


class OperationInput():
def __init__(self, creator_public_key, content: Operation, sender_private_key):
class OperationInput:
def __init__(
self, creator_public_key: str, content: Operation, sender_private_key: str
):
self.creator_public_key = creator_public_key
self.signature = content.sign(
creator_public_key, sender_private_key).decode("utf-8")
self.signature = content.sign(creator_public_key, sender_private_key).decode(
"utf-8"
)
self.serialized_content = list(content.serialize())


Expand Down Expand Up @@ -116,7 +118,7 @@ def __init__(
self.amount = amount

def serialize(self) -> bytes:
# guess varint.encode(0) forces user address
# varint.encode(0) forces user address
recipient_address = varint.encode(0) + base58.b58decode_check(
self.recipient_address[2:]
)
Expand All @@ -137,17 +139,20 @@ def serialize(self) -> bytes:
enc_data_len = varint.encode(len(self.data))
enc_data = self.data
enc_datastore = self.datastore.serialize()
return bytes(enc_max_gas + enc_max_coins + enc_data_len + enc_data + enc_datastore)
return bytes(
enc_max_gas + enc_max_coins + enc_data_len + enc_data + enc_datastore
)


class CallSC(InnerOp):
def __init__(
self,
target_address,
target_func: str,
param: bytes,
max_gas: int,
coins: int):
self,
target_address: str,
target_func: str,
param: bytes,
max_gas: int,
coins: int,
):
self.target_address = target_address
self.target_func = target_func
self.param = param
Expand All @@ -157,18 +162,28 @@ def __init__(
def serialize(self) -> bytes:
enc_max_gas = varint.encode(int(self.max_gas))
enc_coins = varint.encode(int(self.coins))
target_address = varint.encode(0) + base58.b58decode_check(
# TODO Add an Address class that serialize according to the type of address
# varint.encode(1) forces smart contract address
target_address = varint.encode(1) + base58.b58decode_check(
self.target_address[2:]
)
target_func = self.target_func.encode('utf-8')
target_func = self.target_func.encode("utf-8")
target_func_len_enc = varint.encode(len(target_func))
param_len_enc = varint.encode(len(self.param))
return bytes(enc_max_gas + enc_coins + target_address + target_func_len_enc + target_func + param_len_enc + self.param)
return bytes(
enc_max_gas
+ enc_coins
+ target_address
+ target_func_len_enc
+ target_func
+ param_len_enc
+ self.param
)


def create_roll_buy(
sender_private_key,
creator_public_key,
sender_private_key: str,
creator_public_key: str,
fee: int,
expire_period: int,
roll_count: int,
Expand All @@ -179,8 +194,8 @@ def create_roll_buy(


def create_roll_sell(
sender_private_key,
creator_public_key,
sender_private_key: str,
creator_public_key: str,
fee: int,
expire_period: int,
roll_count: int,
Expand All @@ -191,47 +206,46 @@ def create_roll_sell(


def create_transaction(
sender_private_key,
creator_public_key,
sender_private_key: str,
creator_public_key: str,
fee: int,
expire_period: int,
recipient_address,
recipient_address: str,
amount: int,
):
op = Operation(fee, expire_period,
Transaction(recipient_address, amount))
op = Operation(fee, expire_period, Transaction(recipient_address, amount))
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__


def create_call_sc(
sender_private_key,
creator_public_key,
sender_private_key: str,
creator_public_key: str,
fee: int,
expire_period: int,
target_address,
target_address: str,
target_func: str,
param: bytes,
max_gas: int,
coins: int,
):
op = Operation(fee, expire_period,
CallSC(target_address, target_func, param, max_gas, coins))
op = Operation(
fee, expire_period, CallSC(target_address, target_func, param, max_gas, coins)
)
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__


def create_execute_sc(
sender_private_key,
creator_public_key,
sender_private_key: str,
creator_public_key: str,
fee: int,
expire_period: int,
data: bytes,
max_gas: int,
max_coins: int,
datastore: Datastore,
):
op = Operation(fee, expire_period,
ExecuteSC(data, max_gas, max_coins, datastore))
op = Operation(fee, expire_period, ExecuteSC(data, max_gas, max_coins, datastore))
op_in = OperationInput(creator_public_key, op, sender_private_key)
return op_in.__dict__
8 changes: 4 additions & 4 deletions massa_test_framework/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import requests
import tomlkit


class Node:
def __init__(self, server: Server, compile_unit: CompileUnit):
"""Init a Node (e.g. a Massa Node) object
Expand Down Expand Up @@ -267,7 +266,7 @@ def stop(self, process):
def edit_config(self):
"""Edit config.toml (as a context manager). Must be called before start()"""
# print("Editing config", self.config_path)
fp = self.server.open(self.config_files["config.toml"], "r+")
fp = self.server.open(str(self.config_files["config.toml"]), "r+")
cfg = tomlkit.load(fp)
try:
yield cfg
Expand All @@ -281,6 +280,7 @@ def edit_config(self):
# print("is fp closed:", fp.closed)
# print("end of edit_config")


@contextmanager
def edit_json(self, json_filepath: Path, mode: str = "r+", default_json=None):
fp = self.server.open(json_filepath, mode)
Expand Down Expand Up @@ -499,9 +499,9 @@ def get_stakers_grpc(self) -> GetStakersResponse:
self.grpc_host, self.pub_grpc_port, "get_stakers", GetStakersRequest()
)
)

return get_stakers_response

def wait_ready(self, timeout=20) -> None:
"""Wait for node to be ready
Expand Down

0 comments on commit f0f8468

Please sign in to comment.