Esempio n. 1
0
 public CreditDebitData(AccountInfo ai, long Money, long Credits, long Debits)
 {
     this.ai = ai;
     this.Money = Money;
     this.Credits = Credits;
     this.Debits = Debits;
 }
        public void Deserialize(byte[] Data)
        {
            List<ProtocolDataType> PDTs = ProtocolPackager.UnPackRaw(Data);
            int cnt = 0;

            Init();

            while (cnt < (int)PDTs.Count)
            {
                ProtocolDataType PDT = PDTs[cnt++];

                if (PDT.NameType == 0)
                {
                    ProtocolPackager.UnpackVarint(PDT, 0, ref LeafCount);
                }
                else if (PDT.NameType == 1)
                {
                    byte[] _bv = new byte[0];
                    if (ProtocolPackager.UnpackByteVector(PDT, 1, ref _bv))
                    {
                        AccountInfo nie = new AccountInfo();
                        nie.Deserialize(_bv);
                        Leaves.Add(nie);
                    }
                }
            }
        }
Esempio n. 3
0
 public bool TryFetch(Hash publicKey, out AccountInfo account)
 {
     bool okay = false;
     LeafDataType ldt ;
     if (LedgerTree.GetNodeData(publicKey, out ldt) == TraverseResult.Success)
     {
         account = (AccountInfo)ldt;
         okay = true;
     }
     account = new AccountInfo();
     return okay;
 }
Esempio n. 4
0
        /// <summary>
        /// TODO: PROPER TESTBENCH { CRITICAL }
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public TreeResponseType AddUserToLedger(AccountInfo userInfo)
        {
            TreeResponseType response = LedgerTree.AddUpdate(userInfo);

            if (response == TreeResponseType.Added || response == TreeResponseType.Updated)
            {
                string address = userInfo.GetAddress();
                if (AddressAccountInfoMap.ContainsKey(address))
                {
                    AddressAccountInfoMap[address] = userInfo; // Update
                }
                else
                {
                    AddressAccountInfoMap.Add(address, userInfo); // Add
                }

                if (Utils.ValidateUserName( userInfo.Name))
                {
                    if (NameAccountInfoMap.ContainsKey(userInfo.Name))
                    {
                        NameAccountInfoMap[userInfo.Name] = userInfo; // Update
                    }
                    else
                    {
                        NameAccountInfoMap.Add(userInfo.Name, userInfo); // Add
                    }
                }

                if (initialLoading)
                {
                    if ((_load_stats % 100 == 0) && (LedgerEvent != null))
                        LedgerEvent(LedgerEventType.Progress, "Loaded " + _load_stats + " Accounts.");

                    _load_stats++;
                }

            }

            return response;
        }
        public async Task<Tuple<DBResponse, long>> FetchAllAccountsAsync(AccountFetchEventHandler accountFetch)
        {
            DBResponse response = DBResponse.FetchFailed;
            long Records = 0;

            await Task.Run(() =>
            {

                using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Ledger;", sqliteConnection))
                {
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Hash _publicKey = new Hash((byte[])reader[0]);
                                string userName = (string)reader[1];
                                long balance = (long)reader[2];
                                long accountState = (long)reader[3];

                                long networkType = (long)reader[4];
                                long accountType = (long)reader[5];

                                long lastTransactionTime = (long)reader[6];

                                if (accountFetch != null)
                                {
                                    AccountInfo accountInfo = new AccountInfo(_publicKey, balance, userName, (AccountState)accountState,
                                        (NetworkType)networkType, (AccountType)accountType, lastTransactionTime);

                                    accountFetch(accountInfo);
                                }

                                response = DBResponse.FetchSuccess;

                                Records++;
                            }
                        }
                    }
                }

            });

            return new Tuple<DBResponse, long>(response, Records);
        }
        public DBResponse AddUpdate(AccountInfo accountInfo, DbTransaction transaction)
        {
            bool doUpdate = false;

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT PublicKey FROM Ledger WHERE PublicKey = @publicKey;",
                sqliteConnection, (SQLiteTransaction)transaction))
            {
                cmd.Parameters.Add(new SQLiteParameter("@publicKey", accountInfo.PublicKey.Hex));
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        doUpdate = true; // Perform update as the entry already exists.
                    }
                }
            }

            DBResponse response = DBResponse.Exception;

            if (doUpdate)
            {
                // /////////////  Perform the UPDATE  ///////////////

                using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Ledger SET UserName = @userName, Balance = @balance, AccountState = @accountState, NetworkType=@networkType, AccountType=@accountType, LastTransactionTime = @lastTransactionTime WHERE PublicKey = @publicKey;",
                    sqliteConnection, (SQLiteTransaction)transaction))
                {
                    cmd.Parameters.Add(new SQLiteParameter("@publicKey", accountInfo.PublicKey.Hex));
                    cmd.Parameters.Add(new SQLiteParameter("@userName", accountInfo.Name));
                    cmd.Parameters.Add(new SQLiteParameter("@balance", accountInfo.Money));
                    cmd.Parameters.Add(new SQLiteParameter("@accountState", (byte)accountInfo.AccountState));
                    cmd.Parameters.Add(new SQLiteParameter("@networkType", (byte)accountInfo.NetworkType));
                    cmd.Parameters.Add(new SQLiteParameter("@accountType", (byte)accountInfo.AccountType));
                    cmd.Parameters.Add(new SQLiteParameter("@lastTransactionTime", accountInfo.LastTransactionTime));

                    if (cmd.ExecuteNonQuery() != 1)
                    {
                        response = DBResponse.UpdateFailed;
                    }
                    else
                    {
                        response = DBResponse.UpdateSuccess;
                    }
                }
            }
            else
            {
                // /////////////  Perform the INSERT  ///////////////

                using (SQLiteCommand cmd = new SQLiteCommand("INSERT INTO Ledger VALUES(@publicKey, @userName, @balance, @accountState, @networkType, @accountType, @lastTransactionTime);",
                    sqliteConnection, (SQLiteTransaction)transaction))
                {
                    cmd.Parameters.Add(new SQLiteParameter("@publicKey", accountInfo.PublicKey.Hex));
                    cmd.Parameters.Add(new SQLiteParameter("@userName", accountInfo.Name));
                    cmd.Parameters.Add(new SQLiteParameter("@balance", accountInfo.Money));
                    cmd.Parameters.Add(new SQLiteParameter("@accountState", (byte)accountInfo.AccountState));
                    cmd.Parameters.Add(new SQLiteParameter("@networkType", (byte)accountInfo.NetworkType));
                    cmd.Parameters.Add(new SQLiteParameter("@accountType", (byte)accountInfo.AccountType));
                    cmd.Parameters.Add(new SQLiteParameter("@lastTransactionTime", accountInfo.LastTransactionTime));

                    if (cmd.ExecuteNonQuery() != 1)
                    {
                        response = DBResponse.InsertFailed;
                    }
                    else
                    {
                        response = DBResponse.InsertSuccess;
                    }
                }
            }

            return response;
        }
        // //////////////////////////////////////

        public DBResponse AddUpdate(AccountInfo accountInfo)
        {
            SQLiteTransaction st = sqliteConnection.BeginTransaction();
            DBResponse resp = AddUpdate(accountInfo, st);
            st.Commit();
            return resp;
        }
        ////////////////////////

        public DBResponse FetchAccount(out AccountInfo accountInfo, string UserName)
        {
            DBResponse response = DBResponse.FetchFailed;
            accountInfo = default(AccountInfo);

            if (Utils.ValidateUserName(UserName))
            {
                using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Ledger WHERE UserName = @userName;", sqliteConnection))
                {
                    cmd.Parameters.Add(new SQLiteParameter("@userName", UserName));
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                Hash _publicKey = new Hash((byte[])reader[0]);
                                string userName = (string)reader[1];
                                long balance = (long)reader[2];
                                long accountState = (long)reader[3];

                                long networkType = (long)reader[4];
                                long accountType = (long)reader[5];

                                long lastTransactionTime = (long)reader[6];

                                if (userName == UserName)
                                {
                                    accountInfo = new AccountInfo(_publicKey, balance, userName, (AccountState)accountState,
                                        (NetworkType)networkType, (AccountType)accountType, lastTransactionTime);

                                    response = DBResponse.FetchSuccess;
                                }
                            }
                        }
                    }
                }
            }

            return response;
        }
Esempio n. 9
0
        public JS_AccountReply(AccountInfo accountInfo)
        {
            PublicKey = accountInfo.PublicKey.Hex;
            Money = accountInfo.Money;
            Name = accountInfo.Name;

            Address = accountInfo.GetAddress();

            NetworkType = accountInfo.NetworkType;
            AccountType = accountInfo.AccountType;

            AccountState = accountInfo.AccountState;
            LastTransactionTime = accountInfo.LastTransactionTime;
        }
 public void Add(AccountInfo entry)
 {
     Leaves.Add(entry);
     LeafCount++;
 }