Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
0559134e
Commit
0559134e
authored
Mar 19, 2021
by
jiangpeng
Committed by
vipwzw
Mar 24, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
privacy:update privacy tx storing
* add pub2priv tx db index * refactor add del block tx in wallet
parent
22f27b21
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
253 additions
and
223 deletions
+253
-223
privacy.go
plugin/dapp/privacy/wallet/privacy.go
+222
-189
privacy_test.go
plugin/dapp/privacy/wallet/privacy_test.go
+1
-1
privacybizpolicy.go
plugin/dapp/privacy/wallet/privacybizpolicy.go
+20
-10
privacystore.go
plugin/dapp/privacy/wallet/privacystore.go
+7
-15
privacystore_test.go
plugin/dapp/privacy/wallet/privacystore_test.go
+3
-8
No files found.
plugin/dapp/privacy/wallet/privacy.go
View file @
0559134e
...
@@ -8,13 +8,14 @@ import (
...
@@ -8,13 +8,14 @@ import (
"bytes"
"bytes"
"encoding/hex"
"encoding/hex"
"errors"
"errors"
"fmt"
"sort"
"sort"
"strings"
"strings"
"sync"
"sync"
"time"
"time"
"unsafe"
"unsafe"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto"
...
@@ -965,46 +966,42 @@ func (policy *privacyPolicy) signatureTx(tx *types.Transaction, privacyInput *pr
...
@@ -965,46 +966,42 @@ func (policy *privacyPolicy) signatureTx(tx *types.Transaction, privacyInput *pr
}
}
func
(
policy
*
privacyPolicy
)
buildAndStoreWalletTxDetail
(
param
*
buildStoreWalletTxDetailParam
)
{
func
(
policy
*
privacyPolicy
)
buildAndStoreWalletTxDetail
(
param
*
buildStoreWalletTxDetailParam
)
{
blockheight
:=
param
.
block
.
Block
.
Height
*
maxTxNumPerBlock
+
int64
(
param
.
index
)
heightstr
:=
fmt
.
Sprintf
(
"%018d"
,
blockheight
)
txInfo
:=
param
.
txInfo
bizlog
.
Debug
(
"buildAndStoreWalletTxDetail"
,
"heightstr"
,
heightstr
,
"addDelType"
,
param
.
addDelType
)
heightstr
:=
dapp
.
HeightIndexStr
(
txInfo
.
blockHeight
,
int64
(
txInfo
.
txIndex
))
if
AddTx
==
param
.
addDelType
{
bizlog
.
Debug
(
"buildAndStoreWalletTxDetail"
,
"heightstr"
,
heightstr
,
"isRollBack"
,
txInfo
.
isRollBack
)
if
!
txInfo
.
isRollBack
{
var
txdetail
types
.
WalletTxDetail
var
txdetail
types
.
WalletTxDetail
key
:=
calcTxKey
(
heightstr
)
key
:=
calcTxKey
(
heightstr
)
txdetail
.
Tx
=
param
.
tx
txdetail
.
Tx
=
txInfo
.
tx
txdetail
.
Height
=
param
.
block
.
Block
.
Height
txdetail
.
Height
=
txInfo
.
block
Height
txdetail
.
Index
=
int64
(
param
.
i
ndex
)
txdetail
.
Index
=
int64
(
txInfo
.
txI
ndex
)
txdetail
.
Receipt
=
param
.
block
.
Receipts
[
param
.
i
ndex
]
txdetail
.
Receipt
=
txInfo
.
blockDetail
.
Receipts
[
txInfo
.
txI
ndex
]
txdetail
.
Blocktime
=
param
.
block
.
Block
.
BlockTime
txdetail
.
Blocktime
=
txInfo
.
blockDetail
.
Block
.
BlockTime
txdetail
.
ActionName
=
txdetail
.
Tx
.
ActionName
()
txdetail
.
ActionName
=
txInfo
.
actionName
txdetail
.
Amount
,
_
=
param
.
tx
.
Amount
()
txdetail
.
Amount
,
_
=
txInfo
.
tx
.
Amount
()
txdetail
.
Fromaddr
=
param
.
senderRecver
txdetail
.
Fromaddr
=
param
.
addr
//txdetail.Spendrecv = param.utxos
txdetailbyte
,
err
:=
proto
.
Marshal
(
&
txdetail
)
txdetailbyte
,
err
:=
proto
.
Marshal
(
&
txdetail
)
if
err
!=
nil
{
if
err
!=
nil
{
bizlog
.
Error
(
"buildAndStoreWalletTxDetail err"
,
"Height"
,
param
.
block
.
Block
.
Height
,
"index"
,
param
.
ind
ex
)
bizlog
.
Error
(
"buildAndStoreWalletTxDetail err"
,
"Height"
,
txInfo
.
blockHeight
,
"txHash"
,
txInfo
.
txHashH
ex
)
return
return
}
}
param
.
newbatch
.
Set
(
key
,
txdetailbyte
)
txInfo
.
batch
.
Set
(
key
,
txdetailbyte
)
if
param
.
isprivacy
{
//额外存储可以快速定位到接收隐私的交易
//额外存储可以快速定位到接收隐私的交易
if
sendTx
==
param
.
sendRecvFlag
{
if
sendTx
==
param
.
sendRecvFlag
{
txInfo
.
batch
.
Set
(
calcSendPrivacyTxKey
(
txInfo
.
assetExec
,
txInfo
.
assetSymbol
,
param
.
addr
,
heightstr
),
key
)
param
.
newbatch
.
Set
(
calcSendPrivacyTxKey
(
param
.
assetExec
,
param
.
tokenname
,
param
.
senderRecver
,
heightstr
),
key
)
}
else
if
recvTx
==
param
.
sendRecvFlag
{
}
else
if
recvTx
==
param
.
sendRecvFlag
{
txInfo
.
batch
.
Set
(
calcRecvPrivacyTxKey
(
txInfo
.
assetExec
,
txInfo
.
assetSymbol
,
param
.
addr
,
heightstr
),
key
)
param
.
newbatch
.
Set
(
calcRecvPrivacyTxKey
(
param
.
assetExec
,
param
.
tokenname
,
param
.
senderRecver
,
heightstr
),
key
)
}
}
}
}
else
{
}
else
{
param
.
newbatch
.
Delete
(
calcTxKey
(
heightstr
))
txInfo
.
batch
.
Delete
(
calcTxKey
(
heightstr
))
if
param
.
isprivacy
{
if
sendTx
==
param
.
sendRecvFlag
{
if
sendTx
==
param
.
sendRecvFlag
{
txInfo
.
batch
.
Delete
(
calcSendPrivacyTxKey
(
txInfo
.
assetExec
,
txInfo
.
assetSymbol
,
param
.
addr
,
heightstr
))
param
.
newbatch
.
Delete
(
calcSendPrivacyTxKey
(
param
.
assetExec
,
param
.
tokenname
,
param
.
senderRecver
,
heightstr
))
}
else
if
recvTx
==
param
.
sendRecvFlag
{
}
else
if
recvTx
==
param
.
sendRecvFlag
{
txInfo
.
batch
.
Delete
(
calcRecvPrivacyTxKey
(
txInfo
.
assetExec
,
txInfo
.
assetSymbol
,
param
.
addr
,
heightstr
))
param
.
newbatch
.
Delete
(
calcRecvPrivacyTxKey
(
param
.
assetExec
,
param
.
tokenname
,
param
.
senderRecver
,
heightstr
))
}
}
}
}
}
}
}
...
@@ -1055,193 +1052,229 @@ func (policy *privacyPolicy) checkWalletStoreData() {
...
@@ -1055,193 +1052,229 @@ func (policy *privacyPolicy) checkWalletStoreData() {
}
}
}
}
func
(
policy
*
privacyPolicy
)
addDelPrivacyTxsFromBlock
(
tx
*
types
.
Transaction
,
index
int32
,
block
*
types
.
BlockDetail
,
newbatch
db
.
Batch
,
addDelType
int32
)
{
func
(
policy
*
privacyPolicy
)
addDelPrivacyTxsFromBlock
(
tx
*
types
.
Transaction
,
index
int32
,
block
*
types
.
BlockDetail
,
batch
db
.
Batch
,
addDelType
int32
)
{
txhash
:=
tx
.
Hash
()
txhashstr
:=
hex
.
EncodeToString
(
txhash
)
_
,
err
:=
tx
.
Amount
()
if
err
!=
nil
{
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
,
"index"
,
index
,
"tx.Amount() error"
,
err
)
return
}
cfg
:=
policy
.
getWalletOperate
()
.
GetAPI
()
.
GetConfig
()
privacyPairs
,
err
:=
policy
.
getPrivacyKeyPairs
()
txExecRes
:=
block
.
Receipts
[
index
]
.
Ty
//钱包未开启隐私功能,或不存在地址直接返回
var
privateAction
privacytypes
.
PrivacyAction
if
len
(
privacyPairs
)
==
0
{
if
err
:=
types
.
Decode
(
tx
.
GetPayload
(),
&
privateAction
);
err
!=
nil
{
bizlog
.
Debug
(
"addDelPrivacyTxsFromBlock"
,
"getPrivacyKeyPairs err"
,
err
)
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
,
"index"
,
index
,
"Decode tx.GetPayload() error"
,
err
)
return
return
}
}
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"Enter addDelPrivacyTxsFromBlock txhash"
,
txhashstr
,
"index"
,
index
,
"addDelType"
,
addDelType
)
privacyOutput
:=
privateAction
.
GetOutput
()
txhash
:=
tx
.
Hash
()
if
privacyOutput
==
nil
{
txhashHex
:=
hex
.
EncodeToString
(
txhash
)
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
,
"index"
,
index
,
"privacyOutput is"
,
privacyOutput
)
var
action
privacytypes
.
PrivacyAction
if
err
:=
types
.
Decode
(
tx
.
GetPayload
(),
&
action
);
err
!=
nil
{
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashHex
,
"addDelType"
,
addDelType
,
"index"
,
index
,
"Decode tx.GetPayload() error"
,
err
)
return
return
}
}
assetExec
,
tokenname
:=
privateAction
.
GetAssetExecSymbol
()
assetExec
,
assetSymbol
:=
action
.
GetAssetExecSymbol
()
if
assetExec
==
""
{
if
assetExec
==
""
{
assetExec
=
"coins"
assetExec
=
"coins"
}
}
RpubKey
:=
privacyOutput
.
GetRpubKeytx
()
txInfo
:=
&
privacyTxInfo
{
totalUtxosLeft
:=
len
(
privacyOutput
.
Keyoutput
)
tx
:
tx
,
//处理output
blockDetail
:
block
,
if
privacyInfo
,
err
:=
policy
.
getPrivacyKeyPairs
();
err
==
nil
{
blockHeight
:
block
.
GetBlock
()
.
GetHeight
(),
matchedCount
:=
0
actionName
:
action
.
GetActionName
(),
utxoProcessed
:=
make
([]
bool
,
len
(
privacyOutput
.
Keyoutput
))
actionTy
:
action
.
GetTy
(),
for
_
,
info
:=
range
privacyInfo
{
input
:
action
.
GetInput
(),
privacykeyParirs
:=
info
.
PrivacyKeyPair
output
:
action
.
GetOutput
(),
matched4addr
:=
false
txIndex
:
index
,
var
utxos
[]
*
privacytypes
.
UTXO
txHash
:
txhash
,
for
indexoutput
,
output
:=
range
privacyOutput
.
Keyoutput
{
txHashHex
:
txhashHex
,
if
utxoProcessed
[
indexoutput
]
{
batch
:
batch
,
continue
assetSymbol
:
assetSymbol
,
}
assetExec
:
assetExec
,
priv
,
err
:=
privacy
.
RecoverOnetimePriKey
(
RpubKey
,
privacykeyParirs
.
ViewPrivKey
,
privacykeyParirs
.
SpendPrivKey
,
int64
(
indexoutput
))
isRollBack
:
addDelType
!=
AddTx
,
if
err
==
nil
{
isExecOk
:
types
.
ExecOk
==
block
.
Receipts
[
index
]
.
Ty
,
recoverPub
:=
priv
.
PubKey
()
.
Bytes
()[
:
]
}
if
bytes
.
Equal
(
recoverPub
,
output
.
Onetimepubkey
)
{
//为了避免匹配成功之后不必要的验证计算,需要统计匹配次数
bizlog
.
Debug
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashHex
,
"actionName"
,
txInfo
.
actionName
,
//因为目前只会往一个隐私账户转账,
"index"
,
index
,
"isRollBack"
,
txInfo
.
isRollBack
,
"isExecOk"
,
txInfo
.
isExecOk
)
//1.一般情况下,只会匹配一次,如果是往其他钱包账户转账,
//2.但是如果是往本钱包的其他地址转账,因为可能存在的change,会匹配2次
if
txInfo
.
actionTy
==
privacytypes
.
ActionPublic2Privacy
{
matched4addr
=
true
totalUtxosLeft
--
// 公对私的发送方是公开的,检测是否为本钱包地址
utxoProcessed
[
indexoutput
]
=
true
txFrom
:=
tx
.
From
()
//只有当该交易执行成功才进行相应的UTXO的处理
for
_
,
key
:=
range
privacyPairs
{
if
types
.
ExecOk
==
txExecRes
{
if
*
key
.
Addr
==
txFrom
{
if
AddTx
==
addDelType
{
param
:=
&
buildStoreWalletTxDetailParam
{
info2store
:=
&
privacytypes
.
PrivacyDBStore
{
txInfo
:
txInfo
,
AssetExec
:
assetExec
,
addr
:
txInfo
.
tx
.
From
(),
Txhash
:
txhash
,
sendRecvFlag
:
sendTx
,
Tokenname
:
tokenname
,
Amount
:
output
.
Amount
,
OutIndex
:
int32
(
indexoutput
),
TxPublicKeyR
:
RpubKey
,
OnetimePublicKey
:
output
.
Onetimepubkey
,
Owner
:
*
info
.
Addr
,
Height
:
block
.
Block
.
Height
,
Txindex
:
index
,
Blockhash
:
block
.
Block
.
Hash
(
cfg
),
}
utxoGlobalIndex
:=
&
privacytypes
.
UTXOGlobalIndex
{
Outindex
:
int32
(
indexoutput
),
Txhash
:
txhash
,
}
utxoCreated
:=
&
privacytypes
.
UTXO
{
Amount
:
output
.
Amount
,
UtxoBasic
:
&
privacytypes
.
UTXOBasic
{
UtxoGlobalIndex
:
utxoGlobalIndex
,
OnetimePubkey
:
output
.
Onetimepubkey
,
},
}
utxos
=
append
(
utxos
,
utxoCreated
)
policy
.
store
.
setUTXO
(
info
.
Addr
,
&
txhashstr
,
indexoutput
,
info2store
,
newbatch
)
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"add tx txhash"
,
txhashstr
,
"setUTXO addr "
,
*
info
.
Addr
,
"indexoutput"
,
indexoutput
)
}
else
{
policy
.
store
.
unsetUTXO
(
assetExec
,
tokenname
,
*
info
.
Addr
,
txhashstr
,
indexoutput
,
newbatch
)
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"delete tx txhash"
,
txhashstr
,
"unsetUTXO addr "
,
*
info
.
Addr
,
"indexoutput"
,
indexoutput
)
}
}
else
{
//对于执行失败的交易,只需要将该交易记录在钱包就行
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"txExecRes"
,
txExecRes
)
break
}
}
}
else
{
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"RecoverOnetimePriKey error"
,
err
)
}
}
policy
.
buildAndStoreWalletTxDetail
(
param
)
break
}
}
if
matched4addr
{
}
matchedCount
++
policy
.
processOutputUtxos
(
txFrom
,
privacyPairs
,
txInfo
)
//匹配次数达到2次,不再对本钱包中的其他地址进行匹配尝试
}
else
if
txInfo
.
actionTy
==
privacytypes
.
ActionPrivacy2Privacy
{
bizlog
.
Debug
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"address"
,
*
info
.
Addr
,
"totalUtxosLeft"
,
totalUtxosLeft
,
"matchedCount"
,
matchedCount
)
sender
:=
policy
.
processInputUtxos
(
txInfo
)
policy
.
processOutputUtxos
(
sender
,
privacyPairs
,
txInfo
)
}
else
if
txInfo
.
actionTy
==
privacytypes
.
ActionPrivacy2Public
{
sender
:=
policy
.
processInputUtxos
(
txInfo
)
policy
.
processOutputUtxos
(
sender
,
privacyPairs
,
txInfo
)
// 私有到公开的接收方是公开的,检测是否为本钱包地址
txTo
:=
action
.
GetPrivacy2Public
()
.
GetTo
()
for
_
,
key
:=
range
privacyPairs
{
if
*
key
.
Addr
==
txTo
{
param
:=
&
buildStoreWalletTxDetailParam
{
param
:=
&
buildStoreWalletTxDetailParam
{
assetExec
:
assetExec
,
txInfo
:
txInfo
,
tokenname
:
tokenname
,
addr
:
txTo
,
block
:
block
,
tx
:
tx
,
index
:
int
(
index
),
newbatch
:
newbatch
,
senderRecver
:
*
info
.
Addr
,
isprivacy
:
true
,
addDelType
:
addDelType
,
sendRecvFlag
:
recvTx
,
sendRecvFlag
:
recvTx
,
utxos
:
utxos
,
}
}
policy
.
buildAndStoreWalletTxDetail
(
param
)
policy
.
buildAndStoreWalletTxDetail
(
param
)
if
2
==
matchedCount
||
0
==
totalUtxosLeft
||
types
.
ExecOk
!=
txExecRes
{
break
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"Get matched privacy transfer for address address"
,
*
info
.
Addr
,
"totalUtxosLeft"
,
totalUtxosLeft
,
"matchedCount"
,
matchedCount
)
break
}
}
}
}
}
}
}
}
func
(
policy
*
privacyPolicy
)
processInputUtxos
(
txInfo
*
privacyTxInfo
)
string
{
bizlog
.
Debug
(
"processInputUtxos"
,
"txhash"
,
txInfo
.
txHashHex
,
"actionName"
,
txInfo
.
actionName
,
"isExecOk"
,
txInfo
.
isExecOk
,
"isRollBack"
,
txInfo
.
isRollBack
)
// utxo发送者
utxoSender
:=
""
var
err
error
// 区块回滚
if
txInfo
.
isRollBack
{
//当发生交易回撤时,从记录的STXO中查找相关的交易,并将其重置为FTXO,因为该交易大概率会在其他区块中再次执行
stxosInOneTx
,
_
,
_
:=
policy
.
store
.
getWalletFtxoStxo
(
STXOs4Tx
)
for
_
,
stxo
:=
range
stxosInOneTx
{
if
stxo
.
Txhash
!=
txInfo
.
txHashHex
{
continue
}
if
txInfo
.
isExecOk
{
err
=
policy
.
store
.
moveSTXO2FTXO
(
txInfo
.
tx
,
txInfo
.
txHashHex
,
txInfo
.
batch
)
utxoSender
=
stxo
.
Sender
}
if
err
!=
nil
{
bizlog
.
Error
(
"processInputUtxos"
,
"txHash"
,
txInfo
.
txHashHex
,
"moveSTXO2FTXO err"
,
err
)
}
}
}
else
{
//处理input,对于公对私的交易类型,只会出现在output类型处理中
//如果该隐私交易是本钱包中的地址发送出去的,则需要对相应的utxo进行处理 TODO:处理其他节点构造并发起的隐私input(需要比较keyimage)
if
AddTx
==
addDelType
{
ftxos
,
keys
:=
policy
.
store
.
getFTXOlist
()
ftxos
,
keys
:=
policy
.
store
.
getFTXOlist
()
for
i
,
ftxo
:=
range
ftxos
{
for
i
,
ftxo
:=
range
ftxos
{
//查询确认该交易是否为记录的支付交易
if
ftxo
.
Txhash
!=
txInfo
.
txHashHex
{
if
ftxo
.
Txhash
!=
txhashstr
{
continue
continue
}
}
if
types
.
ExecOk
==
txExecRes
&&
privacytypes
.
ActionPublic2Privacy
!=
privateAction
.
Ty
{
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
,
"moveFTXO2STXO, key"
,
string
(
keys
[
i
]),
"txExecRes"
,
txExecRes
)
if
txInfo
.
isExecOk
{
policy
.
store
.
moveFTXO2STXO
(
keys
[
i
],
txhashstr
,
newbatch
)
err
=
policy
.
store
.
moveFTXO2STXO
(
keys
[
i
],
txInfo
.
txHashHex
,
txInfo
.
batch
)
}
else
if
types
.
ExecOk
!=
txExecRes
&&
privacytypes
.
ActionPublic2Privacy
!=
privateAction
.
Ty
{
}
else
{
//如果执行失败
policy
.
store
.
moveFTXO2UTXO
(
keys
[
i
],
txInfo
.
batch
)
bizlog
.
Info
(
"PrivacyTrading AddDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
,
"moveFTXO2UTXO, key"
,
string
(
keys
[
i
]),
"txExecRes"
,
txExecRes
)
policy
.
store
.
moveFTXO2UTXO
(
keys
[
i
],
newbatch
)
}
}
//该交易正常执行完毕,删除对其的关注
utxoSender
=
ftxo
.
Sender
param
:=
&
buildStoreWalletTxDetailParam
{
if
err
!=
nil
{
assetExec
:
assetExec
,
bizlog
.
Error
(
"processInputUtxos"
,
"txHash"
,
txInfo
.
txHashHex
,
"moveSTXO2FTXO err"
,
err
)
tokenname
:
tokenname
,
block
:
block
,
tx
:
tx
,
index
:
int
(
index
),
newbatch
:
newbatch
,
senderRecver
:
ftxo
.
Sender
,
isprivacy
:
true
,
addDelType
:
addDelType
,
sendRecvFlag
:
sendTx
,
utxos
:
nil
,
}
}
policy
.
buildAndStoreWalletTxDetail
(
param
)
}
}
}
else
{
}
//当发生交易回撤时,从记录的STXO中查找相关的交易,并将其重置为FTXO,因为该交易大概率会在其他区块中再次执行
// 解析出发送者是本地钱包地址,记录交易
stxosInOneTx
,
_
,
_
:=
policy
.
store
.
getWalletFtxoStxo
(
STXOs4Tx
)
if
utxoSender
!=
""
{
for
_
,
ftxo
:=
range
stxosInOneTx
{
param
:=
&
buildStoreWalletTxDetailParam
{
if
ftxo
.
Txhash
==
txhashstr
{
txInfo
:
txInfo
,
param
:=
&
buildStoreWalletTxDetailParam
{
addr
:
utxoSender
,
assetExec
:
assetExec
,
sendRecvFlag
:
sendTx
,
tokenname
:
tokenname
,
}
block
:
block
,
policy
.
buildAndStoreWalletTxDetail
(
param
)
tx
:
tx
,
}
index
:
int
(
index
),
return
utxoSender
newbatch
:
newbatch
,
}
senderRecver
:
""
,
isprivacy
:
true
,
addDelType
:
addDelType
,
sendRecvFlag
:
sendTx
,
utxos
:
nil
,
}
if
types
.
ExecOk
==
txExecRes
&&
privacytypes
.
ActionPublic2Privacy
!=
privateAction
.
Ty
{
func
(
policy
*
privacyPolicy
)
processOutputUtxos
(
utxoSender
string
,
keys
[]
addrAndprivacy
,
txInfo
*
privacyTxInfo
)
{
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
,
"moveSTXO2FTXO txExecRes"
,
txExecRes
)
policy
.
store
.
moveSTXO2FTXO
(
tx
,
txhashstr
,
newbatch
)
bizlog
.
Debug
(
"processOutputUtxos"
,
"txhash"
,
txInfo
.
txHashHex
,
"actionName"
,
txInfo
.
actionName
,
"isExecOK"
,
txInfo
.
isExecOk
,
"isRollBack"
,
txInfo
.
isRollBack
)
policy
.
buildAndStoreWalletTxDetail
(
param
)
if
txInfo
.
output
==
nil
{
}
else
if
types
.
ExecOk
!=
txExecRes
&&
privacytypes
.
ActionPublic2Privacy
!=
privateAction
.
Ty
{
return
bizlog
.
Info
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txhashstr
,
"addDelType"
,
addDelType
)
}
policy
.
buildAndStoreWalletTxDetail
(
param
)
var
recvUtxos
[]
*
privacytypes
.
PrivacyDBStore
}
// check utxo owner
omitCheck
:=
false
owner
:=
""
receivers
:=
make
(
map
[
string
]
struct
{})
for
index
,
out
:=
range
txInfo
.
output
.
GetKeyoutput
()
{
for
_
,
key
:=
range
keys
{
if
omitCheck
{
break
}
owner
=
""
priv
,
err
:=
privacy
.
RecoverOnetimePriKey
(
txInfo
.
output
.
GetRpubKeytx
(),
key
.
PrivacyKeyPair
.
ViewPrivKey
,
key
.
PrivacyKeyPair
.
SpendPrivKey
,
int64
(
index
))
if
err
!=
nil
{
bizlog
.
Error
(
"addDelPrivacyTxsFromBlock"
,
"txhash"
,
txInfo
.
txHashHex
,
"actionName"
,
txInfo
.
actionName
,
"RecoverOnetimePriKey error"
,
err
)
}
if
bytes
.
Equal
(
priv
.
PubKey
()
.
Bytes
()[
:
],
out
.
GetOnetimepubkey
())
{
owner
=
*
key
.
Addr
receivers
[
owner
]
=
struct
{}{}
break
}
}
}
}
if
len
(
owner
)
>
0
{
info2store
:=
&
privacytypes
.
PrivacyDBStore
{
AssetExec
:
txInfo
.
assetExec
,
Txhash
:
txInfo
.
txHash
,
Tokenname
:
txInfo
.
assetSymbol
,
Amount
:
out
.
Amount
,
OutIndex
:
int32
(
index
),
TxPublicKeyR
:
txInfo
.
output
.
GetRpubKeytx
(),
OnetimePublicKey
:
out
.
Onetimepubkey
,
Owner
:
owner
,
Height
:
txInfo
.
blockHeight
,
Txindex
:
txInfo
.
txIndex
,
}
recvUtxos
=
append
(
recvUtxos
,
info2store
)
}
// 公对私的utxo只属于一个地址,只需检测第一个utxo
if
txInfo
.
actionTy
==
privacytypes
.
ActionPublic2Privacy
{
omitCheck
=
true
}
}
// add or del utxo in wallet
for
_
,
utxo
:=
range
recvUtxos
{
if
!
txInfo
.
isExecOk
{
//对于执行失败的交易,只需要将该交易记录在钱包就行
break
}
var
err
error
if
!
txInfo
.
isRollBack
{
err
=
policy
.
store
.
setUTXO
(
utxo
,
txInfo
.
txHashHex
,
txInfo
.
batch
)
}
else
{
err
=
policy
.
store
.
unsetUTXO
(
utxo
.
AssetExec
,
utxo
.
Tokenname
,
utxo
.
Owner
,
txInfo
.
txHashHex
,
int
(
utxo
.
OutIndex
),
txInfo
.
batch
)
}
if
err
!=
nil
{
bizlog
.
Error
(
"processOutputUtxos"
,
"txHash"
,
txInfo
.
txHashHex
,
"actionName"
,
txInfo
.
actionName
,
"isRollBack"
,
txInfo
.
isRollBack
,
"setUtxoErr"
,
err
)
}
}
// handle recv tx index
for
receiver
:=
range
receivers
{
// 如果utxo接收者有2个,说明发送者和接收者都是本钱包地址,且存在找零情况
// 这里需要过滤utxo找零接收情况,找零接收不是实际的交易接收方
if
len
(
receivers
)
>
1
&&
receiver
==
utxoSender
{
continue
}
// 私到公转账utxo接收者只可能是找零情况,不需要记录
if
txInfo
.
actionTy
==
privacytypes
.
ActionPrivacy2Public
{
continue
}
param
:=
&
buildStoreWalletTxDetailParam
{
txInfo
:
txInfo
,
addr
:
receiver
,
sendRecvFlag
:
recvTx
,
}
policy
.
buildAndStoreWalletTxDetail
(
param
)
}
}
}
}
plugin/dapp/privacy/wallet/privacy_test.go
View file @
0559134e
...
@@ -181,7 +181,7 @@ func (mock *PrivacyMock) CreateUTXOs(sender string, pubkeypair string, amount in
...
@@ -181,7 +181,7 @@ func (mock *PrivacyMock) CreateUTXOs(sender string, pubkeypair string, amount in
}
}
utxos
=
append
(
utxos
,
utxoCreated
)
utxos
=
append
(
utxos
,
utxoCreated
)
mock
.
store
.
setUTXO
(
info
.
Addr
,
&
txhashstr
,
indexoutput
,
info2store
,
dbbatch
)
mock
.
store
.
setUTXO
(
info
2store
,
txhashstr
,
dbbatch
)
}
}
}
}
}
}
...
...
plugin/dapp/privacy/wallet/privacybizpolicy.go
View file @
0559134e
...
@@ -170,18 +170,28 @@ func (policy *privacyPolicy) SignTransaction(key crypto.PrivKey, req *types.ReqS
...
@@ -170,18 +170,28 @@ func (policy *privacyPolicy) SignTransaction(key crypto.PrivKey, req *types.ReqS
return
return
}
}
type
privacyTxInfo
struct
{
tx
*
types
.
Transaction
blockDetail
*
types
.
BlockDetail
actionTy
int32
actionName
string
input
*
privacytypes
.
PrivacyInput
output
*
privacytypes
.
PrivacyOutput
txIndex
int32
blockHeight
int64
isExecOk
bool
isRollBack
bool
txHash
[]
byte
txHashHex
string
assetExec
string
assetSymbol
string
batch
db
.
Batch
}
type
buildStoreWalletTxDetailParam
struct
{
type
buildStoreWalletTxDetailParam
struct
{
assetExec
string
txInfo
*
privacyTxInfo
tokenname
string
addr
string
block
*
types
.
BlockDetail
tx
*
types
.
Transaction
index
int
newbatch
db
.
Batch
senderRecver
string
isprivacy
bool
addDelType
int32
sendRecvFlag
int32
sendRecvFlag
int32
utxos
[]
*
privacytypes
.
UTXO
}
}
// OnAddBlockTx 响应区块交易添加的处理
// OnAddBlockTx 响应区块交易添加的处理
...
...
plugin/dapp/privacy/wallet/privacystore.go
View file @
0559134e
...
@@ -642,7 +642,7 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
...
@@ -642,7 +642,7 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
}
}
utxos
=
append
(
utxos
,
utxoCreated
)
utxos
=
append
(
utxos
,
utxoCreated
)
store
.
setUTXO
(
info
.
Addr
,
&
txhash
,
indexoutput
,
info2store
,
newbatch
)
store
.
setUTXO
(
info
2store
,
txhash
,
newbatch
)
}
}
}
}
}
}
...
@@ -671,25 +671,17 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
...
@@ -671,25 +671,17 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
//UTXO---->moveUTXO2FTXO---->FTXO---->moveFTXO2STXO---->STXO
//UTXO---->moveUTXO2FTXO---->FTXO---->moveFTXO2STXO---->STXO
//1.calcUTXOKey------------>types.PrivacyDBStore 该kv值在db中的存储一旦写入就不再改变,除非产生该UTXO的交易被撤销
//1.calcUTXOKey------------>types.PrivacyDBStore 该kv值在db中的存储一旦写入就不再改变,除非产生该UTXO的交易被撤销
//2.calcUTXOKey4TokenAddr-->calcUTXOKey,创建kv,方便查询现在某个地址下某种token的可用utxo
//2.calcUTXOKey4TokenAddr-->calcUTXOKey,创建kv,方便查询现在某个地址下某种token的可用utxo
func
(
store
*
privacyStore
)
setUTXO
(
addr
,
txhash
*
string
,
outindex
int
,
dbStore
*
privacytypes
.
PrivacyDBStore
,
newbatch
db
.
Batch
)
error
{
func
(
store
*
privacyStore
)
setUTXO
(
utxoInfo
*
privacytypes
.
PrivacyDBStore
,
txHash
string
,
newbatch
db
.
Batch
)
error
{
if
0
==
len
(
*
addr
)
||
0
==
len
(
*
txhash
)
{
bizlog
.
Error
(
"setUTXO addr or txhash is nil"
)
return
types
.
ErrInvalidParam
}
if
dbStore
==
nil
{
bizlog
.
Error
(
"setUTXO privacy is nil"
)
return
types
.
ErrInvalidParam
}
privacyStorebyte
,
err
:=
proto
.
Marshal
(
dbStore
)
privacyStorebyte
,
err
:=
proto
.
Marshal
(
utxoInfo
)
if
err
!=
nil
{
if
err
!=
nil
{
bizlog
.
Error
(
"setUTXO proto.Marshal err!"
,
"err"
,
err
)
bizlog
.
Error
(
"setUTXO proto.Marshal err!"
,
"err"
,
err
)
return
types
.
ErrMarshal
return
types
.
ErrMarshal
}
}
outIndex
:=
int
(
utxoInfo
.
OutIndex
)
utxoKey
:=
calcUTXOKey
(
*
txhash
,
outi
ndex
)
utxoKey
:=
calcUTXOKey
(
txHash
,
outI
ndex
)
bizlog
.
Debug
(
"setUTXO"
,
"addr"
,
*
addr
,
"tx with hash"
,
*
txhash
,
"amount:"
,
dbStore
.
Amount
/
types
.
Coin
)
bizlog
.
Debug
(
"setUTXO"
,
"addr"
,
utxoInfo
.
Owner
,
"tx with hash"
,
txHash
,
"amount:"
,
utxoInfo
.
Amount
/
types
.
Coin
)
newbatch
.
Set
(
calcUTXOKey4TokenAddr
(
dbStore
.
AssetExec
,
dbStore
.
Tokenname
,
*
addr
,
*
txhash
,
outi
ndex
),
utxoKey
)
newbatch
.
Set
(
calcUTXOKey4TokenAddr
(
utxoInfo
.
AssetExec
,
utxoInfo
.
Tokenname
,
utxoInfo
.
Owner
,
txHash
,
outI
ndex
),
utxoKey
)
newbatch
.
Set
(
utxoKey
,
privacyStorebyte
)
newbatch
.
Set
(
utxoKey
,
privacyStorebyte
)
return
nil
return
nil
}
}
...
...
plugin/dapp/privacy/wallet/privacystore_test.go
View file @
0559134e
...
@@ -117,16 +117,11 @@ func testStore_storeScanPrivacyInputUTXO(t *testing.T) {
...
@@ -117,16 +117,11 @@ func testStore_storeScanPrivacyInputUTXO(t *testing.T) {
}
}
func
testStore_setUTXO
(
t
*
testing
.
T
)
{
func
testStore_setUTXO
(
t
*
testing
.
T
)
{
var
addr
,
txhash
string
var
txhash
string
store
:=
createStore
(
t
)
store
:=
createStore
(
t
)
dbbatch
:=
store
.
NewBatch
(
true
)
dbbatch
:=
store
.
NewBatch
(
true
)
err
:=
store
.
setUTXO
(
&
addr
,
&
txhash
,
0
,
nil
,
dbbatch
)
err
:=
store
.
setUTXO
(
&
pt
.
PrivacyDBStore
{},
txhash
,
dbbatch
)
assert
.
NotNil
(
t
,
err
)
assert
.
Nil
(
t
,
err
)
addr
=
"setUTXO"
txhash
=
"TXHASH"
err
=
store
.
setUTXO
(
&
addr
,
&
txhash
,
0
,
nil
,
dbbatch
)
assert
.
NotNil
(
t
,
err
)
}
}
func
testStore_selectPrivacyTransactionToWallet
(
t
*
testing
.
T
)
{
func
testStore_selectPrivacyTransactionToWallet
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment