Exemple #1
0
        static void test_db_tablewrite(string[] words)
        {
            try
            {
                Console.WriteLine("test db table");

                using (var snap = db.UseSnapShot())
                {
                    var writetask = db.CreateWriteTask();
                    {
                        //写100个uint32
                        for (var i = 0; i < 100; i++)
                        {
                            var key = ("key" + i).ToBytes_UTF8Encode();
                            writetask.Put(new byte[] { 0x01, 0x02, 0x03 }, key, DBValue.FromValue(DBValue.Type.UINT32, (UInt32)i));
                        }
                        //写100个字符串
                        for (var i = 0; i < 100; i++)
                        {
                            var key = ("skey" + i).ToBytes_UTF8Encode();
                            writetask.Put(new byte[] { 0x01, 0x02, 0x03 }, key, DBValue.FromValue(DBValue.Type.String, "abcdefg" + i));
                        }

                        db.Write(writetask);
                    }
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("error:" + err.Message);
            }
        }
Exemple #2
0
        static void test_db_tablecreate(string[] words)
        {
            try
            {
                Console.WriteLine("test db table");

                using (var snap = db.UseSnapShot())
                {
                    var writetask = db.CreateWriteTask();
                    {
                        var info = new TableInfo(
                            new byte[] { 0x01, 0x02, 0x03 }, //tableid 是区分表格的数据,至少长度2,太短的不允许
                                                             //下面三个参数都是提供表的信息,无所谓什么
                            "mytable",                       //tablename
                            "testtable0001",                 //tabledesc
                            DBValue.Type.String              //tablekeytype
                            );
                        writetask.CreateTable(info);

                        for (var i = 0; i < 100; i++)
                        {
                            var key = ("key" + i).ToBytes_UTF8Encode();
                            writetask.Put(new byte[] { 0x01, 0x02, 0x03 }, key, DBValue.FromValue(DBValue.Type.UINT32, (UInt32)i));
                        }
                        db.Write(writetask);
                    }
                }
                Console.WriteLine("create table and write 100 item.");
            }
            catch (Exception err)
            {
                Console.WriteLine("error:" + err.Message);
            }
        }
        static void WriteTable(string[] words)
        {
            if (lasthash == null)
            {
                Console.WriteLine("get block hash first.");
                return;
            }
            WriteTask write = new WriteTask();

            //必须添加上一个块的hash,要不然服务器不会接受的
            write.extData             = new System.Collections.Generic.Dictionary <string, byte[]>();
            write.extData["lasthash"] = lasthash;

            //createtable 123;

            //write.CreateTable(new TableInfo(new byte[] { 0x01, 0x02, 0x03 }, "hello world", "", DBValue.Type.String));

            write.Put(new byte[] { 0x01, 0x02, 0x03 }, "123".ToBytes_UTF8Encode(), DBValue.FromValue(DBValue.Type.String, "balabala"));

            var srcdata = write.ToBytes();


            var wiftest = "L2CmHCqgeNHL1i9XFhTLzUXsdr5LGjag4d56YY98FqEi4j5d83Mv";//对应地址 AdsNmzKPPG7HfmQpacZ4ixbv9XJHJs2ACz 作为服务器配置的writer
            var prikey  = ThinNeo.Helper_NEO.GetPrivateKeyFromWIF(wiftest);
            var pubkey  = ThinNeo.Helper_NEO.GetPublicKey_FromPrivateKey(prikey);
            var address = ThinNeo.Helper_NEO.GetAddress_FromPublicKey(pubkey);

            SignData signdata = SignData.Sign(prikey, srcdata);

            var b = signdata.VerifySign(address, srcdata);

            Console.WriteLine("sign result=" + b);
            var msg = client.Post_write(srcdata, signdata);

            Console.WriteLine("post write result block[" + msg.blockid + "] = " + msg.blockhash.ToString_Hex());
        }
Exemple #4
0
        public async Task OnDB_Write(NetMessage msgRecv, byte[] id)
        {
            var msg = NetMessage.Create("_db.write.back");

            msg.Params["_id"] = id;
            try
            {
                var data      = msgRecv.Params["taskdata"];
                var signdata  = msgRecv.Params["signdata"];
                var writetask = WriteTask.FromRaw(data);
                if (writetask.extData?.ContainsKey("lasthash") == true)
                {
                    var lastblockhashRecv = writetask.extData["lasthash"];
                    lock (dblock)
                    {
                        using (var snap = Program.storage.maindb.UseSnapShot())
                        {
                            var blockidlast = BitConverter.GetBytes((UInt64)(snap.DataHeight - 1));

                            byte[] taskhash = Helper.CalcHash256(data);//.Sha256.ComputeHash(data);
                            //不够用,还需要block高度
                            var lasthashFind = snap.GetValue(StorageService.tableID_BlockID2Hash, blockidlast).value;

                            if (Helper.BytesEquals(lastblockhashRecv, lasthashFind))
                            {
                                //数据追加处理
                                Action <WriteTask, byte[], IWriteBatch> afterparser = (_task, _data, _wb) =>
                                {
                                    if (_wb.snapshot.DataHeight != snap.DataHeight)
                                    {
                                        throw new Exception("sync problem,diff snap found.");
                                    }
                                    _wb.Put(StorageService.tableID_BlockID2Hash, snap.DataHeightBuf, DBValue.FromValue(DBValue.Type.Bytes, taskhash));
                                };
                                //写入数据
                                Program.storage.maindb.Write(writetask, afterparser);

                                //进表
                                msg.Params["blockid"]   = snap.DataHeightBuf;
                                msg.Params["blockhash"] = taskhash;
                            }
                            else
                            {
                                msg.Params["_error"] = "block hash is error".ToBytes_UTF8Encode();
                            }
                        }
                    }
                }
                else
                {
                    msg.Params["_error"] = "no last block hash.".ToBytes_UTF8Encode();
                }
            }
            catch (Exception err)
            {
                msg.Params["_error"] = err.Message.ToBytes_UTF8Encode();
            }

            //这个完全可以不要等待呀
            SendToClient(msg);
        }
        public void Init()
        {
            Console.CursorLeft = 0;
            Console.WriteLine(" == Open DB ==");


            maindb = new LightDB();

            state_DBOpen = false;
            string fullpath = System.IO.Path.GetFullPath(Program.config.server_storage_path);

            if (System.IO.Directory.Exists(fullpath) == false)
            {
                System.IO.Directory.CreateDirectory(fullpath);
            }
            string pathDB = System.IO.Path.Combine(fullpath, "maindb");

            try
            {
                maindb.Open(pathDB);
                state_DBOpen = true;
                Console.WriteLine("db opened in:" + pathDB);
            }
            catch (Exception err)
            {
                Console.WriteLine("error msg:" + err.Message);
            }
            if (state_DBOpen == false)
            {
                Console.WriteLine("open database fail. try to create it.");
                try
                {
                    DBCreateOption createop = new DBCreateOption();
                    createop.MagicStr  = Program.config.storage_maindb_magic;
                    createop.FirstTask = new WriteTask();

                    createop.FirstTask.CreateTable(new TableInfo(tableID_Writer, "_writeraddress_", "", DBValue.Type.String));

                    createop.FirstTask.Put(tableID_Writer, Program.config.storage_maindb_firstwriter_address.ToBytes_UTF8Encode(), DBValue.FromValue(DBValue.Type.BOOL, true));

                    createop.FirstTask.CreateTable(new TableInfo(tableID_BlockID2Hash, "_block:index->hash_", "", DBValue.Type.String));
                    createop.FirstTask.CreateTable(new TableInfo(tableID_BlockID2Verifiy, "_block:index->hash_", "", DBValue.Type.String));

                    var srcdata = createop.FirstTask.ToBytes();
                    var hash    = Helper.Sha256.ComputeHash(srcdata);

                    //用后处理写入hash
                    createop.afterparser = (_task, _data, _wb) =>
                    {
                        var keyindexzero = new byte[8];

                        //填充创世块hash
                        _wb.Put(StorageService.tableID_BlockID2Hash, keyindexzero, DBValue.FromValue(DBValue.Type.Bytes, hash));

                        //填充一个空字,校验数据
                        _wb.Put(StorageService.tableID_BlockID2Verifiy, keyindexzero, DBValue.FromValue(DBValue.Type.Bytes, new byte[1]));
                    };

                    maindb.Open(pathDB, createop);
                    Console.WriteLine("db created in:" + pathDB);
                    state_DBOpen = true;
                }
                catch (Exception err)
                {
                    Console.WriteLine("error msg:" + err.Message);
                }
            }
        }
Exemple #6
0
 public void Put(Slice key, Slice value)
 {
     base.Put(tableId, key.buffer, DBValue.FromValue(DBValue.Type.Bytes, value.buffer));
 }