From 7f783bf26cabb62eb5e060eecff6dd97e46a9c39 Mon Sep 17 00:00:00 2001 From: Rob Myers Date: Mon, 12 May 2014 19:35:09 -0700 Subject: [PATCH] Custom and numbered work examples updated to work without byte arrays. Revisit this if/when byte array returns work in pyethereum. --- serpent/custom_works.se | 37 ++++++++++++++++++++----------------- serpent/numbered_works.se | 18 +++++------------- tests/test_custom_works.py | 22 ++++++++++++---------- tests/test_numbered_works.py | 19 +++++++++++++------ 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/serpent/custom_works.se b/serpent/custom_works.se index df4485a..cb1ba11 100644 --- a/serpent/custom_works.se +++ b/serpent/custom_works.se @@ -1,7 +1,7 @@ -HEX = "0123456789ABCDEF" -ARTWORK.BASE = "" -ARTWORK.LENGTH = 80 -ARTWORK.INSERT = 65 +HEX = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"] +ARTWORK = [""] +ARTWORK.LENGTH = 10 +ARTWORK.INSERT.END = 8 // Make sure we have enough gas to run the contact if tx.value < tx.basefee * 500: @@ -11,20 +11,23 @@ if tx.value < tx.basefee * 500: // If there are enough arguments // and the command to create the work is being given if msg.datasize == 1 and msg.data[0] == "create": + artwork = array(ARTWORK.LENGTH) + artwork[0] = ARTWORK[0] + artwork[1] = ARTWORK[1] + artwork[2] = ARTWORK[2] + artwork[9] = ARTWORK[3] // Copy the most significant hex bytes of the key as an html colour - work = ARTWORK.BASE - work[ARTWORK.INSERT] = HEX[msg.sender[0] & 0x0F] - work[ARTWORK.INSERT + 1] = HEX[msg.sender[0] >> 4] - work[ARTWORK.INSERT + 2] = HEX[msg.sender[1] & 0x0F] - work[ARTWORK.INSERT + 3] = HEX[msg.sender[1] >> 4] - work[ARTWORK.INSERT + 4] = HEX[msg.sender[2] & 0x0F] - work[ARTWORK.INSERT + 5] = HEX[msg.sender[2] >> 4] - return(work, ARTWORK.LENGTH) -// If there are enough arguments -// and the command to get the artwork return size is given -if msg.datasize == 1 and msg.data[0] == "size": - // Return the size of the artwork that will be created - return(ARTWORK.LENGTH) + index = 0 + hash.bytes = msg.sender + while index < 3: + current.byte = hash.bytes % 256 + hash.bytes = hash.bytes / 256 + hi = HEX[current.byte / 16] + lo = HEX[current.byte % 16] + artwork[ARTWORK.INSERT.END - (index * 2)] = lo + artwork[ARTWORK.INSERT.END - (index * 2) + 1] = hi + index = index + 1 + return(artwork, ARTWORK.LENGTH) // Otherwise else: // Logical false for failure diff --git a/serpent/numbered_works.se b/serpent/numbered_works.se index 55e8df1..4d819ac 100644 --- a/serpent/numbered_works.se +++ b/serpent/numbered_works.se @@ -1,27 +1,19 @@ init: ARTWORK.NUMBER.INDEX = 1001 - contract.storage[ARTWORK.NUMBER.INDEX] = 0 + contract.storage[ARTWORK.NUMBER.INDEX] = 1 code: - ARTWORK.BASE = "Work # " - ARTWORK.LENGTH = 26 - ARTWORK.INSERT = 6 + ARTWORK = "Work #" ARTWORK.NUMBER.INDEX = 1001 + // Make sure we have enough gas to run the contact if tx.value < tx.basefee * 400: // If not, stop stop // Get the number of the work to produce - num = contract.storage[ARTWORK.NUMBER.INDEX] - // And format it into the string containing the artwork - index = ARTWORK.INSERT - work = ARTWORK.BASE - while num: - work[index] = "0123456789"[num % 10] - num = num / 10 - index = index + 1 + number = contract.storage[ARTWORK.NUMBER.INDEX] // Store the number to use for the next work contract.storage[ARTWORK.NUMBER.INDEX] = contract.storage[ARTWORK.NUMBER.INDEX] + 1 // Return the work - return(work) + return([ARTWORK, number], 2) diff --git a/tests/test_custom_works.py b/tests/test_custom_works.py index 6c12a2b..e6ca908 100644 --- a/tests/test_custom_works.py +++ b/tests/test_custom_works.py @@ -5,24 +5,26 @@ class TestCustomWorks(object): ARTIST = Key('artist') # 8802b7f0bfa5e9f5825f2fc708e1ad00d2c2b5d6 BEHOLDER = Key('beholder') # 7e5188934964c0c267839653f7a49c879c2c8dfc - ARTWORK_LENGTH = 130 + ARTWORK_FOR_BEHOLDER = """""" @classmethod def setup_class(cls): - cls.code = load_serpent('serpent/owned_stored_work.se') + cls.code = load_serpent('serpent/custom_works.se') cls.sim = Simulator({cls.ARTIST.address: 10**18, cls.BEHOLDER.address: 10**18}) - @classmethod - def setup_class(cls): - cls.code = load_serpent('serpent/custom_works.se') - cls.sim = Simulator({cls.ARTIST.address: 10**18}) - def setup_method(self, method): self.sim.reset() self.contract = self.sim.load_contract(self.ARTIST, self.code) - def test_size(self): - data = ["size"] + def test_do_nothing(self): + data = [] + response = self.sim.tx(self.BEHOLDER, self.contract, 0, data) + assert response[0] == 0 + + def test_create_work(self): + data = ["create"] response = self.sim.tx(self.BEHOLDER, self.contract, 0, data) - assert response[0] == self.ARTWORK_LENGTH + artwork = "".join([coerce_to_bytes(fragment) for fragment in response]) + assert artwork == self.ARTWORK_FOR_BEHOLDER + diff --git a/tests/test_numbered_works.py b/tests/test_numbered_works.py index 8fb9059..dd9b434 100644 --- a/tests/test_numbered_works.py +++ b/tests/test_numbered_works.py @@ -3,16 +3,17 @@ from pyethereum.utils import coerce_to_bytes class TestNumberedWorks(object): - ARTIST = Key('artist') - AUDIENCE = Key('audience') + ARTIST = Key('artist') # 8802b7f0bfa5e9f5825f2fc708e1ad00d2c2b5d6 + BEHOLDER = Key('beholder') # 7e5188934964c0c267839653f7a49c879c2c8dfc INDEX1001 = coerce_to_bytes(1001) - ARTWORK_LENGTH = 26 - ARTWORK_INSERT = 6 + ARTWORK = "Work #" + ARTWORK_NUMBER = 1 @classmethod def setup_class(cls): cls.code = load_serpent('serpent/numbered_works.se') - cls.sim = Simulator({cls.ARTIST.address: 10**18}) + cls.sim = Simulator({cls.ARTIST.address: 10**18, + cls.BEHOLDER.address: 10**18}) def setup_method(self, method): self.sim.reset() @@ -20,4 +21,10 @@ class TestNumberedWorks(object): def test_initial_state(self): # Get storage data only returns int... - assert self.sim.get_storage_data(self.contract, self.INDEX1001) == 0 + assert self.sim.get_storage_data(self.contract, self.INDEX1001) == 1 + + def test_create_work(self): + data = [] + response = self.sim.tx(self.BEHOLDER, self.contract, 0, data) + assert coerce_to_bytes(response[0]) == self.ARTWORK + assert response[1] == self.ARTWORK_NUMBER -- 2.1.4