Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
465827b
add scan LTC
gaozhengxin Feb 7, 2021
ab7f69e
add cosmos and terra
gaozhengxin Feb 21, 2021
641a5c6
update cosmos and terra, implements scan/verify swapin, build tx, rem…
gaozhengxin Feb 23, 2021
5fe81fa
update cosmos and terra, implements signtx, verify txhash, modify sca…
gaozhengxin Feb 24, 2021
05bab3a
update cosmos and terra, implements broadcast, modify dependencies
gaozhengxin Feb 25, 2021
5921876
go fmt, add logs
gaozhengxin Feb 25, 2021
aeb985b
add genAddress, add terra config example
gaozhengxin Feb 25, 2021
c2869dc
export terra.InitSDK
gaozhengxin Feb 25, 2021
4b7219b
add cosmos config example, modify cosmos genAddress tool
gaozhengxin Feb 26, 2021
7fb3ac9
use TxResponse in processTx, in order not to query Tx with hash again
gaozhengxin Feb 26, 2021
ea6fa89
add multisend msg in searchTxs and searchTxsWithHash
gaozhengxin Feb 26, 2021
65436f8
move genAddress to cosmosTools, modify terra and cosmos configs
gaozhengxin Feb 26, 2021
82f0d6d
update cosmos and terra init functions, use amino CDC to unmarshal re…
gaozhengxin Feb 26, 2021
a7aae68
modify cosmos and terra configs
gaozhengxin Feb 26, 2021
03b1455
fix bug, terra InitSDK config not saved
gaozhengxin Feb 26, 2021
60b4f3f
check tx code in cosmos restclient
gaozhengxin Feb 26, 2021
4c5be4f
modify cosmos and terra init coins
gaozhengxin Feb 26, 2021
7f5c353
1. add IsSwapoutToStringAddress, 2. add cosmos rest4 functions
gaozhengxin Mar 1, 2021
47f9afd
modify go.mod
gaozhengxin Mar 1, 2021
cec0034
remove rest4client because i notice rest api still supported in gaia …
gaozhengxin Mar 1, 2021
a3abe38
update cosmos and terra, use swapinfo for every coin output when veri…
gaozhengxin Mar 2, 2021
69461dc
update cosmos and terra, add comments, use Fee
gaozhengxin Mar 2, 2021
85bdc66
modify terra, register cosmos-sdk/x/auth/StdTx as core/StdTx
gaozhengxin Mar 2, 2021
22b7fc2
modify cosmos and terra, register bank msgs
gaozhengxin Mar 2, 2021
4741427
modify GetBindAddressFromMemo
gaozhengxin Mar 2, 2021
4634611
modify cosmos and terra, get bind address lower
gaozhengxin Mar 2, 2021
612c9b9
modify cosmos, txid to lower
gaozhengxin Mar 3, 2021
066fac8
cosmos verify swapin aggregate output
gaozhengxin Mar 3, 2021
0e0bde4
modify cosmos verify tx, add Hash
gaozhengxin Mar 3, 2021
1059be6
modify cosmos and terra, dynamic add tokens
gaozhengxin Mar 4, 2021
8cfd2c5
modify cosmos and terra, LoadCoins and GetCoin convert to Upper
gaozhengxin Mar 4, 2021
e2368d1
modify cosmos and terra load coins
gaozhengxin Mar 4, 2021
ba84521
fix cosmos bugs, accountNumberCached not initiated, parse cosmos addr…
gaozhengxin Mar 4, 2021
0d09a80
fix cosmos bugs: verify tx with args
gaozhengxin Mar 4, 2021
ab20daa
modify cosmos and terra, fix rest api types not match, BuildTx args n…
gaozhengxin Mar 5, 2021
733fd9a
add cosmos sign and broadcast demo
gaozhengxin Mar 8, 2021
6d4c62b
modify cosmos, 1. use hash from broadcast result, 2. add muon as fee …
gaozhengxin Mar 8, 2021
19f6f55
modify muon decimal
gaozhengxin Mar 8, 2021
d510c19
add cosmos sign byte modifier to adapt to terra
gaozhengxin Mar 8, 2021
5fe3e40
add logs
gaozhengxin Mar 9, 2021
012d992
modify cosmos search tx
gaozhengxin Mar 10, 2021
7d569a7
modify cosmos search txs, page start at 1
gaozhengxin Mar 10, 2021
8adcf95
set cosmos scan peroid to 10 blocks
gaozhengxin Mar 10, 2021
f09b22c
remove logs
gaozhengxin Mar 10, 2021
c205f39
add cosmos/terra sign tx tools, fix cosmos rest client bugs, add Sign…
gaozhengxin Mar 10, 2021
8abcbe5
merge terra
gaozhengxin Mar 10, 2021
64ca5d0
merge terra
gaozhengxin Mar 10, 2021
03657b1
modify go mod
gaozhengxin Mar 12, 2021
bdb0f82
fix cosmos bugs, choose swapinfo for given pairID in VerifyTransaction
gaozhengxin Mar 12, 2021
d56e7b1
Merge remote-tracking branch 'origin/terra' into solana # 请输入一个提交信息以解…
gaozhengxin Mar 12, 2021
cb0f9e9
update swapResult after send cosmos tx
gaozhengxin Mar 16, 2021
c218438
Merge remote-tracking branch 'origin/terra' into solana
gaozhengxin Mar 16, 2021
d6a1618
init ckb
gaozhengxin Mar 16, 2021
33ab023
add type swapincommitment
gaozhengxin Mar 22, 2021
89660b8
update ckb
gaozhengxin Mar 22, 2021
f3ad685
merge ckb update
gaozhengxin Mar 22, 2021
88f9c00
update solana test
gaozhengxin Mar 22, 2021
45b76b5
add solana rpc
gaozhengxin Mar 23, 2021
040d2f7
rename type swapinCommitment to swapinPromise, add swapinPromise mong…
gaozhengxin Mar 24, 2021
2acf511
add solana verify swapin
gaozhengxin Mar 24, 2021
7f6386c
add solana sign tx
gaozhengxin Mar 25, 2021
05ce43c
add ed25519 in dcrm sign
gaozhengxin Mar 26, 2021
de8b5ad
remove test codes
gaozhengxin Mar 26, 2021
0bc0568
fix swapin promise code errors
gaozhengxin Mar 26, 2021
b919ad6
fix solana bridges code errors
gaozhengxin Mar 26, 2021
b71e865
rename tokens.SwapinPromise to tokens.SwapAgreement, add solana ws su…
gaozhengxin Mar 29, 2021
7df358e
modify code error
gaozhengxin Mar 29, 2021
a0b06c4
scan solana txids with address, add solana scanned txid apis
gaozhengxin Mar 29, 2021
2e6cd7e
clean code
gaozhengxin Mar 30, 2021
10990d0
clean code
gaozhengxin Mar 30, 2021
719e18f
modify swap agreement api and mongo methods, add json, remove rest sw…
gaozhengxin Mar 30, 2021
daf95ac
modify solana verify tx, bind swapinfos and errs
gaozhengxin Mar 30, 2021
737c347
init tron
gaozhengxin Mar 30, 2021
a09c7ba
add solanaAddress, rename cosmosTools to cosmosdev, rename solanaTool…
gaozhengxin Mar 30, 2021
60bf8a1
Merge remote-tracking branch 'origin/solana' into tron
gaozhengxin Mar 30, 2021
4741d3b
modify trontools ethToTron, add prefix 0x41
gaozhengxin Mar 31, 2021
1a7d9e8
add tron build tx
gaozhengxin Apr 2, 2021
442a8c3
add tron verify tx
gaozhengxin Apr 6, 2021
2e59fe5
add tron sign and broadcast tx
gaozhengxin Apr 6, 2021
32bd397
ready to build tron bridge
gaozhengxin Apr 6, 2021
1276b37
use eth format address in tron2eth
gaozhengxin Apr 7, 2021
57204a7
fix pubkey to eth address in trontools
gaozhengxin Apr 7, 2021
1ce6321
fix tron pubkey to address
gaozhengxin Apr 7, 2021
235fe9c
modify tron verify contracts
gaozhengxin Apr 7, 2021
e1e5913
fix tronToEth address convert error
gaozhengxin Apr 7, 2021
f7d2a54
fix tron errors, use tron addr format in building tx
gaozhengxin Apr 8, 2021
bbbfa17
add marshaled tx in BuildTxArgs, unmarshal tx from BuildTxArgs when r…
gaozhengxin Apr 9, 2021
f6a1d8f
modify tron accept swapout
gaozhengxin Apr 12, 2021
a392cac
modify trc20 swapin verify
gaozhengxin Apr 12, 2021
d89d929
modify trc20 swapout
gaozhengxin Apr 12, 2021
a7a81f6
add tron verify tx with args
gaozhengxin Apr 12, 2021
147661b
modify verify tron rebuilt tx
gaozhengxin Apr 13, 2021
6f0cee1
add tron verify swapin, add tron verify logs, add tron check tx and c…
gaozhengxin Apr 14, 2021
dcd27df
fix verify tron swapout
gaozhengxin Apr 14, 2021
1370691
add fee limit, check tx res and contract res in GetTransactionStatus
gaozhengxin Apr 15, 2021
7545dca
modify tron feelimit and expiration
gaozhengxin Apr 15, 2021
d0503d3
modify handle check tron tx fail, modify tron scan
gaozhengxin Apr 15, 2021
135718b
modify tron scan
gaozhengxin Apr 15, 2021
4ef063a
modify verify tron swapin log
gaozhengxin Apr 15, 2021
2678f87
add custome check stable, use custome check stable for tron
gaozhengxin Apr 23, 2021
3cc6a13
merge anyswap master
gaozhengxin Apr 26, 2021
14bd03d
remove block test
gaozhengxin Apr 26, 2021
6634dae
code refactory, go test passed
gaozhengxin Apr 26, 2021
971a821
update
gaozhengxin Apr 26, 2021
6d04b80
modify check tron status, non-contract tx has empty contract res
gaozhengxin Apr 27, 2021
a6ac2c3
modify tron check status
gaozhengxin Apr 28, 2021
98f08ce
modify tron verify status
gaozhengxin Apr 28, 2021
b80aadb
modify tron dcrm sign
gaozhengxin Apr 28, 2021
8bd7dcd
modify costom check stable
gaozhengxin Apr 28, 2021
86bbc14
update tron
gaozhengxin Apr 29, 2021
a0add61
modify tron get status
gaozhengxin Apr 30, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/swapscan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func initApp() {
app.Commands = []*cli.Command{
scanEthCommand,
scanBtcCommand,
scanLtcCommand,
utils.LicenseCommand,
utils.VersionCommand,
}
Expand Down
349 changes: 349 additions & 0 deletions cmd/swapscan/scanltc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,349 @@
package main

import (
"fmt"
"sync"
"time"

"github.com/anyswap/CrossChain-Bridge/cmd/utils"
"github.com/anyswap/CrossChain-Bridge/log"
"github.com/anyswap/CrossChain-Bridge/mongodb"
"github.com/anyswap/CrossChain-Bridge/params"
"github.com/anyswap/CrossChain-Bridge/rpc/client"
"github.com/anyswap/CrossChain-Bridge/tokens"
"github.com/anyswap/CrossChain-Bridge/tokens/btc/electrs"
"github.com/anyswap/CrossChain-Bridge/tokens/eth"
"github.com/anyswap/CrossChain-Bridge/tokens/ltc"
"github.com/anyswap/CrossChain-Bridge/tokens/tools"
"github.com/urfave/cli/v2"
)

var (
scanLtcCommand = &cli.Command{
Action: scanLtc,
Name: "scanltc",
Usage: "scan swap on ltc",
ArgsUsage: " ",
Description: `
scan swap on ltc
`,
Flags: []cli.Flag{
testnetFlag,
mongoURLFlag,
dbNameFlag,
dbUserFlag,
dbPassFlag,
utils.GatewayFlag,
utils.SwapServerFlag,
utils.DepositAddressFlag,
utils.StartHeightFlag,
utils.EndHeightFlag,
utils.StableHeightFlag,
utils.JobsFlag,
},
}
)

type ltcSwapScanner struct {
useTestnet bool
gateway string
swapServer string
depositAddress string
startHeight uint64
endHeight uint64
stableHeight uint64
jobCount uint64

rpcInterval time.Duration
rpcRetryCount int

bridge *ltc.Bridge
}

func scanLtc(ctx *cli.Context) error {
utils.SetLogger(ctx)
scanner := &ltcSwapScanner{
rpcInterval: 1 * time.Second,
rpcRetryCount: 3,
}
scanner.useTestnet = ctx.Bool(testnetFlag.Name)
scanner.gateway = ctx.String(utils.GatewayFlag.Name)
scanner.swapServer = ctx.String(utils.SwapServerFlag.Name)
scanner.depositAddress = ctx.String(utils.DepositAddressFlag.Name)
scanner.startHeight = ctx.Uint64(utils.StartHeightFlag.Name)
scanner.endHeight = ctx.Uint64(utils.EndHeightFlag.Name)
scanner.stableHeight = ctx.Uint64(utils.StableHeightFlag.Name)
scanner.jobCount = ctx.Uint64(utils.JobsFlag.Name)

log.Info("get argument success",
"testnet", scanner.useTestnet,
"gateway", scanner.gateway,
"swapServer", scanner.swapServer,
"depositAddress", scanner.depositAddress,
"start", scanner.startHeight,
"end", scanner.endHeight,
"stable", scanner.stableHeight,
"jobs", scanner.jobCount,
)

scanner.initMongodb(ctx)
scanner.initBridge()
scanner.verifyOptions()
scanner.run()
return nil
}

func (scanner *ltcSwapScanner) verifyOptions() {
if !scanner.bridge.IsValidAddress(scanner.depositAddress) {
log.Fatalf("invalid deposit address '%v'", scanner.depositAddress)
}
if scanner.gateway == "" {
log.Fatal("must specify gateway address")
}
if scanner.swapServer == "" {
log.Fatal("must specify swap server address")
}

oracle := params.OracleConfig{
ServerAPIAddress: scanner.swapServer,
}
err := oracle.CheckConfig()
if err != nil {
log.Fatalf("check swap server failed. %v", err)
}

start := scanner.startHeight
end := scanner.endHeight
jobs := scanner.jobCount
if end != 0 && start >= end {
log.Fatalf("wrong scan range [%v, %v)", start, end)
}
if jobs == 0 {
log.Fatal("zero jobs specified")
}
}

func (scanner *ltcSwapScanner) initMongodb(ctx *cli.Context) {
dbURL := ctx.String(mongoURLFlag.Name)
dbName := ctx.String(dbNameFlag.Name)
userName := ctx.String(dbUserFlag.Name)
passwd := ctx.String(dbPassFlag.Name)
if dbName != "" {
mongodb.MongoServerInit([]string{dbURL}, dbName, userName, passwd)
}
}

func (scanner *ltcSwapScanner) initBridge() {
scanner.bridge = ltc.NewCrossChainBridge(true)
scanner.bridge.GatewayConfig = &tokens.GatewayConfig{
APIAddress: []string{scanner.gateway},
}
ltcDecimals := uint8(8)
netID := "Mainnet"
if scanner.useTestnet {
netID = "TestNet3"
}
scanner.bridge.ChainConfig = &tokens.ChainConfig{
BlockChain: "Bitcoin",
NetID: netID,
Confirmations: &scanner.stableHeight,
}
pairConfig := &tokens.TokenPairConfig{
PairID: ltc.PairID,
SrcToken: &tokens.TokenConfig{
ID: "LTC",
Name: "LTC",
Symbol: "LTC",
Decimals: &ltcDecimals,
DepositAddress: scanner.depositAddress,
},
}
pairsConfig := make(map[string]*tokens.TokenPairConfig)
pairsConfig[ltc.PairID] = pairConfig
tokens.SetTokenPairsConfig(pairsConfig, false)
tokens.SrcBridge = scanner.bridge
tokens.DstBridge = eth.NewCrossChainBridge(false)
}

func (scanner *ltcSwapScanner) run() {
start := scanner.startHeight
wend := scanner.endHeight
if wend == 0 {
wend = tools.LoopGetLatestBlockNumber(scanner.bridge)
}
if start == 0 {
start = wend
}

scanner.doScanRangeJob(start, wend)

if scanner.endHeight == 0 {
go scanner.scanPool()
scanner.scanLoop(wend)
}
}

// nolint:dupl // in diff sub command
func (scanner *ltcSwapScanner) doScanRangeJob(start, end uint64) {
if start >= end {
return
}
jobs := scanner.jobCount
count := end - start
step := count / jobs
if step == 0 {
jobs = 1
step = count
}
wg := new(sync.WaitGroup)
for i := uint64(0); i < jobs; i++ {
from := start + i*step
to := start + (i+1)*step
if i+1 == jobs {
to = end
}
wg.Add(1)
go scanner.scanRange(i+1, from, to, wg)
}
if scanner.endHeight != 0 {
wg.Wait()
}
}

func (scanner *ltcSwapScanner) scanRange(job, from, to uint64, wg *sync.WaitGroup) {
defer wg.Done()
log.Info(fmt.Sprintf("[%v] start scan range", job), "from", from, "to", to)

for h := from; h < to; h++ {
scanner.scanBlock(job, h, false)
}

log.Info(fmt.Sprintf("[%v] scan range finish", job), "from", from, "to", to)
}

func (scanner *ltcSwapScanner) scanPool() {
scanner.bridge.StartPoolTransactionScanJob()
}

func (scanner *ltcSwapScanner) scanLoop(from uint64) {
stable := scanner.stableHeight
log.Info("start scan loop", "from", from, "stable", stable)
for {
latest := tools.LoopGetLatestBlockNumber(scanner.bridge)
for h := latest; h > from; h-- {
scanner.scanBlock(0, h, true)
}
if from+stable < latest {
from = latest - stable
}
time.Sleep(5 * time.Second)
}
}

func (scanner *ltcSwapScanner) loopGetBlockHash(height uint64) string {
for {
blockHash, err := scanner.bridge.GetBlockHash(height)
if err == nil {
return blockHash
}
log.Warn("get block hash failed", "height", height, "err", err)
time.Sleep(scanner.rpcInterval)
}
}

func (scanner *ltcSwapScanner) scanBlock(job, height uint64, cache bool) {
blockHash := scanner.loopGetBlockHash(height)
if cache && ltcCachedBlocks.isScanned(blockHash) {
return
}
block, err := scanner.bridge.GetBlock(blockHash)
if err != nil {
log.Warn("get block failed", "height", height, "hash", blockHash, "err", err)
return
}
txCount := *block.TxCount
log.Info(fmt.Sprintf("[%v] scan block %v start", job, height), "hash", blockHash, "txs", txCount)

startIndex := uint32(0)
for startIndex < txCount {
var txs []*electrs.ElectTx
for i := 0; i < scanner.rpcRetryCount; i++ {
txs, err = scanner.bridge.GetBlockTransactions(blockHash, startIndex)
if err == nil {
break
}
log.Warn("get block txs failed", "height", height, "startIndex", startIndex, "err", err)
time.Sleep(scanner.rpcInterval)
}
for i, tx := range txs {
log.Trace(fmt.Sprintf("[%v] scan block %v process tx", job, height), "txid", *tx.Txid, "index", startIndex+uint32(i))
scanner.processTx(tx)
}
log.Trace(fmt.Sprintf("[%v] scan block %v process txs", job, height), "startIndex", startIndex, "total", txCount)
startIndex += 25 // 25 is elctrs API defined
}

if cache {
ltcCachedBlocks.addBlock(blockHash)
}
log.Info(fmt.Sprintf("[%v] scan block %v finish", job, height))
}

func (scanner *ltcSwapScanner) processTx(tx *electrs.ElectTx) {
txid := *tx.Txid
p2shBindAddrs, err := scanner.bridge.CheckSwapinTxType(tx)
if err != nil {
return
}
if len(p2shBindAddrs) > 0 {
for _, p2shBindAddr := range p2shBindAddrs {
log.Info("post p2sh swapin register", "txid", txid, "bind", p2shBindAddr)
args := map[string]interface{}{
"txid": txid,
"bind": p2shBindAddr,
}
var result interface{}
for i := 0; i < scanner.rpcRetryCount; i++ {
err = client.RPCPost(&result, scanner.swapServer, "swap.P2shSwapin", args)
if tokens.ShouldRegisterSwapForError(err) {
break
}
if tools.IsSwapAlreadyExistRegisterError(err) {
break
}
log.Warn("post p2sh swapin register failed", "txid", txid, "bind", p2shBindAddr, "err", err)
}
}
} else {
value, memoScript, rightReceiver := scanner.bridge.GetReceivedValue(tx.Vout, scanner.depositAddress, "p2pkh")
if !rightReceiver || value == 0 {
return
}
bindAddress, bindOk := ltc.GetBindAddressFromMemoScipt(memoScript)
if !bindOk {
return
}
log.Info("post swapin register", "txid", txid, "pairid", ltc.PairID, "bind", bindAddress)
args := map[string]interface{}{
"txid": txid,
"pairid": ltc.PairID,
}
var result interface{}
for i := 0; i < scanner.rpcRetryCount; i++ {
err = client.RPCPost(&result, scanner.swapServer, "swap.Swapin", args)
if tokens.ShouldRegisterSwapForError(err) {
break
}
if tools.IsSwapAlreadyExistRegisterError(err) {
break
}
log.Warn("post swapin register failed", "txid", txid, "bind", bindAddress, "err", err)
}
}
}

var ltcCachedBlocks = &cachedSacnnedBlocks{
capacity: 100,
nextIndex: 0,
hashes: make([]string, 100),
}
Loading