Commit 0ccc4d97 authored by madengji's avatar madengji Committed by vipwzw

add checkTx

parent aa4ca7cd
......@@ -73,7 +73,8 @@ grpcFuncWhitelist=["*"]
[mempool]
name="para"
poolCacheSize=10240
minTxFeeRate=100000
#平行链的最小feeRate为0
#minTxFeeRate=100000
maxTxNumPerAccount=10000
[consensus]
......@@ -119,11 +120,13 @@ writeBlockSeconds=2
authAccount=""
#创世地址额度
genesisAmount=100000000
#主链计算blockhash forkheight,需要和主链保持严格一致,不可修改,209186是bityuan主链对应高度, ycc或其他按实际修改
mainBlockHashForkHeight=209186
#主链计算blockhash forkheight,需要和主链保持严格一致,不可修改,1是bityuan主链对应高度, ycc或其他按实际修改
mainBlockHashForkHeight=1
#主链支持平行链共识tx分叉高度,需要和主链保持严格一致,不可修改,2270000是bityuan主链对应高度, ycc或其他按实际修改
#不可为0,主链Local时候需特殊配置
mainForkParacrossCommitTx=2270000
#主链开启循环检查共识交易done的fork高度,需要和主链保持严格一致,不可修改,4320000是bityuan主链对应高度, ycc或其他按实际修改
#不可为0,主链Local时候需特殊配置
mainLoopCheckCommitTxDoneForkHeight=4320000
#无平行链交易的主链区块间隔,平行链产生一个空块,从高度0开始,配置[blockHeight:interval],比如["0:50","1000:100"]
emptyBlockInterval=["0:50"]
......
......@@ -326,6 +326,7 @@ func validMainBlocks(txs *types.ParaTxDetails) *types.ParaTxDetails {
return txs
}
//主链blockchain支持按过滤平行链交易后,此接口弃用
func (client *client) requestTxsFromBlock(currSeq int64, preMainBlockHash []byte) (*types.ParaTxDetails, error) {
cfg := client.GetAPI().GetConfig()
blockSeq, err := client.GetBlockOnMainBySeq(currSeq)
......
......@@ -73,9 +73,11 @@ func CommitValuePart(circuit *frontend.CS, spendValue *frontend.Constraint) {
// set point G in the circuit
pointGSnark := twistededwards_gadget.NewPointGadget(circuit, nil, nil)
//scalar := circuit.ALLOCATE("-1")
//100000000*1e8 to avoid <0 values input
circuit.MUSTBE_LESS_OR_EQ(spendValue, 10000000000000000, 256)
//to avoid <0 values input
//negOne := circuit.ALLOCATE("-1")
//negSpendVal := circuit.MUL(spendValue,negOne)
//circuit.MUSTBE_LESS_OR_EQ(negSpendVal, 0, 256)
circuit.MUSTBE_LESS_OR_EQ(spendValue, 1000000000000000000, 256)
// set point G in the circuit
pointGSnark.ScalarMulFixedBase(circuit, edgadget.BaseX, edgadget.BaseY, spendValue, edgadget)
......
......@@ -67,7 +67,7 @@ func (a *action) authorizeVerify(proof *mixTy.ZkProofInfo) (*mixTy.AuthorizePubl
}
//zk-proof校验
err = a.zkProofVerify(proof, mixTy.VerifyType_AUTHORIZE)
err = zkProofVerify(a.db, proof, mixTy.VerifyType_AUTHORIZE)
if err != nil {
return nil, err
}
......
......@@ -5,6 +5,7 @@
package executor
import (
dbm "github.com/33cn/chain33/common/db"
manager "github.com/33cn/chain33/system/dapp/manage/types"
"github.com/33cn/chain33/types"
mixTy "github.com/33cn/plugin/plugin/dapp/mix/types"
......@@ -60,9 +61,9 @@ func makeConfigVerifyKeyReceipt(data *mixTy.ZkVerifyKeys) *types.Receipt {
}
func (a *action) getVerifyKeys() (*mixTy.ZkVerifyKeys, error) {
func getVerifyKeys(db dbm.KV) (*mixTy.ZkVerifyKeys, error) {
key := getVerifyKeysKey()
v, err := a.db.Get(key)
v, err := db.Get(key)
if err != nil {
return nil, errors.Wrapf(err, "get db verify key")
}
......@@ -76,7 +77,7 @@ func (a *action) getVerifyKeys() (*mixTy.ZkVerifyKeys, error) {
}
func (a *action) ConfigAddVerifyKey(newKey *mixTy.ZkVerifyKey) (*types.Receipt, error) {
keys, err := a.getVerifyKeys()
keys, err := getVerifyKeys(a.db)
if isNotFound(errors.Cause(err)) {
keys := &mixTy.ZkVerifyKeys{}
keys.Data = append(keys.Data, newKey)
......@@ -92,7 +93,7 @@ func (a *action) ConfigAddVerifyKey(newKey *mixTy.ZkVerifyKey) (*types.Receipt,
}
func (a *action) ConfigDeleteVerifyKey(config *mixTy.ZkVerifyKey) (*types.Receipt, error) {
keys, err := a.getVerifyKeys()
keys, err := getVerifyKeys(a.db)
if err != nil {
return nil, err
}
......
......@@ -10,6 +10,7 @@ import (
"strconv"
"github.com/33cn/chain33/common/address"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/mix/executor/zksnark"
mixTy "github.com/33cn/plugin/plugin/dapp/mix/types"
......@@ -23,8 +24,8 @@ func makeNullifierSetReceipt(hash string, data proto.Message) *types.Receipt {
}
func (a *action) zkProofVerify(proof *mixTy.ZkProofInfo, verifyTy mixTy.VerifyType) error {
keys, err := a.getVerifyKeys()
func zkProofVerify(db dbm.KV, proof *mixTy.ZkProofInfo, verifyTy mixTy.VerifyType) error {
keys, err := getVerifyKeys(db)
if err != nil {
return err
}
......@@ -65,7 +66,7 @@ func (a *action) depositVerify(proof *mixTy.ZkProofInfo) (string, uint64, error)
return "", 0, errors.Wrapf(err, "parseUint=%s", input.Amount)
}
err = a.zkProofVerify(proof, mixTy.VerifyType_DEPOSIT)
err = zkProofVerify(a.db, proof, mixTy.VerifyType_DEPOSIT)
if err != nil {
return "", 0, err
}
......
......@@ -50,3 +50,24 @@ func newMix() drivers.Driver {
func (m *Mix) GetDriverName() string {
return mixTy.MixX
}
// CheckTx check transaction
func (m *Mix) CheckTx(tx *types.Transaction, index int) error {
action := new(mixTy.MixAction)
if err := types.Decode(tx.Payload, action); err != nil {
mlog.Error("CheckTx decode", "err", err)
return err
}
if action.Ty != mixTy.MixActionTransfer {
// mix隐私交易,只私对私需要特殊签名验证
return m.DriverBase.CheckTx(tx, index)
}
_, _, err := MixTransferInfoVerify(m.GetStateDB(), action.GetTransfer())
if err != nil {
mlog.Error("checkTx", "err", err)
return err
}
return nil
}
......@@ -12,6 +12,7 @@ import (
mixTy "github.com/33cn/plugin/plugin/dapp/mix/types"
"github.com/consensys/gurvy/bn256/twistededwards"
dbm "github.com/33cn/chain33/common/db"
"github.com/consensys/gurvy/bn256/fr"
"github.com/pkg/errors"
)
......@@ -21,7 +22,7 @@ import (
2. check if exist in authorize pool and nullifier pool
*/
func (a *action) transferInputVerify(proof *mixTy.ZkProofInfo) (*mixTy.TransferInputPublicInput, error) {
func transferInputVerify(db dbm.KV, proof *mixTy.ZkProofInfo) (*mixTy.TransferInputPublicInput, error) {
var input mixTy.TransferInputPublicInput
data, err := hex.DecodeString(proof.PublicInput)
if err != nil {
......@@ -32,12 +33,12 @@ func (a *action) transferInputVerify(proof *mixTy.ZkProofInfo) (*mixTy.TransferI
return nil, errors.Wrapf(err, "transferInput verify unmarshal string=%s", proof.PublicInput)
}
err = a.spendVerify(input.TreeRootHash, input.NullifierHash, input.AuthorizeSpendHash)
err = spendVerify(db, input.TreeRootHash, input.NullifierHash, input.AuthorizeSpendHash)
if err != nil {
return nil, errors.Wrap(err, "transferInput verify spendVerify")
}
err = a.zkProofVerify(proof, mixTy.VerifyType_TRANSFERINPUT)
err = zkProofVerify(db, proof, mixTy.VerifyType_TRANSFERINPUT)
if err != nil {
return nil, errors.Wrap(err, "transferInput verify proof verify")
}
......@@ -51,7 +52,7 @@ func (a *action) transferInputVerify(proof *mixTy.ZkProofInfo) (*mixTy.TransferI
2. check if exist in authorize pool and nullifier pool
*/
func (a *action) transferOutputVerify(proof *mixTy.ZkProofInfo) (*mixTy.TransferOutputPublicInput, error) {
func transferOutputVerify(db dbm.KV, proof *mixTy.ZkProofInfo) (*mixTy.TransferOutputPublicInput, error) {
var input mixTy.TransferOutputPublicInput
data, err := hex.DecodeString(proof.PublicInput)
if err != nil {
......@@ -62,7 +63,7 @@ func (a *action) transferOutputVerify(proof *mixTy.ZkProofInfo) (*mixTy.Transfer
return nil, errors.Wrapf(err, "Output verify unmarshal string=%s", proof.PublicInput)
}
err = a.zkProofVerify(proof, mixTy.VerifyType_TRANSFEROUTPUT)
err = zkProofVerify(db, proof, mixTy.VerifyType_TRANSFEROUTPUT)
if err != nil {
return nil, errors.Wrap(err, "Output verify proof verify")
}
......@@ -75,10 +76,11 @@ func getFee() *twistededwards.Point {
//手续费 可配, 缺省100000, 即0.001, point=fee*G + 0*H
var fee fr.Element
fee.SetUint64(100000).FromMont()
var pFee twistededwards.Point
var pointFee twistededwards.Point
ed := twistededwards.GetEdwardsCurve()
pFee.ScalarMul(&ed.Base, fee)
return &pFee
pointFee.ScalarMul(&ed.Base, fee)
return &pointFee
}
func VerifyCommitValues(inputs []*mixTy.TransferInputPublicInput, outputs []*mixTy.TransferOutputPublicInput) bool {
......@@ -115,33 +117,42 @@ func VerifyCommitValues(inputs []*mixTy.TransferInputPublicInput, outputs []*mix
return false
}
/*
1. verify(zk-proof, sum value of spend and new commits)
2. check if exist in authorize pool and nullifier pool
3. add nullifier to pool
*/
func (a *action) Transfer(transfer *mixTy.MixTransferAction) (*types.Receipt, error) {
func MixTransferInfoVerify(db dbm.KV, transfer *mixTy.MixTransferAction) ([]*mixTy.TransferInputPublicInput, []*mixTy.TransferOutputPublicInput, error) {
var inputs []*mixTy.TransferInputPublicInput
var outputs []*mixTy.TransferOutputPublicInput
for _, k := range transfer.Input {
in, err := a.transferInputVerify(k)
in, err := transferInputVerify(db, k)
if err != nil {
return nil, err
return nil, nil, err
}
inputs = append(inputs, in)
}
for _, k := range transfer.Output {
out, err := a.transferOutputVerify(k)
out, err := transferOutputVerify(db, k)
if err != nil {
return nil, err
return nil, nil, err
}
outputs = append(outputs, out)
}
if !VerifyCommitValues(inputs, outputs) {
return nil, errors.Wrap(mixTy.ErrSpendInOutValueNotMatch, "verifyValue")
return nil, nil, errors.Wrap(mixTy.ErrSpendInOutValueNotMatch, "verifyValue")
}
return inputs, outputs, nil
}
/*
1. verify(zk-proof, sum value of spend and new commits)
2. check if exist in authorize pool and nullifier pool
3. add nullifier to pool
*/
func (a *action) Transfer(transfer *mixTy.MixTransferAction) (*types.Receipt, error) {
inputs, outputs, err := MixTransferInfoVerify(a.db, transfer)
if err != nil {
return nil, errors.Wrap(err, "Transfer.MixTransferInfoVerify")
}
receipt := &types.Receipt{Ty: types.ExecOk}
......@@ -157,7 +168,7 @@ func (a *action) Transfer(transfer *mixTy.MixTransferAction) (*types.Receipt, er
}
rpt, err := pushTree(a.db, leaves)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "transfer.pushTree")
}
mergeReceipt(receipt, rpt)
return receipt, nil
......
......@@ -10,21 +10,22 @@ import (
"strconv"
"github.com/33cn/chain33/common/address"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
mixTy "github.com/33cn/plugin/plugin/dapp/mix/types"
"github.com/pkg/errors"
)
func (a *action) spendVerify(treeRootHash, nulliferHash, authorizeSpendHash string) error {
func spendVerify(db dbm.KV, treeRootHash, nulliferHash, authorizeSpendHash string) error {
//zk-proof校验
//check tree rootHash exist
if !checkTreeRootHashExist(a.db, transferFr2Bytes(treeRootHash)) {
if !checkTreeRootHashExist(db, transferFr2Bytes(treeRootHash)) {
return errors.Wrapf(mixTy.ErrTreeRootHashNotFound, "roothash=%s", treeRootHash)
}
//nullifier should not exist
nullifierKey := calcNullifierHashKey(nulliferHash)
_, err := a.db.Get(nullifierKey)
_, err := db.Get(nullifierKey)
if err == nil {
return errors.Wrapf(mixTy.ErrNulliferHashExist, "nullifier=%s", nulliferHash)
}
......@@ -35,7 +36,7 @@ func (a *action) spendVerify(treeRootHash, nulliferHash, authorizeSpendHash stri
// authorize should exist if needed
if len(authorizeSpendHash) > 1 {
authKey := calcAuthorizeSpendHashKey(authorizeSpendHash)
_, err = a.db.Get(authKey)
_, err = db.Get(authKey)
if err != nil {
return errors.Wrapf(err, "authorize=%s", authorizeSpendHash)
}
......@@ -60,12 +61,12 @@ func (a *action) withdrawVerify(proof *mixTy.ZkProofInfo) (string, uint64, error
return "", 0, errors.Wrapf(err, "parseUint=%s", input.Amount)
}
err = a.spendVerify(input.TreeRootHash, input.NullifierHash, input.AuthorizeSpendHash)
err = spendVerify(a.db, input.TreeRootHash, input.NullifierHash, input.AuthorizeSpendHash)
if err != nil {
return "", 0, err
}
err = a.zkProofVerify(proof, mixTy.VerifyType_WITHDRAW)
err = zkProofVerify(a.db, proof, mixTy.VerifyType_WITHDRAW)
if err != nil {
return "", 0, err
}
......
......@@ -12,6 +12,8 @@ import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
......@@ -44,7 +46,7 @@ func (r *MixSignature) Bytes() []byte {
// IsZero check is zero
func (r *MixSignature) IsZero() bool {
return false
return len(r.sign.Output) == 0 || len(r.sign.Input) == 0
}
// String convert to string
......@@ -100,14 +102,14 @@ func (pubkey *MixSignPublicKey) Bytes() []byte {
return pubkey.key[:]
}
func verifyCommitAmount(transfer *mixTy.MixTransferAction) bool {
func verifyCommitAmount(transfer *mixTy.MixTransferAction) error {
var inputs []*mixTy.TransferInputPublicInput
var outputs []*mixTy.TransferOutputPublicInput
for _, k := range transfer.Input {
v, err := mixTy.DecodePubInput(mixTy.VerifyType_TRANSFERINPUT, k.PublicInput)
if err != nil {
return false
return errors.Wrap(types.ErrInvalidParam, "decode transfer Input")
}
inputs = append(inputs, v.(*mixTy.TransferInputPublicInput))
}
......@@ -115,15 +117,15 @@ func verifyCommitAmount(transfer *mixTy.MixTransferAction) bool {
for _, k := range transfer.Output {
v, err := mixTy.DecodePubInput(mixTy.VerifyType_TRANSFEROUTPUT, k.PublicInput)
if err != nil {
return false
return errors.Wrap(types.ErrInvalidParam, "decode transfer output")
}
outputs = append(outputs, v.(*mixTy.TransferOutputPublicInput))
}
if !mixExec.VerifyCommitValues(inputs, outputs) {
return false
return errors.Wrap(types.ErrInvalidParam, "verify commit amount")
}
return true
return nil
}
// VerifyBytes verify bytes
......@@ -135,23 +137,28 @@ func (pubkey *MixSignPublicKey) VerifyBytes(msg []byte, sign crypto.Signature) b
tx := new(types.Transaction)
if err := types.Decode(msg, tx); err != nil || !bytes.Equal([]byte(mixTy.MixX), types.GetRealExecName(tx.Execer)) {
// mix特定执行器的签名
bizlog.Error("pubkey.VerifyBytes", "err", err, "exec", string(types.GetRealExecName(tx.Execer)))
return false
}
action := new(mixTy.MixAction)
if err := types.Decode(tx.Payload, action); err != nil {
bizlog.Error("pubkey.VerifyBytes decode tx")
return false
}
if action.Ty != mixTy.MixActionTransfer {
// mix隐私交易,只私对私需要特殊签名验证
bizlog.Error("pubkey.VerifyBytes", "ty", action.Ty)
return false
}
//确保签名数据和tx 一致
if !bytes.Equal(sign.Bytes(), common.BytesToHash(types.Encode(action.GetTransfer())).Bytes()) {
bizlog.Error("pubkey.VerifyBytes tx and sign not match", "sign", common.ToHex(sign.Bytes()), "tx", common.ToHex(common.BytesToHash(types.Encode(action.GetTransfer())).Bytes()))
return false
}
if !verifyCommitAmount(action.GetTransfer()) {
if err := verifyCommitAmount(action.GetTransfer()); err != nil {
bizlog.Error("pubkey.VerifyBytes verify amount", "err", err)
return false
}
......
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