示例#1
0
    def Blockchain_GetBlock(self, engine):
        data = engine.EvaluationStack.Pop()

        if data:
            data = data.GetByteArray()
        else:
            return False

        block = None

        if len(data) <= 5:
            height = BigInteger.FromBytes(data)

            if Blockchain.Default() is not None:

                block = Blockchain.Default().GetBlockByHeight(height)

            elif height == 0:

                block = Blockchain.GenesisBlock()

        elif len(data) == 32:

            hash = UInt256(data=data).ToBytes()

            if Blockchain.Default() is not None:

                block = Blockchain.Default().GetBlockByHash(hash=hash)

            elif hash == Blockchain.GenesisBlock().Hash:

                block = Blockchain.GenesisBlock().Header

        engine.EvaluationStack.PushT(StackItem.FromInterface(block))
        return True
示例#2
0
    def Blockchain_GetHeader(self, engine):
        data = engine.EvaluationStack.Pop().GetByteArray()

        header = None

        if len(data) <= 5:

            height = BigInteger.FromBytes(data)

            if Blockchain.Default() is not None:

                header = Blockchain.Default().GetHeaderBy(
                    height_or_hash=height)

            elif height == 0:

                header = Blockchain.GenesisBlock().Header

        elif len(data) == 32:

            hash = UInt256(data=data)

            if Blockchain.Default() is not None:

                header = Blockchain.Default().GetHeaderBy(height_or_hash=hash)

            elif hash == Blockchain.GenesisBlock().Hash:

                header = Blockchain.GenesisBlock().Header

        engine.EvaluationStack.PushT(StackItem.FromInterface(header))
        return True
示例#3
0
 def setUpClass(cls):
     settings.setup_unittest_net()
     Blockchain.DeregisterBlockchain()
     cls._blockchain = LevelDBBlockchain(path=cls.LEVELDB_TESTPATH,
                                         skip_version_check=True)
     Blockchain.RegisterBlockchain(cls._blockchain)
     cls._genesis = Blockchain.GenesisBlock()
示例#4
0
 def test_cant_serialize_iop_item(self, mocked_logger):
     genesis = Blockchain.GenesisBlock()
     self.econtext.EvaluationStack.PushT(StackItem.FromInterface(genesis))
     self.engine.InvocationStack.PushT(self.econtext)
     cant_do = self.state_reader.Runtime_Serialize(self.engine)
     self.assertEqual(cant_do, False)
     mocked_logger.assert_called_with(StringIn('Cannot serialize item IOp Interface: <neo.Core.Block.Block object'))
示例#5
0
    def __init__(self, path):
        super(LevelDBBlockchain, self).__init__()
        self._path = path
        self.__log.debug('Initialized LEVELDB')

        self._header_index = []
        self._header_index.append(
            Blockchain.GenesisBlock().Header().HashToByteString())

        try:
            self._db = plyvel.DB(self._path, create_if_missing=True)
        except Exception as e:
            print(
                "leveldb unavailable, you may already be running this process: %s "
                % e)
            raise Exception('Leveldb Unavailable')

        version = self._db.get_property(SYS_Version)
        self.__log.debug("current version %s " % version)

        self._current_block_height = self._db.get(SYS_CurrentBlock, 0)

        current_header_height = self._db.get(SYS_CurrentHeader,
                                             self._current_block_height)

        self.__log.debug("current header height, hashes %s %s " %
                         (self._current_block_height, self._header_index))

        hashes = []
        for key, value in self._db.iterator(start=IX_HeaderHashList):
            hashes.append({'index': key, 'hash': value})

        sorted(hashes, key=lambda i: i['index'])

        for h in hashes:
            if not h['hash'] == Blockchain.GenesisBlock().Hash():
                self._header_index.append(h['hash'])
            self._stored_header_count += 1

        if self._stored_header_count == 0:
            headers = []
            for key, value in self._db.iterator(start=DATA_Block):
                headers.append(Header.FromTrimmedData(bytearray(value)[4:], 0))
            sorted(headers, key=lambda h: h.Index)

            for h in headers:
                self._header_index.append(h.HashToByteString())
示例#6
0
    def Runtime_GetCurrentTime(self, engine: ExecutionEngine):
        BC = Blockchain.Default()
        header = BC.GetHeaderByHeight(BC.Height)
        if header is None:
            header = Blockchain.GenesisBlock()

        engine.CurrentContext.EvaluationStack.PushT(header.Timestamp + Blockchain.SECONDS_PER_BLOCK)
        return True
