Skip to content

Fix deep copy of plutus data #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pycardano/plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ def from_json(cls: Type[PlutusData], data: str) -> PlutusData:
obj = json.loads(data)
return cls.from_dict(obj)

def __deepcopy__(self, memo):
return self.__class__.from_cbor(self.to_cbor())


@dataclass
class RawPlutusData(CBORSerializable):
Expand All @@ -692,6 +695,9 @@ def _dfs(obj):
def from_primitive(cls: Type[RawPlutusData], value: CBORTag) -> RawPlutusData:
return cls(value)

def __deepcopy__(self, memo):
return self.__class__.from_cbor(self.to_cbor())


Datum = Union[PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, RawPlutusData]
"""Plutus Datum type. A Union type that contains all valid datum types."""
Expand Down
32 changes: 32 additions & 0 deletions test/pycardano/test_plutus.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import copy
import unittest
from dataclasses import dataclass
from test.pycardano.util import check_two_way_cbor
from typing import Dict, List, Union

import pytest
from cbor2 import CBORTag

from pycardano.exception import DeserializeException, SerializeException
from pycardano.plutus import (
Expand Down Expand Up @@ -284,3 +286,33 @@ def test_raw_plutus_data():
raw_plutus_data = RawPlutusData.from_cbor(raw_plutus_cbor)
assert raw_plutus_data.to_cbor() == raw_plutus_cbor
check_two_way_cbor(raw_plutus_data)


def test_clone_raw_plutus_data():
tag = RawPlutusData(CBORTag(121, [1000]))

cloned_tag = copy.deepcopy(tag)
assert cloned_tag == tag
assert cloned_tag.to_cbor() == tag.to_cbor()

tag.data.value = [1001]

assert cloned_tag != tag


def test_clone_plutus_data():
key_hash = bytes.fromhex("c2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a")
deadline = 1643235300000
testa = BigTest(MyTest(123, b"1234", IndefiniteList([4, 5, 6]), {1: b"1", 2: b"2"}))
testb = LargestTest()
my_vesting = VestingParam(
beneficiary=key_hash, deadline=deadline, testa=testa, testb=testb
)

cloned_vesting = copy.deepcopy(my_vesting)
assert cloned_vesting == my_vesting
assert cloned_vesting.to_cbor() == my_vesting.to_cbor()

my_vesting.deadline = 1643235300001

assert cloned_vesting != my_vesting