Commit 52f1e829 authored by mdj33's avatar mdj33

relay multi coins support

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