Squashed commit of the following:

commit 05d142664d
Author: Mohamed Sohail 天明 <sohailsameja@gmail.com>
Date:   Mon Oct 7 15:12:58 2024 +0300

    feat: handle contract creation (#43)

    * feat: add contract creation handler

    * fix: process contract creations

    * fix: redis keys name

commit 4b2ad3daf9
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Oct 7 15:12:15 2024 +0300

    build(deps): bump github.com/knadh/koanf/providers/env (#37)

    Bumps [github.com/knadh/koanf/providers/env](https://github.com/knadh/koanf) from 0.1.0 to 1.0.0.
    - [Release notes](https://github.com/knadh/koanf/releases)
    - [Commits](https://github.com/knadh/koanf/compare/v0.1.0...v1.0.0)

    ---
    updated-dependencies:
    - dependency-name: github.com/knadh/koanf/providers/env
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...

    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit f1086fcdc1
Author: Mohamed Sohail 天明 <sohailsameja@gmail.com>
Date:   Mon Oct 7 10:07:11 2024 +0300

    feat: optimize exists to check multiple keys in one call (#40)

    * closes #32

commit fd59d286f5
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Mon Oct 7 09:49:01 2024 +0300

    feat: add custodial registration proxy handler
This commit is contained in:
Mohamed Sohail 2024-10-07 15:14:24 +03:00
parent 8e3e027044
commit 725c18c0ff
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
12 changed files with 206 additions and 30 deletions

View File

@ -230,12 +230,12 @@ func bootstrapCache() error {
} }
for _, address := range ko.MustStrings("bootstrap.watchlist") { for _, address := range ko.MustStrings("bootstrap.watchlist") {
if err := cache.Add(ctx, address); err != nil { if err := cache.Add(ctx, ethutils.HexToAddress(address).Hex()); err != nil {
return err return err
} }
} }
for _, address := range ko.MustStrings("bootstrap.blacklist") { for _, address := range ko.MustStrings("bootstrap.blacklist") {
if err := cache.Remove(ctx, address); err != nil { if err := cache.Remove(ctx, ethutils.HexToAddress(address).Hex()); err != nil {
return err return err
} }
} }

View File

@ -11,6 +11,8 @@ func bootstrapEventRouter(cacheProvider cache.Cache, pubCB router.Callback) *rou
handlerContainer := handler.New(cacheProvider) handlerContainer := handler.New(cacheProvider)
router := router.New(pubCB) router := router.New(pubCB)
router.RegisterContractCreationHandler(handler.HandleContractCreation())
router.RegisterLogRoute(w3.H("0x26162814817e23ec5035d6a2edc6c422da2da2119e27cfca6be65cc2dc55ca4c"), handler.HandleFaucetGiveLog()) router.RegisterLogRoute(w3.H("0x26162814817e23ec5035d6a2edc6c422da2da2119e27cfca6be65cc2dc55ca4c"), handler.HandleFaucetGiveLog())
router.RegisterLogRoute(w3.H("0xa226db3f664042183ee0281230bba26cbf7b5057e50aee7f25a175ff45ce4d7f"), handler.HandleIndexAddLog(handlerContainer)) router.RegisterLogRoute(w3.H("0xa226db3f664042183ee0281230bba26cbf7b5057e50aee7f25a175ff45ce4d7f"), handler.HandleIndexAddLog(handlerContainer))
router.RegisterLogRoute(w3.H("0x24a12366c02e13fe4a9e03d86a8952e85bb74a456c16e4a18b6d8295700b74bb"), handler.HandleIndexRemoveLog(handlerContainer)) router.RegisterLogRoute(w3.H("0x24a12366c02e13fe4a9e03d86a8952e85bb74a456c16e4a18b6d8295700b74bb"), handler.HandleIndexRemoveLog(handlerContainer))
@ -21,6 +23,7 @@ func bootstrapEventRouter(cacheProvider cache.Cache, pubCB router.Callback) *rou
router.RegisterLogRoute(w3.H("0x6b7e2e653f93b645d4ed7292d6429f96637084363e477c8aaea1a43ed13c284e"), handler.HandleSealStateChangeLog()) router.RegisterLogRoute(w3.H("0x6b7e2e653f93b645d4ed7292d6429f96637084363e477c8aaea1a43ed13c284e"), handler.HandleSealStateChangeLog())
router.RegisterLogRoute(w3.H("0xcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5"), handler.HandleTokenBurnLog()) router.RegisterLogRoute(w3.H("0xcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5"), handler.HandleTokenBurnLog())
router.RegisterLogRoute(w3.H("0xab8530f87dc9b59234c4623bf917212bb2536d647574c8e7e5da92c2ede0c9f8"), handler.HandleTokenMintLog()) router.RegisterLogRoute(w3.H("0xab8530f87dc9b59234c4623bf917212bb2536d647574c8e7e5da92c2ede0c9f8"), handler.HandleTokenMintLog())
router.RegisterLogRoute(w3.H("0x894e56e1dac400b4475c83d8af0f0aa44de17c62764bd82f6e768a504e242461"), handler.HandleCustodialRegistrationLog())
router.RegisterLogRoute(w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), handler.HandleTokenTransferLog(handlerContainer)) router.RegisterLogRoute(w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), handler.HandleTokenTransferLog(handlerContainer))
router.RegisterInputDataRoute("63e4bff4", handler.HandleFaucetGiveInputData()) router.RegisterInputDataRoute("63e4bff4", handler.HandleFaucetGiveInputData())
@ -35,6 +38,7 @@ func bootstrapEventRouter(cacheProvider cache.Cache, pubCB router.Callback) *rou
router.RegisterInputDataRoute("86fe212d", handler.HandleSealStateChangeInputData()) router.RegisterInputDataRoute("86fe212d", handler.HandleSealStateChangeInputData())
router.RegisterInputDataRoute("42966c68", handler.HandleTokenBurnInputData()) router.RegisterInputDataRoute("42966c68", handler.HandleTokenBurnInputData())
router.RegisterInputDataRoute("449a52f8", handler.HandleTokenMintInputData()) router.RegisterInputDataRoute("449a52f8", handler.HandleTokenMintInputData())
router.RegisterInputDataRoute("4420e486", handler.HandleCustodialRegistrationInputData())
router.RegisterInputDataRoute("a9059cbb", handler.HandleTokenTransferInputData(handlerContainer)) router.RegisterInputDataRoute("a9059cbb", handler.HandleTokenTransferInputData(handlerContainer))
router.RegisterInputDataRoute("23b872dd", handler.HandleTokenTransferInputData(handlerContainer)) router.RegisterInputDataRoute("23b872dd", handler.HandleTokenTransferInputData(handlerContainer))

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/grassrootseconomics/ethutils v1.3.0 github.com/grassrootseconomics/ethutils v1.3.0
github.com/kamikazechaser/common v0.2.0 github.com/kamikazechaser/common v0.2.0
github.com/knadh/koanf/parsers/toml v0.1.0 github.com/knadh/koanf/parsers/toml v0.1.0
github.com/knadh/koanf/providers/env v0.1.0 github.com/knadh/koanf/providers/env v1.0.0
github.com/knadh/koanf/providers/file v1.1.0 github.com/knadh/koanf/providers/file v1.1.0
github.com/knadh/koanf/v2 v2.1.1 github.com/knadh/koanf/v2 v2.1.1
github.com/lmittmann/w3 v0.17.0 github.com/lmittmann/w3 v0.17.0

4
go.sum
View File

@ -112,8 +112,8 @@ github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NI
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6ODgOub/6LCI= github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6ODgOub/6LCI=
github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18= github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18=
github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg= github.com/knadh/koanf/providers/env v1.0.0 h1:ufePaI9BnWH+ajuxGGiJ8pdTG0uLEUWC7/HDDPGLah0=
github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ= github.com/knadh/koanf/providers/env v1.0.0/go.mod h1:mzFyRZueYhb37oPmC1HAv/oGEEuyvJDA98r3XAa8Gak=
github.com/knadh/koanf/providers/file v1.1.0 h1:MTjA+gRrVl1zqgetEAIaXHqYje0XSosxSiMD4/7kz0o= github.com/knadh/koanf/providers/file v1.1.0 h1:MTjA+gRrVl1zqgetEAIaXHqYje0XSosxSiMD4/7kz0o=
github.com/knadh/koanf/providers/file v1.1.0/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI= github.com/knadh/koanf/providers/file v1.1.0/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM= github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM=

View File

@ -9,7 +9,7 @@ type (
Cache interface { Cache interface {
Add(context.Context, string) error Add(context.Context, string) error
Remove(context.Context, string) error Remove(context.Context, string) error
Exists(context.Context, string) (bool, error) Exists(context.Context, ...string) (bool, error)
Size(context.Context) (int64, error) Size(context.Context) (int64, error)
} }

View File

@ -40,8 +40,8 @@ func (c *redisCache) Remove(ctx context.Context, key string) error {
return c.client.Do(ctx, cmd).Error() return c.client.Do(ctx, cmd).Error()
} }
func (c *redisCache) Exists(ctx context.Context, key string) (bool, error) { func (c *redisCache) Exists(ctx context.Context, keys ...string) (bool, error) {
cmd := c.client.B().Exists().Key(key).Build() cmd := c.client.B().Exists().Key(keys...).Build()
res, err := c.client.Do(ctx, cmd).AsBool() res, err := c.client.Do(ctx, cmd).AsBool()
if err != nil { if err != nil {
return false, err return false, err

View File

@ -26,9 +26,14 @@ func (c *mapCache) Remove(_ context.Context, key string) error {
return nil return nil
} }
func (c *mapCache) Exists(_ context.Context, key string) (bool, error) { func (c *mapCache) Exists(_ context.Context, key ...string) (bool, error) {
_, ok := c.xmap.Load(key) for _, v := range key {
return ok, nil _, ok := c.xmap.Load(v)
if ok {
return true, nil
}
}
return false, nil
} }
func (c *mapCache) Size(_ context.Context) (int64, error) { func (c *mapCache) Size(_ context.Context) (int64, error) {

View File

@ -0,0 +1,28 @@
package handler
import (
"context"
"github.com/grassrootseconomics/eth-tracker/pkg/event"
"github.com/grassrootseconomics/eth-tracker/pkg/router"
)
const contractCreationEventName = "CONTRACT_CREATION"
func HandleContractCreation() router.ContractCreationHandlerFunc {
return func(ctx context.Context, ccp router.ContractCreationPayload, c router.Callback) error {
contractCreationEvent := event.Event{
Block: ccp.Block,
ContractAddress: ccp.ContractAddress,
Success: ccp.Success,
Timestamp: ccp.Timestamp,
TxHash: ccp.TxHash,
TxType: contractCreationEventName,
Payload: map[string]any{
"from": ccp.From,
},
}
return c(ctx, contractCreationEvent)
}
}

View File

@ -0,0 +1,66 @@
package handler
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/eth-tracker/pkg/event"
"github.com/grassrootseconomics/eth-tracker/pkg/router"
"github.com/lmittmann/w3"
)
const custodialRegistrationEventName = "CUSTODIAL_REGISTRATION"
var (
custodialRegistrationEvent = w3.MustNewEvent("NewRegistration(address indexed subject)")
custodialRegistrationSig = w3.MustNewFunc("register(address)", "")
)
func HandleCustodialRegistrationLog() router.LogHandlerFunc {
return func(ctx context.Context, lp router.LogPayload, c router.Callback) error {
var account common.Address
if err := custodialRegistrationEvent.DecodeArgs(lp.Log, &account); err != nil {
return err
}
custodialRegistrationEvent := event.Event{
Index: lp.Log.Index,
Block: lp.Log.BlockNumber,
ContractAddress: lp.Log.Address.Hex(),
Success: true,
Timestamp: lp.Timestamp,
TxHash: lp.Log.TxHash.Hex(),
TxType: custodialRegistrationEventName,
Payload: map[string]any{
"account": account.Hex(),
},
}
return c(ctx, custodialRegistrationEvent)
}
}
func HandleCustodialRegistrationInputData() router.InputDataHandlerFunc {
return func(ctx context.Context, idp router.InputDataPayload, c router.Callback) error {
var account common.Address
if err := custodialRegistrationSig.DecodeArgs(w3.B(idp.InputData), &account); err != nil {
return err
}
custodialRegistrationEvent := event.Event{
Block: idp.Block,
ContractAddress: idp.ContractAddress,
Success: false,
Timestamp: idp.Timestamp,
TxHash: idp.TxHash,
TxType: custodialRegistrationEventName,
Payload: map[string]any{
"account": account.Hex(),
},
}
return c(ctx, custodialRegistrationEvent)
}
}

View File

@ -147,16 +147,10 @@ func (hc *HandlerContainer) checkStables(ctx context.Context, from string, to st
return true, nil return true, nil
} }
// TODO: Pipeline this check on Redis with a new method exists, err := hc.cache.Exists(ctx, from, to)
fromExists, err := hc.cache.Exists(ctx, from)
if err != nil { if err != nil {
return false, err return false, err
} }
toExists, err := hc.cache.Exists(ctx, to) return exists, nil
if err != nil {
return false, err
}
return fromExists || toExists, nil
} }

View File

@ -54,7 +54,7 @@ func (p *Processor) ProcessBlock(ctx context.Context, blockNumber uint64) error
} }
for _, receipt := range receipts { for _, receipt := range receipts {
if receipt.Status > 0 { if receipt.Status == 1 {
for _, log := range receipt.Logs { for _, log := range receipt.Logs {
exists, err := p.cache.Exists(ctx, log.Address.Hex()) exists, err := p.cache.Exists(ctx, log.Address.Hex())
if err != nil { if err != nil {
@ -72,13 +72,72 @@ func (p *Processor) ProcessBlock(ctx context.Context, blockNumber uint64) error
} }
} }
} }
} else {
if receipt.ContractAddress != (common.Address{}) {
tx, err := p.chain.GetTransaction(ctx, receipt.TxHash)
if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("get transaction error: tx %s: %v", receipt.TxHash.Hex(), err)
}
from, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
if err != nil {
return fmt.Errorf("transaction decode error: tx %s: %v", receipt.TxHash.Hex(), err)
}
exists, err := p.cache.Exists(ctx, from.Hex())
if err != nil {
return err
}
if exists {
if err := p.router.ProcessContractCreation(
ctx,
router.ContractCreationPayload{
From: from.Hex(),
Block: blockNumber,
ContractAddress: receipt.ContractAddress.Hex(),
Timestamp: block.Time(),
TxHash: receipt.TxHash.Hex(),
Success: true,
},
); err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("route success contract creation error: tx %s: %v", receipt.TxHash.Hex(), err)
}
}
}
}
if receipt.Status == 0 {
tx, err := p.chain.GetTransaction(ctx, receipt.TxHash) tx, err := p.chain.GetTransaction(ctx, receipt.TxHash)
if err != nil && !errors.Is(err, context.Canceled) { if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("get transaction error: tx %s: %v", receipt.TxHash.Hex(), err) return fmt.Errorf("get transaction error: tx %s: %v", receipt.TxHash.Hex(), err)
} }
if tx.To() == nil {
from, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
if err != nil {
return fmt.Errorf("transaction decode error: tx %s: %v", receipt.TxHash.Hex(), err)
}
if tx.To() != nil { exists, err := p.cache.Exists(ctx, from.Hex())
if err != nil {
return err
}
if exists {
if err := p.router.ProcessContractCreation(
ctx,
router.ContractCreationPayload{
From: from.Hex(),
Block: blockNumber,
ContractAddress: receipt.ContractAddress.Hex(),
Timestamp: block.Time(),
TxHash: receipt.TxHash.Hex(),
Success: false,
},
); err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("route reverted contract creation error: tx %s: %v", receipt.TxHash.Hex(), err)
}
}
} else {
exists, err := p.cache.Exists(ctx, tx.To().Hex()) exists, err := p.cache.Exists(ctx, tx.To().Hex())
if err != nil { if err != nil {
return err return err

View File

@ -25,8 +25,18 @@ type (
TxHash string TxHash string
} }
LogHandlerFunc func(context.Context, LogPayload, Callback) error ContractCreationPayload struct {
InputDataHandlerFunc func(context.Context, InputDataPayload, Callback) error From string
ContractAddress string
Block uint64
Timestamp uint64
TxHash string
Success bool
}
LogHandlerFunc func(context.Context, LogPayload, Callback) error
InputDataHandlerFunc func(context.Context, InputDataPayload, Callback) error
ContractCreationHandlerFunc func(context.Context, ContractCreationPayload, Callback) error
LogRouteEntry struct { LogRouteEntry struct {
Signature common.Hash Signature common.Hash
@ -39,17 +49,19 @@ type (
} }
Router struct { Router struct {
callbackFn Callback callbackFn Callback
logHandlers map[common.Hash]LogRouteEntry logHandlers map[common.Hash]LogRouteEntry
inputDataHandlers map[string]InputDataEntry inputDataHandlers map[string]InputDataEntry
contractCreationHandler ContractCreationHandlerFunc
} }
) )
func New(callbackFn Callback) *Router { func New(callbackFn Callback) *Router {
return &Router{ return &Router{
callbackFn: callbackFn, callbackFn: callbackFn,
logHandlers: make(map[common.Hash]LogRouteEntry), logHandlers: make(map[common.Hash]LogRouteEntry),
inputDataHandlers: make(map[string]InputDataEntry), inputDataHandlers: make(map[string]InputDataEntry),
contractCreationHandler: nil,
} }
} }
@ -67,6 +79,10 @@ func (r *Router) RegisterInputDataRoute(signature string, handlerFunc InputDataH
} }
} }
func (r *Router) RegisterContractCreationHandler(handlerFunc ContractCreationHandlerFunc) {
r.contractCreationHandler = handlerFunc
}
func (r *Router) ProcessLog(ctx context.Context, payload LogPayload) error { func (r *Router) ProcessLog(ctx context.Context, payload LogPayload) error {
handler, ok := r.logHandlers[payload.Log.Topics[0]] handler, ok := r.logHandlers[payload.Log.Topics[0]]
if ok { if ok {
@ -88,3 +104,7 @@ func (r *Router) ProcessInputData(ctx context.Context, payload InputDataPayload)
return nil return nil
} }
func (r *Router) ProcessContractCreation(ctx context.Context, payload ContractCreationPayload) error {
return r.contractCreationHandler(ctx, payload, r.callbackFn)
}