From 7b5e6b809c0e2f6a8615896d57e2b0d2db98c80b Mon Sep 17 00:00:00 2001 From: jeff <113397187+cyberhorsey@users.noreply.github.com> Date: Wed, 23 Nov 2022 07:52:04 -0800 Subject: [PATCH] feat(relayer): HTTP api for exposing events table for bridge UI (#271) * WIP on HTTP api for exposing events table for bridge UI * error checking * escape string * newHTTPServer tests --- go.mod | 15 +++- go.sum | 42 +++++++++- packages/relayer/.default.env | 4 +- packages/relayer/cli/cli.go | 23 +++++- packages/relayer/cli/cli_test.go | 18 +++++ packages/relayer/errors.go | 9 ++- packages/relayer/event.go | 7 +- .../relayer/http/get_events_by_address.go | 28 +++++++ .../http/get_events_by_address_test.go | 71 +++++++++++++++++ packages/relayer/http/routes.go | 2 + packages/relayer/http/server.go | 17 +++- packages/relayer/http/server_test.go | 21 ++++- packages/relayer/indexer/handle_event.go | 2 +- packages/relayer/message/process_message.go | 2 +- packages/relayer/mock/event_repository.go | 77 ++++++++++++++++++- packages/relayer/repo/event.go | 11 ++- packages/relayer/repo/event_test.go | 11 +-- 17 files changed, 325 insertions(+), 35 deletions(-) create mode 100644 packages/relayer/http/get_events_by_address.go create mode 100644 packages/relayer/http/get_events_by_address_test.go diff --git a/go.mod b/go.mod index ed86d5a56a2..b6bdaa8a0c5 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,12 @@ module github.com/taikoxyz/taiko-mono go 1.19 -replace github.com/umbracle/ethgo => ../../ethgo - require ( github.com/cyberhorsey/errors v0.0.0-20220929234051-087d6d8bb841 + github.com/cyberhorsey/webutils v0.0.0-20220929234343-770c4fa3beba github.com/ethereum/go-ethereum v1.10.25 github.com/joho/godotenv v1.4.0 + github.com/labstack/echo-contrib v0.13.0 github.com/labstack/echo/v4 v4.9.1 github.com/pkg/errors v0.9.1 github.com/pressly/goose v2.7.0+incompatible @@ -27,20 +27,26 @@ require ( github.com/Microsoft/go-winio v0.5.2 // indirect github.com/Microsoft/hcsshim v0.9.4 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect + github.com/aymerick/douceur v0.2.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/chris-ramon/douceur v0.2.0 // indirect github.com/containerd/cgroups v1.0.4 // indirect github.com/containerd/containerd v1.6.8 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect + github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/docker/distribution v2.8.1+incompatible // indirect github.com/docker/docker v20.10.17+incompatible // indirect github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/go-ole/go-ole v1.2.1 // indirect + github.com/go-playground/locales v0.13.0 // indirect + github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/validator/v10 v10.4.0 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/go-stack/stack v1.8.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -48,19 +54,22 @@ require ( github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/uuid v1.3.0 // indirect + github.com/gorilla/css v1.0.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/labstack/echo-contrib v0.13.0 // indirect github.com/labstack/gommon v0.4.0 // indirect + github.com/leodido/go-urn v1.2.0 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect + github.com/microcosm-cc/bluemonday v1.0.4 // indirect github.com/moby/sys/mount v0.3.3 // indirect github.com/moby/sys/mountinfo v0.6.2 // indirect github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect github.com/morikuni/aec v1.0.0 // indirect + github.com/neko-neko/echo-logrus/v2 v2.0.1 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect github.com/opencontainers/runc v1.1.3 // indirect diff --git a/go.sum b/go.sum index 770b7140517..94a9e82987c 100644 --- a/go.sum +++ b/go.sum @@ -91,9 +91,12 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0= +github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= +github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= @@ -118,7 +121,6 @@ github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8 github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= @@ -126,6 +128,8 @@ github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL github.com/checkpoint-restore/go-criu/v4 v4.1.0/go.mod h1:xUQBLp4RLc5zJtWY++yjOoMoB5lihDt7fai+75m+rGw= github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M= github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E= +github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27glkU= +github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -260,6 +264,8 @@ github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw= github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cyberhorsey/errors v0.0.0-20220929234051-087d6d8bb841 h1:FaPua89k9KmkkiptlTAmYzcTsn1IFdQsqneuFO6IAu8= github.com/cyberhorsey/errors v0.0.0-20220929234051-087d6d8bb841/go.mod h1:qSH/IvpdArCjfny4ODMko/7U0z4JoNIWctgCnUrapnI= +github.com/cyberhorsey/webutils v0.0.0-20220929234343-770c4fa3beba h1:bNdCrXULXOZo7PLF+Y7u5njDCHr0jgLEKgLsLCFZtck= +github.com/cyberhorsey/webutils v0.0.0-20220929234343-770c4fa3beba/go.mod h1:SsnedeS2nEcRNpaMO2cQ+0jv86D++V7oMPhF5lKxOwo= github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4= github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1SMSibvLzxjeJLnrYEVLULFNiHY9YfQ= @@ -279,6 +285,7 @@ github.com/denisenkom/go-mssqldb v0.12.0/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfz github.com/denisenkom/go-mssqldb v0.12.2 h1:1OcPn5GBIobjWNd+8yjfHNIaFX14B1pWI3F9HZy5KXw= github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0= github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -360,6 +367,14 @@ github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8 github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I= github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kVYAvjEvpT85l70= +github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= @@ -461,6 +476,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= +github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gorilla/handlers v0.0.0-20150720190736-60c7bfde3e33/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= @@ -589,10 +606,16 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/labstack/echo-contrib v0.13.0 h1:bzSG0SpuZZd7BmJLvsWtPfU23W0Enh3K0tok3aENVKA= github.com/labstack/echo-contrib v0.13.0/go.mod h1:IF9+MJu22ADOZEHD+bAV67XMIO3vNXUy7Naz/ABPHEs= +github.com/labstack/echo/v4 v4.0.0/go.mod h1:tZv7nai5buKSg5h/8E6zz4LsD/Dqh9/91Mvs7Z5Zyno= +github.com/labstack/echo/v4 v4.1.15/go.mod h1:GWO5IBVzI371K8XJe50CSvHjQCafK6cw8R/moLhEU6o= github.com/labstack/echo/v4 v4.9.1 h1:GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y= github.com/labstack/echo/v4 v4.9.1/go.mod h1:Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo= +github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= +github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -608,6 +631,7 @@ github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7 github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= @@ -615,6 +639,8 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= @@ -629,6 +655,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5 github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY= +github.com/microcosm-cc/bluemonday v1.0.4 h1:p0L+CTpo/PLFdkoPcJemLXG+fpMD7pYOoDEq1axMbGg= +github.com/microcosm-cc/bluemonday v1.0.4/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w= github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -663,6 +691,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= +github.com/neko-neko/echo-logrus/v2 v2.0.1 h1:BX2U6uv2N3UiUY75y+SntQak5S1AJIel9j+5Y6h4Nb4= +github.com/neko-neko/echo-logrus/v2 v2.0.1/go.mod h1:GDYWo9CY4VXk/vn5ac5reoutYEkZEexlFI01MzHXVG0= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= @@ -797,6 +827,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= @@ -862,6 +893,9 @@ github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX github.com/urfave/cli/v2 v2.10.2 h1:x3p8awjp/2arX+Nl/G2040AZpOCHS/eMJJ1/a+mye4Y= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/vishvananda/netlink v0.0.0-20181108222139-023a6dafdcdf/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk= @@ -915,6 +949,7 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= golang.org/x/crypto v0.0.0-20171113213409-9f005a07e0d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181009213950-7c1a557ab941/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -923,6 +958,7 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -1042,6 +1078,7 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1149,8 +1186,6 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs= -golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 h1:ftMN5LMiBFjbzleLqtoBZk7KdJwhuybIU+FckUHgoyQ= golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -1298,7 +1333,6 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= -google.golang.org/grpc v1.47.0 h1:9n77onPX5F3qfFCqjy9dhn8PbNQsIKeVU04J9G7umt8= google.golang.org/grpc v1.47.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= google.golang.org/grpc v1.48.0 h1:rQOsyJ/8+ufEDJd/Gdsz7HG220Mh9HAhFHRGnIjda0w= google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= diff --git a/packages/relayer/.default.env b/packages/relayer/.default.env index 39442131702..ecdfd7843b7 100644 --- a/packages/relayer/.default.env +++ b/packages/relayer/.default.env @@ -6,9 +6,9 @@ MYSQL_DATABASE=relayer MYSQL_HOST=localhost:3306 RELAYER_ECDSA_KEY= L1_BRIDGE_ADDRESS=0xB12d6112D64B213880Fa53F815aF1F29c91CaCe9 -L2_BRIDGE_ADDRESS=0x4eA05A0f7713333AeB4bB73F17aEeFE146CF13E3 +L2_BRIDGE_ADDRESS=0xA53a0f12879Aba509A24D2e5C959a36cdD18F9d0 L1_TAIKO_ADDRESS=0x9b557777Be33A8A2fE6aF93E017A0d139B439E5D -L2_TAIKO_ADDRESS=0x0027f309f7F94A8Efb6A3DBfb30827f1062803F4 +L2_TAIKO_ADDRESS=0x80EDBd3f3618b348526F0970AEAFA52b6f204449 L1_RPC_URL=ws://34.132.67.34:8546 L2_RPC_URL=ws://ws.a1.testnet.taiko.xyz BLOCK_BATCH_SIZE=2 diff --git a/packages/relayer/cli/cli.go b/packages/relayer/cli/cli.go index 095a0864c16..6b026377d29 100644 --- a/packages/relayer/cli/cli.go +++ b/packages/relayer/cli/cli.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strconv" + "strings" "time" "github.com/ethereum/go-ethereum/common" @@ -80,9 +81,7 @@ func Run(mode relayer.Mode, watchMode relayer.WatchMode, layer relayer.Layer) { log.Fatal(err) } - srv, err := http.NewServer(http.NewServerOpts{ - Echo: echo.New(), - }) + srv, err := newHTTPServer(db) if err != nil { log.Fatal(err) } @@ -318,3 +317,21 @@ func loadAndValidateEnv() error { return errors.Errorf("Missing env vars: %v", missing) } + +func newHTTPServer(db relayer.DB) (*http.Server, error) { + eventRepo, err := repo.NewEventRepository(db) + if err != nil { + return nil, err + } + + srv, err := http.NewServer(http.NewServerOpts{ + EventRepo: eventRepo, + Echo: echo.New(), + CorsOrigins: strings.Split(os.Getenv("CORS_ORIGINS"), ","), + }) + if err != nil { + return nil, err + } + + return srv, nil +} diff --git a/packages/relayer/cli/cli_test.go b/packages/relayer/cli/cli_test.go index c6b0f919d26..1f762cee220 100644 --- a/packages/relayer/cli/cli_test.go +++ b/packages/relayer/cli/cli_test.go @@ -217,3 +217,21 @@ func Test_makeIndexers(t *testing.T) { }) } } + +func Test_newHTTPServer(t *testing.T) { + db, cancel, err := testMysql(t) + if err != nil { + t.Fatal(err) + } + + defer cancel() + + srv, err := newHTTPServer(db) + assert.Nil(t, err) + assert.NotNil(t, srv) +} + +func Test_newHTTPServer_nilDB(t *testing.T) { + _, err := newHTTPServer(nil) + assert.NotNil(t, err) +} diff --git a/packages/relayer/errors.go b/packages/relayer/errors.go index 3c0f0a83303..db7911e06b4 100644 --- a/packages/relayer/errors.go +++ b/packages/relayer/errors.go @@ -11,10 +11,11 @@ var ( "ERR_NO_BLOCK_REPOSITORY", "BlockRepository is required", ) - ErrNoProver = errors.Validation.NewWithKeyAndDetail("ERR_NO_PROVER", "Prover is required") - ErrNoRPCClient = errors.Validation.NewWithKeyAndDetail("ERR_NO_RPC_CLIENT", "RPCClient is required") - ErrNoBridge = errors.Validation.NewWithKeyAndDetail("ERR_NO_BRIDGE", "Bridge is required") - ErrNoTaikoL2 = errors.Validation.NewWithKeyAndDetail("ERR_NO_TAIKO_L2", "TaikoL2 is required") + ErrNoCORSOrigins = errors.Validation.NewWithKeyAndDetail("ERR_NO_CORS_ORIGINS", "CORS Origins are required") + ErrNoProver = errors.Validation.NewWithKeyAndDetail("ERR_NO_PROVER", "Prover is required") + ErrNoRPCClient = errors.Validation.NewWithKeyAndDetail("ERR_NO_RPC_CLIENT", "RPCClient is required") + ErrNoBridge = errors.Validation.NewWithKeyAndDetail("ERR_NO_BRIDGE", "Bridge is required") + ErrNoTaikoL2 = errors.Validation.NewWithKeyAndDetail("ERR_NO_TAIKO_L2", "TaikoL2 is required") ErrInvalidConfirmations = errors.Validation.NewWithKeyAndDetail( "ERR_INVALID_CONFIRMATIONS", diff --git a/packages/relayer/event.go b/packages/relayer/event.go index bf27aabf5f6..374a679ee94 100644 --- a/packages/relayer/event.go +++ b/packages/relayer/event.go @@ -1,8 +1,10 @@ package relayer import ( + "context" "math/big" + "github.com/ethereum/go-ethereum/common" "gorm.io/datatypes" ) @@ -47,6 +49,7 @@ type SaveEventOpts struct { // EventRepository is used to interact with events in the store type EventRepository interface { - Save(opts SaveEventOpts) (*Event, error) - UpdateStatus(id int, status EventStatus) error + Save(ctx context.Context, opts SaveEventOpts) (*Event, error) + UpdateStatus(ctx context.Context, id int, status EventStatus) error + FindAllByAddress(ctx context.Context, chainID *big.Int, address common.Address) ([]*Event, error) } diff --git a/packages/relayer/http/get_events_by_address.go b/packages/relayer/http/get_events_by_address.go new file mode 100644 index 00000000000..580793e910b --- /dev/null +++ b/packages/relayer/http/get_events_by_address.go @@ -0,0 +1,28 @@ +package http + +import ( + "errors" + "html" + "math/big" + "net/http" + + "github.com/cyberhorsey/webutils" + "github.com/ethereum/go-ethereum/common" + "github.com/labstack/echo/v4" +) + +func (srv *Server) GetEventsByAddress(c echo.Context) error { + chainID, ok := new(big.Int).SetString(c.QueryParam("chainID"), 10) + if !ok { + return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, errors.New("invalid chain id")) + } + + address := html.EscapeString(c.QueryParam("address")) + + events, err := srv.eventRepo.FindAllByAddress(c.Request().Context(), chainID, common.HexToAddress(address)) + if err != nil { + return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err) + } + + return c.JSON(http.StatusOK, events) +} diff --git a/packages/relayer/http/get_events_by_address_test.go b/packages/relayer/http/get_events_by_address_test.go new file mode 100644 index 00000000000..4121dc674aa --- /dev/null +++ b/packages/relayer/http/get_events_by_address_test.go @@ -0,0 +1,71 @@ +package http + +import ( + "context" + "fmt" + "math/big" + "net/http" + "net/http/httptest" + "testing" + + "github.com/cyberhorsey/webutils/testutils" + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "github.com/taikoxyz/taiko-mono/packages/relayer" +) + +func Test_GetEventsByAddress(t *testing.T) { + srv := newTestServer("") + + _, err := srv.eventRepo.Save(context.Background(), relayer.SaveEventOpts{ + Name: "name", + Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`, + ChainID: big.NewInt(167001), + Status: relayer.EventStatusNew, + }) + + assert.Equal(t, nil, err) + + tests := []struct { + name string + address string + chainID string + wantStatus int + wantBodyRegexpMatches []string + }{ + { + "successEmptyList", + "0x456", + "167001", + http.StatusOK, + []string{`\[\]`}, + }, + { + "success", + "0x0000000000000000000000000000000000000123", + "167001", + http.StatusOK, + []string{`[{"id":780800018316137516,"name":"name", + "data":{"Owner":"0x0000000000000000000000000000000000000123"},"status":0,"chainID":167001}]`}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := testutils.NewUnauthenticatedRequest( + echo.GET, + fmt.Sprintf("/events?address=%v&chainID=%v", + tt.address, + tt.chainID), + + nil, + ) + + rec := httptest.NewRecorder() + + srv.ServeHTTP(rec, req) + + testutils.AssertStatusAndBody(t, rec, tt.wantStatus, tt.wantBodyRegexpMatches) + }) + } +} diff --git a/packages/relayer/http/routes.go b/packages/relayer/http/routes.go index 0b416279db1..6effc11542e 100644 --- a/packages/relayer/http/routes.go +++ b/packages/relayer/http/routes.go @@ -2,4 +2,6 @@ package http func (srv *Server) configureRoutes() { srv.echo.GET("/healthz", srv.Health) + + srv.echo.GET("/events", srv.GetEventsByAddress) } diff --git a/packages/relayer/http/server.go b/packages/relayer/http/server.go index 8df99750f60..85171f087c7 100644 --- a/packages/relayer/http/server.go +++ b/packages/relayer/http/server.go @@ -7,17 +7,20 @@ import ( "os" "github.com/labstack/echo/v4/middleware" + "github.com/taikoxyz/taiko-mono/packages/relayer" echoprom "github.com/labstack/echo-contrib/prometheus" echo "github.com/labstack/echo/v4" ) type Server struct { - echo *echo.Echo + echo *echo.Echo + eventRepo relayer.EventRepository } type NewServerOpts struct { Echo *echo.Echo + EventRepo relayer.EventRepository CorsOrigins []string } @@ -26,6 +29,14 @@ func (opts NewServerOpts) Validate() error { return ErrNoHTTPFramework } + if opts.EventRepo == nil { + return relayer.ErrNoEventRepository + } + + if opts.CorsOrigins == nil { + return relayer.ErrNoCORSOrigins + } + return nil } @@ -35,7 +46,8 @@ func NewServer(opts NewServerOpts) (*Server, error) { } srv := &Server{ - echo: opts.Echo, + echo: opts.Echo, + eventRepo: opts.EventRepo, } corsOrigins := opts.CorsOrigins @@ -43,6 +55,7 @@ func NewServer(opts NewServerOpts) (*Server, error) { corsOrigins = []string{"*"} } + srv.configureRoutes() srv.configureMiddleware(corsOrigins) return srv, nil diff --git a/packages/relayer/http/server_test.go b/packages/relayer/http/server_test.go index 6e05bbcbf67..471b9b7d011 100644 --- a/packages/relayer/http/server_test.go +++ b/packages/relayer/http/server_test.go @@ -9,13 +9,17 @@ import ( "github.com/joho/godotenv" echo "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" + "github.com/taikoxyz/taiko-mono/packages/relayer" + "github.com/taikoxyz/taiko-mono/packages/relayer/mock" + "github.com/taikoxyz/taiko-mono/packages/relayer/repo" ) func newTestServer(url string) *Server { _ = godotenv.Load("../.test.env") srv := &Server{ - echo: echo.New(), + echo: echo.New(), + eventRepo: mock.NewEventRepository(), } srv.configureMiddleware([]string{"*"}) @@ -35,20 +39,31 @@ func Test_NewServer(t *testing.T) { "success", NewServerOpts{ Echo: echo.New(), + EventRepo: &repo.EventRepository{}, CorsOrigins: make([]string, 0), }, nil, }, + { + "noEventRepo", + NewServerOpts{ + Echo: echo.New(), + CorsOrigins: make([]string, 0), + }, + relayer.ErrNoEventRepository, + }, { "noCorsOrigins", NewServerOpts{ - Echo: echo.New(), + Echo: echo.New(), + EventRepo: &repo.EventRepository{}, }, - nil, + relayer.ErrNoCORSOrigins, }, { "noHttpFramework", NewServerOpts{ + EventRepo: &repo.EventRepository{}, CorsOrigins: make([]string, 0), }, ErrNoHTTPFramework, diff --git a/packages/relayer/indexer/handle_event.go b/packages/relayer/indexer/handle_event.go index 81640e191b7..c356cd2ed31 100644 --- a/packages/relayer/indexer/handle_event.go +++ b/packages/relayer/indexer/handle_event.go @@ -38,7 +38,7 @@ func (svc *Service) handleEvent( return errors.Wrap(err, "json.Marshal(event)") } - e, err := svc.eventRepo.Save(relayer.SaveEventOpts{ + e, err := svc.eventRepo.Save(ctx, relayer.SaveEventOpts{ Name: eventName, Data: string(marshaled), ChainID: chainID, diff --git a/packages/relayer/message/process_message.go b/packages/relayer/message/process_message.go index ba6fb4145e0..abfd6e5fa73 100644 --- a/packages/relayer/message/process_message.go +++ b/packages/relayer/message/process_message.go @@ -100,7 +100,7 @@ func (p *Processor) ProcessMessage( log.Infof("updating message status to: %v", relayer.EventStatus(messageStatus).String()) // update message status - if err := p.eventRepo.UpdateStatus(e.ID, relayer.EventStatus(messageStatus)); err != nil { + if err := p.eventRepo.UpdateStatus(ctx, e.ID, relayer.EventStatus(messageStatus)); err != nil { return errors.Wrap(err, "s.eventRepo.UpdateStatus") } diff --git a/packages/relayer/mock/event_repository.go b/packages/relayer/mock/event_repository.go index 89b2e4fda36..3145a81497e 100644 --- a/packages/relayer/mock/event_repository.go +++ b/packages/relayer/mock/event_repository.go @@ -1,16 +1,89 @@ package mock import ( + "context" + "encoding/json" + "math/big" + "math/rand" + + "github.com/ethereum/go-ethereum/common" "github.com/taikoxyz/taiko-mono/packages/relayer" + "gorm.io/datatypes" ) type EventRepository struct { + events []*relayer.Event +} + +func NewEventRepository() *EventRepository { + return &EventRepository{ + events: make([]*relayer.Event, 0), + } } +func (r *EventRepository) Save(ctx context.Context, opts relayer.SaveEventOpts) (*relayer.Event, error) { + r.events = append(r.events, &relayer.Event{ + ID: rand.Int(), // nolint: gosec + Data: datatypes.JSON(opts.Data), + Status: opts.Status, + ChainID: opts.ChainID.Int64(), + Name: opts.Name, + }) -func (r *EventRepository) Save(opts relayer.SaveEventOpts) (*relayer.Event, error) { return nil, nil } -func (r *EventRepository) UpdateStatus(id int, status relayer.EventStatus) error { +func (r *EventRepository) UpdateStatus(ctx context.Context, id int, status relayer.EventStatus) error { + var event *relayer.Event + + var index int + + for i, e := range r.events { + if e.ID == id { + event = e + index = i + + break + } + } + + if event == nil { + return nil + } + + event.Status = status + + r.events[index] = event + return nil } + +func (r *EventRepository) FindAllByAddress( + ctx context.Context, + chainID *big.Int, + address common.Address, +) ([]*relayer.Event, error) { + type d struct { + Owner string `json:"Owner"` + } + + events := make([]*relayer.Event, 0) + + for _, e := range r.events { + m, err := e.Data.MarshalJSON() + if err != nil { + return nil, err + } + + data := &d{} + if err := json.Unmarshal(m, data); err != nil { + return nil, err + } + + if data.Owner == address.Hex() { + events = append(events, e) + break + } + } + + return events, nil +} diff --git a/packages/relayer/repo/event.go b/packages/relayer/repo/event.go index 3caa91f89ad..13eedbc4834 100644 --- a/packages/relayer/repo/event.go +++ b/packages/relayer/repo/event.go @@ -1,6 +1,7 @@ package repo import ( + "context" "math/big" "github.com/ethereum/go-ethereum/common" @@ -23,7 +24,7 @@ func NewEventRepository(db relayer.DB) (*EventRepository, error) { }, nil } -func (r *EventRepository) Save(opts relayer.SaveEventOpts) (*relayer.Event, error) { +func (r *EventRepository) Save(ctx context.Context, opts relayer.SaveEventOpts) (*relayer.Event, error) { e := &relayer.Event{ Data: datatypes.JSON(opts.Data), Status: opts.Status, @@ -37,7 +38,7 @@ func (r *EventRepository) Save(opts relayer.SaveEventOpts) (*relayer.Event, erro return e, nil } -func (r *EventRepository) UpdateStatus(id int, status relayer.EventStatus) error { +func (r *EventRepository) UpdateStatus(ctx context.Context, id int, status relayer.EventStatus) error { e := &relayer.Event{} if err := r.db.GormDB().Where("id = ?", id).First(e).Error; err != nil { return errors.Wrap(err, "r.db.First") @@ -51,7 +52,11 @@ func (r *EventRepository) UpdateStatus(id int, status relayer.EventStatus) error return nil } -func (r *EventRepository) FindAllByAddress(chainID *big.Int, address common.Address) ([]*relayer.Event, error) { +func (r *EventRepository) FindAllByAddress( + ctx context.Context, + chainID *big.Int, + address common.Address, +) ([]*relayer.Event, error) { e := make([]*relayer.Event, 0) if err := r.db.GormDB().Where("chain_id = ?", chainID.Int64()). Find(&e, datatypes.JSONQuery("data"). diff --git a/packages/relayer/repo/event_test.go b/packages/relayer/repo/event_test.go index ebdb69d80d7..dfdd98f081d 100644 --- a/packages/relayer/repo/event_test.go +++ b/packages/relayer/repo/event_test.go @@ -1,6 +1,7 @@ package repo import ( + "context" "fmt" "math/big" "testing" @@ -64,7 +65,7 @@ func TestIntegration_Event_Save(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err = eventRepo.Save(tt.opts) + _, err = eventRepo.Save(context.Background(), tt.opts) assert.Equal(t, tt.wantErr, err) }) } @@ -102,7 +103,7 @@ func TestIntegration_Event_UpdateStatus(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.name == "success" { - _, err := eventRepo.Save( + _, err := eventRepo.Save(context.Background(), relayer.SaveEventOpts{ Name: "test", ChainID: big.NewInt(1), @@ -111,7 +112,7 @@ func TestIntegration_Event_UpdateStatus(t *testing.T) { ) assert.Equal(t, nil, err) } - err := eventRepo.UpdateStatus(tt.id, tt.status) + err := eventRepo.UpdateStatus(context.Background(), tt.id, tt.status) assert.Equal(t, tt.wantErr, err != nil) }) } @@ -128,7 +129,7 @@ func TestIntegration_Event_FindAllByAddress(t *testing.T) { addr := common.HexToAddress("0x71C7656EC7ab88b098defB751B7401B5f6d8976F") - _, err = eventRepo.Save(relayer.SaveEventOpts{ + _, err = eventRepo.Save(context.Background(), relayer.SaveEventOpts{ Name: "name", Data: fmt.Sprintf(`{"Owner":"%s"}`, addr.Hex()), ChainID: big.NewInt(1), @@ -175,7 +176,7 @@ func TestIntegration_Event_FindAllByAddress(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - resp, err := eventRepo.FindAllByAddress(tt.chainID, tt.address) + resp, err := eventRepo.FindAllByAddress(context.Background(), tt.chainID, tt.address) assert.Equal(t, tt.wantResp, resp) assert.Equal(t, tt.wantErr, err) })