Sort proofs

This commit is contained in:
nolash 2021-10-10 21:30:18 +02:00
parent fee303cf85
commit a5cdcb3900
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
7 changed files with 93 additions and 34 deletions

View File

@ -1,6 +1,5 @@
# standard imports # standard imports
import os import os
import hashlib
import logging import logging
# local imports # local imports
@ -13,17 +12,9 @@ class Attachment(Data):
def __init__(self, path='.'): def __init__(self, path='.'):
super(Attachment, self).__init__() super(Attachment, self).__init__()
self.contents = [] self.contents = {}
self.digests = []
self.path = path self.path = path
self.attachment_path = os.path.join(self.path, 'attachments') self.attachment_path = os.path.join(self.path, 'attachments')
self.__hasher = self.__basehasher
def __basehasher(self, v):
h = hashlib.sha256()
h.update(v)
return h.digest()
def load(self): def load(self):
@ -32,24 +23,24 @@ class Attachment(Data):
f = open(fp, 'rb') f = open(fp, 'rb')
r = f.read() r = f.read()
f.close() f.close()
self.contents.append(fp)
z = self.__hasher(r) z = self.hash(r).hex()
self.digests.append(z) self.contents[z] = fp
logg.debug('loaded attachment file {} digest {}'.format(fp, z.hex())) logg.debug('loaded attachment file {} digest {}'.format(fp, z))
def start(self): def start(self):
super(Attachment, self).start() super(Attachment, self).start()
os.makedirs(self.attachment_path) os.makedirs(self.attachment_path)
def get(self): def get(self, k):
def maphex(v): return self.contents[k]
return v.hex()
return list(map(maphex, self.digests))
def asdict(self):
return self.contents
def __str__(self): def __str__(self):

View File

@ -1,5 +1,6 @@
# standard imports # standard imports
import os import os
import hashlib
mod_dir = os.path.dirname(os.path.realpath(__file__)) mod_dir = os.path.dirname(os.path.realpath(__file__))
@ -17,6 +18,17 @@ class Data:
self.dirty = False self.dirty = False
self.inited = False self.inited = False
self.__version = self.__default_version self.__version = self.__default_version
self.__hasher = self.__basehasher
def __basehasher(self, v):
h = hashlib.sha256()
h.update(v)
return h.digest()
def hash(self, v):
return self.__hasher(v)
def load(self): def load(self):

View File

@ -27,9 +27,9 @@ def execute(config, eargs):
cmd_mod = importlib.import_module(modname) cmd_mod = importlib.import_module(modname)
ct = Token(path=eargs.directory) ct = Token(path=eargs.directory)
cp = Proof(path=eargs.directory)
cm = Meta(path=eargs.directory) cm = Meta(path=eargs.directory)
ca = Attachment(path=eargs.directory) ca = Attachment(path=eargs.directory)
cp = Proof(path=eargs.directory, attachments=ca)
cn = Network(path=eargs.directory) cn = Network(path=eargs.directory)
ct.load() ct.load()
@ -40,4 +40,4 @@ def execute(config, eargs):
ref = cn.reference(eargs.target) ref = cn.reference(eargs.target)
logg.debug('found reference {} for target {}'.format(ref, eargs.target)) logg.debug('found reference {} for target {}'.format(ref, eargs.target))
getattr(cmd_mod, 'new')(ref, ca.get(), signer_hint=eargs.signer) getattr(cmd_mod, 'new')(ref, cp, signer_hint=eargs.signer)

View File

