forked from 33cn/plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubToPriv.go
79 lines (59 loc) · 2.66 KB
/
pubToPriv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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"
)
//PubToPrivCase pub2priv case
type PubToPrivCase struct {
types.BaseCase
From string `toml:"from"`
To string `toml:"to"`
Amount string `toml:"amount"`
}
// PubToPrivPack public to privacy package
type PubToPrivPack struct {
types.BaseCasePack
}
// SendCommand send command
func (testCase *PubToPrivCase) SendCommand(packID string) (types.PackFunc, error) {
return types.DefaultSend(testCase, &PubToPrivPack{}, packID)
}
// GetCheckHandlerMap get check handler map
func (pack *PubToPrivPack) GetCheckHandlerMap() interface{} {
funcMap := make(types.CheckHandlerMapDiscard, 2)
funcMap["balance"] = pack.checkBalance
funcMap["utxo"] = pack.checkUtxo
return funcMap
}
func (pack *PubToPrivPack) checkBalance(txInfo map[string]interface{}) bool {
interCase := pack.TCase.(*PubToPrivCase)
feeStr := txInfo["tx"].(map[string]interface{})["fee"].(string)
logArr := txInfo["receipt"].(map[string]interface{})["logs"].([]interface{})
logFee := logArr[0].(map[string]interface{})["log"].(map[string]interface{})
logSend := logArr[1].(map[string]interface{})["log"].(map[string]interface{})
fee, _ := strconv.ParseFloat(feeStr, 64)
amount, _ := strconv.ParseFloat(interCase.Amount, 64)
pack.FLog.Info("Pub2PrivateDetails", "TestID", pack.PackID,
"Fee", feeStr, "Amount", interCase.Amount, "FromAddr", interCase.From,
"FromPrev", logSend["prev"].(map[string]interface{})["balance"].(string),
"FromCurr", logSend["current"].(map[string]interface{})["balance"].(string))
return types.CheckBalanceDeltaWithAddr(logFee, interCase.From, -fee) &&
types.CheckBalanceDeltaWithAddr(logSend, interCase.From, -amount)
}
func (pack *PubToPrivPack) checkUtxo(txInfo map[string]interface{}) bool {
interCase := pack.TCase.(*PubToPrivCase)
logArr := txInfo["receipt"].(map[string]interface{})["logs"].([]interface{})
outputLog := logArr[2].(map[string]interface{})["log"].(map[string]interface{})
amount, _ := strconv.ParseFloat(interCase.Amount, 64)
//get available utxo with addr
availUtxo, err := types.CalcUtxoAvailAmount(interCase.To, pack.TxHash)
totalOutput := types.CalcTxUtxoAmount(outputLog, "keyoutput")
availCheck := types.IsBalanceEqualFloat(availUtxo, amount)
pack.FLog.Info("Pub2PrivateUtxoDetail", "TestID", pack.PackID,
"TransferAmount", interCase.Amount, "UtxoOutput", totalOutput,
"ToAddr", interCase.To, "UtxoAvailable", availUtxo, "CalcAvailErr", err)
return availCheck && types.IsBalanceEqualFloat(totalOutput, amount)
}