Commit ff574fb8 authored by jiangpeng's avatar jiangpeng Committed by vipwzw

fix frozen utxo expire

parent e54b919d
......@@ -117,7 +117,7 @@ func createPub2PrivTx(cmd *cobra.Command, args []string) {
}
} else if expiretype == 1 {
if expire <= 0 {
expire = int64(time.Hour / time.Second)
expire = int64(time.Hour)
}
} else {
fmt.Println("Invalid expiretype", expiretype)
......@@ -179,7 +179,7 @@ func createPriv2PrivTx(cmd *cobra.Command, args []string) {
}
} else if expiretype == 1 {
if expire <= 0 {
expire = int64(time.Hour / time.Second)
expire = int64(time.Hour)
}
} else {
fmt.Println("Invalid expiretype", expiretype)
......@@ -243,7 +243,7 @@ func createPriv2PubTx(cmd *cobra.Command, args []string) {
}
} else if expiretype == 1 {
if expire <= 0 {
expire = int64(time.Hour / time.Second)
expire = int64(time.Hour)
}
} else {
fmt.Println("Invalid expiretype", expiretype)
......
......@@ -21,8 +21,7 @@ func (ftxos *FTXOsSTXOsInOneTx) IsExpire(blockheight, blocktime int64) bool {
}
// SetExpire 设定过期
func (ftxos *FTXOsSTXOsInOneTx) SetExpire(tx *types.Transaction) {
expire := tx.Expire
func (ftxos *FTXOsSTXOsInOneTx) SetExpire(expire int64) {
if expire > types.ExpireBound {
// FTXO的超时为时间时,则用Tx的过期时间加上12秒后认为超时
ftxos.Expire = expire + 12
......
......@@ -606,6 +606,7 @@ func (policy *privacyPolicy) createPublic2PrivacyTx(req *types.ReqCreateTransact
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(types.ExecName(privacytypes.PrivacyX)),
}
tx.SetExpire(time.Duration(req.Expire))
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
......@@ -686,6 +687,7 @@ func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransac
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(types.ExecName(privacytypes.PrivacyX)),
}
tx.SetExpire(time.Duration(req.Expire))
if isPara {
tx.Fee, err = tx.GetRealFee(types.GInt("MinFee"))
if err != nil {
......@@ -695,7 +697,7 @@ func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransac
}
// 创建交易成功,将已经使用掉的UTXO冻结,需要注意此处获取的txHash和交易发送时的一致
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
policy.saveFTXOInfo(tx.GetExpire(), req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
......@@ -771,6 +773,7 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(types.ExecName(privacytypes.PrivacyX)),
}
tx.SetExpire(time.Duration(req.Expire))
if isPara {
tx.Fee, err = tx.GetRealFee(types.GInt("MinFee"))
if err != nil {
......@@ -779,7 +782,7 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
}
}
// 创建交易成功,将已经使用掉的UTXO冻结,需要注意此处获取的txHash和交易发送时的一致
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
policy.saveFTXOInfo(tx.GetExpire(), req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
......@@ -790,9 +793,9 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
return tx, nil
}
func (policy *privacyPolicy) saveFTXOInfo(tx *types.Transaction, token, sender, txhash string, selectedUtxos []*txOutputInfo) {
func (policy *privacyPolicy) saveFTXOInfo(expire int64, token, sender, txhash string, selectedUtxos []*txOutputInfo) {
//将已经作为本次交易输入的utxo进行冻结,防止产生双花交易
policy.store.moveUTXO2FTXO(tx, token, sender, txhash, selectedUtxos)
policy.store.moveUTXO2FTXO(expire, token, sender, txhash, selectedUtxos)
//TODO:需要加入超时处理,需要将此处的txhash写入到数据库中,以免钱包瞬间奔溃后没有对该笔隐私交易的记录,
//TODO:然后当该交易得到执行之后,没法将FTXO转化为STXO,added by hezhengjun on 2018.6.5
}
......
......@@ -5,10 +5,6 @@
package wallet
import (
"sync"
"sync/atomic"
"time"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/db"
......@@ -16,6 +12,8 @@ import (
"github.com/33cn/chain33/types"
wcom "github.com/33cn/chain33/wallet/common"
privacytypes "github.com/33cn/plugin/plugin/dapp/privacy/types"
"sync"
"sync/atomic"
)
var (
......@@ -153,11 +151,6 @@ func (policy *privacyPolicy) SignTransaction(key crypto.PrivKey, req *types.ReqS
bizlog.Error("SignTransaction", "action type ", action.Ty, "signature action type ", signParam.ActionType)
return
}
expire, err := types.ParseExpire(req.GetExpire())
if err != nil {
return
}
tx.SetExpire(time.Duration(expire))
switch action.Ty {
case privacytypes.ActionPublic2Privacy:
// 隐私交易的公对私动作,不存在交易组的操作
......
......@@ -339,7 +339,7 @@ func (store *privacyStore) getPrivacyTokenUTXOs(token, addr string) (*walletUTXO
//calcKey4UTXOsSpentInTx------>types.FTXOsSTXOsInOneTx,将当前交易的所有花费的utxo进行打包,设置为ftxo,同时通过支付交易hash索引
//calcKey4FTXOsInTx----------->calcKey4UTXOsSpentInTx,创建该交易冻结的所有的utxo的信息
//状态转移,将utxo转移至ftxo,同时记录该生成tx的花费的utxo,这样在确认执行成功之后就可以快速将相应的FTXO转换成STXO
func (store *privacyStore) moveUTXO2FTXO(tx *types.Transaction, token, sender, txhash string, selectedUtxos []*txOutputInfo) {
func (store *privacyStore) moveUTXO2FTXO(expire int64, token, sender, txhash string, selectedUtxos []*txOutputInfo) {
FTXOsInOneTx := &privacytypes.FTXOsSTXOsInOneTx{}
newbatch := store.NewBatch(true)
for _, txOutputInfo := range selectedUtxos {
......@@ -357,7 +357,7 @@ func (store *privacyStore) moveUTXO2FTXO(tx *types.Transaction, token, sender, t
FTXOsInOneTx.Tokenname = token
FTXOsInOneTx.Sender = sender
FTXOsInOneTx.Txhash = txhash
FTXOsInOneTx.SetExpire(tx)
FTXOsInOneTx.SetExpire(expire)
//设置在该交易中花费的UTXO
key1 := calcKey4UTXOsSpentInTx(txhash)
value1 := types.Encode(FTXOsInOneTx)
......@@ -956,7 +956,7 @@ func (store *privacyStore) moveSTXO2FTXO(tx *types.Transaction, txhash string, n
newbatch.Set(key1, value1)
bizlog.Info("moveSTXO2FTXO", "txhash ", txhash)
ftxosInOneTx.SetExpire(tx)
ftxosInOneTx.SetExpire(tx.GetExpire())
value = types.Encode(&ftxosInOneTx)
newbatch.Set(key, value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment