RomarQ/tezos-sc-utils

Variable name "compute_utils_326" already in use

nbusser opened this issue ยท 4 comments

Hi!

I'm currently writing a contract using smartpy IDE.

When I'm using your package and using twice the same function, I receive an exception Variable name "compute_utils_326" already in use

Here is a minimal example you can test on smartpy IDE.

import smartpy as sp

Utils = sp.io.import_script_from_url("https://raw.githubusercontent.com/RomarQ/tezos-sc-utils/main/smartpy/utils.py")

class Test(sp.Contract):
  def __init__(
    self,
  ):
    self.init()

  @sp.entry_point
  def test(self):
    i1 = Utils.Int.of_bytes(
      sp.bytes("0xFF")
    )

    i2 = Utils.Int.of_bytes(
      sp.bytes("0xFE")
    )

sp.add_compilation_target("Test", Test())

The program returns an error in line 326 of utils.py, i.e the first line of Int.of_bytes method, in the instruction length = sp.compute(sp.compute(sp.len(b)))

I think that smartpy is creating a local value for computed length.
When calling the function for the second time, it tries to re-create the variable with the same name.

I guess you simply forgot to use sp.local(generate_var()) in line 326

Fixed in commit: 204ddac

You can also do:
(This approach does not duplicate the code)

import smartpy as sp

Utils = sp.io.import_script_from_url("https://raw.githubusercontent.com/RomarQ/tezos-sc-utils/main/smartpy/utils.py")

class Test(sp.Contract):
    def __init__(self):
        self.init()

    @sp.private_lambda()
    def int_of_bytes(self, b):
        sp.result(Utils.Int.of_bytes(b))

    @sp.entry_point
    def test(self):
        sp.verify(self.int_of_bytes(sp.bytes("0xFF")) == 255)
        sp.verify(self.int_of_bytes(sp.bytes("0xFFFF")) == 65535)

Thank you ! I was on my way writing a PR but you were very reactive ๐Ÿ˜‰

Thank you for reporting the issue ๐Ÿ‘

Also, this operation will be supported naively in the upcoming M protocol.