Commit f08844a1 authored by sanghg's avatar sanghg Committed by vipwzw

修复隐私合约中golint的警告

修改rpc、types、wallet中的golint警告
parent aa2a8ff1
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package autotest
import (
"strconv"
. "github.com/33cn/chain33/cmd/autotest/types"
"github.com/33cn/chain33/cmd/autotest/types"
)
//pub2priv case
type CreateUtxosCase struct {
BaseCase
types.BaseCase
From string `toml:"from"`
To string `toml:"to"`
Amount string `toml:"amount"`
}
// CreateUtxosPack create utxos package
type CreateUtxosPack struct {
BaseCasePack
types.BaseCasePack
}
func (testCase *CreateUtxosCase) SendCommand(packID string) (PackFunc, error) {
// SendCommand send command
func (testCase *CreateUtxosCase) SendCommand(packID string) (types.PackFunc, error) {
return DefaultSend(testCase, &CreateUtxosPack{}, packID)
return types.DefaultSend(testCase, &CreateUtxosPack{}, packID)
}
// GetCheckHandlerMap get check handler map
func (pack *CreateUtxosPack) GetCheckHandlerMap() interface{} {
funcMap := make(CheckHandlerMapDiscard, 2)
funcMap := make(types.CheckHandlerMapDiscard, 2)
funcMap["balance"] = pack.checkBalance
funcMap["utxo"] = pack.checkUtxo
return funcMap
......@@ -50,8 +54,8 @@ func (pack *CreateUtxosPack) checkBalance(txInfo map[string]interface{}) bool {
"FromPrev", logSend["prev"].(map[string]interface{})["balance"].(string),
"FromCurr", logSend["current"].(map[string]interface{})["balance"].(string))
return CheckBalanceDeltaWithAddr(logFee, interCase.From, -fee) &&
CheckBalanceDeltaWithAddr(logSend, interCase.From, -amount)
return types.CheckBalanceDeltaWithAddr(logFee, interCase.From, -fee) &&
types.CheckBalanceDeltaWithAddr(logSend, interCase.From, -amount)
}
func (pack *CreateUtxosPack) checkUtxo(txInfo map[string]interface{}) bool {
......@@ -62,14 +66,14 @@ func (pack *CreateUtxosPack) checkUtxo(txInfo map[string]interface{}) bool {
amount, _ := strconv.ParseFloat(interCase.Amount, 64)
//get available utxo with addr
availUtxo, err := CalcUtxoAvailAmount(interCase.To, pack.TxHash)
totalOutput := CalcTxUtxoAmount(outputLog, "keyoutput")
availCheck := IsBalanceEqualFloat(availUtxo, amount)
availUtxo, err := types.CalcUtxoAvailAmount(interCase.To, pack.TxHash)
totalOutput := types.CalcTxUtxoAmount(outputLog, "keyoutput")
availCheck := types.IsBalanceEqualFloat(availUtxo, amount)
pack.FLog.Info("PrivCreateutxosDetail", "TestID", pack.PackID,
"TransferAmount", interCase.Amount, "UtxoOutput", totalOutput,
"ToAddr", interCase.To, "UtxoAvailable", availUtxo, "CalcAvailErr", err)
return availCheck && IsBalanceEqualFloat(totalOutput, amount)
return availCheck && types.IsBalanceEqualFloat(totalOutput, amount)
}
......@@ -9,7 +9,7 @@ import (
. "github.com/33cn/chain33/cmd/autotest/types"
)
//pub2priv case
//PubToPrivCase pub2priv case
type PubToPrivCase struct {
BaseCase
From string `toml:"from"`
......@@ -17,15 +17,18 @@ type PubToPrivCase struct {
Amount string `toml:"amount"`
}
// PubToPrivPack public to privacy package
type PubToPrivPack struct {
BaseCasePack
}
// SendCommand send command
func (testCase *PubToPrivCase) SendCommand(packID string) (PackFunc, error) {
return DefaultSend(testCase, &PubToPrivPack{}, packID)
}
// GetCheckHandlerMap get check handler map
func (pack *PubToPrivPack) GetCheckHandlerMap() interface{} {
funcMap := make(CheckHandlerMapDiscard, 2)
......
......@@ -86,6 +86,7 @@ func (g *channelClient) CreateRawTransaction(ctx context.Context, in *types.ReqC
return data.(*types.Transaction), nil
}
// ShowPrivacyAccountInfo display privacy account information for json rpc
func (c *Jrpc) ShowPrivacyAccountInfo(in *pty.ReqPPrivacyAccount, result *json.RawMessage) error {
reply, err := c.cli.ExecWalletFunc(pty.PrivacyX, "ShowPrivacyAccountInfo", in)
if err != nil {
......@@ -96,6 +97,8 @@ func (c *Jrpc) ShowPrivacyAccountInfo(in *pty.ReqPPrivacyAccount, result *json.R
}
/////////////////privacy///////////////
// ShowPrivacyAccountSpend display spend privacy account for json rpc
func (c *Jrpc) ShowPrivacyAccountSpend(in *pty.ReqPrivBal4AddrToken, result *json.RawMessage) error {
if 0 == len(in.Addr) {
return types.ErrInvalidParam
......@@ -109,6 +112,7 @@ func (c *Jrpc) ShowPrivacyAccountSpend(in *pty.ReqPrivBal4AddrToken, result *jso
return err
}
// ShowPrivacykey display privacy key for json rpc
func (c *Jrpc) ShowPrivacykey(in *types.ReqString, result *json.RawMessage) error {
reply, err := c.cli.ShowPrivacyKey(context.Background(), in)
if err != nil {
......@@ -118,6 +122,7 @@ func (c *Jrpc) ShowPrivacykey(in *types.ReqString, result *json.RawMessage) erro
return err
}
// MakeTxPublic2privacy create public to privacy trasaction for json rpc
func (c *Jrpc) MakeTxPublic2privacy(in *pty.ReqPub2Pri, result *interface{}) error {
reply, err := c.cli.MakeTxPublic2Privacy(context.Background(), in)
if err != nil {
......@@ -129,6 +134,7 @@ func (c *Jrpc) MakeTxPublic2privacy(in *pty.ReqPub2Pri, result *interface{}) err
return nil
}
// MakeTxPrivacy2privacy create privacy to privacy transaction
func (c *Jrpc) MakeTxPrivacy2privacy(in *pty.ReqPri2Pri, result *interface{}) error {
reply, err := c.cli.MakeTxPrivacy2Privacy(context.Background(), in)
if err != nil {
......@@ -140,6 +146,7 @@ func (c *Jrpc) MakeTxPrivacy2privacy(in *pty.ReqPri2Pri, result *interface{}) er
return nil
}
// MakeTxPrivacy2public make privacy to public trasaction
func (c *Jrpc) MakeTxPrivacy2public(in *pty.ReqPri2Pub, result *interface{}) error {
reply, err := c.cli.MakeTxPrivacy2Public(context.Background(), in)
if err != nil {
......@@ -150,6 +157,7 @@ func (c *Jrpc) MakeTxPrivacy2public(in *pty.ReqPri2Pub, result *interface{}) err
return nil
}
// CreateUTXOs create utxos for json rpc
func (c *Jrpc) CreateUTXOs(in *pty.ReqCreateUTXOs, result *interface{}) error {
reply, err := c.cli.CreateUTXOs(context.Background(), in)
......@@ -178,6 +186,7 @@ func (c *Jrpc) PrivacyTxList(in *pty.ReqPrivacyTransactionList, result *interfac
return nil
}
// RescanUtxos rescan utxosl for json rpc
func (c *Jrpc) RescanUtxos(in *pty.ReqRescanUtxos, result *json.RawMessage) error {
reply, err := c.cli.RescanUtxos(context.Background(), in)
if err != nil {
......@@ -187,6 +196,7 @@ func (c *Jrpc) RescanUtxos(in *pty.ReqRescanUtxos, result *json.RawMessage) erro
return err
}
// EnablePrivacy enable privacy for json rpc
func (c *Jrpc) EnablePrivacy(in *pty.ReqEnablePrivacy, result *json.RawMessage) error {
reply, err := c.cli.EnablePrivacy(context.Background(), in)
if err != nil {
......@@ -196,8 +206,9 @@ func (c *Jrpc) EnablePrivacy(in *pty.ReqEnablePrivacy, result *json.RawMessage)
return err
}
func (this *Jrpc) CreateRawTransaction(in *types.ReqCreateTransaction, result *interface{}) error {
reply, err := this.cli.CreateRawTransaction(context.Background(), in)
// CreateRawTransaction create raw trasaction for json rpc
func (c *Jrpc) CreateRawTransaction(in *types.ReqCreateTransaction, result *interface{}) error {
reply, err := c.cli.CreateRawTransaction(context.Background(), in)
if err != nil {
return err
}
......
......@@ -12,10 +12,12 @@ import (
var log = log15.New("module", "privacy.rpc")
// Jrpc json rpc class
type Jrpc struct {
cli *channelClient
}
// Grpc grpc class
type Grpc struct {
*channelClient
}
......@@ -24,6 +26,7 @@ type channelClient struct {
types.ChannelClient
}
// Init init rpc server
func Init(name string, s types.RPCServer) {
cli := &channelClient{}
grpc := &Grpc{channelClient: cli}
......
......@@ -11,26 +11,41 @@ import (
"github.com/33cn/chain33/types"
)
// PrivacyX privacy executor name
var PrivacyX = "privacy"
const (
// InvalidAction invalid action type
InvalidAction = 0
//action type for privacy
// ActionPublic2Privacy public to privacy action type
ActionPublic2Privacy = iota + 100
// ActionPrivacy2Privacy privacy to privacy action type
ActionPrivacy2Privacy
// ActionPrivacy2Public privacy to public action type
ActionPrivacy2Public
// log for privacy
// TyLogPrivacyFee privacy fee log type
TyLogPrivacyFee = 500
// TyLogPrivacyInput privacy input type
TyLogPrivacyInput = 501
// TyLogPrivacyOutput privacy output type
TyLogPrivacyOutput = 502
//privacy name of crypto
//SignNameOnetimeED25519 privacy name of crypto
SignNameOnetimeED25519 = "privacy.onetimeed25519"
// SignNameRing signature name ring
SignNameRing = "privacy.RingSignatue"
// OnetimeED25519 one time ED25519
OnetimeED25519 = 4
// RingBaseonED25519 ring raseon ED25519
RingBaseonED25519 = 5
// PrivacyMaxCount max mix utxo cout
PrivacyMaxCount = 16
// PrivacyTxFee privacy tx fee
PrivacyTxFee = types.Coin
)
......@@ -41,6 +56,7 @@ const (
UtxoFlagScanEnd int32 = 2
)
// RescanFlagMapint2string 常量字符串转换关系表
var RescanFlagMapint2string = map[int32]string{
UtxoFlagNoScan: "UtxoFlagNoScan",
UtxoFlagScaning: "UtxoFlagScaning",
......@@ -64,25 +80,30 @@ func init() {
types.RegisterDappFork(PrivacyX, "Enable", 980000)
}
// PrivacyType declare PrivacyType class
type PrivacyType struct {
types.ExecTypeBase
}
// NewType create PrivacyType object
func NewType() *PrivacyType {
c := &PrivacyType{}
c.SetChild(c)
return c
}
func (at *PrivacyType) GetPayload() types.Message {
// GetPayload get PrivacyType payload
func (t *PrivacyType) GetPayload() types.Message {
return &PrivacyAction{}
}
func (at *PrivacyType) GetName() string {
// GetName get PrivacyType name
func (t *PrivacyType) GetName() string {
return PrivacyX
}
func (at *PrivacyType) GetLogMap() map[int64]*types.LogInfo {
// GetLogMap get PrivacyType log map
func (t *PrivacyType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{
TyLogPrivacyFee: {reflect.TypeOf(types.ReceiptExecAccountTransfer{}), "LogPrivacyFee"},
TyLogPrivacyInput: {reflect.TypeOf(PrivacyInput{}), "LogPrivacyInput"},
......@@ -90,7 +111,8 @@ func (at *PrivacyType) GetLogMap() map[int64]*types.LogInfo {
}
}
func (c *PrivacyType) GetTypeMap() map[string]int32 {
// GetTypeMap get PrivacyType type map
func (t *PrivacyType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Public2Privacy": ActionPublic2Privacy,
"Privacy2Privacy": ActionPrivacy2Privacy,
......@@ -98,7 +120,8 @@ func (c *PrivacyType) GetTypeMap() map[string]int32 {
}
}
func (coins PrivacyType) ActionName(tx *types.Transaction) string {
// ActionName get PrivacyType action name
func (t PrivacyType) ActionName(tx *types.Transaction) string {
var action PrivacyAction
err := types.Decode(tx.Payload, &action)
if err != nil {
......@@ -108,29 +131,35 @@ func (coins PrivacyType) ActionName(tx *types.Transaction) string {
}
// TODO 暂时不修改实现, 先完成结构的重构
// CreateTx create transaction
func (t *PrivacyType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
var tx *types.Transaction
return tx, nil
}
// Amount get amout
func (t *PrivacyType) Amount(tx *types.Transaction) (int64, error) {
return 0, nil
}
func (base *PrivacyType) GetCryptoDriver(ty int) (string, error) {
// GetCryptoDriver get crypto driver
func (t *PrivacyType) GetCryptoDriver(ty int) (string, error) {
if name, ok := mapSignType2name[ty]; ok {
return name, nil
}
return "", types.ErrNotSupport
}
func (base *PrivacyType) GetCryptoType(name string) (int, error) {
// GetCryptoType get crypto type
func (t *PrivacyType) GetCryptoType(name string) (int, error) {
if ty, ok := mapSignName2Type[name]; ok {
return ty, nil
}
return 0, types.ErrNotSupport
}
// GetInput get action input information
func (action *PrivacyAction) GetInput() *PrivacyInput {
if action.GetTy() == ActionPrivacy2Privacy && action.GetPrivacy2Privacy() != nil {
return action.GetPrivacy2Privacy().GetInput()
......@@ -141,6 +170,7 @@ func (action *PrivacyAction) GetInput() *PrivacyInput {
return nil
}
// GetOutput get action output information
func (action *PrivacyAction) GetOutput() *PrivacyOutput {
if action.GetTy() == ActionPublic2Privacy && action.GetPublic2Privacy() != nil {
return action.GetPublic2Privacy().GetOutput()
......@@ -152,6 +182,7 @@ func (action *PrivacyAction) GetOutput() *PrivacyOutput {
return nil
}
// GetActionName get action name
func (action *PrivacyAction) GetActionName() string {
if action.Ty == ActionPrivacy2Privacy && action.GetPrivacy2Privacy() != nil {
return "Privacy2Privacy"
......@@ -163,6 +194,7 @@ func (action *PrivacyAction) GetActionName() string {
return "unknow-privacy"
}
// GetTokenName get action token name
func (action *PrivacyAction) GetTokenName() string {
if action.GetTy() == ActionPublic2Privacy && action.GetPublic2Privacy() != nil {
return action.GetPublic2Privacy().GetTokenname()
......
......@@ -5,8 +5,9 @@
package wallet
const (
// 交易操作的方向
// AddTx 增加交易
AddTx int32 = 20001
// DelTx 删除交易
DelTx int32 = 20002
// 交易收发方向
sendTx int32 = 30001
......
......@@ -7,53 +7,53 @@ package wallet
import "fmt"
const (
// 隐私交易运行过程中,需要使用到钱包数据库存储的数据库版本信息的KEY值
// PrivacyDBVersion 隐私交易运行过程中,需要使用到钱包数据库存储的数据库版本信息的KEY值
PrivacyDBVersion = "Privacy-DBVersion"
// 存储隐私交易保存账户的隐私公钥对信息的KEY值
// Privacy4Addr 存储隐私交易保存账户的隐私公钥对信息的KEY值
// KEY值格式为 Privacy4Addr-账号地址
// VALUE值格式为 types.WalletAccountPrivacy, 存储隐私公钥对
Privacy4Addr = "Privacy-Addr"
// 当前钱包内对应地址下可用UTXO的信息索引KEY值
// AvailUTXOs 当前钱包内对应地址下可用UTXO的信息索引KEY值
// KEY值格式为 AvailUTXOs-tokenname-address-outtxhash-outindex 其中outtxhash是输出该UTXO的交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 types.PrivacyDBStore,存储当前钱包地址下可用UTXO的详细信息
AvailUTXOs = "Privacy-UTXO"
// 创建一笔隐私交易后,创建该交易使用的UTXO信息,用以记录某一笔交易,占用了某些UTXO以及该交易的一些摘要信息
// UTXOsSpentInTx 创建一笔隐私交易后,创建该交易使用的UTXO信息,用以记录某一笔交易,占用了某些UTXO以及该交易的一些摘要信息
// KEY值格式为 UTXOsSpentInTx:costtxhash 其中costtxhash是创建的隐私交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 types.FTXOsSTXOsInOneTx
UTXOsSpentInTx = "Privacy-UTXOsSpentInTx"
// 通过钱包创建交易后,会将该交易使用到的UTXO信息进行冻结,冻结信息的索引KEY值
// FrozenUTXOs 通过钱包创建交易后,会将该交易使用到的UTXO信息进行冻结,冻结信息的索引KEY值
// KEY值格式为 FrozenUTXOs:tokenname-address-costtxhash 其中costtxhash是使用UTXO的交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 是指向UTXOsSpentInTx的KEY值的串
FrozenUTXOs = "Privacy-FUTXO4Tx"
// 通过钱包创建的交易被确认打包到区块后,可用UTXO变成已花费UTXO,用来保存已花费UTXO信息的KEY值
// PrivacySTXO 通过钱包创建的交易被确认打包到区块后,可用UTXO变成已花费UTXO,用来保存已花费UTXO信息的KEY值
// KEY值格式为 PrivacySTXO-tokenname-address-costtxhash 其中costtxhash是使用UTXO的交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 是指向UTXOsSpentInTx的KEY值的串
PrivacySTXO = "Privacy-SUTXO"
// 保存花费了UTXO的交易索引的KEY值
// STXOs4Tx 保存花费了UTXO的交易索引的KEY值
// KEY值格式为 STXOs4Tx:costtxhash 其中costtxhash是使用UTXO的交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 是指向UTXOsSpentInTx的KEY值的串
STXOs4Tx = "Privacy-SUTXO4Tx"
// 缓存因为区块发生回退时作为花费输出的UTXO信息
// RevertSendtx 缓存因为区块发生回退时作为花费输出的UTXO信息
// KEY值格式为 RevertSendtx:tokenname-address-costtxhash 其中costtxhash是使用UTXO的交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 是指向UTXOsSpentInTx的KEY值的串
RevertSendtx = "Privacy-RevertSendtx"
// 记录本钱包收到的发送给钱包内地址的交易信息KEY值
// RecvPrivacyTx 记录本钱包收到的发送给钱包内地址的交易信息KEY值
// KEY值格式为 RecvPrivacyTx:tokenname-address-heighstr 其中heighstr是区块高度乘以types.MaxTxsPerBlock加上当前交易在该区块上的位置index
// VALUE值格式为 指向PrivacyTX定义的KEY串
RecvPrivacyTx = "Privacy-RecvTX"
// 记录从本钱包花费出去的的交易信息KEY值
// SendPrivacyTx 记录从本钱包花费出去的的交易信息KEY值
// KEY值格式为 SendPrivacyTx:tokenname-address-heighstr 其中heighstr是区块高度乘以types.MaxTxsPerBlock加上当前交易在该区块上的位置index
// VALUE值格式为 指向PrivacyTX定义的KEY串
SendPrivacyTx = "Privacy-SendTX"
// 用以保存钱包收到的所有隐私交易具体信息的索引KEY值
// PrivacyTX 用以保存钱包收到的所有隐私交易具体信息的索引KEY值
// KEY值格式为 PrivacyTX:heighstr 其中heighstr是区块高度乘以types.MaxTxsPerBlock加上当前交易在该区块上的位置index
// VALUE值格式为 types.WalletTxDetail
PrivacyTX = "Privacy-TX"
// 通过扫描全链表,保存隐私交易中所有给当前钱包地址的交易的输入UTXO信息
// ScanPrivacyInput 通过扫描全链表,保存隐私交易中所有给当前钱包地址的交易的输入UTXO信息
// KEY值格式为 ScanPrivacyInput-outtxhash-outindex 其中outtxhash是输出该UTXO的交易哈希,使用common.Byte2Hex()生成
// VALUE值格式为 types.UTXOGlobalIndex
ScanPrivacyInput = "Privacy-ScaneInput"
// 存储是否处于重新扫描UTXO信息的标识
// ReScanUtxosFlag 存储是否处于重新扫描UTXO信息的标识
// KEY值格式为 ReScanUtxosFlag
// VALUE值格式为 types.Int64,具体含义
// UtxoFlagNoScan int32 = 0
......
......@@ -232,13 +232,13 @@ func (policy *privacyPolicy) getPrivacykeyPair(addr string) (*privacy.Privacy, e
copy(privacyInfo.SpendPrivKey[:], decrypteredSpend)
return privacyInfo, nil
} else {
}
_, err := policy.getPrivKeyByAddr(addr)
if err != nil {
return nil, err
}
return nil, privacytypes.ErrPrivacyNotEnabled
}
}
func (policy *privacyPolicy) savePrivacykeyPair(addr string) (*privacy.Privacy, error) {
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// Use of policy source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wallet
......@@ -19,7 +19,9 @@ import (
var (
bizlog = log15.New("module", "wallet.privacy")
// MaxTxHashsPerTime 单词处理的最大哈希书
MaxTxHashsPerTime int64 = 100
// maxTxNumPerBlock 单个区块最大数
maxTxNumPerBlock int64 = types.MaxTxsPerBlock
)
......@@ -27,6 +29,7 @@ func init() {
wcom.RegisterPolicy(privacytypes.PrivacyX, New())
}
// New 创建一盒钱包业务策略
func New() wcom.WalletBizPolicy {
return &privacyPolicy{
mtx: &sync.Mutex{},
......@@ -55,56 +58,66 @@ func (policy *privacyPolicy) getWalletOperate() wcom.WalletOperate {
return policy.walletOperate
}
// Init 初始化处理
func (policy *privacyPolicy) Init(walletOperate wcom.WalletOperate, sub []byte) {
policy.setWalletOperate(walletOperate)
policy.store = NewStore(walletOperate.GetDBStore())
policy.store = newStore(walletOperate.GetDBStore())
// 启动定时检查超期FTXO的协程
walletOperate.GetWaitGroup().Add(1)
go policy.checkWalletStoreData()
}
// OnCreateNewAccount 在账号创建时做一些处理
func (policy *privacyPolicy) OnCreateNewAccount(acc *types.Account) {
wg := policy.getWalletOperate().GetWaitGroup()
wg.Add(1)
go policy.rescanReqTxDetailByAddr(acc.Addr, wg)
}
// OnImportPrivateKey 在私钥导入时做一些处理
func (policy *privacyPolicy) OnImportPrivateKey(acc *types.Account) {
wg := policy.getWalletOperate().GetWaitGroup()
wg.Add(1)
go policy.rescanReqTxDetailByAddr(acc.Addr, wg)
}
// OnAddBlockFinish 在区块被添加成功时做一些处理
func (policy *privacyPolicy) OnAddBlockFinish(block *types.BlockDetail) {
}
// OnDeleteBlockFinish 在区块被删除成功时做一些处理
func (policy *privacyPolicy) OnDeleteBlockFinish(block *types.BlockDetail) {
}
// OnClose 在钱包关闭时做一些处理
func (policy *privacyPolicy) OnClose() {
}
func (this *privacyPolicy) OnSetQueueClient() {
version := this.store.getVersion()
// OnSetQueueClient 在钱包消息队列初始化时做一些处理
func (policy *privacyPolicy) OnSetQueueClient() {
version := policy.store.getVersion()
if version < PRIVACYDBVERSION {
this.rescanAllTxAddToUpdateUTXOs()
this.store.setVersion()
policy.rescanAllTxAddToUpdateUTXOs()
policy.store.setVersion()
}
}
// OnWalletLocked 在钱包加锁时做一些处理
func (policy *privacyPolicy) OnWalletLocked() {
}
// OnWalletUnlocked 在钱包解锁时做一些处理
func (policy *privacyPolicy) OnWalletUnlocked(WalletUnLock *types.WalletUnLock) {
}
func (this *privacyPolicy) Call(funName string, in types.Message) (ret types.Message, err error) {
// Call 调用隐私的方法
func (policy *privacyPolicy) Call(funName string, in types.Message) (ret types.Message, err error) {
switch funName {
case "GetUTXOScaningFlag":
isok := this.GetRescanFlag() == privacytypes.UtxoFlagScaning
isok := policy.GetRescanFlag() == privacytypes.UtxoFlagScaning
ret = &types.Reply{IsOk: isok}
default:
err = types.ErrNotSupport
......@@ -112,6 +125,7 @@ func (this *privacyPolicy) Call(funName string, in types.Message) (ret types.Mes
return
}
// SignTransaction 对隐私交易进行签名
func (policy *privacyPolicy) SignTransaction(key crypto.PrivKey, req *types.ReqSignRawTx) (needSysSign bool, signtxhex string, err error) {
needSysSign = false
bytes, err := common.FromHex(req.GetTxHex())
......@@ -169,22 +183,26 @@ type buildStoreWalletTxDetailParam struct {
utxos []*privacytypes.UTXO
}
// OnAddBlockTx 响应区块交易添加的处理
func (policy *privacyPolicy) OnAddBlockTx(block *types.BlockDetail, tx *types.Transaction, index int32, dbbatch db.Batch) *types.WalletTxDetail {
policy.addDelPrivacyTxsFromBlock(tx, index, block, dbbatch, AddTx)
// 自己处理掉所有事务,部需要外部处理了
return nil
}
// OnDeleteBlockTx 响应删除区块交易的处理
func (policy *privacyPolicy) OnDeleteBlockTx(block *types.BlockDetail, tx *types.Transaction, index int32, dbbatch db.Batch) *types.WalletTxDetail {
policy.addDelPrivacyTxsFromBlock(tx, index, block, dbbatch, DelTx)
// 自己处理掉所有事务,部需要外部处理了
return nil
}
func (this *privacyPolicy) GetRescanFlag() int32 {
return atomic.LoadInt32(&this.rescanUTXOflag)
// GetRescanFlag get rescan utxo flag
func (policy *privacyPolicy) GetRescanFlag() int32 {
return atomic.LoadInt32(&policy.rescanUTXOflag)
}
func (this *privacyPolicy) SetRescanFlag(flag int32) {
atomic.StoreInt32(&this.rescanUTXOflag, flag)
// SetRescanFlag set rescan utxos flag
func (policy *privacyPolicy) SetRescanFlag(flag int32) {
atomic.StoreInt32(&policy.rescanUTXOflag, flag)
}
......@@ -19,11 +19,11 @@ import (
)
const (
// 隐私交易数据库版本号
// PRIVACYDBVERSION 隐私交易数据库版本号
PRIVACYDBVERSION int64 = 1
)
func NewStore(db db.DB) *privacyStore {
func newStore(db db.DB) *privacyStore {
return &privacyStore{Store: wcom.NewStore(db)}
}
......
......@@ -66,30 +66,30 @@ func decomAmount2Nature(amount int64, order int64) []int64 {
return res
}
// 62387455827 -> 455827 + 7000000 + 80000000 + 300000000 + 2000000000 + 60000000000, where 455827 <= dust_threshold
// 62387455827 -> 455827 + 7000000 + 80000000 + 300000000 + 2000000000 + 60000000000, where 455827 <= dustThreshold
//res:[455827, 7000000, 80000000, 300000000, 2000000000, 60000000000]
func decomposeAmount2digits(amount, dust_threshold int64) []int64 {
func decomposeAmount2digits(amount, dustThreshold int64) []int64 {
res := make([]int64, 0)
if 0 >= amount {
return res
}
is_dust_handled := false
var dust int64 = 0
isDustHandled := false
var dust int64
var order int64 = 1
var chunk int64 = 0
var chunk int64
for 0 != amount {
chunk = (amount % 10) * order
amount /= 10
order *= 10
if dust+chunk < dust_threshold {
if dust+chunk < dustThreshold {
dust += chunk //累加小数,直到超过dust_threshold为止
} else {
if !is_dust_handled && 0 != dust {
if !isDustHandled && 0 != dust {
//1st 正常情况下,先把dust保存下来
res = append(res, dust)
is_dust_handled = true
isDustHandled = true
}
if 0 != chunk {
//2nd 然后依次将大的整数额度进行保存
......@@ -99,8 +99,8 @@ func decomposeAmount2digits(amount, dust_threshold int64) []int64 {
}
}
//如果需要被拆分的额度 < dust_threshold,则直接将其进行保存
if !is_dust_handled && 0 != dust {
//如果需要被拆分的额度 < dustThreshold,则直接将其进行保存
if !isDustHandled && 0 != dust {
res = append(res, dust)
}
......
......@@ -99,32 +99,32 @@ func Test_decomAmount2Nature(t *testing.T) {
func Test_decomposeAmount2digits(t *testing.T) {
testCase := []struct {
amount int64
dust_threshold int64
dustThreshold int64
actual []int64
}{
{
amount: 0,
dust_threshold: 0,
dustThreshold: 0,
actual: []int64{},
},
{
amount: -1,
dust_threshold: 0,
dustThreshold: 0,
actual: []int64{},
},
{
amount: 2,
dust_threshold: 1,
dustThreshold: 1,
actual: []int64{2},
},
{
amount: 62387455827,
dust_threshold: types.BTYDustThreshold,
dustThreshold: types.BTYDustThreshold,
actual: []int64{87455827, 1e8, 2e8, 2e9, 5e10, 1e10},
},
}
for _, test := range testCase {
res := decomposeAmount2digits(test.amount, test.dust_threshold)
res := decomposeAmount2digits(test.amount, test.dustThreshold)
require.Equal(t, res, test.actual)
}
}
......
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