Exemple #1
0
 def SaveAocpayDic(self, height, bkobj):
     wallet = aiptool.GetObjFromNameAndHeight("Wallet", self.aipdic)
     addr = ""
     for tx in bkobj.payloads:
         if tx.aipname.value == "aip2.aip2_0.Transaction_Gate":
             if len(tx.ownerships) > 0:
                 addr = wallet.AddressFromPubKey(tx.ownerpubkey.value)
     if addr != "" and wallet.VerifyAddress(addr):
         try:
             with self.conn_g:
                 self.conn_g.execute(
                     "INSERT OR REPLACE INTO " + " aocpay " +
                     "(aocaddress, height_creation)\
                         VALUES(?,?)", (
                         addr,
                         height,
                     ))
         except:
             return False
Exemple #2
0
    def __init__(self, network="mainnet"):
        ## 1. Connect Database
        self.conn_g = sqlite3.connect('globalinfo_' + network + '.db')
        self.conn_b = sqlite3.connect('blockDATA_' + network + '.db')

        ## 2. Database Status
        re, msg = self.OpenCreateDatabaseTable()

        ## 3. current height
        self.bkheight = self.API_GetLatestBlockHeight()
        if self.bkheight == -1:
            with self.conn_g:
                aiplist = [("aip0.aip0_0.Block", "Block", 0), ("aip0.aip0_0.Law", "Law", 0),\
                            ("aip0.aip0_0.Ownership", "Ownership", 0),("aip0.aip0_0.Wallet", "Wallet", 0),\
                            ("aip0.aip0_0.Chain", "Chain", 0),("aip1.aip1_0.Input", "Input", 0),("aip1.aip1_0.Output", "Output", 0),\
                            ("aip1.aip1_0.Transaction_Pay", "Transaction_Pay", 0),("aip2.aip2_0.Transaction_Gate", "Transaction_Gate", 0)]
                self.conn_g.executemany(
                    "INSERT OR REPLACE INTO " + " aip_height_dic " +
                    "(aippath, aip_head, height_creation)\
                VALUES(?,?,?)", (aiplist))
                self.conn_g.execute(
                    "INSERT OR REPLACE INTO " + " aocpay " +
                    "(aocaddress, height_creation) VALUES(?,?)", (
                        "A2Qw5gKjX4k9SPSbZ4zmGS7EiQgfdrp86CHa58CpVhPkBYYoa2d",
                        0,
                    ))

            uc = utxoclass().parse(
                0, 0, 0, 100_000000_000000,
                "A2Qw5gKjX4k9SPSbZ4zmGS7EiQgfdrp86CHa58CpVhPkBYYoa2d", "aoc")
            self.SaveUtxo(uc)

        ## 4. aip dictionary, aocpay owner dic
        self.aipdic = self.API_GetAipDic()
        self.aocpaydic = self.API_GetAocpayDic()
        print(self.aipdic, self.aocpaydic)

        ## 5. Get Latest Chain
        self.Chain = aiptool.GetObjFromNameAndHeight("Chain", self.aipdic)()
        if self.Read100Blocks2Chain():
            print("Load Block Correct, height:", self.bkheight)
        else:
            print("Block Database is wrong")
Exemple #3
0
 def CheckAocpayOwnership(self, bkheight, txs):
     wallet = aiptool.GetObjFromNameAndHeight("Wallet", self.aipdic)
     aocpayaddr = aiptool.GetAocpayAddressFromHeight(
         self.aocpaydic, bkheight)
     for tx in txs:
         if tx.aipname.value == "aip2.aip2_0.Transaction_Gate":
             if len(tx.ownerships) > 0:
                 ##check aoc stakes
                 POS = 0
                 for os in tx.ownerships:
                     POS += self.API_GetUtxoAmount(pubkey=os.pubkey.value)
                 if POS < 62 * 1000000_000000:
                     return False, "no enough fund to depoist or withdraw real token"
                 else:
                     continue
             elif aocpayaddr != wallet.AddressFromPubKey(
                     tx.ownerpubkey.value):
                 return False, "your address is not aocpay address"
     return True, "aocpay success"
Exemple #4
0
 def API_GetUtxoAmount(self,
                       token="aoc",
                       address=b"",
                       pubkey=b"",
                       ListMode=False):
     #"""get the total aoc amount of the address has"""
     if address == b"" and pubkey != b"":
         Wallet = aiptool.GetObjFromNameAndHeight("Wallet", self.aipdic)
         address = Wallet.AddressFromPubKey(pubkey)
     tablename = "utxo_" + token
     utxolist = self.conn_g.execute(
         "SELECT block_height, tx_height, out_height, amount FROM " +
         tablename + " WHERE address = ?", (address, )).fetchall()
     if ListMode:
         return utxolist
     amount = 0
     for utxo in utxolist:
         amount = amount + utxo[3]
     return amount
Exemple #5
0
    def PackBlock(self):
        if self.wc is None:
            return False
        if self.wc.GetLedgerInfo():
            self.height = self.wc.ledgerinfo['BlockHeight']
            self.aipdic = self.wc.ledgerinfo['AipDic']
            txsnum = self.wc.ledgerinfo['PoolInfo'][0]
            self.chinfo = self.wc.ledgerinfo['ChainInfo']

            if txsnum < 1:
                #print("no txs in the pool")
                return False
            self.Block = aiptool.GetObjFromNameAndHeight("Block", self.aipdic)
            self.bkobj = self.Block()
            self.bkobj.blocktype.parse(2)

            bytespre = bytes.fromhex(self.wc.ledgerinfo['CurrentHash'])
            self.bkobj.preblock.parse(bytespre)
            self.bkobj.timestamp.parse(
                max(int(time.time()), self.wc.ledgerinfo['timestamp'] + 1))
            txs = self.wc.GetPool(ltype="Tx")
            if txs is not None:
                self.bkobj.payloads.parse(txs)
                self.bkobj.ownerships.parse(
                    [self.wc.BuildOwnershipObj(self.bkobj)])
                ##smart miner does below
                avepow, avesize, avetime = self.chinfo
                tr = (100 / avetime)**0.1
                sr = avesize / self.bkobj.GetSize()
                smartr = max(int(tr * sr * len(txs) * 1.5), 1)
                if smartr < len(txs):
                    self.bkobj.payloads.parse(txs[:smartr])
                    self.bkobj.ownerships.parse(
                        [self.wc.BuildOwnershipObj(self.bkobj)])
                ##
                return True
        return False
Exemple #6
0
 def CheckUtxoList(self, bkheight, bkobj):
     wallet = aiptool.GetObjFromNameAndHeight("Wallet", self.aipdic)
     owneraddr = wallet.AddressFromPubKey(
         bkobj.ownerships.value[0].pubkey.value)
     return self._manage_utxo(owneraddr, bkobj.payloads, bkheight)