示例#7
0
 def test_cant_serialize_iop_item(self):
     with self.assertLogHandler('vm', logging.DEBUG) as log_context:
         genesis = Blockchain.GenesisBlock()
         self.econtext.EvaluationStack.PushT(
             StackItem.FromInterface(genesis))
         self.engine.InvocationStack.PushT(self.econtext)
         cant_do = self.state_reader.Runtime_Serialize(self.engine)
         self.assertEqual(cant_do, False)
         self.assertTrue(len(log_context.output) > 0)
         expected_msg = 'Cannot serialize item IOp Interface: <neo.Core.Block.Block object'
         self.assertTrue(expected_msg in log_context.output[0])
示例#8
0
    def __init__(self, path, skip_version_check=False):
        super(LevelDBBlockchain, self).__init__()
        self._path = path

        self._header_index = []
        self._header_index.append(Blockchain.GenesisBlock().Header.Hash.ToBytes())

        self.TXProcessed = 0

        print(TXProcessed)
        print("==========begin===========")
        print(Blockchain.GenesisBlock())

        try:
            self._db = plyvel.DB(self._path, create_if_missing=True)
        #            self._db = plyvel.DB(self._path, create_if_missing=True, bloom_filter_bits=16, compression=None)
            logger.info("Created Blockchain DB at %s " % self._path)
        except Exception as e:
            logger.info("leveldb unavailable, you may already be running this process: %s " % e)
            raise Exception('Leveldb Unavailable')

        version = self._db.get(DBPrefix.SYS_Version)

        print(version)

        if skip_version_check:
            self._db.put(DBPrefix.SYS_Version, self._sysversion)
            version = self._sysversion

        if version == self._sysversion:  # or in the future, if version doesn't equal the current version...

            ba = bytearray(self._db.get(DBPrefix.SYS_CurrentBlock, 0))
            self._current_block_height = int.from_bytes(ba[-4:], 'little')

            print("Block height")
            print(self._current_block_height)

            ba = bytearray(self._db.get(DBPrefix.SYS_CurrentHeader, 0))
            current_header_height = int.from_bytes(ba[-4:], 'little')
            current_header_hash = bytes(ba[:64].decode('utf-8'), encoding='utf-8')

            #            logger.info("current header hash!! %s " % current_header_hash)
            #            logger.info("current header height, hashes %s %s %s" %(self._current_block_height, self._header_index, current_header_height) )

            hashes = []
            try:
                for key, value in self._db.iterator(prefix=DBPrefix.IX_HeaderHashList):
                    ms = StreamManager.GetStream(value)
                    reader = BinaryReader(ms)
                    hlist = reader.Read2000256List()
                    key = int.from_bytes(key[-4:], 'little')
                    hashes.append({'k': key, 'v': hlist})
                    StreamManager.ReleaseStream(ms)
            #                hashes.append({'index':int.from_bytes(key, 'little'), 'hash':value})

            except Exception as e:
                logger.info("Could not get stored header hash list: %s " % e)

            print("hashes")
            print(len(hashes))
            print(hashes)

            if len(hashes):
                hashes.sort(key=lambda x: x['k'])
                genstr = Blockchain.GenesisBlock().Hash.ToBytes()
                for hlist in hashes:

                    for hash in hlist['v']:
                        if hash != genstr:
                            self._header_index.append(hash)
                        self._stored_header_count += 1

            if self._stored_header_count == 0:
                headers = []
                for key, value in self._db.iterator(prefix=DBPrefix.DATA_Block):
                    dbhash = bytearray(value)[8:]
                    headers.append(Header.FromTrimmedData(binascii.unhexlify(dbhash), 0))

                headers.sort(key=lambda h: h.Index)
                for h in headers:
                    if h.Index > 0:
                        self._header_index.append(h.Hash.ToBytes())

            elif current_header_height > self._stored_header_count:

                try:
                    hash = current_header_hash
                    targethash = self._header_index[-1]

                    newhashes = []
                    while hash != targethash:
                        header = self.GetHeader(hash)
                        newhashes.insert(0, header)
                        hash = header.PrevHash.ToBytes()

                    self.AddHeaders(newhashes)
                except Exception as e:
                    pass

        elif version is None:
            self.Persist(Blockchain.GenesisBlock())
            self._db.put(DBPrefix.SYS_Version, self._sysversion)
        else:

            logger.error("\n\n")
            logger.warning("Database schema has changed from %s to %s.\n" % (version, self._sysversion))
            logger.warning("You must either resync from scratch, or use the np-bootstrap command to bootstrap the chain.")

            res = prompt("Type 'continue' to erase your current database and sync from new. Otherwise this program will exit:\n> ")
            if res == 'continue':

                with self._db.write_batch() as wb:
                    for key, value in self._db.iterator():
                        wb.delete(key)

                self.Persist(Blockchain.GenesisBlock())
                self._db.put(DBPrefix.SYS_Version, self._sysversion)

            else:
                raise Exception("Database schema changed")
    def __init__(self, path):
        super(LevelDBBlockchain, self).__init__()
        self._path = path

        self._header_index = []
        self._header_index.append(
            Blockchain.GenesisBlock().Header.Hash.ToBytes())

        try:
            self._db = plyvel.DB(self._path, create_if_missing=True)
        #            self._db = plyvel.DB(self._path, create_if_missing=True, bloom_filter_bits=16, compression=None)
        except Exception as e:
            logger.info(
                "leveldb unavailable, you may already be running this process: %s "
                % e)
            raise Exception('Leveldb Unavailable')

        version = self._db.get(DBPrefix.SYS_Version)

        if version == self._sysversion:  # or in the future, if version doesn't equal the current version...

            ba = bytearray(self._db.get(DBPrefix.SYS_CurrentBlock, 0))
            self._current_block_height = int.from_bytes(ba[-4:], 'little')

            ba = bytearray(self._db.get(DBPrefix.SYS_CurrentHeader, 0))
            current_header_height = int.from_bytes(ba[-4:], 'little')
            current_header_hash = bytes(ba[:64].decode('utf-8'),
                                        encoding='utf-8')

            #            logger.info("current header hash!! %s " % current_header_hash)
            #            logger.info("current header height, hashes %s %s %s" %(self._current_block_height, self._header_index, current_header_height) )

            hashes = []
            try:
                for key, value in self._db.iterator(
                        prefix=DBPrefix.IX_HeaderHashList):
                    ms = StreamManager.GetStream(value)
                    reader = BinaryReader(ms)
                    hlist = reader.Read2000256List()
                    key = int.from_bytes(key[-4:], 'little')
                    hashes.append({'k': key, 'v': hlist})
                    StreamManager.ReleaseStream(ms)
            #                hashes.append({'index':int.from_bytes(key, 'little'), 'hash':value})

            except Exception as e:
                logger.info("Could not get stored header hash list: %s " % e)

            if len(hashes):
                hashes.sort(key=lambda x: x['k'])
                genstr = Blockchain.GenesisBlock().Hash.ToBytes()
                for hlist in hashes:

                    for hash in hlist['v']:
                        if hash != genstr:
                            self._header_index.append(hash)
                        self._stored_header_count += 1

            if self._stored_header_count == 0:
                headers = []
                for key, value in self._db.iterator(
                        prefix=DBPrefix.DATA_Block):
                    dbhash = bytearray(value)[8:]
                    headers.append(
                        Header.FromTrimmedData(binascii.unhexlify(dbhash), 0))

                headers.sort(key=lambda h: h.Index)
                for h in headers:
                    if h.Index > 0:
                        self._header_index.append(h.Hash.ToBytes())

            elif current_header_height > self._stored_header_count:

                try:
                    hash = current_header_hash
                    targethash = self._header_index[-1]

                    newhashes = []
                    while hash != targethash:
                        header = self.GetHeader(hash)
                        newhashes.insert(0, header)
                        hash = header.PrevHash.ToBytes()

                    self.AddHeaders(newhashes)
                except Exception as e:
                    pass
        else:
            with self._db.write_batch() as wb:
                for key, value in self._db.iterator():
                    wb.delete(key)

            self.Persist(Blockchain.GenesisBlock())
            self._db.put(DBPrefix.SYS_Version, self._sysversion)
