From 3ffb91adadf95bbe909e8769d5c9285ddc1d1807 Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Thu, 10 Oct 2024 20:07:13 -0300 Subject: [PATCH] feat(claimer): WIP rework of claimer in GO --- .gitignore | 1 + Makefile | 3 +- cmd/cartesi-rollups-claimer/main.go | 18 ++++ cmd/cartesi-rollups-claimer/root/root.go | 46 ++++++++ go.mod | 14 +++ go.sum | 38 +++++++ internal/claimer/service.go | 125 ++++++++++++++++++++++ internal/node/config/config.go | 10 +- internal/node/config/generate/Config.toml | 5 + internal/node/config/generated.go | 12 +++ internal/repository/claimer.go | 79 ++++++++++++++ 11 files changed, 346 insertions(+), 5 deletions(-) create mode 100644 cmd/cartesi-rollups-claimer/main.go create mode 100644 cmd/cartesi-rollups-claimer/root/root.go create mode 100644 internal/claimer/service.go create mode 100644 internal/repository/claimer.go diff --git a/.gitignore b/.gitignore index c3e06ccc4..2ebbfb217 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ machine-snapshot/** /deployment.json /anvil_state.json /cartesi-rollups-authority-claimer +/cartesi-rollups-claimer /cartesi-rollups-cli /cartesi-rollups-evm-reader /cartesi-rollups-node diff --git a/Makefile b/Makefile index b5edc9309..c519822de 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ CLAIMER := cmd/authority-claimer/target/$(BUILD_TYPE)/cartesi-rollups-authority- RUST_ARTIFACTS := $(CLAIMER) # Go artifacts -GO_ARTIFACTS := cartesi-rollups-node cartesi-rollups-cli cartesi-rollups-evm-reader cartesi-rollups-advancer cartesi-rollups-validator +GO_ARTIFACTS := cartesi-rollups-node cartesi-rollups-cli cartesi-rollups-evm-reader cartesi-rollups-advancer cartesi-rollups-validator cartesi-rollups-claimer # fixme(vfusco): path on all oses CGO_CFLAGS:= -I$(PREFIX)/include @@ -78,6 +78,7 @@ env: @echo export CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER="10" @echo export CARTESI_AUTH_MNEMONIC="test test test test test test test test test test test junk" @echo export CARTESI_POSTGRES_ENDPOINT="postgres://postgres:password@localhost:5432/rollupsdb?sslmode=disable" + @echo export CARTESI_FEATURE_CLAIMER_ENABLED=false @echo export CARTESI_TEST_POSTGRES_ENDPOINT="postgres://test_user:password@localhost:5432/test_rollupsdb?sslmode=disable" @echo export CARTESI_TEST_MACHINE_IMAGES_PATH=\"$(CARTESI_TEST_MACHINE_IMAGES_PATH)\" diff --git a/cmd/cartesi-rollups-claimer/main.go b/cmd/cartesi-rollups-claimer/main.go new file mode 100644 index 000000000..b4d82c6cf --- /dev/null +++ b/cmd/cartesi-rollups-claimer/main.go @@ -0,0 +1,18 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package main + +import ( + "os" + + "github.com/cartesi/rollups-node/cmd/cartesi-rollups-claimer/root" +) + +func main() { + err := root.Cmd.Execute() + if err != nil { + os.Exit(1) + } +} + diff --git a/cmd/cartesi-rollups-claimer/root/root.go b/cmd/cartesi-rollups-claimer/root/root.go new file mode 100644 index 000000000..af9c3e77b --- /dev/null +++ b/cmd/cartesi-rollups-claimer/root/root.go @@ -0,0 +1,46 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package root + +import ( + "context" + "time" + + "github.com/cartesi/rollups-node/internal/claimer" + "github.com/cartesi/rollups-node/internal/node/config" + + "github.com/spf13/cobra" +) + +var ( + // Should be overridden during the final release build with ldflags + // to contain the actual version number + buildVersion = "devel" +) + +const ( + CMD_NAME = "authority-claimer2" +) + +var Cmd = &cobra.Command{ + Use: CMD_NAME, + Short: "Runs Authority Claimer", + Long: `Runs Authority Claimer in standalone mode`, + Run: run, +} + +func run(cmd *cobra.Command, args []string) { + ctx := context.Background() + c := config.FromEnv() + + claimer, err := claimer.Create(&claimer.CreateInfo{ + Auth: c.Auth, + BlockchainHttpEndpoint: c.BlockchainHttpEndpoint, + PostgresEndpoint: c.PostgresEndpoint, + Context: ctx, + PollInterval: 1000 * time.Millisecond, + }) + cobra.CheckErr(err) + claimer.Start() +} diff --git a/go.mod b/go.mod index a0b5ddf51..cb2af6f75 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,9 @@ require github.com/BurntSushi/toml v1.4.0 require ( github.com/Khan/genqlient v0.7.0 + github.com/aws/aws-sdk-go-v2 v1.32.2 + github.com/aws/aws-sdk-go-v2/config v1.18.45 + github.com/aws/aws-sdk-go-v2/service/kms v1.37.2 github.com/deepmap/oapi-codegen/v2 v2.1.0 github.com/golang-migrate/migrate/v4 v4.17.1 github.com/jackc/pgx/v5 v5.6.0 @@ -32,6 +35,16 @@ require ( github.com/alexflint/go-arg v1.4.3 // indirect github.com/alexflint/go-scalar v1.2.0 // indirect github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.13.43 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.21 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.21 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.3.45 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.37 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.15.2 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.17.3 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.23.2 // indirect + github.com/aws/smithy-go v1.22.0 // indirect github.com/bits-and-blooms/bitset v1.13.0 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect github.com/consensys/bavard v0.1.13 // indirect @@ -60,6 +73,7 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/lib/pq v1.10.9 // indirect diff --git a/go.sum b/go.sum index e5e5990d0..44538a23a 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,36 @@ github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7D github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE= +github.com/aws/aws-sdk-go-v2 v1.21.2/go.mod h1:ErQhvNuEMhJjweavOYhxVkn2RUx7kQXVATHrjKtxIpM= +github.com/aws/aws-sdk-go-v2 v1.32.2 h1:AkNLZEyYMLnx/Q/mSKkcMqwNFXMAvFto9bNsHqcTduI= +github.com/aws/aws-sdk-go-v2 v1.32.2/go.mod h1:2SK5n0a2karNTv5tbP1SjsX0uhttou00v/HpXKM1ZUo= +github.com/aws/aws-sdk-go-v2/config v1.18.45 h1:Aka9bI7n8ysuwPeFdm77nfbyHCAKQ3z9ghB3S/38zes= +github.com/aws/aws-sdk-go-v2/config v1.18.45/go.mod h1:ZwDUgFnQgsazQTnWfeLWk5GjeqTQTL8lMkoE1UXzxdE= +github.com/aws/aws-sdk-go-v2/credentials v1.13.43 h1:LU8vo40zBlo3R7bAvBVy/ku4nxGEyZe9N8MqAeFTzF8= +github.com/aws/aws-sdk-go-v2/credentials v1.13.43/go.mod h1:zWJBz1Yf1ZtX5NGax9ZdNjhhI4rgjfgsyk6vTY1yfVg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13 h1:PIktER+hwIG286DqXyvVENjgLTAwGgoeriLDD5C+YlQ= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13/go.mod h1:f/Ib/qYjhV2/qdsf79H3QP/eRE4AkVyEf6sk7XfZ1tg= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.43/go.mod h1:auo+PiyLl0n1l8A0e8RIeR8tOzYPfZZH/JNlrJ8igTQ= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.21 h1:UAsR3xA31QGf79WzpG/ixT9FZvQlh5HY1NRqSHBNOCk= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.21/go.mod h1:JNr43NFf5L9YaG3eKTm7HQzls9J+A9YYcGI5Quh1r2Y= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.37/go.mod h1:Qe+2KtKml+FEsQF/DHmDV+xjtche/hwoF75EG4UlHW8= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.21 h1:6jZVETqmYCadGFvrYEQfC5fAQmlo80CeL5psbno6r0s= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.21/go.mod h1:1SR0GbLlnN3QUmYaflZNiH1ql+1qrSiB2vwcJ+4UM60= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.45 h1:hze8YsjSh8Wl1rYa1CJpRmXP21BvOBuc76YhW0HsuQ4= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.45/go.mod h1:lD5M20o09/LCuQ2mE62Mb/iSdSlCNuj6H5ci7tW7OsE= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.37 h1:WWZA/I2K4ptBS1kg0kV1JbBtG/umed0vwHRrmcr9z7k= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.37/go.mod h1:vBmDnwWXWxNPFRMmG2m/3MKOe+xEcMDo1tanpaWCcck= +github.com/aws/aws-sdk-go-v2/service/kms v1.37.2 h1:tfBABi5R6aSZlhgTWHxL+opYUDOnIGoNcJLwVYv0jLM= +github.com/aws/aws-sdk-go-v2/service/kms v1.37.2/go.mod h1:dZYFcQwuoh+cLOlFnZItijZptmyDhRIkOKWFO1CfzV8= +github.com/aws/aws-sdk-go-v2/service/sso v1.15.2 h1:JuPGc7IkOP4AaqcZSIcyqLpFSqBWK32rM9+a1g6u73k= +github.com/aws/aws-sdk-go-v2/service/sso v1.15.2/go.mod h1:gsL4keucRCgW+xA85ALBpRFfdSLH4kHOVSnLMSuBECo= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.17.3 h1:HFiiRkf1SdaAmV3/BHOFZ9DjFynPHj8G/UIO1lQS+fk= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.17.3/go.mod h1:a7bHA82fyUXOm+ZSWKU6PIoBxrjSprdLoM8xPYvzYVg= +github.com/aws/aws-sdk-go-v2/service/sts v1.23.2 h1:0BkLfgeDjfZnZ+MhB3ONb01u9pwFYTCZVhlsSSBvlbU= +github.com/aws/aws-sdk-go-v2/service/sts v1.23.2/go.mod h1:Eows6e1uQEsc4ZaHANmsPRzAKcVDrcmjjWiih2+HUUQ= +github.com/aws/smithy-go v1.15.0/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= +github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM= +github.com/aws/smithy-go v1.22.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= @@ -134,6 +164,9 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk= github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= @@ -170,6 +203,10 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= @@ -330,6 +367,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/claimer/service.go b/internal/claimer/service.go new file mode 100644 index 000000000..205e29e4f --- /dev/null +++ b/internal/claimer/service.go @@ -0,0 +1,125 @@ +package claimer + +import ( + "context" + "fmt" + "math/big" + "time" + + signtx "github.com/cartesi/rollups-node/internal/aws-kms-signtx" + conf "github.com/cartesi/rollups-node/internal/node/config" + repo "github.com/cartesi/rollups-node/internal/repository" + "github.com/cartesi/rollups-node/pkg/contracts/iconsensus" + "github.com/cartesi/rollups-node/pkg/ethutil" + + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethclient" + + "github.com/aws/aws-sdk-go-v2/aws" + aws_cfg "github.com/aws/aws-sdk-go-v2/config" + aws_kms "github.com/aws/aws-sdk-go-v2/service/kms" +) + +type CreateInfo struct { + Auth conf.Auth + BlockchainHttpEndpoint conf.Redacted[string] + Context context.Context + EthConn ethclient.Client + PollInterval time.Duration + PostgresEndpoint conf.Redacted[string] +} + +type Service struct { + consensus iconsensus.IConsensus + context context.Context + dbConn *repo.Database + ethConn *ethclient.Client + signer *bind.TransactOpts + ticker *time.Ticker +} + +func createSigner(ctx context.Context, auth conf.Auth, chainID *big.Int) (*bind.TransactOpts, error) { + switch auth := auth.(type) { + case conf.AuthMnemonic: + privateKey, err := ethutil.MnemonicToPrivateKey( + auth.Mnemonic.Value, uint32(auth.AccountIndex.Value)) + if err != nil { + return nil, err + } + return bind.NewKeyedTransactorWithChainID(privateKey, chainID) + case conf.AuthAWS: + awsc, err := aws_cfg.LoadDefaultConfig(ctx) + if err != nil { + return nil, err + } + kms := aws_kms.NewFromConfig(awsc, func(o *aws_kms.Options) { + o.BaseEndpoint = aws.String(auth.EndpointURL.Value) + }) + return signtx.CreateAWSTransactOpts(ctx, kms, + aws.String(auth.KeyID.Value), types.NewEIP155Signer(chainID)) + } + return nil, fmt.Errorf("error: unimplemented authentication method") +} + +func Create(ci *CreateInfo) (s *Service, err error) { + ethconn, err := ethclient.Dial(ci.BlockchainHttpEndpoint.Value) + if err != nil { + return nil, err + } + + chainid, err := ethconn.NetworkID(ci.Context) + if err != nil { + return nil, err + } + + signer, err := createSigner(ci.Context, ci.Auth, chainid) + if err != nil { + return nil, err + } + + dbconn, err := repo.Connect(ci.Context, ci.PostgresEndpoint.Value) + if err != nil { + return nil, err + } + + return &Service{ + context: ci.Context, + dbConn: dbconn, + ethConn: ethconn, + signer: signer, + ticker: time.NewTicker(ci.PollInterval), + }, nil +} + +func (s *Service) onTick() error { + claims, err := s.dbConn.GetComputedClaims(s.context) + if err != nil { + return err + } + + for _, claim := range(claims) { + // TODO: detect duplicates? + + instance, err := iconsensus.NewIConsensus(claim.IConsensusAddress, s.ethConn) + if err != nil { + return err + } + instance.SubmitClaim(s.signer, claim.AppAddress, claim.LastProcessedBlock, claim.Hash) + s.dbConn.SetClaimAsSubmitted(s.context, claim.ID) + } + return nil +} + +// TODO: implement quit channel +func (s *Service) Start() error { + for { + select { + case <-s.ticker.C: + err := s.onTick() + // TODO: recreate DBConn on error + // TODO: recreate EthConn on error + _ = err + } + } +} diff --git a/internal/node/config/config.go b/internal/node/config/config.go index 2478a2f11..20d5e2e68 100644 --- a/internal/node/config/config.go +++ b/internal/node/config/config.go @@ -61,8 +61,9 @@ type AuthMnemonic struct { // AuthAWS allows signing through AWS services. type AuthAWS struct { - KeyID Redacted[string] - Region Redacted[string] + KeyID Redacted[string] + Region Redacted[string] + EndpointURL Redacted[string] } // Redacted is a wrapper that redacts a given field from the logs. @@ -146,8 +147,9 @@ func authFromEnv() Auth { } case AuthKindAWS: return AuthAWS{ - KeyID: Redacted[string]{GetAuthAwsKmsKeyId()}, - Region: Redacted[string]{GetAuthAwsKmsRegion()}, + KeyID: Redacted[string]{GetAuthAwsKmsKeyId()}, + Region: Redacted[string]{GetAuthAwsKmsRegion()}, + EndpointURL: Redacted[string]{GetAuthAwsEndpointUrl()}, } default: panic("invalid auth kind") diff --git a/internal/node/config/generate/Config.toml b/internal/node/config/generate/Config.toml index 6bf9a90b8..d8ed7e5cb 100644 --- a/internal/node/config/generate/Config.toml +++ b/internal/node/config/generate/Config.toml @@ -191,6 +191,11 @@ An AWS KMS Region. Must be set alongside `CARTESI_AUTH_AWS_KMS_KEY_ID`.""" +[auth.CARTESI_AUTH_AWS_ENDPOINT_URL] +go-type = "string" +description = """ +An AWS Endpoint URL.""" + # # Postgres # diff --git a/internal/node/config/generated.go b/internal/node/config/generated.go index 8c58c04aa..6050e1bae 100644 --- a/internal/node/config/generated.go +++ b/internal/node/config/generated.go @@ -120,6 +120,18 @@ var ( // Getters // ------------------------------------------------------------------------------------------------ +func GetAuthAwsEndpointUrl() string { + s, ok := os.LookupEnv("CARTESI_AUTH_AWS_ENDPOINT_URL") + if !ok { + panic("missing env var CARTESI_AUTH_AWS_ENDPOINT_URL") + } + val, err := toString(s) + if err != nil { + panic(fmt.Sprintf("failed to parse CARTESI_AUTH_AWS_ENDPOINT_URL: %v", err)) + } + return val +} + func GetAuthAwsKmsKeyId() string { s, ok := os.LookupEnv("CARTESI_AUTH_AWS_KMS_KEY_ID") if !ok { diff --git a/internal/repository/claimer.go b/internal/repository/claimer.go new file mode 100644 index 000000000..e2458a483 --- /dev/null +++ b/internal/repository/claimer.go @@ -0,0 +1,79 @@ +package repository + +import ( + "context" + "math/big" + // "errors" + // + . "github.com/cartesi/rollups-node/internal/node/model" + //"github.com/ethereum/go-ethereum/accounts/abi/bind" + + "github.com/jackc/pgx/v5" +) + +type ComputedClaims struct { + ID uint64 + Hash Hash + AppAddress Address + IConsensusAddress Address + LastProcessedBlock*big.Int +} + +func (pg *Database) GetComputedClaims(ctx context.Context) ([]ComputedClaims, error) { + query := ` + SELECT + epoch.id + epoch.claim_hash + application.contract_address + application.iconsensus_address + application.last_processed_block + FROM + epoch + INNER JOIN + application + WHERE + epoch.application_address = application.contract_address AND + epoch.status = CLAIM_COMPUTED + ORDER BY + application.iconsensus_address ASC, epoch.index ASC, epoch.id ASC` + + args := pgx.NamedArgs{} + rows, err := pg.db.Query(ctx, query, args) + if err != nil { + return nil, err + } + + var data ComputedClaims + scans := []any{ + &data.ID, + &data.Hash, + &data.AppAddress, + &data.IConsensusAddress, + &data.LastProcessedBlock, + } + + var results []ComputedClaims + _, err = pgx.ForEachRow(rows, scans, func() error { + results = append(results, data) + return nil + }) + return results, err +} + +func (pg *Database) SetClaimAsSubmitted(ctx context.Context, id uint64) (error) { + var block uint64 + + query := ` + UPDATE + epoch + SET + epoch.status = CLAIM_SUBMITTED + WHERE + epoch.id=@id` + + args := pgx.NamedArgs{ + "id": id, + } + + return pg.db.QueryRow(ctx, query, args).Scan(&block) +}