diff --git a/api.go b/api.go index b25893b..6462eea 100644 --- a/api.go +++ b/api.go @@ -456,7 +456,7 @@ func CloseDataAPIHandler(w http.ResponseWriter, r *http.Request) { return } - Log.Debugf("start send transaction to contract for publishing data...merkle root=%v, mode=%v, size=%v, n=%v, s=%v", b.SigmaMKLRoot, b.Mode, b.Size, b.N, b.S) + Log.Debugf("start send transaction to contract for closing data...merkle root=%v, mode=%v, size=%v, n=%v, s=%v", b.SigmaMKLRoot, b.Mode, b.Size, b.N, b.S) t := time.Now() txid, rs, err := closeDataAtContract(bltByte) if err != nil { diff --git a/const.go b/const.go index 667d021..025fac7 100644 --- a/const.go +++ b/const.go @@ -8,7 +8,7 @@ const REQUEST_METHOD_GET = "GET" const DEFAULT_BASIC_CONFGI_FILE = "./basic.json" const DEFAULT_CONFIG_FILE = "./config.json" const DEFAULT_KEYSTORE_FILE = "./keystore" -const DEFAULT_KEY_PATH = "./key" +const DEFAULT_KEY_PATH = "./zkPodParam" const DEFAULT_PUBLISH_BIN_FILE = "./bin/pod_publish" const DEFAULT_BOB_DIR = "./B" const DEFAULT_ALICE_DIR = "./A" @@ -33,6 +33,7 @@ const TRANSACTION_STATUS_NEGO_FAILED = "NegoFailed" const TRANSACTION_STATUS_RECEIVED_REQUEST = "requested" const TRANSACTION_STATUS_INVALID_REQUEST = "invalidRequest" const TRANSACTION_STATUS_RECEIVED_REQUEST_FAILED = "requestFailed" +const TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH = "depositNotEnough" const TRANSACTION_STATUS_RECEIVED_RESPONSE = "responsed" const TRANSACTION_STATUS_GENERATE_RESPONSE = "generateResponse" const TRANSACTION_STATUS_SEND_RESPONSE_FAILED = "responseFailed" diff --git a/contract.go b/contract.go index 252cf8a..91e1934 100644 --- a/contract.go +++ b/contract.go @@ -751,7 +751,7 @@ func readScrtForVRFQ(sessionID string, AliceAddr string, BobAddr string, Log ILo return secret, errors.New("No secret to be read") } -func verifyDeposit(AliceAddr string, BobAddr string, value int64) (bool, error) { +func calcuDeposit(AliceAddr string, BobAddr string, value int64) (bool, error) { dpst, err := ZkPoDExchangeClient.ZkPoDExchangeCaller.BobDeposits(&bind.CallOpts{}, common.HexToAddress(BobAddr), common.HexToAddress(AliceAddr)) if err != nil { return false, fmt.Errorf("failed to read deposit. err=%v", err) @@ -769,6 +769,20 @@ func verifyDeposit(AliceAddr string, BobAddr string, value int64) (bool, error) return true, nil } +func checkDeposit(AliceAddr string, BobAddr string, value int64) (bool, err) { + dpst, err := ZkPoDExchangeClient.ZkPoDExchangeCaller.BobDeposits(&bind.CallOpts{}, common.HexToAddress(BobAddr), common.HexToAddress(AliceAddr)) + if err != nil { + return false, fmt.Errorf("failed to read deposit. err=%v", err) + } + if dpst.Value.Int64() < value { + return false, nil + } + if dpst.UnDepositAt != 0 && dpst.UnDepositAt+28800 < time.Now().Unix()+600 && dpst.Stat == 1 { + return false, nil + } + return true, nil +} + func calcuBltKey(b Bulletin) ([32]byte, error) { size, _ := strconv.ParseUint(b.Size, 10, 64) s, _ := strconv.ParseUint(b.S, 10, 64) diff --git a/seller_tx.go b/seller_tx.go index 3119794..7e8aeb0 100644 --- a/seller_tx.go +++ b/seller_tx.go @@ -183,6 +183,27 @@ func AliceTxForPC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify transaction request and generate transaction response for Alice") + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -196,7 +217,8 @@ func AliceTxForPC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo Log.Debugf("success to send transaction response for Alice") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -208,6 +230,14 @@ func AliceTxForPC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive transaction receipt.") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.PlainComplaint.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -220,26 +250,6 @@ func AliceTxForPC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify receipt and generate secret.") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) - return fmt.Errorf( - "no enough deposit eth") - } - - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx @@ -388,6 +398,27 @@ func AliceTxForPOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL } AliceTxMap[tx.SessionID] = tx + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -401,7 +432,8 @@ func AliceTxForPOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -413,6 +445,14 @@ func AliceTxForPOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL tx.Status = TRANSACTION_STATUS_RECEIPT AliceTxMap[tx.SessionID] = tx + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.PlainOTComplaint.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -425,32 +465,21 @@ func AliceTxForPOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL tx.Status = TRANSACTION_STATUS_GENERATE_SECRET AliceTxMap[tx.SessionID] = tx - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -526,6 +555,27 @@ func AliceTxForPAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL } AliceTxMap[tx.SessionID] = tx + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -539,7 +589,8 @@ func AliceTxForPAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -551,6 +602,14 @@ func AliceTxForPAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL tx.Status = TRANSACTION_STATUS_RECEIPT AliceTxMap[tx.SessionID] = tx + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.PlainAtomicSwap.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -563,31 +622,21 @@ func AliceTxForPAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL tx.Status = TRANSACTION_STATUS_GENERATE_SECRET AliceTxMap[tx.SessionID] = tx - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth.") + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -600,6 +649,7 @@ func AliceTxForPAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL return fmt.Errorf( "failed to send secret") } + Log.Debugf("success to submit secret to contract...txid=%v, time cost=%v", txid, time.Since(t)) _, err = readScrtForAtomicSwap(tx.SessionID, tx.AliceAddr, tx.BobAddr, Log) @@ -663,6 +713,27 @@ func AliceTxForPASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log } AliceTxMap[tx.SessionID] = tx + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -676,7 +747,8 @@ func AliceTxForPASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log AliceTxMap[tx.SessionID] = tx var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -688,6 +760,14 @@ func AliceTxForPASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log tx.Status = TRANSACTION_STATUS_RECEIPT AliceTxMap[tx.SessionID] = tx + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.PlainAtomicSwapVc.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -700,31 +780,20 @@ func AliceTxForPASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log tx.Status = TRANSACTION_STATUS_GENERATE_SECRET AliceTxMap[tx.SessionID] = tx - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth.") + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -801,6 +870,27 @@ func AliceTxForTC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify transaction request and generate transaction response.") + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -814,7 +904,8 @@ func AliceTxForTC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo Log.Debugf("success to send transaction response to Bob.") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -826,6 +917,14 @@ func AliceTxForTC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive receipt from Bob.") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.TableComplaint.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -838,32 +937,20 @@ func AliceTxForTC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify receipt and generate secret.") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -1006,6 +1093,27 @@ func AliceTxForTOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("verify transaction request file and GENERATE response file...") + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -1019,7 +1127,8 @@ func AliceTxForTOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL Log.Debugf("send transaction response...") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -1031,6 +1140,14 @@ func AliceTxForTOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("receive transaction receipt...") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.TableOTComplaint.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -1043,32 +1160,20 @@ func AliceTxForTOC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("verify receipt file and GENERATE secret file...") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -1145,6 +1250,27 @@ func AliceTxForTAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify transaction request and generate response.") + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -1158,7 +1284,8 @@ func AliceTxForTAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL Log.Debugf("success to send transaction response to Bob.") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -1170,6 +1297,14 @@ func AliceTxForTAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive receipt from Bob.") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.TableAtomicSwap.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -1182,32 +1317,20 @@ func AliceTxForTAS(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify receipt and generate secret.") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -1284,6 +1407,27 @@ func AliceTxForTASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify transaction request and generate response.") + tx.Price = tx.Count * tx.UnitPrice + rs, err = calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + err = AliceSendPODResp(node, responseFile) if err != nil { tx.Status = TRANSACTION_STATUS_SEND_RESPONSE_FAILED @@ -1297,7 +1441,8 @@ func AliceTxForTASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log Log.Debugf("success to send transaction response to Bob.") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -1309,6 +1454,14 @@ func AliceTxForTASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive receipt from Bob.") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.TableAtomicSwapVc.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -1321,32 +1474,20 @@ func AliceTxForTASVC(node *pod_net.Node, key *keystore.Key, tx Transaction, Log AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify receipt and generate secret.") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -1392,7 +1533,28 @@ func AliceTxForTQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo delete(AliceTxMap, tx.SessionID) }() - err := AliceRcvPODReq(node, requestFile) + tx.Price = tx.UnitPrice + rs, err := calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + + err = AliceRcvPODReq(node, requestFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_REQUEST_FAILED AliceTxMap[tx.SessionID] = tx @@ -1404,7 +1566,7 @@ func AliceTxForTQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive transaction request for Alice.") - rs := tx.TableVRF.AliceVerifyReq(requestFile, responseFile, Log) + rs = tx.TableVRF.AliceVerifyReq(requestFile, responseFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_INVALID_REQUEST AliceTxMap[tx.SessionID] = tx @@ -1429,7 +1591,8 @@ func AliceTxForTQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo Log.Debugf("success to send transaction response for Alice.") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -1441,6 +1604,14 @@ func AliceTxForTQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive receipt from Bob.") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.TableVRF.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED @@ -1453,32 +1624,20 @@ func AliceTxForTQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log ILo AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify receipt.") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...") @@ -1529,7 +1688,28 @@ func AliceTxForTOQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL delete(AliceTxMap, tx.SessionID) }() - err := AliceReceiveNegoReq(node, BobNegoRequestFile) + tx.Price = tx.UnitPrice + rs, err := calcuDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("failed to verify deposit eth. err=%v", err) + return fmt.Errorf( + "failed to verify deposit eth") + } + if !rs { + tx.Status = TRANSACTION_STATUS_RECEIVED_DEPOSIT_NOT_ENOUGH + AliceTxMap[tx.SessionID] = tx + Log.Warnf("no enough deposit eth. err=%v", err) + return fmt.Errorf( + "no enough deposit eth") + } + + defer func() { + DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price + }() + + err = AliceReceiveNegoReq(node, BobNegoRequestFile) if err != nil { tx.Status = TRANSACTION_STATUS_NEGO_FAILED AliceTxMap[tx.SessionID] = tx @@ -1539,7 +1719,7 @@ func AliceTxForTOQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL } Log.Debugf("success to receive transaction nego request.") - rs := tx.TableOTVRF.AliceGeneNegoResp(BobNegoRequestFile, AliceNegoResponseFile, Log) + rs = tx.TableOTVRF.AliceGeneNegoResp(BobNegoRequestFile, AliceNegoResponseFile, Log) if !rs { tx.Status = TRANSACTION_STATUS_NEGO_FAILED AliceTxMap[tx.SessionID] = tx @@ -1628,7 +1808,8 @@ func AliceTxForTOQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL Log.Debugf("success to send transaction response to Bob") var sign []byte - sign, tx.Price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) + var price int64 + sign, price, tx.ExpireAt, err = AliceRcvPODRecpt(node, receiptFile) if err != nil { tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx @@ -1640,9 +1821,17 @@ func AliceTxForTOQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("success to receive receipt for Alice") + if price != tx.Price { + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED + AliceTxMap[tx.SessionID] = tx + Log.Warnf("invalid price in signature. price=%v, real price=%v", price, tx.Price) + return fmt.Errorf( + "invalid price in signature") + } + rs = tx.TableOTVRF.AliceVerifyReceipt(receiptFile, secretFile, Log) if !rs { - tx.Status = TRANSACTION_STATUS_GENERATE_SECRET_FAILED + tx.Status = TRANSACTION_STATUS_RECEIVED_RECEIPT_FAILED AliceTxMap[tx.SessionID] = tx Log.Warnf("invalid receipt file or secret file. err=%v", err) return fmt.Errorf( @@ -1652,32 +1841,21 @@ func AliceTxForTOQ(node *pod_net.Node, key *keystore.Key, tx Transaction, Log IL AliceTxMap[tx.SessionID] = tx Log.Debugf("success to verify receipt and generate secret") - rs, err = verifyDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) - if err != nil { - tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED - AliceTxMap[tx.SessionID] = tx - Log.Warnf("failed to verify deposit eth. err=%v", err) - return fmt.Errorf( - "failed to verify deposit eth") - } - if !rs { + if time.Now().Unix()+600 > tx.ExpireAt { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("no enough deposit eth. err=%v", err) + Log.Warnf("the receipt signature will timeout soon.") return fmt.Errorf( - "no enough deposit eth") + "the receipt signature timeout") } - defer func() { - DepositLockMap[tx.AliceAddr+tx.BobAddr] -= tx.Price - }() - - if time.Now().Unix()+600 > tx.ExpireAt { + rs, err = checkDeposit(tx.AliceAddr, tx.BobAddr, tx.Price) + if err != nil { tx.Status = TRANSACTION_STATUS_SEND_SECRET_TERMINATED AliceTxMap[tx.SessionID] = tx - Log.Warnf("the receipt signature will timeout soon.") + Log.Warnf("check deposit...unpass") return fmt.Errorf( - "the receipt signature timeout") + "deposit is not enough or expired") } Log.Debugf("start send transaction to submit contract from contract...")