Skip to content

Commit

Permalink
Merge pull request #99 from 0xcregis/release-0.5.3
Browse files Browse the repository at this point in the history
feat: Release 0.5.3
  • Loading branch information
sunjiangjun authored Feb 1, 2024
2 parents a145644 + bb1c2bd commit f90204d
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 12 deletions.
16 changes: 16 additions & 0 deletions blockchain/service/polygon/polygon_pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func (e *PolygonPos) UnSubscribe(chainCode int64, subId string) (string, error)
}

func (e *PolygonPos) GetBlockReceiptByBlockNumber(chainCode int64, number string) (string, error) {
start := time.Now()
defer func() {
e.log.Printf("GetBlockReceiptByBlockNumber,Duration=%v", time.Since(start))
}()
query := `{
"id": 1,
"jsonrpc": "2.0",
Expand All @@ -218,6 +222,10 @@ func (e *PolygonPos) GetBlockReceiptByBlockNumber(chainCode int64, number string
}

func (e *PolygonPos) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, error) {
start := time.Now()
defer func() {
e.log.Printf("GetBlockReceiptByBlockHash,Duration=%v", time.Since(start))
}()
query := `{
"id": 1,
"jsonrpc": "2.0",
Expand Down Expand Up @@ -249,6 +257,10 @@ func (e *PolygonPos) GetTransactionReceiptByHash(chainCode int64, hash string) (
}

func (e *PolygonPos) GetBlockByHash(chainCode int64, hash string, flag bool) (string, error) {
start := time.Now()
defer func() {
e.log.Printf("GetBlockByHash,Duration=%v", time.Since(start))
}()
req := `
{
"id": 1,
Expand All @@ -265,6 +277,10 @@ func (e *PolygonPos) GetBlockByHash(chainCode int64, hash string, flag bool) (st
}

func (e *PolygonPos) GetBlockByNumber(chainCode int64, number string, flag bool) (string, error) {
start := time.Now()
defer func() {
e.log.Printf("GetBlockByNumber,Duration=%v", time.Since(start))
}()
req := `
{
"id": 1,
Expand Down
23 changes: 21 additions & 2 deletions blockchain/service/polygon/polygon_pos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import (

"github.com/0xcregis/easynode/blockchain"
"github.com/0xcregis/easynode/blockchain/config"
"github.com/0xcregis/easynode/common/util"
"github.com/sunjiangjun/xlog"
"github.com/tidwall/gjson"
)

func Init() blockchain.API {
cfg := config.LoadConfig("./../../../cmd/blockchain/config_polygon.json")
return NewPolygonPos(cfg.Cluster[201], 201, xlog.NewXLogger())
return NewPolygonPos(cfg.Cluster[66], 66, xlog.NewXLogger())
}

func Init2() blockchain.ExApi {
cfg := config.LoadConfig("./../../../cmd/blockchain/config_polygon.json")
return NewPolygonPos2(cfg.Cluster[64], 64, xlog.NewXLogger())
}

func TestPolygonPos_GetLatestBlock(t *testing.T) {
Expand Down Expand Up @@ -76,10 +83,22 @@ func TestPolygonPos_GetBlockReceiptByBlockNumber(t *testing.T) {

func TestPolygonPos_GetTransactionReceiptByHash(t *testing.T) {
s := Init()
resp, err := s.GetTransactionReceiptByHash(201, "0x9f656ad21cad7853f58aa05191ec4c11bd0459f40bec1a259f089fce4c80232f")
resp, err := s.GetTransactionReceiptByHash(66, "0x9a022bff505dec115478d2f368092918c4bbbf82c63f541e576ab0407485885a")
if err != nil {
t.Error(err)
} else {
t.Log(resp)
}
}

func TestPolygonPos_GasPrice(t *testing.T) {
s := Init2()
resp, err := s.GasPrice(64)
if err != nil {
t.Error(err)
} else {
gas := gjson.Parse(resp).Get("result").String()
gas, _ = util.HexToInt(gas)
t.Log(gas)
}
}
19 changes: 17 additions & 2 deletions cmd/blockchain/config_polygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@
},
"Nodes": [
{
"BlockChain": 201,
"NodeUrl": "https://polygon-bor.publicnode.com",
"BlockChain": 66,
"NodeUrl": "https://rpc.ankr.com/base",
"NodeToken": "053a22d65617d79be4ba5f1a7a181a24b3ab258a3c506bd6f02a338713e03457"
},
{
"BlockChain": 63,
"NodeUrl": "https://arb1.arbitrum.io/rpc",
"NodeToken": ""
},
{
"BlockChain": 65,
"NodeUrl": "https://rpc.ankr.com/avalanche",
"NodeToken": "1f1f091ef4d9eefb93f6c0f77821cf6ab377d3d2d334d9151303e28c1ddae902"
},
{
"BlockChain": 64,
"NodeUrl": "https://rpc.ankr.com/optimism",
"NodeToken": "1f1f091ef4d9eefb93f6c0f77821cf6ab377d3d2d334d9151303e28c1ddae902"
}
]
}
5 changes: 4 additions & 1 deletion cmd/collect/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Chains": [
{
"BlockChainName": "eth",
"BlockChainCode": 200,
"BlockChainCode": 60,
"PullReceipt": 0,
"PullTx": 1,
"TaskKafka": {
Expand All @@ -35,6 +35,7 @@
"DB": 0
},
"BlockTask": {
"WorkerCount": 10,
"FromCluster": [
{
"Host": "https://ethereum.publicnode.com",
Expand All @@ -47,6 +48,7 @@
}
},
"TxTask": {
"WorkerCount": 20,
"FromCluster": [
{
"Host": "https://ethereum.publicnode.com",
Expand All @@ -59,6 +61,7 @@
}
},
"ReceiptTask": {
"WorkerCount": 10,
"FromCluster": [
{
"Host": "https://ethereum.publicnode.com",
Expand Down
3 changes: 3 additions & 0 deletions collect/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ type Kafka struct {
type BlockTask struct {
FromCluster []*FromCluster `json:"FromCluster"`
Kafka *Kafka `json:"Kafka"`
WorkerCount int64 `json:"WorkerCount"`
}

type TxTask struct {
FromCluster []*FromCluster `json:"FromCluster"`
Kafka *Kafka `json:"Kafka"`
WorkerCount int64 `json:"WorkerCount"`
}

type ReceiptTask struct {
FromCluster []*FromCluster `json:"FromCluster"`
Kafka *Kafka `json:"Kafka"`
WorkerCount int64 `json:"WorkerCount"`
}

type Chain struct {
Expand Down
8 changes: 8 additions & 0 deletions collect/service/cmd/chain/polygonpos/block_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func GetTxFromJson(json string) *collect.Tx {
"cumulativeGasUsed": "0xc2ec5d",
"from": "0xf4e07370db628044ee8556d1dedb0417bd518970",
"gasUsed": "0xb5d7",
"l1Fee":"0x2b7056aa4da2",
"l1FeeScalar":"0.684",
"l1GasPrice":"0x8855737e0",
"l1GasUsed":"0x774",
"logsBloom": "0x00000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000002000000000100000400000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000002090000000000000200000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce",
Expand Down Expand Up @@ -249,6 +253,10 @@ func GetReceiptFromJson(js string) *collect.Receipt {
receipt.CumulativeGasUsed = r.Get("cumulativeGasUsed").String()
receipt.From = r.Get("from").String()
receipt.GasUsed = r.Get("gasUsed").String()
receipt.L1Fee = r.Get("l1Fee").String()
receipt.L1GasPrice = r.Get("l1GasPrice").String()
receipt.L1FeeScalar = r.Get("l1FeeScalar").String()
receipt.L1GasUsed = r.Get("l1GasUsed").String()
receipt.LogsBloom = r.Get("logsBloom").String()
receipt.Status = r.Get("status").String()
receipt.To = r.Get("to").String()
Expand Down
21 changes: 18 additions & 3 deletions collect/service/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func NewService(cfg *config.Chain, logConfig *config.LogConfig, nodeId string) *

func getBlockChainApi(c *config.Chain, logConfig *config.LogConfig, store collect.StoreTaskInterface, nodeId string) collect.BlockChainInterface {
srv := chain.GetBlockchain(c.BlockChainCode, c, store, logConfig, nodeId)
if srv == nil {
panic("init blockchain srv is error")
}
//第三方节点监控
srv.Monitor()
return srv
Expand Down Expand Up @@ -272,7 +275,11 @@ func (c *Cmd) HandlerKafkaRespMessage(msList []*kafka.Message) {
}

func (c *Cmd) ExecReceiptTask(receiptChan chan *collect.NodeTask, kf chan []*kafka.Message) {
buffCh := make(chan struct{}, 10)
var WorkerCount int64 = 10
if c.chain.BlockTask.WorkerCount > 1 {
WorkerCount = c.chain.BlockTask.WorkerCount
}
buffCh := make(chan struct{}, WorkerCount)
for {
//控制协程数量
buffCh <- struct{}{}
Expand Down Expand Up @@ -382,7 +389,11 @@ func (c *Cmd) execSingleReceipt(taskReceipt *collect.NodeTask, log *logrus.Entry
}

func (c *Cmd) ExecTxTask(txCh chan *collect.NodeTask, kf chan []*kafka.Message) {
buffCh := make(chan struct{}, 20)
var WorkerCount int64 = 20
if c.chain.BlockTask.WorkerCount > 1 {
WorkerCount = c.chain.BlockTask.WorkerCount
}
buffCh := make(chan struct{}, WorkerCount)
for {
//控制协程数量
buffCh <- struct{}{}
Expand Down Expand Up @@ -568,7 +579,11 @@ func (c *Cmd) execSingleTx(taskTx *collect.NodeTask, log *logrus.Entry) []*kafka
}

func (c *Cmd) ExecBlockTask(blockCh chan *collect.NodeTask, kf chan []*kafka.Message) {
buffCh := make(chan struct{}, 10)
var WorkerCount int64 = 10
if c.chain.BlockTask.WorkerCount > 1 {
WorkerCount = c.chain.BlockTask.WorkerCount
}
buffCh := make(chan struct{}, WorkerCount)
for {
//控制协程数量
buffCh <- struct{}{}
Expand Down
8 changes: 8 additions & 0 deletions collect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type TxInterface struct {
"cumulativeGasUsed": "0xc2ec5d",
"from": "0xf4e07370db628044ee8556d1dedb0417bd518970",
"gasUsed": "0xb5d7",
"l1Fee":"0x2b7056aa4da2",
"l1FeeScalar":"0.684",
"l1GasPrice":"0x8855737e0",
"l1GasUsed":"0x774",
"logsBloom": "0x00000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000002000000000100000400000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000002090000000000000200000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce",
Expand All @@ -158,6 +162,10 @@ type Receipt struct {
Type string `json:"type" gorm:"column:tx_type"`
TransactionHash string `json:"transactionHash" gorm:"column:transaction_hash"`
GasUsed string `json:"gasUsed" gorm:"column:gas_used"`
L1Fee string `json:"l1Fee" gorm:"-"`
L1FeeScalar string `json:"l1FeeScalar" gorm:"-"`
L1GasPrice string `json:"l1GasPrice" gorm:"-"`
L1GasUsed string `json:"l1GasUsed" gorm:"-"`
BlockNumber string `json:"blockNumber" gorm:"column:block_number"`
CumulativeGasUsed string `json:"cumulativeGasUsed" gorm:"column:cumulative_gas_used"`
From string `json:"from" gorm:"column:from_addr"`
Expand Down
25 changes: 21 additions & 4 deletions store/chain/gw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,31 @@ func TestGetCoreAddress(t *testing.T) {
func TestParseTx(t *testing.T) {
req := []byte(`
{
"blockId": "00000000025fc02f22f068a6e04913196756836fb46c73bd80b9340a7ca37ced",
"receipt": "{\"id\":\"e7757a0b0d2df629bf363f39062934c977b3a204f82f8a882f019cadeddf6298\",\"fee\":267000,\"blockNumber\":39829551,\"blockTimeStamp\":1702881432000,\"from\":\"\",\"to\":\"\",\"contractResult\":[\"\"],\"contract_address\":\"\",\"receipt\":{\"energy_fee\":0,\"energy_usage_total\":0,\"net_usage\":0,\"result\":\"\",\"energy_penalty_total\":0},\"log\":null,\"internal_transactions\":null}",
"tx": "{\"ret\":[{\"contractRet\":\"SUCCESS\"}],\"signature\":[\"65c15050fb80bde6e911e9be9322a99cf5d3fd65a6eb6e7fa56b2139df670bd17d2b8f5bb9f082b6873606e9181e9daaeb82d0fc605268dfeee3c435dc6ab69e1c\"],\"txID\":\"e7757a0b0d2df629bf363f39062934c977b3a204f82f8a882f019cadeddf6298\",\"raw_data\":{\"contract\":[{\"parameter\":{\"value\":{\"amount\":1000000,\"owner_address\":\"418e5ad0c377005da36e592424e84ea0fa317321c2\",\"to_address\":\"412f83df6e9705f82e001e32e7d3f45551fda36d03\"},\"type_url\":\"type.googleapis.com/protocol.TransferContract\"},\"type\":\"TransferContract\"}],\"ref_block_bytes\":\"c02d\",\"ref_block_hash\":\"e732b171fc746141\",\"expiration\":1702881486000,\"timestamp\":1702881426000},\"raw_data_hex\":\"0a02c02d2208e732b171fc74614140b0c995ddc7315a67080112630a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412320a15418e5ad0c377005da36e592424e84ea0fa317321c21215412f83df6e9705f82e001e32e7d3f45551fda36d0318c0843d70d0f491ddc731\"}"
"id": 1706598246238798305,
"hash": "0xabcca0ed01b966327f2f1f239f8a3201b5e81bf1523083b6872c12137b0b900a",
"txTime": "1706598233",
"txStatus": "",
"blockNumber": "115499728",
"from": "0x545f731e3ce6ab51c7a30ca08bf0f1a953e30826",
"to": "0x65cb3ff66cdd12ade34cab66a95108a3af034543",
"value": "0x38d7ea4c68000",
"fee": "",
"gasPrice": "0x61cde8a",
"maxFeePerGas": "",
"gas": "0x5208",
"gasUsed": "",
"baseFeePerGas": "0x5f872b1",
"maxPriorityFeePerGas": "",
"input": "0x",
"blockHash": "0xc7467877690f58fd7403eee2912c4d44324fffb183b245afffae7d16f2eb839a",
"transactionIndex": "0x3",
"type": "0x0",
"receipt": "{\"id\":1706598246425046657,\"blockHash\":\"0xc7467877690f58fd7403eee2912c4d44324fffb183b245afffae7d16f2eb839a\",\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"contractAddress\":\"\",\"transactionIndex\":\"0x3\",\"type\":\"0x0\",\"transactionHash\":\"0xabcca0ed01b966327f2f1f239f8a3201b5e81bf1523083b6872c12137b0b900a\",\"gasUsed\":\"0x5208\",\"l1Fee\":\"0x2b7056aa4da2\",\"blockNumber\":\"115499728\",\"cumulativeGasUsed\":\"0x37a16\",\"from\":\"0x545f731e3ce6ab51c7a30ca08bf0f1a953e30826\",\"to\":\"0x65cb3ff66cdd12ade34cab66a95108a3af034543\",\"effectiveGasPrice\":\"0x61cde8a\",\"logs\":[],\"createTime\":\"2024-01-30\",\"status\":\"0x1\"}"
}
`)
msg := &kafka.Message{}
msg.Value = req
sub, err := ParseTx(198, msg)
sub, err := ParseTx(64, msg)
if err != nil {
t.Error(err)
} else {
Expand Down
10 changes: 10 additions & 0 deletions store/chain/polygonpos/kafka_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,21 @@ func ParseTx(body []byte, transferTopic, nftTransferSingleTopic string, blocchai
gasUsed := receiptRoot.Get("gasUsed").String()
//gas, _ := util.HexToInt2(gasUsed)

l1Fee := receiptRoot.Get("l1Fee").String()
bigL1Fee, _ := new(big.Int).SetString(l1Fee, 0)
if bigL1Fee != nil {
r.L1Fee = bigL1Fee.String()
}

bigPrice, b := new(big.Int).SetString(gasPrice, 0)
bigGas, b2 := new(big.Int).SetString(gasUsed, 0)

if b && b2 {
fee := bigPrice.Mul(bigPrice, bigGas)
r.L2Fee = fee.String()
if bigL1Fee != nil {
fee = fee.Add(fee, bigL1Fee)
}
r.Fee = util.Div(fmt.Sprintf("%v", fee), 18)
r.FeeDetail = map[string]string{"gasPrice": fmt.Sprintf("%v", bigPrice.String()), "gasUsed": fmt.Sprintf("%v", bigGas.String())}
} else {
Expand Down
6 changes: 6 additions & 0 deletions store/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ type Receipt struct {
Type string `json:"type" gorm:"column:tx_type"`
TransactionHash string `json:"transactionHash" gorm:"column:transaction_hash"`
GasUsed string `json:"gasUsed" gorm:"column:gas_used"`
L1Fee string `json:"l1Fee" gorm:"-"`
L1FeeScalar string `json:"l1FeeScalar" gorm:"-"`
L1GasPrice string `json:"l1GasPrice" gorm:"-"`
L1GasUsed string `json:"l1GasUsed" gorm:"-"`
BlockNumber string `json:"blockNumber" gorm:"column:block_number"`
CumulativeGasUsed string `json:"cumulativeGasUsed" gorm:"column:cumulative_gas_used"`
From string `json:"from" gorm:"column:from_addr"`
Expand Down Expand Up @@ -273,6 +277,8 @@ type SubTx struct {
ContractTx []*ContractTx `json:"txs" gorm:"-"`
ContractTxs string `json:"-" gorm:"column:contract_tx"`
Fee string `json:"fee" gorm:"column:fee"`
L1Fee string `json:"l1fee" gorm:"-"`
L2Fee string `json:"l2fee" gorm:"-"`
FeeDetail interface{} `json:"-" gorm:"-"`
FeeDetails string `json:"-" gorm:"column:fee_detail"`
From string `json:"from" gorm:"column:from_addr"`
Expand Down

0 comments on commit f90204d

Please sign in to comment.