示例#10
0
 def setUpClass(self):
     print("setup")
     self._blockchain = RocksDBBlockchain(path=self.ROCKSDB_TESTPATH)
     Blockchain.RegisterBlockchain(self._blockchain)
     self._genesis = Blockchain.GenesisBlock()
示例#11
0
 def setUpClass(self):
     self._blockchain = LevelDBBlockchain(path=self.LEVELDB_TESTPATH,
                                          skip_version_check=True)
     Blockchain.RegisterBlockchain(self._blockchain)
     self._genesis = Blockchain.GenesisBlock()
示例#12
0
    def __init__(self, path):
        super(RocksDBBlockchain, self).__init__()
        self._path = path

        self._header_index = []
        self._header_index.append(
            Blockchain.GenesisBlock().Header.Hash.ToBytes())

        logger.info("start to create database")
        logger.info("db path is: ", self._path)
        try:
            opts = rocksdb.Options()
            opts.create_if_missing = True
            opts.prefix_extractor = StaticPrefix()
            #           self._db = plyvel.DB(self._path, create_if_missing=True)
            self._db = rocksdb.DB(self._path, opts)
            #           self._db = plyvel.DB(self._path, create_if_missing=True, bloom_filter_bits=16, compression=None)
            logger.info("rocksdb is created successfully")
        except Exception as e:
            logger.info(
                "RocksDB unavailable, you may already be running this process: %s "
                % e)
            raise Exception('RocksDB Unavailable')

        version = self._db.get(DBPrefix.SYS_Version)

        logger.info("database is created successfully, version is: ", version)
        logger.info("database is created successfully, self_sysversion is: ",
                    self._sysversion)

        if version == self._sysversion:  # or in the future, if version doesn't equal the current version...

            ba = bytearray(self._db.get(DBPrefix.SYS_CurrentBlock, 0))
            logger.debug("current block", ba)
            logger.debug("current block", ba[-4:])
            self._current_block_height = int.from_bytes(ba[-4:], 'little')
            logger.debug("current block height: ", self._current_block_height)

            ba = bytearray(self._db.get(DBPrefix.SYS_CurrentHeader, 0))
            logger.debug("current header", ba)
            current_header_height = int.from_bytes(ba[-4:], 'little')
            current_header_hash = bytes(ba[:64].decode('utf-8'),
                                        encoding='utf-8')

            #            logger.info("current header hash!! %s " % current_header_hash)
            #            logger.info("current header height, hashes %s %s %s" %(self._current_block_height, self._header_index, current_header_height) )

            hashes = []
            try:
                it = self._db.iteritems()
                it.seek(DBPrefix.IX_HeaderHashList)
                #                print ( dict(itertools.takewhile(lambda item: item[0].startswith(DBPrefix.IX_HeaderHashList), it)))
                #                for key in self._db.iterkeys().seek(prefix=DBPrefix.IX_HeaderHashList):
                for key, value in dict(
                        itertools.takewhile(
                            lambda item: item[0].startswith(
                                DBPrefix.IX_HeaderHashList), it)).items():
                    ms = StreamManager.GetStream(value)
                    reader = BinaryReader(ms)
                    hlist = reader.Read2000256List()
                    key = int.from_bytes(key[-4:], 'little')
                    hashes.append({'k': key, 'v': hlist})
                    StreamManager.ReleaseStream(ms)

    #                hashes.append({'index':int.from_bytes(key, 'little'), 'hash':value})

            except Exception as e:
                logger.info("Couldnt get stored header hash list: %s " % e)

            if len(hashes):
                hashes.sort(key=lambda x: x['k'])
                genstr = Blockchain.GenesisBlock().Hash.ToBytes()
                for hlist in hashes:

                    for hash in hlist['v']:
                        if hash != genstr:
                            self._header_index.append(hash)
                        self._stored_header_count += 1

            if self._stored_header_count == 0:
                headers = []
                # for key, value in self._db.iterator(prefix=DBPrefix.DATA_Block):
                it = self._db.iteritems()
                # print ("it before seek ", it)
                logger.debug("seek key prefix: ", DBPrefix.DATA_Block)
                it.seek(DBPrefix.DATA_Block)
                # print ("it after seek ", it)
                # print ( dict(itertools.takewhile(lambda item: item[0].startswith(DBPrefix.DATA_Block), it)))
                for key, value in dict(
                        itertools.takewhile(
                            lambda item: item[0].startswith(DBPrefix.DATA_Block
                                                            ), it)).items():
                    # print("key is ", key)
                    # print("value is ", value)
                    dbhash = bytearray(value)[8:]
                    # print (bytearray(value))
                    # print ("dbhash is ", dbhash)
                    headers.append(
                        Header.FromTrimmedData(binascii.unhexlify(dbhash), 0))
                    # print ("header is ", headers)

                headers.sort(key=lambda h: h.Index)
                for h in headers:
                    if h.Index > 0:
                        self._header_index.append(h.Hash.ToBytes())

            elif current_header_height > self._stored_header_count:

                try:
                    hash = current_header_hash
                    targethash = self._header_index[-1]

                    newhashes = []
                    while hash != targethash:
                        header = self.GetHeader(hash)
                        newhashes.insert(0, header)
                        hash = header.PrevHash.ToBytes()

                    self.AddHeaders(newhashes)
                except Exception as e:
                    pass
        else:
            #  with self._db.write_batch() as wb:
            wb = rocksdb.WriteBatch()
            for key in self._db.iterkeys():
                wb.delete(key)
            self._db.write(wb)

            self.Persist(Blockchain.GenesisBlock())
            self._db.put(DBPrefix.SYS_Version, self._sysversion)
示例#13
0
 def setUpClass(self):
     self._blockchain = LevelDBBlockchain(path=self.LEVELDB_TESTPATH)
     Blockchain.RegisterBlockchain(self._blockchain)
     self._genesis = Blockchain.GenesisBlock()
示例#14
0
def GetGenesis():
    from neo.Core.Blockchain import Blockchain
    return Blockchain.GenesisBlock()
 def test_cant_serialize_iop_item(self):
     genesis = Blockchain.GenesisBlock()
     self.econtext.EvaluationStack.PushT(StackItem.FromInterface(genesis))
     self.engine.InvocationStack.PushT(self.econtext)
     cant_do = self.state_reader.Runtime_Serialize(self.engine)
     self.assertEqual(cant_do, False)