Skip to content

Commit

Permalink
pkg/cosmos/txm: use sqlutil.Transact helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored and cfal committed Nov 14, 2023
1 parent cb1ea90 commit 7ff0bf5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 45 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
46 changes: 7 additions & 39 deletions pkg/cosmos/txm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ff0bf5

Please sign in to comment.