Skip to content

Commit

Permalink
major refactor: bootstrap from registry map and token index (see notes)
Browse files Browse the repository at this point in the history
* the cache is now bootstrapped from the token index and registry map
* a token index filter auto updates the cache when a token is added (using add sig)
* minor fixes to filters
  • Loading branch information
kamikazechaser committed Mar 29, 2023
1 parent f592a62 commit 8085168
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 88 deletions.
81 changes: 63 additions & 18 deletions cmd/service/filters.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,71 @@
package main

import (
"context"
"fmt"
"math/big"
"strings"
"sync"
"time"

"github.com/celo-org/celo-blockchain/common"
"github.com/grassrootseconomics/celoutils"
"github.com/grassrootseconomics/cic-chain-events/internal/filter"
"github.com/grassrootseconomics/cic-chain-events/internal/pub"
"github.com/grassrootseconomics/w3-celo-patch"
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
"github.com/grassrootseconomics/w3-celo-patch/w3types"
)

var (
systemAddress string
)
func initAddressFilter(celoProvider *celoutils.Provider, cache *sync.Map) filter.Filter {
var (
tokenIndexEntryCount big.Int
)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

registryMap, err := celoProvider.RegistryMap(ctx, celoutils.HexToAddress(ko.MustString("chain.registry_address")))
if err != nil {
lo.Fatal("init: critical error creating address filter", "error", err)
}

for k, v := range registryMap {
cache.Store(strings.ToLower(v.Hex()), k)
}

if err := celoProvider.Client.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("entryCount()", "uint256"), registryMap[celoutils.TokenIndex]).Returns(&tokenIndexEntryCount),
); err != nil {
lo.Fatal("init: critical error creating address filter", "error", err)
}

calls := make([]w3types.Caller, tokenIndexEntryCount.Int64())
tokenAddresses := make([]common.Address, tokenIndexEntryCount.Int64())

func initAddressFilter() filter.Filter {
// TODO: Temporary shortcut
systemAddress = strings.ToLower(ko.MustString("chain.system_address"))
entrySig := w3.MustNewFunc("entry(uint256 _idx)", "address")

// TODO: Bootstrap addresses from smart contract
// TODO: Add route to update cache
cache := &sync.Map{}
// TODO: There is a 5MB limit to a RPC batch call size.
// Test if 10k entries will raise an error (future proofed for a lot of years)
for i := 0; i < int(tokenIndexEntryCount.Int64()); i++ {
calls[i] = eth.CallFunc(entrySig, registryMap[celoutils.TokenIndex], new(big.Int).SetInt64(int64(i))).Returns(&tokenAddresses[i])
}

cache.Store(strings.ToLower(ko.MustString("chain.token_index_address")), "TokenIndex")
cache.Store(strings.ToLower(ko.MustString("chain.gas_faucet_address")), "GasFaucet")
cache.Store(strings.ToLower(ko.MustString("chain.user_index_address")), "UserIndex")
if err := celoProvider.Client.CallCtx(
ctx,
calls...,
); err != nil {
lo.Fatal("init: critical error creating address filter", "error", err)
}

for i, v := range tokenAddresses {
cache.Store(strings.ToLower(v.Hex()), fmt.Sprintf("TOKEN_%d", i))
}

return filter.NewAddressFilter(filter.AddressFilterOpts{
Cache: cache,
Logg: lo,
SystemAddress: systemAddress,
Cache: cache,
Logg: lo,
})
}

Expand All @@ -41,9 +79,8 @@ func initTransferFilter(pub *pub.Pub) filter.Filter {

func initGasGiftFilter(pub *pub.Pub) filter.Filter {
return filter.NewGasFilter(filter.GasFilterOpts{
Pub: pub,
Logg: lo,
SystemAddress: systemAddress,
Pub: pub,
Logg: lo,
})
}

Expand All @@ -53,3 +90,11 @@ func initRegisterFilter(pub *pub.Pub) filter.Filter {
Logg: lo,
})
}

