Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
52f1e829
Commit
52f1e829
authored
Feb 06, 2020
by
mdj33
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
relay multi coins support
parent
98d25c0a
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
145 additions
and
173 deletions
+145
-173
build.sh
plugin/dapp/relay/cmd/build.sh
+8
-8
testcase.sh
plugin/dapp/relay/cmd/build/testcase.sh
+0
-0
relayd.toml
plugin/dapp/relay/cmd/relayd/relayd.toml
+2
-2
relayd.go
plugin/dapp/relay/cmd/relayd/relayd/relayd.go
+1
-1
relay.go
plugin/dapp/relay/commands/relay.go
+13
-13
types.go
plugin/dapp/relay/commands/types.go
+3
-1
kv.go
plugin/dapp/relay/executor/kv.go
+13
-5
relay.go
plugin/dapp/relay/executor/relay.go
+5
-1
relay_test.go
plugin/dapp/relay/executor/relay_test.go
+11
-11
relaybtc.go
plugin/dapp/relay/executor/relaybtc.go
+2
-2
relaybtc_test.go
plugin/dapp/relay/executor/relaybtc_test.go
+2
-2
relaydb.go
plugin/dapp/relay/executor/relaydb.go
+0
-0
relaydb_test.go
plugin/dapp/relay/executor/relaydb_test.go
+55
-97
relay.proto
plugin/dapp/relay/proto/relay.proto
+29
-23
relay.go
plugin/dapp/relay/types/relay.go
+1
-7
No files found.
plugin/dapp/relay/cmd/build.sh
View file @
52f1e829
...
...
@@ -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
}
"
...
...
plugin/dapp/relay/cmd/build/testcase.sh
View file @
52f1e829
This diff is collapsed.
Click to expand it.
plugin/dapp/relay/cmd/relayd/relayd.toml
View file @
52f1e829
...
...
@@ -10,8 +10,8 @@ syncSetup = 100
syncSetupCount
=
10
[auth]
# test private key
privateKey
=
"
4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01
"
# test private key
1G5Cjy8LuQex2fuYv3gzb7B8MxAnxLEqt3
privateKey
=
"
22968d29c6de695381a8719ef7bf00e2edb6cce500bb59a4fc73c41887610962
"
publicKey
=
""
address
=
""
...
...
plugin/dapp/relay/cmd/relayd/relayd/relayd.go
View file @
52f1e829
...
...
@@ -344,7 +344,7 @@ func (r *Relayd) dealOrder() {
for
_
,
value
:=
range
result
.
GetOrders
()
{
// TODO save db ???
tx
,
err
:=
r
.
btcClient
.
GetTransaction
(
value
.
Coin
TxHash
)
tx
,
err
:=
r
.
btcClient
.
GetTransaction
(
value
.
X
TxHash
)
if
err
!=
nil
{
log
.
Error
(
"dealOrder"
,
"dealOrder GetTransaction error: "
,
err
)
continue
...
...
plugin/dapp/relay/commands/relay.go
View file @
52f1e829
...
...
@@ -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
.
Coin
Addr
show
.
CoinAmount
=
strconv
.
FormatFloat
(
float64
(
order
.
Coin
Amount
)
/
float64
(
types
.
Coin
),
'f'
,
4
,
64
)
show
.
CoinWaits
=
order
.
Coin
Waits
show
.
CoinOperation
=
order
.
Operation
show
.
Amount
=
strconv
.
FormatFloat
(
float64
(
order
.
LocalCoin
Amount
)
/
float64
(
types
.
Coin
),
'f'
,
4
,
64
)
show
.
Coin
=
order
.
X
Coin
show
.
CoinAddr
=
order
.
X
Addr
show
.
CoinAmount
=
strconv
.
FormatFloat
(
float64
(
order
.
X
Amount
)
/
float64
(
types
.
Coin
),
'f'
,
4
,
64
)
show
.
CoinWaits
=
order
.
XBlock
Waits
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
,
Bty
Amount
:
btyUInt64
*
1e4
,
XAmount
:
coinUInt64
*
1e4
,
XCoin
:
coin
,
XAddr
:
coinaddr
,
XBlockWaits
:
coinwait
,
LocalCoin
Amount
:
btyUInt64
*
1e4
,
}
var
res
string
...
...
@@ -435,8 +435,8 @@ func relayAccept(cmd *cobra.Command, args []string) {
params
:=
&
ty
.
RelayAccept
{
OrderId
:
orderID
,
CoinAddr
:
coinaddr
,
Coin
Waits
:
coinwait
,
XAddr
:
coinaddr
,
XBlock
Waits
:
coinwait
,
}
var
res
string
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"relay.CreateRawRelayAcceptTx"
,
params
,
&
res
)
...
...
plugin/dapp/relay/commands/types.go
View file @
52f1e829
...
...
@@ -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"`
}
plugin/dapp/relay/executor/kv.go
View file @
52f1e829
...
...
@@ -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
.
X
Coin
,
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
.
X
Coin
,
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
.
X
Coin
,
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
.
X
Coin
,
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
.
X
Coin
,
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
]
}
plugin/dapp/relay/executor/relay.go
View file @
52f1e829
...
...
@@ -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
.
LocalCoin
Amount
{
index
=
i
break
}
...
...
plugin/dapp/relay/executor/relay_test.go
View file @
52f1e829
...
...
@@ -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
,
Bty
Amount
:
200
*
1e8
,
XCoin
:
"BTC"
,
XAmount
:
0.299
*
1e8
,
XAddr
:
addrBtc
,
LocalCoin
Amount
:
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
.
Tx
Amount
)
s
.
Equal
(
uint64
(
10
),
log
.
Coin
Height
)
s
.
Equal
(
"200.0000"
,
log
.
LocalCoin
Amount
)
s
.
Equal
(
uint64
(
10
),
log
.
X
Height
)
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
.
Tx
Amount
)
s
.
Equal
(
uint64
(
20
),
log
.
Coin
Height
)
s
.
Equal
(
"200.0000"
,
log
.
LocalCoin
Amount
)
s
.
Equal
(
uint64
(
20
),
log
.
X
Height
)
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
.
Tx
Amount
)
s
.
Equal
(
uint64
(
30
),
log
.
Coin
Height
)
s
.
Equal
(
"200.0000"
,
log
.
LocalCoin
Amount
)
s
.
Equal
(
uint64
(
30
),
log
.
X
Height
)
s
.
Equal
(
ty
.
RelayOrderStatus_confirming
.
String
(),
log
.
CurStatus
)
}
...
...
plugin/dapp/relay/executor/relaybtc.go
View file @
52f1e829
...
...
@@ -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
.
Coin
Amount
{
if
outtx
.
Address
==
order
.
XAddr
&&
outtx
.
Value
>=
order
.
X
Amount
{
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
.
Coin
Waits
)
>
uint64
(
height
)
{
if
verify
.
Tx
.
BlockHeight
+
uint64
(
order
.
XBlock
Waits
)
>
uint64
(
height
)
{
return
ty
.
ErrRelayWaitBlocksErr
}
...
...
plugin/dapp/relay/executor/relaybtc_test.go
View file @
52f1e829
...
...
@@ -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
,
}
...
...
plugin/dapp/relay/executor/relaydb.go
View file @
52f1e829
This diff is collapsed.
Click to expand it.
plugin/dapp/relay/executor/relaydb_test.go
View file @
52f1e829
This diff is collapsed.
Click to expand it.
plugin/dapp/relay/proto/relay.proto
View file @
52f1e829
...
...
@@ -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
c
oin
=
7
;
uint64
coinAmount
=
8
;
string
coinAddr
=
9
;
string
coinTxHash
=
10
;
uint32
operation
=
6
;
string
xC
oin
=
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
tx
Amount
=
4
;
string
localCoin
Amount
=
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
{
...
...
plugin/dapp/relay/types/relay.go
View file @
52f1e829
...
...
@@ -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
()
.
Bty
Amount
),
nil
return
int64
(
relay
.
GetCreate
()
.
LocalCoin
Amount
),
nil
}
return
0
,
nil
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment