public static void ExecuteDataInsert(MySqlConnection conn, string tableName, List <string> parameter) { return; try { string mysql = $"insert into " + tableName + " values (null,"; foreach (string param in parameter) { mysql += "'" + param + "',"; } mysql = mysql.Substring(0, mysql.Length - 1); mysql += ");"; MySqlCommand mc = new MySqlCommand(mysql, conn); mc.ExecuteNonQuery(); } catch (Exception e) { LogConfig.Log($"Error when execute insert with {tableName}, reason: {e.Message}", LogConfig.LogLevel.Error); throw e; } }
public void Save(MySqlConnection conn, JObject jObject, string path) { JObject result = new JObject(); result["version"] = jObject["version"]; result["id"] = jObject["txid"]; result["type"] = jObject["asset"]["type"]; result["name"] = jObject["asset"]["name"]; result["amount"] = jObject["asset"]["amount"]; result["available"] = 1; result["precision"] = jObject["asset"]["precision"]; result["owner"] = jObject["asset"]["owner"]; result["admin"] = jObject["asset"]["admin"]; result["issuer"] = 1; result["expiration"] = 0; result["frozen"] = 0; List <string> slist = new List <string>(); slist.Add(SpiderHelper.getString(result["version"].ToString())); slist.Add(SpiderHelper.getString(result["id"].ToString())); slist.Add(SpiderHelper.getString(result["type"].ToString())); slist.Add(SpiderHelper.getString(result["name"].ToString())); slist.Add(SpiderHelper.getString(result["amount"].ToString())); slist.Add(SpiderHelper.getString(result["available"].ToString())); slist.Add(SpiderHelper.getString(result["precision"].ToString())); slist.Add(SpiderHelper.getString(result["owner"].ToString())); slist.Add(SpiderHelper.getString(result["admin"].ToString())); slist.Add(SpiderHelper.getString(result["issuer"].ToString())); slist.Add(SpiderHelper.getString(result["expiration"].ToString())); slist.Add(SpiderHelper.getString(result["frozen"].ToString())); { MysqlConn.ExecuteDataInsert(conn, DataTableName, slist); } LogConfig.Log($"SaveAsset {ChainHash} {result["name"]}", LogConfig.LogLevel.Info, ChainHash.ToString()); }
public static DataSet ExecuteDataSet(string tableName, Dictionary <string, string> where) { MySqlConnection conn = new MySqlConnection(conf); try { conn.Open(); string select = "select * from " + tableName; if (where.Count != 0) { select += " where"; } foreach (var dir in where) { select += " " + dir.Key + "='" + dir.Value + "'"; select += " and"; } if (where.Count > 0) { select = select.Substring(0, select.Length - 4); } MySqlDataAdapter adapter = new MySqlDataAdapter(select, conf); DataSet ds = new DataSet(); adapter.Fill(ds); return(ds); } catch (Exception e) { LogConfig.Log($"Error when execute select {tableName}, reason:{e.Message}", LogConfig.LogLevel.Error); throw e; } finally { conn.Close(); } }
private uint GetBlock(uint height) { try { Block block = blockchain.Store.GetBlock(height); JObject json = block.ToJson(); json["confirmations"] = blockchain.Height - block.Index + 1; UInt256 hash = blockchain.Store.GetNextBlockHash(block.Hash); if (hash != null) { json["nextblockhash"] = hash.ToString(); } tran = new TransactionDal(); tran.BeginTransaction(); { this.block.Save(tran.conn, json, height); //每获取一个块做一次高度记录,方便下次启动时做开始高度 listHeight.Save(tran.conn, chainHash.ToString(), height.ToString()); } tran.CommitTransaction(); tran.DisposeTransaction(); return(height + 1); } catch (MySqlException e) { retryNum++; LogConfig.Log($"error occured when call getblock {height} {chainHash}, reason:{e.Message}", LogConfig.LogLevel.Error); tran.RollbackTransaction(); if (retryNum > 1) { throw e; } return(GetBlock(height)); } }
public void Save(MySqlConnection conn, JObject jToken) { JObject executions = null; try { if (jToken != null) { executions = jToken["executions"]; } } catch (Exception e) { } //try //{ // if (!TryGetDB(ChainHash, out DB db)) return; // if (db.IsDisposed) return; // UInt256 hash = UInt256.Parse(SpiderHelper.getString(jToken["txid"].ToString())); // if (!db.TryGet(ReadOptions.Default, hash.ToArray(), out Slice value)) // throw new RpcException(-100, "Unknown transaction"); // result = JObject.Parse(value.ToString()); // if (result != null) // executions = result["executions"]; //} //catch (Exception e) //{ // LogConfig.Log($"error occured when call getapplicationlog, chain:{ChainHash} height:{blockHeight}, reason:{e.Message}", LogConfig.LogLevel.Error); // //throw e; //} if (jToken != null && executions != null) { foreach (var execution in executions as JArray) { List <string> slist = new List <string>(); slist.Add(SpiderHelper.getString(jToken["txid"].ToString())); slist.Add(SpiderHelper.getString(execution["vmstate"].ToString())); slist.Add(SpiderHelper.getString(execution["gas_consumed"].ToString())); slist.Add(SpiderHelper.getString(execution["stack"].ToString())); slist.Add(SpiderHelper.getString(execution["notifications"].ToString().Replace(@"[/n/r]", ""))); Dictionary <string, string> where = new Dictionary <string, string>(); where.Add("txid", SpiderHelper.getString(jToken["txid"].ToString())); MysqlConn.Delete(conn, DataTableName, where); MysqlConn.ExecuteDataInsert(conn, DataTableName, slist); LogConfig.Log($"SaveNotify {ChainHash} {jToken["txid"]}", LogConfig.LogLevel.Info, ChainHash.ToString()); if (execution["vmstate"].ToString().Contains("FAULT")) { return; } JObject notifications = execution["notifications"]; foreach (JObject notify in notifications as JArray) { JArray values = notify["state"]["value"] as JArray; if (values[0]["type"].ToString() == "ByteArray") { string transfer = Encoding.UTF8.GetString(SpiderHelper.HexString2Bytes(SpiderHelper.getString(values[0]["value"].ToString()))); string contract = SpiderHelper.getString(notify["contract"].ToString()); if (transfer == "transfer") { JObject nep5 = new JObject(); nep5["assetid"] = contract; nep5Asset.Save(conn, nep5); //存储Nep5Transfer内容 JObject tx = new JObject(); tx["txid"] = SpiderHelper.getString(jToken["txid"].ToString()); tx["n"] = 0; tx["asset"] = contract; if (SpiderHelper.getString(values[1]["value"].ToString()) == "") { tx["from"] = ""; } else { tx["from"] = UInt160.Parse(SpiderHelper.getString(values[1]["value"].ToString())).ToAddress(); } tx["to"] = UInt160.Parse(SpiderHelper.getString(values[2]["value"].ToString())).ToAddress(); if (SpiderHelper.getString(values[3]["type"].ToString()) == "ByteArray") { tx["value"] = new BigInteger(SpiderHelper.HexString2Bytes(SpiderHelper.getString(values[3]["value"].ToString()))).ToString(); } else { tx["value"] = BigInteger.Parse(SpiderHelper.getString(values[3]["value"].ToString()), NumberStyles.AllowHexSpecifier).ToString(); } JObject j = new JObject(); j["address"] = SpiderHelper.getString(tx["to"].ToString()); j["txid"] = SpiderHelper.getString(tx["txid"].ToString()); address.Save(conn, j); addressAsset.Save(conn, SpiderHelper.getString(tx["to"].ToString()), contract, ""); address_tx.Save(conn, j); nep5Transfer.Save(conn, tx); } } } } } }
public static void CreateTable(string type, string tableName) { string createSql = ""; switch (type) { case TableType.Block: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, hash varchar(255), size varchar(255), version varchar(255)," + " time int(11), indexx int(11), script varchar(2048), txcount varchar(45))"; break; case TableType.Address: createSql = "create table " + tableName + " (id int(11) primary key auto_increment, addr varchar(255)," + " firsttxid varchar(255), lasttxid varchar(255), txcount int(11))"; break; case TableType.Address_tx: createSql = "create table " + tableName + " (id int(11) primary key auto_increment, addr varchar(255)," + " txid varchar(255))"; break; case TableType.Transaction: createSql = "create table " + tableName + " (id int(11) primary key auto_increment, txid varchar(255)," + " size int(11), type varchar(45), version tinyint(3), attributes varchar(2048)," + " sys_fee int(11), blockindex varchar(45), gas_limit varchar(45), gas_price varchar(45), account varchar(255))"; break; case TableType.Notify: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, txid varchar(255), vmstate varchar(255), gas_consumed varchar(255)," + " stack varchar(2048))"; break; case TableType.NEP5Asset: createSql = "create table " + tableName + " (id int(11) primary key auto_increment, assetid varchar(150), totalsupply varchar(45)," + " name varchar(150), symbol varchar(150), decimals varchar(45))"; break; case TableType.NEP5Transfer: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, txid varchar(255)," + " n int(11), asset varchar(255), fromx varchar(255), tox varchar(255), value varchar(255))"; break; case TableType.UTXO: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, addr varchar(255), txid varchar(255)," + " n int(11), asset varchar(255), value varchar(255), createHeight int(11), used varchar(255), useHeight int(11), claimed varchar(255))"; break; case TableType.Hash_List: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, hashlist longtext)"; break; case TableType.Appchainstate: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, version varchar(255), hash varchar(255), name varchar(255)," + " owner varchar(255), timestamp varchar(255), seedlist varchar(2048), validators varchar(2048))"; break; case TableType.Chainlistheight: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, chainhash varchar(255), chainheight varchar(255))"; break; case TableType.Address_Asset: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, addr varchar(255), asset varchar(255), type varchar(255))"; break; case TableType.Tx_Script_Method: createSql = "create table " + tableName + " (id bigint(20) primary key auto_increment, txid varchar(255), calltype varchar(255), method varchar(255), contract varchar(255), blockheight varchar(255))"; break; } using (MySqlConnection conn = new MySqlConnection(conf)) { conn.Open(); try { using (MySqlCommand cmd = new MySqlCommand(createSql, conn)) { cmd.ExecuteNonQuery(); } LogConfig.Log("建表成功 " + tableName, LogConfig.LogLevel.Info); } catch (Exception e) { LogConfig.Log($"建表失败 {tableName}, reason:{e.Message}", LogConfig.LogLevel.Fatal); throw e; } finally { conn.Close(); AlterTable(type, tableName); } } }
public static void AlterTable(string type, string tableName) { string alterSql = ""; switch (type) { case TableType.Block: alterSql = "alter table " + tableName + " add index index_name (indexx)"; break; case TableType.Address: alterSql = "alter table " + tableName + " add index index_name (addr)"; break; case TableType.Address_tx: alterSql = "alter table " + tableName + " add index index_name (addr)"; break; case TableType.Transaction: alterSql = "alter table " + tableName + " add index index_name (txid)"; break; case TableType.Notify: alterSql = "alter table " + tableName + " add index index_name (txid)"; break; case TableType.NEP5Transfer: alterSql = "alter table " + tableName + " add index index_name (txid)"; break; case TableType.UTXO: alterSql = "alter table " + tableName + " add index index_name (addr,used)"; break; case TableType.Address_Asset: alterSql = "alter table " + tableName + " add index index_name (addr,asset)"; break; case TableType.Tx_Script_Method: alterSql = "alter table " + tableName + " add index index_name (txid,blockheight)"; break; default: return; } using (MySqlConnection conn = new MySqlConnection(conf)) { conn.Open(); try { using (MySqlCommand cmd = new MySqlCommand(alterSql, conn)) { cmd.ExecuteNonQuery(); } LogConfig.Log("插入索引成功 " + tableName, LogConfig.LogLevel.Info); } catch (Exception e) { LogConfig.Log($"插入索引失败 {tableName}, reason:{e.Message}", LogConfig.LogLevel.Fatal); throw e; } finally { conn.Close(); } } }