Ejemplo n.º 1
0
func MakeTest(txSlice [][]byte, blockData *utils.BlockData, txType string, testType string) error {

	db := DbConn()

	parser := new(dcparser.Parser)
	parser.DCDB = db
	parser.TxSlice = txSlice
	parser.BlockData = blockData
	parser.TxHash = []byte("111111111111111")
	parser.Variables, _ = db.GetAllVariables()

	// делаем снимок БД в виде хэшей до начала тестов
	hashesStart, err := AllHashes(db)
	if err != nil {
		return err
	}

	//fmt.Println("dcparser."+txType+"Init")
	err0 := utils.CallMethod(parser, txType+"Init")
	if i, ok := err0.(error); ok {
		fmt.Println(err0.(error), i)
		return err0.(error)
	}

	if testType == "work_and_rollback" {

		err0 = utils.CallMethod(parser, txType)
		if i, ok := err0.(error); ok {
			fmt.Println(err0.(error), i)
			return err0.(error)
		}

		//fmt.Println("-------------------")
		// узнаем, какие таблицы были затронуты в результате выполнения основного метода
		hashesMiddle, err := AllHashes(db)
		if err != nil {
			return utils.ErrInfo(err)
		}
		var tables []string
		//fmt.Println("hashesMiddle", hashesMiddle)
		//fmt.Println("hashesStart", hashesStart)
		for table, hash := range hashesMiddle {
			if hash != hashesStart[table] {
				tables = append(tables, table)
			}
		}
		fmt.Println("tables", tables)

		// rollback
		err0 := utils.CallMethod(parser, txType+"Rollback")
		if i, ok := err0.(error); ok {
			fmt.Println(err0.(error), i)
			return err0.(error)
		}

		// сраниим хэши, которые были до начала и те, что получились после роллбэка
		hashesEnd, err := AllHashes(db)
		if err != nil {
			return utils.ErrInfo(err)
		}
		for table, hash := range hashesEnd {
			if hash != hashesStart[table] {
				fmt.Println("ERROR in table ", table)
			}
		}

	} else if (len(os.Args) > 1 && os.Args[1] == "w") || testType == "work" {
		err0 = utils.CallMethod(parser, txType)
		if i, ok := err0.(error); ok {
			fmt.Println(err0.(error), i)
			return err0.(error)
		}
	} else if (len(os.Args) > 1 && os.Args[1] == "r") || testType == "rollback" {
		err0 = utils.CallMethod(parser, txType+"Rollback")
		if i, ok := err0.(error); ok {
			fmt.Println(err0.(error), i)
			return err0.(error)
		}
	}
	return nil
}
Ejemplo n.º 2
0
func MakeFrontTest(transactionArray [][]byte, time int64, dataForSign string, txType string, userId int64, MY_PREFIX string, blockId int64) error {

	db := DbConn()

	priv, pub := genKeys()

	nodeArr := []string{"new_admin", "votes_node_new_miner", "NewPct"}
	var binSign []byte
	if utils.InSliceString(txType, nodeArr) {

		err := db.ExecSql("UPDATE my_node_keys SET private_key = ?", priv)
		if err != nil {
			return utils.ErrInfo(err)
		}
		err = db.ExecSql("UPDATE miners_data SET node_public_key = [hex] WHERE user_id = ?", pub, userId)
		if err != nil {
			return utils.ErrInfo(err)
		}

		k, err := db.GetNodePrivateKey(MY_PREFIX)
		if err != nil {
			return utils.ErrInfo(err)
		}
		fmt.Println("k", k)
		privateKey, err := utils.MakePrivateKey(k)
		if err != nil {
			return utils.ErrInfo(err)
		}
		//fmt.Println("privateKey.PublicKey", privateKey.PublicKey)
		//fmt.Println("privateKey.D", privateKey.D)
		//fmt.Printf("privateKey.N %x\n", privateKey.N)
		//fmt.Println("privateKey.Public", privateKey.Public())
		binSign, err = rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA1, utils.HashSha1(dataForSign))
		//nodePublicKey, err := db.GetNodePublicKey(userId)
		//fmt.Println("nodePublicKey", nodePublicKey)
		//if err != nil {
		//	return utils.ErrInfo(err)
		//}
		//CheckSignResult, err := utils.CheckSign([][]byte{nodePublicKey}, dataForSign, binSign, true);
		//fmt.Printf("binSign: %x\n", binSign)
		//fmt.Println("err", err)
		//fmt.Println("CheckSignResult", CheckSignResult)

	} else {

		err := db.ExecSql("UPDATE my_keys SET private_key = ?", priv)
		if err != nil {
			return utils.ErrInfo(err)
		}
		err = db.ExecSql("UPDATE users SET public_key_0 = [hex]", pub)
		if err != nil {
			return utils.ErrInfo(err)
		}

		k, err := db.GetPrivateKey(MY_PREFIX)
		privateKey, err := utils.MakePrivateKey(k)
		if err != nil {
			return utils.ErrInfo(err)
		}
		binSign, err = rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA1, utils.HashSha1(dataForSign))
		binSign = utils.EncodeLengthPlusData(binSign)
	}

	//fmt.Println("HashSha1", utils.HashSha1(dataForSign))
	//fmt.Printf("binSign %x\n", binSign)
	//fmt.Println("dataForSign", dataForSign)
	transactionArray = append(transactionArray, binSign)

	parser := new(dcparser.Parser)
	parser.DCDB = db
	parser.GoroutineName = "test"
	parser.TxSlice = transactionArray
	parser.BlockData = &utils.BlockData{BlockId: blockId, Time: time, UserId: userId}
	parser.TxHash = []byte("111111111111111")
	parser.Variables, _ = parser.DCDB.GetAllVariables()

	err0 := utils.CallMethod(parser, txType+"Init")
	if i, ok := err0.(error); ok {
		fmt.Println(err0.(error), i)
		return err0.(error)
	}
	err0 = utils.CallMethod(parser, txType+"Front")
	if i, ok := err0.(error); ok {
		fmt.Println(err0.(error), i)
		return err0.(error)
	}
	err0 = utils.CallMethod(parser, txType+"RollbackFront")
	if i, ok := err0.(error); ok {
		fmt.Println(err0.(error), i)
		return err0.(error)
	}
	return nil
}