Skip to content

Commit

Permalink
Merge pull request #8 from everFinance/feature/subscibe-blocks
Browse files Browse the repository at this point in the history
Feature/subscibe blocks
  • Loading branch information
zyjblockchain authored Aug 3, 2022
2 parents 7288748 + f35710e commit e53e5cd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ and all transactions are returned in on-chain order.

### New
```go
syncer := New(startHeight, filterParams, arNode, conNum, stableBlockDistance)
syncer := New(startHeight, filterParams, arNode, conNum, stableBlockDistance, subscribeType)
```
`startHeight` block height at the start of the sync
`filterParams` filter tx
`arNode` arweave node url
`conNum` runtime concurrency number, default is 10
`stableBlockDistance` stable block height distance, default is 15
`subscribeType` subscribe tx or block, or both.

### Run
```go
Expand All @@ -39,12 +40,15 @@ for {
// process sTx
...
}
case sBlock := <-s.SubscribeBlockCh():
// process sBlock
...
}
```


### Example
1. Get all transactions example: [./example/all-tx-syncer.go](https://github.com/everFinance/ar-syncer/blob/v1.0.0/example/all-tx-syncer.go)
1. Get all transactions and blocks example: [./example/all-tx-syncer.go](https://github.com/everFinance/ar-syncer/blob/v1.0.0/example/all-tx-syncer.go)
2. Get the transactions for the specified sender: [./example/from-tx-syncer.go](https://github.com/everFinance/ar-syncer/blob/v1.0.0/example/from-tx-syncer.go)
3. Get the transactions for the specified target: [./example/target-tx-syncer.go](https://github.com/everFinance/ar-syncer/blob/v1.0.0/example/target-tx-syncer.go)
4. Get all smart contract transactions: [./example/swc-syncer.go](https://github.com/everFinance/ar-syncer/blob/v1.0.0/example/swc-syncer.go)
Expand Down
2 changes: 1 addition & 1 deletion example/all-tx-syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
startHeight := int64(879220)
arNode := "https://arweave.net"
concurrencyNumber := 10 // runtime concurrency number, default 10
s := arsyncer.New(startHeight, nullFilterParams, arNode, concurrencyNumber, 15, true)
s := arsyncer.New(startHeight, nullFilterParams, arNode, concurrencyNumber, 15, "subscribe_block_and_tx")

// run
s.Run()
Expand Down
2 changes: 1 addition & 1 deletion example/from-tx-syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
startHeight := int64(804524)
arNode := "https://arweave.net"
concurrencyNumber := 100 // runtime concurrency number, default 10
s := arsyncer.New(startHeight, ownerFilterParams, arNode, concurrencyNumber, 15, false)
s := arsyncer.New(startHeight, ownerFilterParams, arNode, concurrencyNumber, 15, "subscribe_tx")

// run
s.Run()
Expand Down
2 changes: 1 addition & 1 deletion example/swc-syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
startHeight := int64(472810)
arNode := "https://arweave.net"
concurrencyNumber := 50 // runtime concurrency number, default 10
s := arsyncer.New(startHeight, swcFilterParams, arNode, concurrencyNumber, 15, false)
s := arsyncer.New(startHeight, swcFilterParams, arNode, concurrencyNumber, 15, "subscribe_tx")

// run
s.Run()
Expand Down
2 changes: 1 addition & 1 deletion example/target-tx-syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
startHeight := int64(811484)
arNode := "https://arweave.net"
concurrencyNumber := 100 // runtime concurrency number, default 10
s := arsyncer.New(startHeight, ownerFilterParams, arNode, concurrencyNumber, 15, false)
s := arsyncer.New(startHeight, ownerFilterParams, arNode, concurrencyNumber, 15, "subscribe_tx")

// run
s.Run()
Expand Down
19 changes: 14 additions & 5 deletions syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (

var log = NewLog("syncer")

const (
SubscribeTypeTx = "subscribe_tx"
SubscribeTypeBlock = "subscribe_block"
SubscribeTypeBlockAndTx = "subscribe_block_and_tx"
)

type Syncer struct {
curHeight int64
FilterParams
Expand All @@ -29,11 +35,11 @@ type Syncer struct {
scheduler *gocron.Scheduler
peers []string

subscribeBlock bool
subscribeType string
SubscribeBlockChan chan *types.Block
}

func New(startHeight int64, filterParams FilterParams, arNode string, conNum int, stableDistance int64, subscribeBlock bool) *Syncer {
func New(startHeight int64, filterParams FilterParams, arNode string, conNum int, stableDistance int64, subscribeType string) *Syncer {
if conNum <= 0 {
conNum = 10 // default concurrency of number is 10
}
Expand Down Expand Up @@ -68,7 +74,7 @@ func New(startHeight int64, filterParams FilterParams, arNode string, conNum int
blockIdxs: idxs,
scheduler: gocron.NewScheduler(time.UTC),
peers: peers,
subscribeBlock: subscribeBlock,
subscribeType: subscribeType,
}
}

Expand All @@ -83,6 +89,7 @@ func (s *Syncer) Close() (subscribeHeight int64) {
close(s.blockChan)
close(s.blockTxsChan)
close(s.SubscribeChan)
close(s.SubscribeBlockChan)
return s.nextSubscribeTxBlock - 1
}

Expand Down Expand Up @@ -127,8 +134,10 @@ func (s *Syncer) pollingBlock() {
s.curHeight = end + 1
// add chan
for _, b := range blocks {
s.blockChan <- b
if s.subscribeBlock {
if s.subscribeType == SubscribeTypeTx || s.subscribeType == SubscribeTypeBlockAndTx {
s.blockChan <- b
}
if s.subscribeType == SubscribeTypeBlock || s.subscribeType == SubscribeTypeBlockAndTx {
s.SubscribeBlockChan <- b
}
}
Expand Down

0 comments on commit e53e5cd

Please sign in to comment.