Ejemplo n.º 1
0
        public void MinerSave()
        {
            if (httpPool != null)
            {
                Dictionary <string, MinerTask> miners = httpPool.GetMinerRewardMin(out long miningHeight);
                if (miners != null && miningHeight + 3 < httpPool.height)
                {
                    using (DbSnapshot snapshot = PoolDBStore.GetSnapshot(0, true))
                    {
                        string json         = snapshot.Get("Pool_H_Miner");
                        long   height_miner = -1;
                        if (!string.IsNullOrEmpty(json))
                        {
                            long.TryParse(json, out height_miner);
                        }

                        if (height_miner == -1 || height_miner < miningHeight)
                        {
                            snapshot.Add("Pool_H_Miner", miningHeight.ToString());
                            snapshot.Add("Pool_H2_" + miningHeight, JsonHelper.ToJson(miners));
                            snapshot.Commit();
                            httpPool.DelMiner(miningHeight);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public bool AddBlock(Block blk, bool replace = false)
        {
            if (blk != null && (replace || GetBlock(blk.hash) == null))
            {
                if (!Entity.Root.GetComponent <Consensus>().Check(blk))
                {
                    Log.Warning($"Block Check Error: {blk.ToStringEx()}");
                    return(false);
                }
                using (DbSnapshot snapshot = levelDBStore.GetSnapshot(0, true))
                {
                    List <string> list = snapshot.Heights.Get(blk.height.ToString());
                    if (list == null)
                    {
                        list = new List <string>();
                    }
                    list.Remove(blk.hash);
                    list.Add(blk.hash);

                    snapshot.Heights.Add(blk.height.ToString(), list);
                    snapshot.Blocks.Add(blk.hash, blk);
                    snapshot.Commit();
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        public bool AddBlock(Block blk)
        {
            if (blk != null && GetBlock(blk.hash) == null)
            {
                if (!Entity.Root.GetComponent <Consensus>().Check(blk))
                {
                    return(false);
                }
                using (DbSnapshot snapshot = levelDBStore.GetSnapshot())
                {
                    List <string> list = snapshot.Heights.Get(blk.height.ToString());
                    if (list == null)
                    {
                        list = new List <string>();
                    }
                    list.Remove(blk.hash);
                    list.Add(blk.hash);
                    snapshot.Heights.Add(blk.height.ToString(), list);

                    snapshot.Blocks.Add(blk.hash, blk);
                    snapshot.Commit();
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
        public static bool Test(string[] args)
        {
            //
            //DBTests tests = new DBTests();
            //tests.SetUp();
            //tests.Snapshot();
            var          tempPath     = System.IO.Directory.GetCurrentDirectory();
            var          randName     = "LevelDB";
            var          DatabasePath = System.IO.Path.Combine(tempPath, randName);
            LevelDBStore dbstore      = new LevelDBStore().Init(DatabasePath);

            using (DbSnapshot snapshot = dbstore.GetSnapshot(1))
            {
                snapshot.Blocks.Add("11", new Block()
                {
                    Address = "11"
                });
                snapshot.Blocks.Add("22", new Block()
                {
                    Address = "22"
                });
                var value1 = dbstore.Blocks.Get("11");
                var value2 = dbstore.Blocks.Get("22");
                snapshot.Commit();
                var result1 = dbstore.Blocks.Get("11");
                var result2 = dbstore.Blocks.Get("22");
            }

            using (DbSnapshot snapshot = dbstore.GetSnapshot(1))
            {
                snapshot.Blocks.Add("11", new Block()
                {
                    Address = "11"
                });
                snapshot.Blocks.Add("22", new Block()
                {
                    Address = "22"
                });
                var value1 = dbstore.Blocks.Get("11");
                var value2 = dbstore.Blocks.Get("22");
                snapshot.Commit();
                var result1 = dbstore.Blocks.Get("11");
                var result2 = dbstore.Blocks.Get("22");
            }

            return(true);
        }
Ejemplo n.º 5
0
        public static void test_delete(string[] args)
        {
            var          tempPath     = System.IO.Directory.GetCurrentDirectory();
            var          randName     = "LevelDB";
            var          DatabasePath = System.IO.Path.Combine(tempPath, randName);
            LevelDBStore dbstore      = new LevelDBStore().Init(DatabasePath);

            using (DbSnapshot snapshot = dbstore.GetSnapshot(1))
            {
                snapshot.Blocks.Add("11", new Block()
                {
                    Address = "11"
                });
                snapshot.Blocks.Add("22", new Block()
                {
                    Address = "22"
                });
                var value1 = dbstore.Blocks.Get("11");
                var value2 = dbstore.Blocks.Get("22");
                System.Console.WriteLine($"dbstore.test_delete value1: {value1}");
                snapshot.Commit();
                var result1 = dbstore.Blocks.Get("11");
                var result2 = dbstore.Blocks.Get("22");
                System.Console.WriteLine($"dbstore.test_delete value1: {result1}");
            }

            using (DbSnapshot snapshot = dbstore.GetSnapshot(1))
            {
                snapshot.Blocks.Delete("11");
                snapshot.Blocks.Delete("22");
                snapshot.Commit();
                var result1 = dbstore.Blocks.Get("11");
                var result2 = dbstore.Blocks.Get("22");
                System.Console.WriteLine($"dbstore.test_delete value1: {result1}");
            }
        }
Ejemplo n.º 6
0
        public override void Start()
        {
            rule = Entity.Root.GetComponent <Rule>();
            ComponentNetMsg componentNetMsg = Entity.Root.GetComponent <ComponentNetMsg>();

            componentNetMsg.registerMsg(NetOpcode.P2P_NewBlock, P2P_NewBlock_Handle);

            string genesisText = File.ReadAllText("./Data/genesisBlock.dat");
            Block  blk         = JsonHelper.FromJson <Block>(genesisText);

            superAddress = blk.Address;

            long.TryParse(Entity.Root.GetComponent <LevelDBStore>().Get("UndoHeight"), out long UndoHeight);
            if (UndoHeight == 0)
            {
                if (true)
                {
                    blockMgr.AddBlock(blk);
                    ApplyGenesis(blk);
                }
            }

            string consData = Base58.Encode(FileHelper.GetFileData("./Data/Contract/RuleContract_v1.0.lua").ToByteArray());

            consAddress = Wallet.ToAddress(CryptoHelper.Sha256(Encoding.UTF8.GetBytes(consData)));

            using (DbSnapshot snapshot = levelDBStore.GetSnapshot())
            {
                LuaVMScript luaVMScript = new LuaVMScript()
                {
                    script = FileHelper.GetFileData("./Data/Contract/RuleContract_curr.lua").ToByteArray()
                };
                snapshot.Contracts.Add(consAddress, luaVMScript);
                snapshot.Commit();
            }
            ruleInfos = luaVMEnv.GetRules(consAddress, UndoHeight);

            if (bRun)
            {
                Run();
            }
        }
Ejemplo n.º 7
0
        public static void test_undo(string[] args)
        {
            System.Console.WriteLine($"test_undo ...");

            //
            //DBTests tests = new DBTests();
            //tests.SetUp();
            //tests.Snapshot();
            var          tempPath     = System.IO.Directory.GetCurrentDirectory();
            var          randName     = "LevelDB";
            var          DatabasePath = System.IO.Path.Combine(tempPath, randName);
            LevelDBStore dbstore      = new LevelDBStore().Init(DatabasePath);

            for (int rr = 1; rr <= 30; rr++)
            {
                long.TryParse(dbstore.Get("UndoHeight"), out long UndoHeight);
                int random1 = 1000 + RandomHelper.Random() % 1000;

                if (UndoHeight < random1)
                {
                    for (long i = UndoHeight + 1; i <= random1; i++)
                    {
                        using (DbSnapshot snapshot = dbstore.GetSnapshot(i))
                        {
                            snapshot.Transfers.Add("undos_test", new BlockSub()
                            {
                                hash = $"Address_{i}"
                            });
                            snapshot.Commit();
                        }
                    }
                }

                {
                    using (DbSnapshot snapshot = dbstore.GetSnapshot(0))
                    {
                        var result1 = snapshot.Transfers.Get("undos_test");

                        long.TryParse(dbstore.Get("UndoHeight"), out long UndoHeight2);
                        if (result1.hash != $"Address_{UndoHeight2.ToString()}")
                        {
                            System.Console.WriteLine($"dbstore.Undo {random1} error1: {result1.hash}");
                        }
                        //System.Console.WriteLine($"dbstore.Undo {random1} error1: {result1.txid}");
                    }
                }

                if (UndoHeight > random1)
                {
                    dbstore.UndoTransfers(random1);
                }

                using (DbSnapshot snapshot = dbstore.GetSnapshot(0))
                {
                    var result2 = snapshot.Transfers.Get("undos_test");

                    long.TryParse(dbstore.Get("UndoHeight"), out long UndoHeight2);
                    if (result2.hash != $"Address_{UndoHeight2.ToString()}")
                    {
                        System.Console.WriteLine($"dbstore.Undo {random1} error2: {result2.hash}");
                    }
                    //System.Console.WriteLine($"dbstore.Undo {random1} error2: {result2.txid}");
                }
            }
        }
Ejemplo n.º 8
0
        static public void MakeSnapshot(Dictionary <string, string> param)
        {
            Console.WriteLine($"levelDB.Init {param["db"]}");
            LevelDBStore levelDB = new LevelDBStore();

            levelDB.Init(param["db"]);

            if (param.ContainsKey("height") && long.TryParse(param["height"], out long height))
            {
                levelDB.UndoTransfers(height);
            }
            long.TryParse(levelDB.Get("UndoHeight"), out long transferHeight);
            Console.WriteLine($"transferHeight: {transferHeight}");

            var DatabasePath = $"./Data/LevelDB_Snapshot_{transferHeight}";

            if (Directory.Exists(DatabasePath))
            {
                Console.WriteLine($"Directory LevelDB_Snapshot Exists");
                return;
            }
            LevelDBStore snapshotDB = new LevelDBStore();

            snapshotDB.Init(DatabasePath);

            int count = 0;

            using (var it = levelDB.db.CreateIterator())
            {
                for (it.SeekToFirst(); it.IsValid(); it.Next(), count++)
                {
                    //Log.Info($"Value as string: {it.KeyAsString()}");
                    if (it.KeyAsString().IndexOf("_undo_") == -1 &&
                        it.KeyAsString().IndexOf("Blocks") != 0 &&
                        it.KeyAsString().IndexOf("BlockChain") != 0 &&
                        it.KeyAsString().IndexOf("Queue") != 0 &&
                        it.KeyAsString().IndexOf("List") != 0 &&
                        it.KeyAsString().IndexOf("Heights") != 0 &&
                        it.KeyAsString().IndexOf("Undos___") != 0)
                    {
                        if (it.KeyAsString().IndexOf("Trans___") == 0)
                        {
                            var slice = JsonHelper.FromJson <DbCache <BlockSub> .Slice>(it.ValueAsString());
                            if (slice != null && slice.obj.height != 0)
                            {
                                snapshotDB.Put(it.KeyAsString(), $"{{\"obj\":{{\"height\":{slice.obj.height}}}}}");
                                Console.WriteLine($"Processed tran: {it.KeyAsString()}");
                            }
                        }
                        else
                        if (it.KeyAsString().IndexOf("Snap___") == 0)
                        {
                            if (it.KeyAsString().IndexOf("Snap___Rule_") == 0)
                            {
                                var key         = it.KeyAsString();
                                var pos1        = "Snap___Rule_".Length;
                                var pos2        = key.Length;
                                var hegihtTemp1 = key.Substring(pos1, pos2 - pos1);
                                var hegihtTemp2 = long.Parse(hegihtTemp1);
                                if (hegihtTemp2 > transferHeight - 5 && hegihtTemp2 < transferHeight + 5)
                                {
                                    snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                                    Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                                }
                            }
                            else
                            if (it.KeyAsString().IndexOf("_Reward") != -1)
                            {
                                var key  = it.KeyAsString();
                                var pos1 = "Snap___".Length;
                                var pos2 = key.IndexOf("_Reward");

                                var hegihtTemp1 = key.Substring(pos1, pos2 - pos1);
                                var hegihtTemp2 = long.Parse(hegihtTemp1);
                                if (hegihtTemp2 > transferHeight - 5 && hegihtTemp2 < transferHeight + 5)
                                {
                                    snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                                    Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                                }
                            }
                            else
                            {
                                snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                                Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                            }
                        }
                        else
                        {
                            snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                            Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                        }
                    }

                    if (count % 1000000 == 0)
                    {
                        Console.WriteLine($"Processed Count:{count}");
                    }
                }
            }

            using (DbSnapshot dbNew = snapshotDB.GetSnapshot(0, true))
                using (DbSnapshot dbOld = levelDB.GetSnapshot())
                {
                    for (long ii = transferHeight - 3; ii <= transferHeight + 2; ii++)
                    {
                        Console.WriteLine($"Processed height: {ii}");
                        var heights = dbOld.Heights.Get(ii.ToString());
                        for (int jj = 0; jj < heights.Count; jj++)
                        {
                            dbNew.Blocks.Add(heights[jj], dbOld.Blocks.Get(heights[jj]));
                        }

                        dbNew.Heights.Add(ii.ToString(), heights);
                        dbNew.BlockChains.Add(ii.ToString(), dbOld.BlockChains.Get(ii.ToString()));
                    }

                    dbNew.Commit();
                }

            Console.WriteLine($"MakeSnapshot Complete");

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }