From 7ff0bf55ab9197728b506815ae083e60f368fe6e Mon Sep 17 00:00:00 2001 From: Jordan Krage Date: Mon, 13 Nov 2023 19:15:05 -0600 Subject: [PATCH] pkg/cosmos/txm: use sqlutil.Transact helper --- go.mod | 2 +- go.sum | 4 ++-- integration-tests/go.mod | 2 +- integration-tests/go.sum | 4 ++-- pkg/cosmos/txm/orm.go | 46 ++++++---------------------------------- 5 files changed, 13 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index a68f9750..cd18a357 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.17.0 github.com/shopspring/decimal v1.3.1 - github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 + github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231114165715-7689cd229357 github.com/smartcontractkit/libocr v0.0.0-20230925165524-ffa38fe11ef8 github.com/stretchr/testify v1.8.4 github.com/tidwall/gjson v1.16.0 diff --git a/go.sum b/go.sum index 2181282d..32a66910 100644 --- a/go.sum +++ b/go.sum @@ -674,8 +674,8 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 h1:CElKhWq0WIa9Rmg5Ssajs5Hp3m3u/nYIQdXtpj2gbcc= -github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ= +github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231114165715-7689cd229357 h1:r1waaZ6ldGCkUYULdOKBbt1Z90FV6T2euJOd8Y2+I4U= +github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231114165715-7689cd229357/go.mod h1:rOayi690YxLlkQy959PD8INhOAIAUi9LoN0G+J/CEf4= github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f h1:hgJif132UCdjo8u43i7iPN1/MFnu49hv7lFGFftCHKU= github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f/go.mod h1:MvMXoufZAtqExNexqi4cjrNYE9MefKddKylxjS+//n0= github.com/smartcontractkit/libocr v0.0.0-20230925165524-ffa38fe11ef8 h1:R9NkVN+1fooUJFsN9zj9gDY1B+zv54zNO785RQZRVfE= diff --git a/integration-tests/go.mod b/integration-tests/go.mod index 015f8ad0..d52b7b5e 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -13,7 +13,7 @@ require ( github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20230913032705-f924d753cc47 github.com/smartcontractkit/chainlink-cosmos/ops v0.0.0-20230615043323-5fe39e28b7ee github.com/smartcontractkit/chainlink-env v0.36.0 - github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 + github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231114165715-7689cd229357 github.com/smartcontractkit/chainlink-testing-framework v1.15.3 github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20230818010150-dc7e03125cd0 github.com/smartcontractkit/chainlink/v2 v2.6.0 diff --git a/integration-tests/go.sum b/integration-tests/go.sum index 26c5a80d..d699488f 100644 --- a/integration-tests/go.sum +++ b/integration-tests/go.sum @@ -1473,8 +1473,8 @@ github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 h1:T3lFWumv github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M= github.com/smartcontractkit/chainlink-env v0.36.0 h1:CFOjs0c0y3lrHi/fl5qseCH9EQa5W/6CFyOvmhe2VnA= github.com/smartcontractkit/chainlink-env v0.36.0/go.mod h1:NbRExHmJGnKSYXmvNuJx5VErSx26GtE1AEN/CRzYOg8= -github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 h1:CElKhWq0WIa9Rmg5Ssajs5Hp3m3u/nYIQdXtpj2gbcc= -github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ= +github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231114165715-7689cd229357 h1:r1waaZ6ldGCkUYULdOKBbt1Z90FV6T2euJOd8Y2+I4U= +github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231114165715-7689cd229357/go.mod h1:rOayi690YxLlkQy959PD8INhOAIAUi9LoN0G+J/CEf4= github.com/smartcontractkit/chainlink-solana v1.0.3-0.20230831134610-680240b97aca h1:x7M0m512gtXw5Z4B1WJPZ52VgshoIv+IvHqQ8hsH4AE= github.com/smartcontractkit/chainlink-solana v1.0.3-0.20230831134610-680240b97aca/go.mod h1:RIUJXn7EVp24TL2p4FW79dYjyno23x5mjt1nKN+5WEk= github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20230901115736-bbabe542a918 h1:ByVauKFXphRlSNG47lNuxZ9aicu+r8AoNp933VRPpCw= diff --git a/pkg/cosmos/txm/orm.go b/pkg/cosmos/txm/orm.go index 98e1d3c5..b70d93ea 100644 --- a/pkg/cosmos/txm/orm.go +++ b/pkg/cosmos/txm/orm.go @@ -6,65 +6,33 @@ import ( "errors" "fmt" - "github.com/jmoiron/sqlx" + "github.com/smartcontractkit/chainlink-relay/pkg/sqlutil" "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/adapters" "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/db" ) -type DB interface { - BeginTxx(context.Context, *sql.TxOptions) (*sqlx.Tx, error) -} - -type Queryer interface { - GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error - ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) - SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error -} - // ORM manages the data model for cosmos tx management. type ORM struct { chainID string - db Queryer + db sqlutil.Queryer } // NewORM creates an ORM scoped to chainID. -func NewORM(chainID string, db Queryer) *ORM { +func NewORM(chainID string, db sqlutil.Queryer) *ORM { return &ORM{ chainID: chainID, db: db, } } -// TODO this is a simplified version of https://github.com/smartcontractkit/chainlink/blob/5c0ac3879bef46b828d460960702c1587f730909/core/services/pg/transaction.go#L88 -// we should have a common shared impl instead, maybe in relay/pkg/db -// TODO make generic func (o *ORM) Transaction(ctx context.Context, fn func(*ORM) error) (err error) { - db, ok := o.db.(DB) - if !ok { - return fn(NewORM(o.chainID, o.db)) - } - tx, err := db.BeginTxx(ctx, nil) - if err != nil { - return err - } - defer func() { - if p := recover(); p != nil { - _ = tx.Rollback() - panic(p) - } else if err != nil { - if err2 := tx.Rollback(); err2 != nil { - err = errors.Join(err, err2) - } - return - } - err = tx.Commit() - }() - // TODO set timeouts? - err = fn(NewORM(o.chainID, tx)) - return + return sqlutil.Transact(ctx, o.new, o.db, nil, fn) } +// new returns a NewORM like o, but backed by q. +func (o *ORM) new(q sqlutil.Queryer) *ORM { return NewORM(o.chainID, q) } + // InsertMsg inserts a cosmos msg, assumed to be a serialized cosmos ExecuteContractMsg. func (o *ORM) InsertMsg(ctx context.Context, contractID, typeURL string, msg []byte) (int64, error) { var tm adapters.Msg