func initTokenIndexFilter(cache *sync.Map, pub *pub.Pub) filter.Filter {
return filter.NewTokenIndexFilter(filter.TokenIndexFilterOpts{
Cache: cache,
Pub: pub,
Logg: lo,
})
}
20 changes: 20 additions & 0 deletions cmd/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/alitto/pond"
"github.com/grassrootseconomics/celoutils"
"github.com/grassrootseconomics/cic-chain-events/internal/pool"
"github.com/grassrootseconomics/cic-chain-events/internal/pub"
"github.com/grassrootseconomics/cic-chain-events/internal/store"
Expand Down Expand Up @@ -128,3 +129,22 @@ func initPub(natsConn *nats.Conn, jsCtx nats.JetStreamContext) *pub.Pub {

return pub
}

func initCeloProvider() *celoutils.Provider {
providerOpts := celoutils.ProviderOpts{
RpcEndpoint: ko.MustString("chain.rpc_endpoint"),
}

if ko.Bool("chain.testnet") {
providerOpts.ChainId = celoutils.TestnetChainId
} else {
providerOpts.ChainId = celoutils.MainnetChainId
}

provider, err := celoutils.NewProvider(providerOpts)
if err != nil {
lo.Fatal("init: critical error loading chain provider", "error", err)
}

return provider
}
10 changes: 7 additions & 3 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type (
internalServiceContainer struct {
internalServicesContainer struct {
apiService *echo.Echo
pub *pub.Pub
}
Expand Down Expand Up @@ -55,19 +55,23 @@ func main() {
natsConn, jsCtx := initJetStream()
jsPub := initPub(natsConn, jsCtx)

celoProvider := initCeloProvider()
cache := &sync.Map{}

pipeline := pipeline.NewPipeline(pipeline.PipelineOpts{
BlockFetcher: graphqlFetcher,
Filters: []filter.Filter{
initAddressFilter(),
initAddressFilter(celoProvider, cache),
initGasGiftFilter(jsPub),
initTransferFilter(jsPub),
initRegisterFilter(jsPub),
initTokenIndexFilter(cache, jsPub),
},
Logg: lo,
Store: pgStore,
})

internalServices := &internalServiceContainer{
internalServices := &internalServicesContainer{
pub: jsPub,
}
syncerStats := &syncer.Stats{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func createSigChannel() (chan os.Signal, func()) {
}
}

func startGracefulShutdown(ctx context.Context, internalServices *internalServiceContainer) {
func startGracefulShutdown(ctx context.Context, internalServices *internalServicesContainer) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

Expand Down
11 changes: 5 additions & 6 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ address = ":5000"

# Geth API endpoints
[chain]
graphql_endpoint = ""
ws_endpoint = ""
system_address = ""
token_index_address = ""
gas_faucet_address = ""
user_index_address = ""
graphql_endpoint = ""
ws_endpoint = ""
rpc_endpoint = ""
testnet = true
registry_address = ""

# Syncer configs
[syncer]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/VictoriaMetrics/metrics v1.23.1
github.com/alitto/pond v1.8.3
github.com/celo-org/celo-blockchain v1.7.2
github.com/grassrootseconomics/celoutils v1.1.1
github.com/grassrootseconomics/celoutils v1.2.1
github.com/grassrootseconomics/w3-celo-patch v0.2.0
github.com/jackc/pgx/v5 v5.3.1
github.com/jackc/tern/v2 v2.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/grassrootseconomics/celoutils v1.1.1 h1:REsndvfBkPN8UKOoQFNEGm/sCwKtTm+woYtgMl3bfZ0=
github.com/grassrootseconomics/celoutils v1.1.1/go.mod h1:Uo5YRy6AGLAHDZj9jaOI+AWoQ1H3L0v79728pPMkm9Q=
github.com/grassrootseconomics/celoutils v1.2.1 h1:ndM4h7Df0d57m2kdRXRStrnunqOL61wQ51rnOanX1KI=
github.com/grassrootseconomics/celoutils v1.2.1/go.mod h1:Uo5YRy6AGLAHDZj9jaOI+AWoQ1H3L0v79728pPMkm9Q=
github.com/grassrootseconomics/w3-celo-patch v0.2.0 h1:YqibbPzX0tQKmxU1nUGzThPKk/fiYeYZY6Aif3eyu8U=
github.com/grassrootseconomics/w3-celo-patch v0.2.0/go.mod h1:WhBXNzNIvHmS6B2hAeShs56oa9Azb4jQSrOMKuMdBWw=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
19 changes: 6 additions & 13 deletions internal/filter/address_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,24 @@ import (

type (
AddressFilterOpts struct {
Cache *sync.Map
Logg logf.Logger
SystemAddress string
Cache *sync.Map
Logg logf.Logger
}

AddressFilter struct {
cache *sync.Map
logg logf.Logger
systemAddress string
cache *sync.Map
logg logf.Logger
}
)

func NewAddressFilter(o AddressFilterOpts) Filter {
return &AddressFilter{
cache: o.Cache,
logg: o.Logg,
systemAddress: o.SystemAddress,
cache: o.Cache,
logg: o.Logg,
}
}

func (f *AddressFilter) Execute(_ context.Context, transaction *fetch.Transaction) (bool, error) {
if transaction.From.Address == f.systemAddress {
return true, nil
}

if _, found := f.cache.Load(transaction.To.Address); found {
return true, nil
}
Expand Down
56 changes: 31 additions & 25 deletions internal/filter/gas_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,69 @@ package filter
import (
"context"

"github.com/celo-org/celo-blockchain/common/hexutil"
"github.com/celo-org/celo-blockchain/common"
"github.com/grassrootseconomics/celoutils"
"github.com/grassrootseconomics/cic-chain-events/internal/pub"
"github.com/grassrootseconomics/cic-chain-events/pkg/fetch"
"github.com/grassrootseconomics/w3-celo-patch"
"github.com/zerodha/logf"
)

const (
gasFilterEventSubject = "CHAIN.gas"
gasEventSubject = "CHAIN.gas"
)

var (
giveToSig = w3.MustNewFunc("giveTo(address)", "uint256")
)

type (
GasFilterOpts struct {
Logg logf.Logger
Pub *pub.Pub
SystemAddress string
Logg logf.Logger
Pub *pub.Pub
}

GasFilter struct {
logg logf.Logger
pub *pub.Pub
systemAddress string
logg logf.Logger
pub *pub.Pub
}
)

func NewGasFilter(o GasFilterOpts) Filter {
return &GasFilter{
logg: o.Logg,
pub: o.Pub,
systemAddress: o.SystemAddress,
logg: o.Logg,
pub: o.Pub,
}
}

func (f *GasFilter) Execute(_ context.Context, transaction *fetch.Transaction) (bool, error) {
transferValue, err := hexutil.DecodeUint64(transaction.Value)
if err != nil {
return false, err
if len(transaction.InputData) < 10 {
return true, nil
}

// TODO: This is a temporary shortcut to gift gas. Switch to gas faucet contract.
if transaction.From.Address == f.systemAddress && transferValue > 0 {
transferEvent := &pub.MinimalTxInfo{
Block: transaction.Block.Number,
To: celoutils.ChecksumAddress(transaction.To.Address),
TxHash: transaction.Hash,
TxIndex: transaction.Index,
Value: transferValue,
if transaction.InputData[:10] == "0x63e4bff4" {
var address common.Address

if err := giveToSig.DecodeArgs(w3.B(transaction.InputData), &address); err != nil {
return false, err
}

giveToEvent := &pub.MinimalTxInfo{
Block: transaction.Block.Number,
ContractAddress: celoutils.ChecksumAddress(transaction.To.Address),
To: address.Hex(),
TxHash: transaction.Hash,
TxIndex: transaction.Index,
}

if transaction.Status == 1 {
transferEvent.Success = true
giveToEvent.Success = true
}

if err := f.pub.Publish(
gasFilterEventSubject,
gasEventSubject,
transaction.Hash,
transferEvent,
giveToEvent,
); err != nil {
return false, err
}
Expand Down

0 comments on commit 8085168

Please sign in to comment.