-
Notifications
You must be signed in to change notification settings - Fork 656
/
dbtxmanager.go
33 lines (26 loc) · 950 Bytes
/
dbtxmanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package jsonrpc
import (
"context"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/jackc/pgx/v4"
)
type dbTxManager struct{}
type dbTxScopedFn func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error)
func (f *dbTxManager) NewDbTxScope(st types.StateInterface, scopedFn dbTxScopedFn) (interface{}, types.Error) {
ctx := context.Background()
dbTx, err := st.BeginStateTransaction(ctx)
if err != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to connect to the state", err)
}
v, rpcErr := scopedFn(ctx, dbTx)
if rpcErr != nil {
if txErr := dbTx.Rollback(context.Background()); txErr != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to rollback db transaction", txErr)
}
return v, rpcErr
}
if txErr := dbTx.Commit(context.Background()); txErr != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to commit db transaction", txErr)
}
return v, rpcErr
}