Skip to content

Commit

Permalink
Merge branch 'master' into fe/post-transaction-balance
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds authored Mar 7, 2024
2 parents f506554 + ca68b9e commit 8f7f5dd
Show file tree
Hide file tree
Showing 108 changed files with 3,146 additions and 724 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ jobs:
with:
working-directory: ${{matrix.package}}/
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.56.1
version: v1.56.2
# see: https://github.com/golangci/golangci-lint/issues/2654
args: --timeout=60m
env:
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/goreleaser-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0 # needed if using new-from-rev (see: https://golangci-lint.run/usage/configuration/#issues-configuration)
submodules: true


- name: Cache Docker images.
Expand Down Expand Up @@ -169,11 +168,6 @@ jobs:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.20.x

-
name: Login to GitHub Container Registry
uses: docker/login-action@v1
Expand Down
3 changes: 2 additions & 1 deletion contrib/promexporter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func DecodeConfig(filePath string) (_ Config, err error) {
Address: "0x230a1ac45690b9ae1176389434610b9526d2f21b",
ChainIDs: types.ToInts(types.ETH, types.OPTIMISM, types.CRONOS, types.BSC, types.POLYGON, types.FANTOM, types.BOBA,
types.METIS, types.MOONBEAM, types.MOONRIVER, types.DOGECHAIN, types.CANTO, types.KLAYTN,
types.BASE, types.ARBITRUM, types.AVALANCHE, types.DFK, types.AURORA, types.HARMONY),
types.BASE, types.ARBITRUM, types.AVALANCHE, types.DFK, types.AURORA, types.HARMONY, types.BLAST),
Name: "validators",
},
{
Expand Down Expand Up @@ -130,6 +130,7 @@ func DecodeConfig(filePath string) (_ Config, err error) {
types.CANTO.Int(): "0xDde5BEC4815E1CeCf336fb973Ca578e8D83606E0",
types.DOGECHAIN.Int(): "0x9508BF380c1e6f751D97604732eF1Bae6673f299",
types.BASE.Int(): "0xf07d1C752fAb503E47FEF309bf14fbDD3E867089",
types.BLAST.Int(): "0x55769bAF6ec39B3bf4aAE948eB890eA33307Ef3C",
}

cfg.VpriceCheckTokens = []string{"nUSD", "nETH"}
Expand Down
1 change: 1 addition & 0 deletions contrib/promexporter/internal/types/chainID.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
CRONOS ChainID = 25
KOVAN ChainID = 42
BSC ChainID = 56
BLAST ChainID = 81457
POLYGON ChainID = 137
FANTOM ChainID = 250
BOBA ChainID = 288
Expand Down
8 changes: 5 additions & 3 deletions contrib/promexporter/internal/types/chainid_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions contrib/screener-api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ type Config struct {
Port int `yaml:"port"`
// Database is the database configuration
Database DatabaseConfig `yaml:"database"`
// VolumeThresholds is the volume thresholds for each risk type
VolumeThresholds []VolumeThreshold `yaml:"volumeThresholds"`
// TODO: This HAS to be re-structured somehow
// Whitelist is a list of addresses to whitelist
Whitelist []string `yaml:"whitelist"`
}

// VolumeThreshold defines thresholds for different risk categories and types.
type VolumeThreshold struct {
Category string `yaml:"category"`
TypeOfRisk string `yaml:"typeOfRisk"`
Incoming float64 `yaml:"incoming"`
Outgoing float64 `yaml:"outgoing"`
}

// GetCacheTime gets how long to use the cache for a given ruleset.
Expand Down
33 changes: 25 additions & 8 deletions contrib/screener-api/screener/internal/risk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package internal

import (
"fmt"
"github.com/synapsecns/sanguine/contrib/screener-api/trmlabs"
"strconv"
"strings"

"github.com/synapsecns/sanguine/contrib/screener-api/config"
"github.com/synapsecns/sanguine/contrib/screener-api/trmlabs"
)

// rulesetManager manages the rulesets.
Expand Down Expand Up @@ -51,7 +53,7 @@ func (rm *rulesetManager) GetRuleset(callerType string) RuleSet {
// RuleSet interface defines methods to work with risk rules.
type RuleSet interface {
HasRisk(riskType string) bool
HasAddressIndicators(riskIndicators ...trmlabs.AddressRiskIndicator) (bool, error)
HasAddressIndicators(thresholds []config.VolumeThreshold, riskIndicators ...trmlabs.AddressRiskIndicator) (bool, error)
}

// CallerRuler implements the RuleSet interface for a specific caller type.
Expand All @@ -72,7 +74,12 @@ func (cr *CallerRuler) HasRisk(riskType string) bool {
}

// HasAddressIndicators returns a list of addressRiskIndicator.
func (cr *CallerRuler) HasAddressIndicators(riskIndicators ...trmlabs.AddressRiskIndicator) (bool, error) {
//
//nolint:cyclop
func (cr *CallerRuler) HasAddressIndicators(thresholds []config.VolumeThreshold, riskIndicators ...trmlabs.AddressRiskIndicator) (bool, error) {
// Initialize a variable to track if any indicator is blocked
anyIndicatorBlocked := false

for _, ri := range riskIndicators {
incoming, err := strconv.ParseFloat(ri.IncomingVolumeUsd, 32)
if err != nil {
Expand All @@ -84,14 +91,24 @@ func (cr *CallerRuler) HasAddressIndicators(riskIndicators ...trmlabs.AddressRis
return false, fmt.Errorf("could not parse outgoing volume: %w", err)
}

riskParam := MakeParam(ri.Category, ri.RiskType)
isBlocked, found := cr.riskRules[riskParam]
if isBlocked && found && (incoming > 0 || outgoing > 0) {
return true, nil
// Check against thresholds
for _, threshold := range thresholds {
if strings.EqualFold(ri.Category, threshold.Category) && strings.EqualFold(ri.RiskType, threshold.TypeOfRisk) {
// If either incoming or outgoing volume exceeds the threshold, the indicator is blocked
if (threshold.Incoming > 0 && incoming > threshold.Incoming) || (threshold.Outgoing > 0 && outgoing > threshold.Outgoing) {
anyIndicatorBlocked = true
break // No need to check other thresholds, this indicator is blocked
}
}
}

if anyIndicatorBlocked {
break // No need to check further indicators, at least one indicator is blocked
}
}

return false, nil
// Return true if any indicator is blocked, otherwise false
return anyIndicatorBlocked, nil
}

// MakeParam creates a risk param from the given category and risk type in a standardized format.
Expand Down
24 changes: 18 additions & 6 deletions contrib/screener-api/screener/internal/risk_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package internal_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/synapsecns/sanguine/contrib/screener-api/config"
"github.com/synapsecns/sanguine/contrib/screener-api/screener/internal"
"github.com/synapsecns/sanguine/contrib/screener-api/trmlabs"
"testing"
)

func TestNewRulesetManager(t *testing.T) {
Expand Down Expand Up @@ -50,35 +52,45 @@ func TestHasAddressIndicators(t *testing.T) {
"category1_risktype1": true,
}

// Define thresholds for testing
thresholds := []config.VolumeThreshold{
{
Category: "Category1",
TypeOfRisk: "RiskType1",
Incoming: 1500, // Set thresholds to allow the test cases to pass or fail as expected
Outgoing: 800,
},
}

cr := internal.NewRuleset(riskRules)

// Test case where the indicator meets risk rules
indicators := []trmlabs.AddressRiskIndicator{
{IncomingVolumeUsd: "1000", OutgoingVolumeUsd: "500", Category: "Category1", RiskType: "RiskType1"},
{IncomingVolumeUsd: "1501", OutgoingVolumeUsd: "500", Category: "Category1", RiskType: "RiskType1"},
}
result, err := cr.HasAddressIndicators(indicators...)
result, err := cr.HasAddressIndicators(thresholds, indicators...)
require.NoError(t, err)
assert.True(t, result)

// Test case where the indicator does not meet risk rules
indicators = []trmlabs.AddressRiskIndicator{
{IncomingVolumeUsd: "100", OutgoingVolumeUsd: "50", Category: "Category2", RiskType: "RiskType2"},
}
result, err = cr.HasAddressIndicators(indicators...)
result, err = cr.HasAddressIndicators(thresholds, indicators...)
require.NoError(t, err)
assert.False(t, result)

// Test case with invalid incoming volume
indicators = []trmlabs.AddressRiskIndicator{
{IncomingVolumeUsd: "invalid", OutgoingVolumeUsd: "500", Category: "Category1", RiskType: "RiskType1"},
}
_, err = cr.HasAddressIndicators(indicators...)
_, err = cr.HasAddressIndicators(thresholds, indicators...)
require.Error(t, err)

// Test case with invalid outgoing volume
indicators = []trmlabs.AddressRiskIndicator{
{IncomingVolumeUsd: "1000", OutgoingVolumeUsd: "invalid", Category: "Category1", RiskType: "RiskType1"},
}
_, err = cr.HasAddressIndicators(indicators...)
_, err = cr.HasAddressIndicators(thresholds, indicators...)
require.Error(t, err)
}
23 changes: 18 additions & 5 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin"
"github.com/ipfs/go-log"
"github.com/synapsecns/sanguine/contrib/screener-api/config"
Expand All @@ -21,10 +26,6 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slices"
"net/http"
"strings"
"sync"
"time"
)

// Screener is the interface for the screener.
Expand All @@ -34,13 +35,15 @@ type Screener interface {

type screenerImpl struct {
rulesManager internal.RulesetManager
thresholds []config.VolumeThreshold
db db.RuleDB
router *gin.Engine
metrics metrics.Handler
cfg config.Config
client trmlabs.Client
blacklist []string
blacklistMux sync.RWMutex
whitelist []string
}

var logger = log.Logger("screener")
Expand All @@ -56,6 +59,11 @@ func NewScreener(ctx context.Context, cfg config.Config, metricHandler metrics.H
if err != nil {
return nil, fmt.Errorf("could not create trm client: %w", err)
}
screener.thresholds = cfg.VolumeThresholds

for _, item := range cfg.Whitelist {
screener.whitelist = append(screener.whitelist, strings.ToLower(item))
}

screener.rulesManager, err = setupScreener(cfg.Rulesets)
if err != nil {
Expand Down Expand Up @@ -152,6 +160,11 @@ func (s *screenerImpl) screenAddress(c *gin.Context) {
}
s.blacklistMux.RUnlock()

if slices.Contains(s.whitelist, address) {
c.JSON(http.StatusOK, gin.H{"risk": false})
return
}

ctx, span := s.metrics.Tracer().Start(c.Request.Context(), "screenAddress", trace.WithAttributes(attribute.String("address", address)))
defer func() {
metrics.EndSpanWithErr(span, err)
Expand All @@ -171,7 +184,7 @@ func (s *screenerImpl) screenAddress(c *gin.Context) {
}

var hasIndicator bool
if hasIndicator, err = currentRules.HasAddressIndicators(indicators...); err != nil {
if hasIndicator, err = currentRules.HasAddressIndicators(s.thresholds, indicators...); err != nil {
c.JSON(http.StatusOK, gin.H{"risk": true})
return
}
Expand Down
9 changes: 5 additions & 4 deletions contrib/screener-api/screener/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package screener
import (
"encoding/csv"
"fmt"
"os"
"strings"

"github.com/gocarina/gocsv"
"github.com/synapsecns/sanguine/contrib/screener-api/config"
"github.com/synapsecns/sanguine/contrib/screener-api/screener/internal"
"os"
"strings"
)

func setupScreener(rulesets map[string]config.RulesetConfig) (internal.RulesetManager, error) {
mgr := internal.NewRulesetManager(map[string]map[string]bool{})
func setupScreener(rulesets map[string]config.RulesetConfig) (mgr internal.RulesetManager, err error) {
mgr = internal.NewRulesetManager(map[string]map[string]bool{})
for csvName, cfg := range rulesets {
csvPath := cfg.Filename
parsedCsv, err := parseCsv(csvPath)
Expand Down
7 changes: 4 additions & 3 deletions contrib/screener-api/screener/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"context"
"errors"
"fmt"
"strconv"
"testing"
"time"

"github.com/Flaque/filet"
"github.com/gocarina/gocsv"
"github.com/phayes/freeport"
Expand All @@ -19,9 +23,6 @@ import (
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/core/metrics/localmetrics"
"github.com/synapsecns/sanguine/core/testsuite"
"strconv"
"testing"
"time"
)

type ScreenerSuite struct {
Expand Down
3 changes: 1 addition & 2 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

Core contains common libraries used across the synapse Go repositories.


## Directory Structure

<pre>
Expand All @@ -20,7 +19,7 @@ root
├── <a href="./merkle">merkle</a>: Provides a go based merkle tree implementation.
├── <a href="./metrics">metrics</a>: Provides a set of utilities for working with metrics/otel tracing.
├── <a href="./mocktesting">mocktesting</a>: Provides a mocked tester for use with `testing.TB`
├── <a href="./observer">observer</a>: Provides an interface for adding/removing listeners.
├── <a href="./observer"><s>observer</s></a>(deprecated): Provides an interface for adding/removing listeners.
├── <a href="./processlog">processlog</a>: Provides a way to interact with detatched processes as streams.
├── <a href="./retry">retry</a>: Retries a function until it succeeds or the timeout is reached. This comes with a set of backoff strategies/options.
├── <a href="./server">server</a>: Provides a context-safe server that can be used to start/stop a server.
Expand Down
8 changes: 4 additions & 4 deletions docker/goreleaser/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM --platform=linux/amd64 debian:11

ARG VERSION_ARG=1.20.0-pro
ARG SHA_ARG=d2d76cf4b212f67cb9995c8539167a1c6d771859aad20ed242bfab640b6d396f
ARG VERSION_ARG=1.24.0-pro
ARG SHA_ARG=01237f7151d2c46c307f21de183eb863ce47a4b5244507487ec663640b077d7d
ARG FILE_ARG=goreleaser-pro_Linux_x86_64.tar.gz
ARG DOWNLOAD_ARG=https://github.com/goreleaser/goreleaser-pro/releases/download/v${VERSION_ARG}/${FILE_ARG}

Expand All @@ -12,8 +12,8 @@ ENV GORELEASER_DOWNLOAD_FILE=$FILE_ARG
ENV GORELEASER_DOWNLOAD_URL=$DOWNLOAD_ARG

# Golang
ENV GOLANG_VERSION=1.21.3
ENV GOLANG_SHA=1241381b2843fae5a9707eec1f8fb2ef94d827990582c7c7c32f5bdfbfd420c8
ENV GOLANG_VERSION=1.22.0
ENV GOLANG_SHA=f6c8a87aa03b92c4b0bf3d558e28ea03006eb29db78917daec5cfb6ec1046265
ENV GOLANG_DOWNLOAD_FILE=go${GOLANG_VERSION}.linux-amd64.tar.gz
ENV GOLANG_DOWNLOAD_URL=https://dl.google.com/go/${GOLANG_DOWNLOAD_FILE}

Expand Down
Loading

0 comments on commit 8f7f5dd

Please sign in to comment.