-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
210 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/autoapev1/indexer/config" | ||
"github.com/autoapev1/indexer/eth" | ||
"github.com/autoapev1/indexer/types" | ||
) | ||
|
||
func main() { | ||
config.Parse("config.toml") | ||
// conf := config.Get().Storage.Postgres | ||
// conf.Name = "ETH" | ||
|
||
// db := storage.NewPostgresDB(conf).WithChainID(1) | ||
// err := db.Init() | ||
// if err != nil { | ||
// panic(err) | ||
// } | ||
|
||
chain := types.Chain{ | ||
Name: "Ethereum", | ||
Http: "http://localhost:7545", | ||
ShortName: "ETH", | ||
ChainID: 1, | ||
} | ||
|
||
eth := eth.NewNetwork(chain, config.Get()) | ||
|
||
if err := eth.Init(); err != nil { | ||
panic(err) | ||
} | ||
|
||
bts, err := eth.GetBlockTimestamps(0, 10) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, v := range bts { | ||
fmt.Printf("Block %d timestamp %d\n", v.Block, v.Timestamp) | ||
} | ||
|
||
// pairAddrs, err := db.GetUniqueAddressesFromPairs() | ||
// if err != nil { | ||
// panic(err) | ||
// } | ||
|
||
// tokenAddrs, err := db.GetUniqueAddressesFromTokens() | ||
// if err != nil { | ||
// panic(err) | ||
// } | ||
|
||
// uniqueAddrs := make(map[string]struct{}) | ||
// for _, v := range pairAddrs { | ||
// uniqueAddrs[v] = struct{}{} | ||
// } | ||
|
||
// for _, v := range tokenAddrs { | ||
// uniqueAddrs[v] = struct{}{} | ||
// } | ||
|
||
// // find addresses in pairs that are not in tokens | ||
// for _, v := range pairAddrs { | ||
// if _, ok := uniqueAddrs[v]; !ok { | ||
// fmt.Printf("Pair address %s not found in tokens\n", v) | ||
// } | ||
// } | ||
|
||
// fmt.Printf("Found %d unique addresses\n", len(uniqueAddrs)) | ||
// fmt.Printf("Found %d unique pair addresses\n", len(pairAddrs)) | ||
// fmt.Printf("Found %d unique token addresses\n", len(tokenAddrs)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package eth | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"sync" | ||
|
||
"github.com/autoapev1/indexer/types" | ||
etypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
) | ||
|
||
func (n *Network) GetBlockTimestamps(from int64, to int64) ([]*types.BlockTimestamp, error) { | ||
var blockTimestamps []*types.BlockTimestamp | ||
|
||
batchSize := n.config.Sync.BlockTimestamps.BatchSize | ||
concurrency := n.config.Sync.BlockTimestamps.BatchSize | ||
|
||
if batchSize <= 0 { | ||
batchSize = 100 | ||
} | ||
|
||
if concurrency <= 0 { | ||
concurrency = 2 | ||
} | ||
|
||
batches := n.makeBlockTimestampBatches(from, to, int64(batchSize)) | ||
|
||
workers := make(chan int, concurrency) | ||
var wg sync.WaitGroup | ||
counter := 0 | ||
for { | ||
if counter >= len(batches) { | ||
break | ||
} | ||
|
||
workers <- 1 | ||
wg.Add(1) | ||
batch := batches[counter] | ||
counter++ | ||
|
||
go func(batch []rpc.BatchElem) { | ||
defer func() { | ||
<-workers | ||
wg.Done() | ||
}() | ||
|
||
bts, err := n.getBlockTimestampBatch(batch) | ||
if err != nil { | ||
slog.Error("getBlockTimestampBatch", "err", err) | ||
return | ||
} | ||
|
||
blockTimestamps = append(blockTimestamps, bts...) | ||
}(batch) | ||
} | ||
|
||
wg.Wait() | ||
|
||
return blockTimestamps, nil | ||
} | ||
|
||
func (n *Network) makeBlockTimestampBatches(from int64, to int64, batchSize int64) [][]rpc.BatchElem { | ||
batchCount := (to - from) / batchSize | ||
if (to-from)%batchSize != 0 { | ||
batchCount++ | ||
} | ||
|
||
batches := make([][]rpc.BatchElem, 0, batchCount) | ||
|
||
for i := from; i <= to; i += batchSize { | ||
end := i + batchSize | ||
if end > to+1 { | ||
end = to + 1 | ||
} | ||
|
||
batch := make([]rpc.BatchElem, 0, end-i) | ||
for j := i; j < end; j++ { | ||
batch = append(batch, rpc.BatchElem{ | ||
Method: "eth_getBlockByNumber", | ||
Args: []interface{}{j, false}, | ||
Result: new(etypes.Header), | ||
}) | ||
} | ||
batches = append(batches, batch) | ||
} | ||
|
||
return batches | ||
} | ||
|
||
func (n *Network) getBlockTimestampBatch(batch []rpc.BatchElem) ([]*types.BlockTimestamp, error) { | ||
var blockTimestamps []*types.BlockTimestamp | ||
|
||
ctx := context.Background() | ||
if err := n.Client.Client().BatchCallContext(ctx, batch); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, b := range batch { | ||
if b.Error != nil { | ||
return nil, b.Error | ||
} | ||
} | ||
|
||
for _, b := range batch { | ||
header := b.Result.(*etypes.Header) | ||
blockTimestamps = append(blockTimestamps, &types.BlockTimestamp{ | ||
Block: header.Number.Int64(), | ||
Timestamp: int64(header.Time), | ||
}) | ||
} | ||
|
||
return blockTimestamps, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters