Skip to content

Commit

Permalink
feat(store): add btc
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhongtao committed Sep 25, 2023
1 parent 7b74b48 commit 718dd44
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 293 deletions.
2 changes: 1 addition & 1 deletion cmd/collect/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func main() {
var configPath string
flag.StringVar(&configPath, "collect", "./cmd/collect/config.json", "The system file of config")
flag.StringVar(&configPath, "collect", "./cmd/collect/config_btc.json", "The system file of config")
flag.Parse()
if len(configPath) < 1 {
panic("can not find config file")
Expand Down
4 changes: 2 additions & 2 deletions cmd/collect/config_btc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"TaskKafka": {
"Host": "192.168.2.9",
"Port": 9092,
"Group": "21",
"StartOffset": -1,
"Group": "1",
"StartOffset":0,
"Partition": 0,
"WritePartitions": [
0
Expand Down
2 changes: 1 addition & 1 deletion cmd/store/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func main() {
var configPath string
flag.StringVar(&configPath, "store", "./cmd/store/config.json", "The system file of config")
flag.StringVar(&configPath, "store", "./cmd/store/config_btc.json", "The system file of config")
flag.Parse()
if len(configPath) < 1 {
panic("can not find config file")
Expand Down
24 changes: 12 additions & 12 deletions cmd/store/config_btc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"Chains": [
{
"BlockChain": 200,
"BlockChain": 300,
"BlockStore": true,
"TxStore": false,
"ReceiptStore": false,
Expand All @@ -31,31 +31,31 @@
"SubTx": {
"Host": "192.168.2.9",
"Port": 9092,
"Topic": "ether_sub_tx",
"Group": "101",
"StartOffset": -1,
"Topic": "btc_sub_tx",
"Group": "1",
"StartOffset": 0,
"Partition": 0
},
"Tx": {
"Host": "192.168.2.9",
"Port": 9092,
"Topic": "ether_tx",
"Group": "120",
"StartOffset": -1,
"Topic": "btc_tx",
"Group": "1",
"StartOffset": 0,
"Partition": 0
},
"Block": {
"Host": "192.168.2.9",
"Port": 9092,
"Topic": "ether_block",
"Group": "110",
"StartOffset": -1,
"Topic": "btc_block",
"Group": "1",
"StartOffset": 0,
"Partition": 0
},
"Receipt": {
"Host": "192.168.2.9",
"Port": 9092,
"Topic": "ether_receipt",
"Topic": "btc_receipt",
"Group": "130",
"StartOffset": -1,
"Partition": 0
Expand All @@ -66,7 +66,7 @@
"Port": 9000,
"User": "test",
"Password": "test",
"DbName": "ether2",
"DbName": "btc",
"TxTable": "tx",
"BlockTable": "block",
"ReceiptTable": "receipt",
Expand Down
2 changes: 1 addition & 1 deletion cmd/task/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func main() {
var configPath string
flag.StringVar(&configPath, "task", "./cmd/task/config_filecoin.json", "The system file of config")
flag.StringVar(&configPath, "task", "./cmd/task/config_btc.json", "The system file of config")
flag.Parse()
if len(configPath) < 1 {
panic("can not find config file")
Expand Down
22 changes: 5 additions & 17 deletions cmd/task/config_btc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,13 @@
{
"Cluster": [
{
"NodeHost": "https://eth-mainnet.g.alchemy.com/v2",
"NodeKey": "RzxBjjh_c4y0LVHZ7GNm8zoXEZR3HYop"
"NodeHost": "https://bitcoin-mainnet-archive.allthatnode.com",
"NodeKey": ""
}
],
"BlockChainName": "eth",
"BlockChainCode": 200,
"BlockMin": 17325771,
"BlockMax": 0
},
{
"Cluster": [
{
"NodeHost": "https://api.trongrid.io",
"NodeKey": "244f918d-56b5-4a16-9665-9637598b1223"
}
],
"BlockChainName": "tron",
"BlockChainCode": 205,
"BlockMin": 51979625,
"BlockChainName": "btc",
"BlockChainCode": 300,
"BlockMin": 809246,
"BlockMax": 0
}
]
Expand Down
52 changes: 7 additions & 45 deletions collect/service/cmd/chain/btc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package btc
import (
"encoding/json"
"strings"
"sync"
"time"

"github.com/0xcregis/easynode/blockchain"
Expand Down Expand Up @@ -85,13 +84,9 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool
return r, nil
}

//fix prevout
txList = s.getTxsOut(txList, eLog)

addressList, err := s.store.GetMonitorAddress(int64(s.chain.BlockChainCode))
if err != nil {
eLog.Errorf("GetBlockByHash|BlockChainName=%v,err=%v,blockHash=%v", s.chain.BlockChainName, err, blockHash)
return nil, nil
}
addressMp := rebuildAddress(addressList)
txs := make([]*collect.TxInterface, 0, len(txList))
Expand Down Expand Up @@ -142,13 +137,9 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag
return r, nil
}

//fix prevout
txList = s.getTxsOut(txList, eLog)

addressList, err := s.store.GetMonitorAddress(int64(s.chain.BlockChainCode))
if err != nil {
eLog.Errorf("GetBlockByNumber|BlockChainName=%v,err=%v,blockNumber=%v", s.chain.BlockChainName, err, blockNumber)
return nil, nil
eLog.Warnf("GetBlockByNumber|BlockChainName=%v,err=%v,blockNumber=%v", s.chain.BlockChainName, err, blockNumber)
}

addressMp := rebuildAddress(addressList)
Expand Down Expand Up @@ -195,16 +186,15 @@ func (s *Service) GetTx(txHash string, eLog *logrus.Entry) *collect.TxInterface
if len(tx.BlockNumber) < 1 {
resp, err = s.txChainClient.GetBlockByHash(int64(s.chain.BlockChainCode), tx.BlockHash, false)
if err != nil {
eLog.Errorf("GetTx|BlockChainName=%v,err=%v,txHash=%v", s.chain.BlockChainName, resp, txHash)
return nil
eLog.Warnf("GetTx|BlockChainName=%v,err=%v,txHash=%v", s.chain.BlockChainName, err, txHash)
} else {
tx.BlockNumber = gjson.Parse(resp).Get("result.height").String()
}
tx.BlockNumber = gjson.Parse(resp).Get("result.height").String()
}

addressList, err := s.store.GetMonitorAddress(int64(s.chain.BlockChainCode))
if err != nil {
eLog.Errorf("GetTx|BlockChainName=%v,err=%v,txHash=%v", s.chain.BlockChainName, err, txHash)
return nil
}

addressMp := rebuildAddress(addressList)
Expand All @@ -228,34 +218,6 @@ func (s *Service) GetReceipt(txHash string, eLog *logrus.Entry) (*collect.Receip
return nil, nil
}

func (s *Service) getTxsOut(list []*collect.Tx, eLog *logrus.Entry) []*collect.Tx {
wg := sync.WaitGroup{}
wg.Add(len(list))
for _, v := range list {
go func(v *collect.Tx) {
defer wg.Done()

resp, err := s.txChainClient.GetTxByHash(int64(s.chain.BlockChainCode), v.TxHash)
//resp, err := ether.Eth_GetTransactionByHash(cluster.Host, cluster.Key, txHash, s.log)
if err != nil {
eLog.Errorf("getTxsOut|BlockChainName=%v,err=%v,txHash=%v", s.chain.BlockChainName, err.Error(), v.TxHash)
return
}

//处理数据
if resp == "" {
eLog.Errorf("getTxsOut|BlockChainName=%v,err=%v,txHash=%v", s.chain.BlockChainName, "tx is empty", v.TxHash)
return
}
from := gjson.Parse(resp).Get("result.vin").String()
v.FromAddr = from
}(v)
}

wg.Wait()
return list
}

func getCoreAddr(addr string) string {
addr = strings.ToLower(addr)
if strings.HasPrefix(addr, "0x") {
Expand All @@ -275,9 +237,9 @@ func rebuildAddress(addrList []string) map[string]int64 {

func (s *Service) CheckAddress(tx []byte, addrList map[string]int64) bool {

//if len(addrList) < 1 || len(tx) < 1 {
// return false
//}
if len(addrList) < 1 || len(tx) < 1 {
return false
}

txAddressList := make(map[string]int64, 10)
root := gjson.ParseBytes(tx)
Expand Down
3 changes: 2 additions & 1 deletion collect/service/cmd/chain/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/0xcregis/easynode/collect"
"github.com/0xcregis/easynode/collect/config"
"github.com/0xcregis/easynode/collect/service/cmd/chain/btc"
"github.com/0xcregis/easynode/collect/service/cmd/chain/ether"
"github.com/0xcregis/easynode/collect/service/cmd/chain/filecoin"
"github.com/0xcregis/easynode/collect/service/cmd/chain/polygonpos"
Expand All @@ -29,7 +30,7 @@ func GetBlockchain(blockchain int, c *config.Chain, store collect.StoreTaskInter
} else if blockchain == 310 {
srv = xrp.NewService(c, x, store, nodeId, "")
} else if blockchain == 300 {

srv = btc.NewService(c, x, store, nodeId)
}

return srv
Expand Down
Loading

0 comments on commit 718dd44

Please sign in to comment.