Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
goDemo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
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
duanjinfei
goDemo
Commits
42785f15
Commit
42785f15
authored
Sep 02, 2022
by
M an
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize scan block task
parent
ac99774f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
57 deletions
+69
-57
ScanBlock.go
ScanBlock.go
+69
-57
No files found.
ScanBlock.go
View file @
42785f15
package
main
package
main
import
(
import
(
"bytes"
"context"
"context"
"fmt"
"fmt"
"log"
"log"
"math/big"
"math/big"
"os"
"os/signal"
"syscall"
"time"
common
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
types
"github.com/ethereum/go-ethereum/core/types"
types
"github.com/ethereum/go-ethereum/core/types"
ethclient
"github.com/ethereum/go-ethereum/ethclient"
ethclient
"github.com/ethereum/go-ethereum/ethclient"
)
)
func
main
()
{
func
main
()
{
client
,
err
:=
ethclient
.
Dial
(
"https://galaxy.block.caduceus.foundation"
)
addressArr
:=
[]
string
{
"0x5b68ff12be7fd90284ebe2702050879d4d50061a"
}
start
(
"https://galaxy.block.caduceus.foundation"
,
addressArr
)
}
func
start
(
url
string
,
addArr
[]
string
)
{
// get operate signal
makeSignal
:=
make
(
chan
os
.
Signal
,
1
)
// monitor all signals
signal
.
Notify
(
makeSignal
,
syscall
.
SIGINT
)
//Block until a signal comes in
fmt
.
Println
(
"monitor start"
)
client
,
err
:=
ethclient
.
Dial
(
url
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
pendingVerifyAddress
:=
"0x61704EFB8b8120c03C210cAC5f5193BF8c80852a"
// Get the last block height
// Get the last block height
num
:=
getLastTimeBlockNumber
()
num
:=
getLastTimeBlockNumber
()
header
,
err
:=
client
.
HeaderByNumber
(
context
.
Background
(),
nil
)
var
currentScanedBlock
int64
=
num
if
err
!=
nil
{
// create ticker
log
.
Fatalf
(
err
.
Error
())
var
ticker
*
time
.
Ticker
=
time
.
NewTicker
(
1
*
time
.
Second
)
}
for
{
newBlockNum
:=
header
.
Number
.
Int64
()
select
{
for
i
:=
num
;
i
<
newBlockNum
;
i
++
{
case
<-
ticker
.
C
:
fmt
.
Println
(
"current scan block number:"
,
i
)
{
if
i
==
6917803
{
header
,
err
:=
client
.
HeaderByNumber
(
context
.
Background
(),
nil
)
break
if
err
!=
nil
{
}
log
.
Fatalf
(
err
.
Error
())
// Get tx with in a block
}
if
getBlockTxThroughAddress
(
client
,
big
.
NewInt
(
i
),
pendingVerifyAddress
)
{
newBlockNum
:=
header
.
Number
.
Int64
()
fmt
.
Println
(
"verify success:"
,
pendingVerifyAddress
)
fmt
.
Println
(
"newBlockNum:"
,
newBlockNum
)
break
if
currentScanedBlock
<
newBlockNum
{
fmt
.
Println
(
"current scan block number:"
,
currentScanedBlock
)
for
_
,
pendingVerifyAddress
:=
range
addArr
{
// Get tx with in a block
if
getBlockTxThroughAddress
(
client
,
big
.
NewInt
(
currentScanedBlock
),
pendingVerifyAddress
)
{
fmt
.
Println
(
"verify success:"
,
pendingVerifyAddress
)
}
else
{
fmt
.
Println
(
"verifing......"
,
pendingVerifyAddress
)
}
}
currentScanedBlock
++
}
}
case
processSignal
:=
<-
makeSignal
:
{
if
processSignal
!=
nil
{
stop
(
currentScanedBlock
,
*
ticker
)
fmt
.
Println
(
"process end:"
,
processSignal
)
os
.
Exit
(
3
)
}
}
}
}
}
}
// Get the tx in the block through the block hash
getTxThroughBlockTx
(
client
,
header
.
Hash
()
.
String
())
}
}
func
getBlockTxThroughAddress
(
client
*
ethclient
.
Client
,
blockNumber
*
big
.
Int
,
verifyAddress
string
)
bool
{
func
getBlockTxThroughAddress
(
client
*
ethclient
.
Client
,
blockNumber
*
big
.
Int
,
verifyAddress
string
)
(
verifyStatus
bool
)
{
// Specify the block number and get the transactions in the block
// Specify the block number and get the transactions in the block
block
,
err
:=
client
.
BlockByNumber
(
context
.
Background
(),
big
.
NewInt
(
blockNumber
.
Int64
()))
block
,
err
:=
client
.
BlockByNumber
(
context
.
Background
(),
big
.
NewInt
(
blockNumber
.
Int64
()))
if
err
!=
nil
{
if
err
!=
nil
{
return
false
return
false
}
}
// Get all tx in block
for
_
,
tx
:=
range
block
.
Transactions
()
{
for
_
,
tx
:=
range
block
.
Transactions
()
{
fmt
.
Println
(
"Hash:"
,
tx
.
Hash
()
.
Hex
())
fmt
.
Println
(
"Value:"
,
tx
.
Value
()
.
String
())
fmt
.
Println
(
"Gas:"
,
tx
.
Gas
())
fmt
.
Println
(
"GasPrice:"
,
tx
.
GasPrice
()
.
Uint64
())
fmt
.
Println
(
"Nonce:"
,
tx
.
Nonce
())
// fmt.Println("Data:", tx.Data())
chainID
,
err
:=
client
.
NetworkID
(
context
.
Background
())
chainID
,
err
:=
client
.
NetworkID
(
context
.
Background
())
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
...
@@ -61,45 +86,32 @@ func getBlockTxThroughAddress(client *ethclient.Client, blockNumber *big.Int, ve
...
@@ -61,45 +86,32 @@ func getBlockTxThroughAddress(client *ethclient.Client, blockNumber *big.Int, ve
if
err
==
nil
{
if
err
==
nil
{
fmt
.
Println
(
"From:"
,
msg
.
From
()
.
Hex
())
fmt
.
Println
(
"From:"
,
msg
.
From
()
.
Hex
())
}
}
toAddress
:=
tx
.
To
()
.
Hex
()
// Get receipt status
if
verifyAddress
==
toAddress
{
fmt
.
Println
(
"To:"
,
toAddress
)
return
true
}
fmt
.
Println
(
"NotTo:"
,
toAddress
)
receipt
,
err
:=
client
.
TransactionReceipt
(
context
.
Background
(),
tx
.
Hash
())
receipt
,
err
:=
client
.
TransactionReceipt
(
context
.
Background
(),
tx
.
Hash
())
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
fmt
.
Println
(
"receiptStatus:"
,
receipt
.
Status
)
fmt
.
Println
(
"receiptStatus:"
,
receipt
.
Status
)
// Get tx status
fmt
.
Println
(
"toAddress:"
,
tx
.
To
()
.
Hex
(),
"Value:"
,
"Hash:"
,
tx
.
Hash
()
.
Hex
(),
tx
.
Value
()
.
String
())
_
,
isPending
,
err
:=
client
.
TransactionByHash
(
context
.
Background
(),
tx
.
Hash
())
verifyAddressByte
:=
common
.
HexToAddress
(
verifyAddress
)
.
Bytes
()
if
err
!=
nil
{
compareRes
:=
bytes
.
Compare
(
tx
.
To
()
.
Bytes
(),
verifyAddressByte
)
log
.
Fatal
(
err
)
if
compareRes
==
0
&&
receipt
.
Status
==
1
{
// storage tx
storageTx
(
tx
.
Hash
()
.
Hex
(),
tx
.
Value
()
.
String
(),
msg
.
From
()
.
Hex
(),
tx
.
To
()
.
Hex
())
return
true
}
}
fmt
.
Println
(
"txStatus:"
,
isPending
)
// false
fmt
.
Println
(
"---------------------------"
)
}
}
return
false
return
false
}
}
func
getTxThroughBlockTx
(
client
*
ethclient
.
Client
,
tx
string
)
{
func
storageTx
(
tx
string
,
amount
string
,
from
string
,
to
string
)
{
// Specify the block Hash to get the transactions in the block
fmt
.
Println
(
"storage hash:"
,
tx
,
"amount:"
,
amount
,
"from:"
,
from
,
"to:"
,
to
)
blockHash
:=
common
.
HexToHash
(
tx
)
count
,
err
:=
client
.
TransactionCount
(
context
.
Background
(),
blockHash
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
for
idx
:=
uint
(
0
);
idx
<
count
;
idx
++
{
tx
,
err
:=
client
.
TransactionInBlock
(
context
.
Background
(),
blockHash
,
idx
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
fmt
.
Println
(
tx
.
Hash
()
.
Hex
())
}
}
}
func
getLastTimeBlockNumber
()
(
number
int64
)
{
func
getLastTimeBlockNumber
()
(
number
int64
)
{
number
=
6916803
number
=
7048864
return
return
}
}
func
stop
(
blockNum
int64
,
ticker
time
.
Ticker
)
{
ticker
.
Stop
()
// In Mysql storage end blockNum
fmt
.
Println
(
"Ticker stopped and storage blockNum:"
,
blockNum
)
}
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