@ -22,11 +22,11 @@ logg = logging.getLogger(__name__)
class CICEth: class CICEth:
def __init__(self, chain_spec, resources, proofs, signer=None, rpc=None, nonce_oracle=None, fee_oracle=None): def __init__(self, chain_spec, resources, proof, signer=None, rpc=None, nonce_oracle=None, fee_oracle=None):
"""resources will be modified """resources will be modified
""" """
self.resources = resources self.resources = resources
self.proofs = proofs self.proof = proof
self.chain_spec = chain_spec self.chain_spec = chain_spec
self.signer = signer self.signer = signer
self.rpc = rpc self.rpc = rpc
@ -170,7 +170,7 @@ class CICEth:
signer_address = self.resources['address_declarator']['key_address'] signer_address = self.resources['address_declarator']['key_address']
r = [] r = []
for proof in self.proofs: for proof in self.proof.get():
o = c.add_declaration(contract_address, signer_address, self.token_address, proof, tx_format=self.tx_format) o = c.add_declaration(contract_address, signer_address, self.token_address, proof, tx_format=self.tx_format)
if self.rpc != None: if self.rpc != None:
r.append(self.rpc.do(o[1])) r.append(self.rpc.do(o[1]))
@ -198,5 +198,5 @@ class CICEth:
getattr(self, 'process_̈́ ' + task)() getattr(self, 'process_̈́ ' + task)()
def new(resources, proofs, signer_hint=None): def new(resources, proof, signer_hint=None):
return CICEth(resources, proofs, signer=None) return CICEth(resources, proof, signer=None)

View File

@ -2,6 +2,11 @@
import os import os
import json import json
import logging import logging
import tempfile
import cbor2
# external imports
from hexathon import strip_0x
# local imports # local imports
from .base import * from .base import *
@ -11,12 +16,15 @@ logg = logging.getLogger(__name__)
class Proof(Data): class Proof(Data):
def __init__(self, path='.'): def __init__(self, path='.', attachments=None):
super(Proof, self).__init__() super(Proof, self).__init__()
self.namespace = 'ge' self.namespace = 'ge'
self.description = None self.description = None
self.path = path self.path = path
self.extra_attachments = attachments
self.attachments = {}
self.proof_path = os.path.join(self.path, 'proof.json') self.proof_path = os.path.join(self.path, 'proof.json')
self.temp_proof_path = tempfile.mkstemp()[1]
def load(self): def load(self):
@ -47,5 +55,41 @@ class Proof(Data):
f.close() f.close()
def asdict(self):
return {
'version': self.version(),
'namespace': self.namespace,
'description': self.description,
}
def __get_ordered_hashes(self):
ks = list(self.attachments.keys())
ks.sort()
return ks
def get(self):
v = self.asdict()
b = cbor2.dumps(v)
f = open(self.temp_proof_path, 'wb')
f.write(b)
f.close()
hsh = self.hash(b).hex()
self.attachments[hsh] = self.temp_proof_path
if self.extra_attachments != None:
a = self.extra_attachments.asdict()
for k in a.keys():
self.attachments[k] = a[k]
hshs = self.__get_ordered_hashes()
return hshs
def __str__(self): def __str__(self):
return "description = {}\n".format(self.description) return "description = {}\n".format(self.description)

View File

@ -1,7 +1,7 @@
# standard imports # standard imports import unittestimport logging
import unittest
import logging
import random import random
import os
import logging
# external imports # external imports
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
@ -14,6 +14,11 @@ from funga.eth.keystore.dict import DictKeystore
# local imports # local imports
from cic.ext.eth import CICEth from cic.ext.eth import CICEth
from cic import Proof
from cic.attachment import Attachment
# test imports
from tests.base_cic import test_data_dir
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
@ -40,6 +45,8 @@ class TestCICEthBase(EthTesterCase):
'key_address': addresses[2], 'key_address': addresses[2],
}, },
} }
self.proofs = [] proof_dir = os.path.join(test_data_dir, 'proof')
for i in range(3): attach = Attachment(path=proof_dir)
self.proofs.append(random.randbytes(32).hex()) attach.load()
self.proofs = Proof(proof_dir, attachments=attach)
self.proofs.load()

5
tests/testdata/proof/proof.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"version": 0,
"namespace": "ge",
"description": "foo bar baz"
}