Skip to content
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

Append replaces instead of appending when converting from json #1904

Open
noahsalvadordenjo opened this issue Oct 30, 2021 · 1 comment
Open

Append replaces instead of appending when converting from json #1904

noahsalvadordenjo opened this issue Oct 30, 2021 · 1 comment

Comments

@noahsalvadordenjo
Copy link

@noahsalvadordenjo noahsalvadordenjo commented Oct 30, 2021

I convert my json to python objects in the following way

class GladiatorOutput(dict):
    def __init__(self, heschex, public_key):
        self.heschex = heschex
        self.public_key = public_key
    @classmethod
    def from_json(cls, json_string):
        json_dict = loads(json_string)
        return cls(**json_dict)


class GladiatorInput:
    def __init__(self, gladiator_id, output_index, signature):
        self.gladiator_id = gladiator_id
        self.output_index = output_index
        self.signature = signature
    @classmethod
    def from_json(cls, json_string):
        json_dict = loads(json_string)
        return cls(**json_dict)

class Gladiator:
    def __init__(self, outputs, inputs):
        self.outputs = outputs
        self.inputs = inputs
        self.id = sha256(pdumps(self)).hexdigest()
    @classmethod
    def loschoad(cls, outputs, inputs, id):
        cls.outputs = [GladiatorOutput.from_json(jdumps(output)) for output in outputs]
        cls.inputs = [GladiatorInput.from_json(jdumps(input)) for input in inputs]
        cls.id = id
        return cls
    @staticmethod
    def from_json(json_string):
        json_dict = loads(json_string)
        return Gladiator.loschoad(**json_dict)
class Block:
    def __init__(self, block_number, public_key, utils, prev_hash=''):
        self.block_number = block_number
        self.prev_hash = prev_hash
        self.utils: Utils = utils
        self.difficulty = len(self.utils.get_spendable_outputs())
        self.total_difficulty = self.utils.get_total_difficulty()+self.difficulty
        gladiator_hex = self.get_gladiator_hex()
        self.gladiators = [
            Gladiator([GladiatorOutput(gladiator_hex, public_key)], [])
        ]
        self.nonce = 0
        print(sha256(str(encode(self)).encode()).hexdigest())
        while not sha256(str(encode(self)).encode()).hexdigest().startswith(self.gladiators[0].outputs[0].heschex):         # print(sha256(pdumps(self)).hexdigest()[63:64])
            self.nonce = self.nonce + 1
        self.haschash = sha256(str(encode(self)).encode()).hexdigest()
    def __copy__(self):
        return Block.loschoad(self.block_number, self.total_difficulty, self.gladiators, self.nonce, self.difficulty, self.prev_hash, self.haschash)
    @classmethod
    def loschoad(cls, block_number, total_difficulty, gladiators, nonce, difficulty, prev_hash, haschash):
        cls.block_number = block_number
        cls.prev_hash = prev_hash
        cls.difficulty = difficulty
        cls.total_difficulty = total_difficulty
        cls.nonce = nonce
        cls.gladiators = [Gladiator.from_json(jdumps(gladiator)) for gladiator in gladiators]
        cls.haschash = haschash
        return cls
    @staticmethod
    def from_json(json_string):
        json_dict = loads(json_string)
        return Block.loschoad(**json_dict)

and the following code

            blocks = []
            _, _, files = next(walk(self.utils.dir))
            for i in range(len(files)):
                try:
                    with open(self.utils.dir + '/nofifty_' + str(i) + '.json') as nofifty:
                        blocks.append(nofifty.read())
                except FileNotFoundError:
                    with open(self.utils.dir + '/genesis.json') as genesis:
                        blocks.append(genesis.read())
                finally:
                    for idx, block in enumerate(blocks):
                        print(idx)
                        print(block)    
        self.blocks = [copy.copy(Block.from_json(copy.copy(block))) for block in blocks]
        for idx, block in enumerate(self.blocks):
            print(idx)
            print(block.haschash)

produces the following output

{
  "difficulty": 0,
  "block_number": 0,
  "nonce": 19,
  "total_difficulty": 0,
  "haschash": "290ceeb4f166570d96a0185638a778db1cd8d4fcbcdd4bf99a579ae327f7947b",
  "gladiators": [
    {
      "id": "266013543047fadde0d7f2ddd3d78a65899d7846884d9acf70aca800e71eae31",
      "outputs": [
        {
          "heschex": "2",
          "public_key": "a69236fd0107077d38dcf3e8166b5242ae76f41246ab76141b71548cc26215edd3c05bfa7524751244d18f8002255b0b"
        }
      ],
      "inputs": []
    }
  ],
  "prev_hash": ""
}

1
{
  "difficulty": 1,
  "block_number": 1,
  "nonce": 59,
  "total_difficulty": 1,
  "haschash": "2b5b6cb6640f3cb3417e4af81b523d238e87a8adba82b7a83131a1f63cabf8f8",
  "gladiators": [
    {
      "id": "5bd73de3a5ce6b5b66d9fc4b228cc42115838be7db7b08d51df21fd2fa9b761c",
      "outputs": [
        {
          "heschex": "2b",
          "public_key": "a69236fd0107077d38dcf3e8166b5242ae76f41246ab76141b71548cc26215edd3c05bfa7524751244d18f8002255b0b"
        }
      ],
      "inputs": []
    }
  ],
  "prev_hash": "290ceeb4f166570d96a0185638a778db1cd8d4fcbcdd4bf99a579ae327f7947b"
}

0
2b5b6cb6640f3cb3417e4af81b523d238e87a8adba82b7a83131a1f63cabf8f8
1
2b5b6cb6640f3cb3417e4af81b523d238e87a8adba82b7a83131a1f63cabf8f8

so the hash has the hash of the latest appended block and not the hash of the genesis block

Am i doing something wrong?

also see
https://stackoverflow.com/questions/69781050/append-is-replacing-when-converting-from-json

i did not have this problem with jsonpickle but couldn't encode and decode over and over again because of ignoring fields
see:
https://stackoverflow.com/questions/69770625/how-to-create-a-custom-serialization-handler
python version
Python 3.9.7

@stevendaprano
Copy link
Member

@stevendaprano stevendaprano commented Dec 18, 2021

Hi noahsalvadordenjo,

This is an issue tracker for bugs on the Python.org website, not a help desk to ask questions about Python.

In the future, please ask these sorts of questions on the many different forums which are intended for asking for help. Go to the Python.org website and click on the Community menu to see various forums, mailing lists and IRC channels were you can ask for help. Or you can try Reddit's r/learnpython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants