Commit 52f1e829 authored by mdj33's avatar mdj33

relay multi coins support

parent 98d25c0a
...@@ -4,14 +4,14 @@ strpwd=$(pwd) ...@@ -4,14 +4,14 @@ strpwd=$(pwd)
strcmd=${strpwd##*dapp/} strcmd=${strpwd##*dapp/}
strapp=${strcmd%/cmd*} strapp=${strcmd%/cmd*}
#OUT_DIR="${1}/$strapp" OUT_DIR="${1}/$strapp"
#SRC_RELAYD=github.com/33cn/plugin/plugin/dapp/relay/cmd/relayd SRC_RELAYD=github.com/33cn/plugin/plugin/dapp/relay/cmd/relayd
#FLAG=$2 FLAG=$2
#
## shellcheck disable=SC2086,1072 # shellcheck disable=SC2086,1072
#go build -i ${FLAG} -v -o "${OUT_DIR}/relayd" "${SRC_RELAYD}" go build -i ${FLAG} -v -o "${OUT_DIR}/relayd" "${SRC_RELAYD}"
#cp ./relayd/relayd.toml "${OUT_DIR}/relayd.toml" cp ./relayd/relayd.toml "${OUT_DIR}/relayd.toml"
#cp ./build/* "${OUT_DIR}" cp ./build/* "${OUT_DIR}"
OUT_TESTDIR="${1}/dapptest/$strapp" OUT_TESTDIR="${1}/dapptest/$strapp"
mkdir -p "${OUT_TESTDIR}" mkdir -p "${OUT_TESTDIR}"
......
This diff is collapsed.
...@@ -10,8 +10,8 @@ syncSetup = 100 ...@@ -10,8 +10,8 @@ syncSetup = 100
syncSetupCount = 10 syncSetupCount = 10
[auth] [auth]
# test private key # test private key 1G5Cjy8LuQex2fuYv3gzb7B8MxAnxLEqt3
privateKey = "4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01" privateKey = "22968d29c6de695381a8719ef7bf00e2edb6cce500bb59a4fc73c41887610962"
publicKey = "" publicKey = ""
address = "" address = ""
......
...@@ -344,7 +344,7 @@ func (r *Relayd) dealOrder() { ...@@ -344,7 +344,7 @@ func (r *Relayd) dealOrder() {
for _, value := range result.GetOrders() { for _, value := range result.GetOrders() {
// TODO save db ??? // TODO save db ???
tx, err := r.btcClient.GetTransaction(value.CoinTxHash) tx, err := r.btcClient.GetTransaction(value.XTxHash)
if err != nil { if err != nil {
log.Error("dealOrder", "dealOrder GetTransaction error: ", err) log.Error("dealOrder", "dealOrder GetTransaction error: ", err)
continue continue
......
...@@ -302,12 +302,12 @@ func parseRelayOrders(res ty.ReplyRelayOrders) { ...@@ -302,12 +302,12 @@ func parseRelayOrders(res ty.ReplyRelayOrders) {
show.OrderID = order.Id show.OrderID = order.Id
show.Status = order.Status.String() show.Status = order.Status.String()
show.Creator = order.CreaterAddr show.Creator = order.CreaterAddr
show.CoinOperation = ty.RelayOrderOperation[order.CoinOperation] show.CoinOperation = order.Operation
show.Amount = strconv.FormatFloat(float64(order.Amount)/float64(types.Coin), 'f', 4, 64) show.Amount = strconv.FormatFloat(float64(order.LocalCoinAmount)/float64(types.Coin), 'f', 4, 64)
show.Coin = order.Coin show.Coin = order.XCoin
show.CoinAddr = order.CoinAddr show.CoinAddr = order.XAddr
show.CoinAmount = strconv.FormatFloat(float64(order.CoinAmount)/float64(types.Coin), 'f', 4, 64) show.CoinAmount = strconv.FormatFloat(float64(order.XAmount)/float64(types.Coin), 'f', 4, 64)
show.CoinWaits = order.CoinWaits show.CoinWaits = order.XBlockWaits
show.CreateTime = order.CreateTime show.CreateTime = order.CreateTime
show.AcceptAddr = order.AcceptAddr show.AcceptAddr = order.AcceptAddr
show.AcceptTime = order.AcceptTime show.AcceptTime = order.AcceptTime
...@@ -389,11 +389,11 @@ func relayOrder(cmd *cobra.Command, args []string) { ...@@ -389,11 +389,11 @@ func relayOrder(cmd *cobra.Command, args []string) {
params := &ty.RelayCreate{ params := &ty.RelayCreate{
Operation: oper, Operation: oper,
Amount: coinUInt64 * 1e4, XAmount: coinUInt64 * 1e4,
Coin: coin, XCoin: coin,
Addr: coinaddr, XAddr: coinaddr,
CoinWaits: coinwait, XBlockWaits: coinwait,
BtyAmount: btyUInt64 * 1e4, LocalCoinAmount: btyUInt64 * 1e4,
} }
var res string var res string
...@@ -435,8 +435,8 @@ func relayAccept(cmd *cobra.Command, args []string) { ...@@ -435,8 +435,8 @@ func relayAccept(cmd *cobra.Command, args []string) {
params := &ty.RelayAccept{ params := &ty.RelayAccept{
OrderId: orderID, OrderId: orderID,
CoinAddr: coinaddr, XAddr: coinaddr,
CoinWaits: coinwait, XBlockWaits: coinwait,
} }
var res string var res string
ctx := jsonclient.NewRPCCtx(rpcLaddr, "relay.CreateRawRelayAcceptTx", params, &res) ctx := jsonclient.NewRPCCtx(rpcLaddr, "relay.CreateRawRelayAcceptTx", params, &res)
......
...@@ -9,7 +9,7 @@ type relayOrder2Show struct { ...@@ -9,7 +9,7 @@ type relayOrder2Show struct {
Status string `json:"status"` Status string `json:"status"`
Creator string `json:"address"` Creator string `json:"address"`
Amount string `json:"amount"` Amount string `json:"amount"`
CoinOperation string `json:"coinoperation"` CoinOperation uint32 `json:"coinoperation"`
Coin string `json:"coin"` Coin string `json:"coin"`
CoinAmount string `json:"coinamount"` CoinAmount string `json:"coinamount"`
CoinAddr string `json:"coinaddr"` CoinAddr string `json:"coinaddr"`
...@@ -21,4 +21,6 @@ type relayOrder2Show struct { ...@@ -21,4 +21,6 @@ type relayOrder2Show struct {
FinishTime int64 `json:"finishtime"` FinishTime int64 `json:"finishtime"`
FinishTxHash string `json:"finishtxhash"` FinishTxHash string `json:"finishtxhash"`
Height int64 `json:"height"` Height int64 `json:"height"`
LocalCoinExec string `json:"localCoinExec"`
LocalCoinSym string `json:"localCoinSym"`
} }
...@@ -7,6 +7,8 @@ package executor ...@@ -7,6 +7,8 @@ package executor
import ( import (
"fmt" "fmt"
"strings"
ty "github.com/33cn/plugin/plugin/dapp/relay/types" ty "github.com/33cn/plugin/plugin/dapp/relay/types"
) )
...@@ -46,25 +48,25 @@ func calcBtcHeaderKeyHeightList(height int64) []byte { ...@@ -46,25 +48,25 @@ func calcBtcHeaderKeyHeightList(height int64) []byte {
func calcOrderKeyStatus(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyStatus(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderSCAIH+"%d:%s:%s:%s:%d", key := fmt.Sprintf(relayOrderSCAIH+"%d:%s:%s:%s:%d",
status, order.Coin, order.CreaterAddr, order.Id, order.Height) status, order.XCoin, order.CreaterAddr, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
func calcOrderKeyCoin(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyCoin(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderCSAIH+"%s:%d:%s:%s:%d", key := fmt.Sprintf(relayOrderCSAIH+"%s:%d:%s:%s:%d",
order.Coin, status, order.CreaterAddr, order.Id, order.Height) order.XCoin, status, order.CreaterAddr, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
func calcOrderKeyAddrStatus(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyAddrStatus(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderASCIH+"%s:%d:%s:%s:%d", key := fmt.Sprintf(relayOrderASCIH+"%s:%d:%s:%s:%d",
order.CreaterAddr, status, order.Coin, order.Id, order.Height) order.CreaterAddr, status, order.XCoin, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
func calcOrderKeyAddrCoin(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyAddrCoin(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderACSIH+"%s:%s:%d:%s:%d", key := fmt.Sprintf(relayOrderACSIH+"%s:%s:%d:%s:%d",
order.CreaterAddr, order.Coin, status, order.Id, order.Height) order.CreaterAddr, order.XCoin, status, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
...@@ -90,7 +92,7 @@ func calcOrderPrefixAddr(addr string) []byte { ...@@ -90,7 +92,7 @@ func calcOrderPrefixAddr(addr string) []byte {
func calcAcceptKeyAddr(order *ty.RelayOrder, status int32) []byte { func calcAcceptKeyAddr(order *ty.RelayOrder, status int32) []byte {
if order.AcceptAddr != "" { if order.AcceptAddr != "" {
return []byte(fmt.Sprintf(relayBuyOrderACSIH+"%s:%s:%d:%s:%d", return []byte(fmt.Sprintf(relayBuyOrderACSIH+"%s:%s:%d:%s:%d",
order.AcceptAddr, order.Coin, status, order.Id, order.Height)) order.AcceptAddr, order.XCoin, status, order.Id, order.Height))
} }
return nil return nil
...@@ -111,3 +113,9 @@ func calcRelayOrderID(hash string) string { ...@@ -111,3 +113,9 @@ func calcRelayOrderID(hash string) string {
func calcCoinHash(hash string) string { func calcCoinHash(hash string) string {
return coinHashPrefix + hash return coinHashPrefix + hash
} }
func getRealTxHashID(id string) string {
ids := strings.Split(id, "-")
return ids[len(ids)-1]
}
...@@ -133,6 +133,10 @@ func (r *relay) getRelayOrderReply(OrderIDs [][]byte) (types.Message, error) { ...@@ -133,6 +133,10 @@ func (r *relay) getRelayOrderReply(OrderIDs [][]byte) (types.Message, error) {
orderIDGot[string(orderID)] = true orderIDGot[string(orderID)] = true
} }
} }
//get remove mavl-xx- prefix
for _, order := range reply.Relayorders {
order.Id = getRealTxHashID(order.Id)
}
return &reply, nil return &reply, nil
} }
...@@ -142,7 +146,7 @@ func insertOrderDescending(toBeInserted *ty.RelayOrder, orders []*ty.RelayOrder) ...@@ -142,7 +146,7 @@ func insertOrderDescending(toBeInserted *ty.RelayOrder, orders []*ty.RelayOrder)
} else { } else {
index := len(orders) index := len(orders)
for i, element := range orders { for i, element := range orders {
if toBeInserted.Amount >= element.Amount { if toBeInserted.LocalCoinAmount >= element.LocalCoinAmount {
index = i index = i
break break
} }
......
...@@ -124,10 +124,10 @@ func (s *suiteRelay) testExecDelLocal(tx *types.Transaction, receipt *types.Rece ...@@ -124,10 +124,10 @@ func (s *suiteRelay) testExecDelLocal(tx *types.Transaction, receipt *types.Rece
func (s *suiteRelay) TestExec_1() { func (s *suiteRelay) TestExec_1() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderSell, Operation: ty.RelayOrderSell,
Coin: "BTC", XCoin: "BTC",
Amount: 0.299 * 1e8, XAmount: 0.299 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
sell := &ty.RelayAction{ sell := &ty.RelayAction{
...@@ -156,8 +156,8 @@ func (s *suiteRelay) TestExec_1() { ...@@ -156,8 +156,8 @@ func (s *suiteRelay) TestExec_1() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
...@@ -170,7 +170,7 @@ func (s *suiteRelay) TestExec_1() { ...@@ -170,7 +170,7 @@ func (s *suiteRelay) TestExec_1() {
func (s *suiteRelay) TestExec_2() { func (s *suiteRelay) TestExec_2() {
order := &ty.RelayAccept{ order := &ty.RelayAccept{
OrderId: s.orderID, OrderId: s.orderID,
CoinAddr: addrBtc, XAddr: addrBtc,
} }
sell := &ty.RelayAction{ sell := &ty.RelayAction{
...@@ -197,8 +197,8 @@ func (s *suiteRelay) TestExec_2() { ...@@ -197,8 +197,8 @@ func (s *suiteRelay) TestExec_2() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus)
} }
...@@ -233,8 +233,8 @@ func (s *suiteRelay) TestExec_3() { ...@@ -233,8 +233,8 @@ func (s *suiteRelay) TestExec_3() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(30), log.CoinHeight) s.Equal(uint64(30), log.XHeight)
s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus)
} }
......
...@@ -197,7 +197,7 @@ func (b *btcStore) getMerkleRootFromHeader(blockhash string) (string, error) { ...@@ -197,7 +197,7 @@ func (b *btcStore) getMerkleRootFromHeader(blockhash string) (string, error) {
func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) error { func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) error {
var foundtx bool var foundtx bool
for _, outtx := range verify.GetTx().GetVout() { for _, outtx := range verify.GetTx().GetVout() {
if outtx.Address == order.CoinAddr && outtx.Value >= order.CoinAmount { if outtx.Address == order.XAddr && outtx.Value >= order.XAmount {
foundtx = true foundtx = true
} }
} }
...@@ -220,7 +220,7 @@ func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) err ...@@ -220,7 +220,7 @@ func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) err
return err return err
} }
if verify.Tx.BlockHeight+uint64(order.CoinWaits) > uint64(height) { if verify.Tx.BlockHeight+uint64(order.XBlockWaits) > uint64(height) {
return ty.ErrRelayWaitBlocksErr return ty.ErrRelayWaitBlocksErr
} }
......
...@@ -197,8 +197,8 @@ func (s *suiteBtcStore) TestGetMerkleRootFromHeader() { ...@@ -197,8 +197,8 @@ func (s *suiteBtcStore) TestGetMerkleRootFromHeader() {
func (s *suiteBtcStore) TestVerifyBtcTx() { func (s *suiteBtcStore) TestVerifyBtcTx() {
order := &ty.RelayOrder{ order := &ty.RelayOrder{
CoinAddr: "1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT", XAddr: "1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT",
CoinAmount: 29900000, XAmount: 29900000,
AcceptTime: 100, AcceptTime: 100,
ConfirmTime: 200, ConfirmTime: 200,
} }
......
This diff is collapsed.
This diff is collapsed.
...@@ -28,13 +28,13 @@ message RelayOrder { ...@@ -28,13 +28,13 @@ message RelayOrder {
string id = 1; string id = 1;
RelayOrderStatus status = 2; RelayOrderStatus status = 2;
RelayOrderStatus preStatus = 3; RelayOrderStatus preStatus = 3;
uint64 amount = 4; uint64 localCoinAmount = 4;
string createrAddr = 5; string createrAddr = 5;
uint32 coinOperation = 6; uint32 operation = 6;
string coin = 7; string xCoin = 7;
uint64 coinAmount = 8; uint64 xAmount = 8;
string coinAddr = 9; string xAddr = 9;
string coinTxHash = 10; string xTxHash = 10;
int64 createTime = 11; int64 createTime = 11;
string acceptAddr = 12; string acceptAddr = 12;
int64 acceptTime = 13; int64 acceptTime = 13;
...@@ -42,23 +42,27 @@ message RelayOrder { ...@@ -42,23 +42,27 @@ message RelayOrder {
int64 finishTime = 15; int64 finishTime = 15;
string finishTxHash = 16; string finishTxHash = 16;
int64 height = 17; int64 height = 17;
uint64 coinHeight = 18; uint64 XHeight = 18;
uint32 coinWaits = 19; uint32 xBlockWaits = 19;
string localCoinSymbol = 20;
string localCoinExec = 21;
} }
message RelayCreate { message RelayCreate {
uint32 operation = 1; // 0: buy, 1: sell uint32 operation = 1; // 0: buy, 1: sell
string coin = 2; // outside coin BTC, ETH... string xCoin = 2; // outside cross coin BTC, ETH...
uint64 amount = 3; uint64 xAmount = 3;
string addr = 4; string xAddr = 4;
uint64 btyAmount = 5; uint64 localCoinAmount = 5;
uint32 coinWaits = 6; // the coin blocks to wait uint32 xBlockWaits = 6; // the coin blocks to wait
string localCoinSymbol = 7;
string localCoinExec = 8;
} }
message RelayAccept { message RelayAccept {
string orderId = 1; string orderId = 1;
string coinAddr = 2; // for sell coin case needed string xAddr = 2; // for sell coin case needed
uint32 coinWaits = 3; uint32 xBlockWaits = 3;
} }
message RelayRevoke { message RelayRevoke {
...@@ -154,21 +158,23 @@ message ReceiptRelayLog { ...@@ -154,21 +158,23 @@ message ReceiptRelayLog {
string orderId = 1; string orderId = 1;
string curStatus = 2; string curStatus = 2;
string preStatus = 3; string preStatus = 3;
string txAmount = 4; string localCoinAmount = 4;
string createrAddr = 5; string createrAddr = 5;
string coinOperation = 6; uint32 coinOperation = 6;
string coin = 7; string xCoin = 7;
string coinAmount = 8; string xAmount = 8;
string coinAddr = 9; string xAddr = 9;
string coinTxHash = 10; string xTxHash = 10;
int64 createTime = 11; int64 createTime = 11;
string acceptAddr = 12; string acceptAddr = 12;
int64 acceptTime = 13; int64 acceptTime = 13;
int64 confirmTime = 14; int64 confirmTime = 14;
int64 finishTime = 15; int64 finishTime = 15;
string finishTxHash = 16; string finishTxHash = 16;
uint64 coinHeight = 17; uint64 xHeight = 17;
uint32 coinWaits = 18; uint32 xBlockWaits = 18;
string localCoinSymbol = 19;
string localCoinExec = 20;
} }
message ReqRelayAddrCoins { message ReqRelayAddrCoins {
......
...@@ -42,12 +42,6 @@ const ( ...@@ -42,12 +42,6 @@ const (
RelayOrderSell RelayOrderSell
) )
// RelayOrderOperation buy or sell operation
var RelayOrderOperation = map[uint32]string{
RelayOrderBuy: "buy",
RelayOrderSell: "sell",
}
const ( const (
// RelayUnlock revoke order // RelayUnlock revoke order
RelayUnlock = iota RelayUnlock = iota
...@@ -171,7 +165,7 @@ func (r *RelayType) Amount(tx *types.Transaction) (int64, error) { ...@@ -171,7 +165,7 @@ func (r *RelayType) Amount(tx *types.Transaction) (int64, error) {
} }
relay := data.(*RelayAction) relay := data.(*RelayAction)
if RelayActionCreate == relay.Ty && relay.GetCreate() != nil { if RelayActionCreate == relay.Ty && relay.GetCreate() != nil {
return int64(relay.GetCreate().BtyAmount), nil return int64(relay.GetCreate().LocalCoinAmount), nil
} }
return 0, nil return 0, nil
} }
......
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