github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/blockchain/api_height.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    11  	"github.com/machinefi/w3bstream/pkg/enums"
    12  	"github.com/machinefi/w3bstream/pkg/errors/status"
    13  	"github.com/machinefi/w3bstream/pkg/models"
    14  	"github.com/machinefi/w3bstream/pkg/types"
    15  )
    16  
    17  type CreateChainHeightReq struct {
    18  	ProjectName string `json:"-"`
    19  	models.ChainHeightInfo
    20  }
    21  
    22  func CreateChainHeight(ctx context.Context, r *CreateChainHeightReq) (*models.ChainHeight, error) {
    23  	d := types.MustMonitorDBExecutorFromContext(ctx)
    24  	idg := confid.MustSFIDGeneratorFromContext(ctx)
    25  
    26  	if err := checkChain(ctx, r.ChainID, r.ChainName); err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	m := &models.ChainHeight{
    31  		RelChainHeight: models.RelChainHeight{ChainHeightID: idg.MustGenSFID()},
    32  		ChainHeightData: models.ChainHeightData{
    33  			ProjectName:     r.ProjectName,
    34  			Uniq:            chainUniqFlag,
    35  			Finished:        datatypes.FALSE,
    36  			ChainHeightInfo: r.ChainHeightInfo,
    37  		},
    38  	}
    39  	if err := m.Create(d); err != nil {
    40  		if sqlx.DBErr(err).IsConflict() {
    41  			return nil, status.ChainHeightConflict
    42  		}
    43  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    44  	}
    45  	return m, nil
    46  }
    47  
    48  func GetChainHeightBySFID(ctx context.Context, id types.SFID) (*models.ChainHeight, error) {
    49  	d := types.MustMonitorDBExecutorFromContext(ctx)
    50  
    51  	m := &models.ChainHeight{RelChainHeight: models.RelChainHeight{ChainHeightID: id}}
    52  	if err := m.FetchByChainHeightID(d); err != nil {
    53  		if sqlx.DBErr(err).IsNotFound() {
    54  			return nil, status.ChainHeightNotFound
    55  		}
    56  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    57  	}
    58  	return m, nil
    59  }
    60  
    61  func ListChainHeightBySFIDs(ctx context.Context, ids []types.SFID) ([]models.ChainHeight, error) {
    62  	d := types.MustMonitorDBExecutorFromContext(ctx)
    63  	m := &models.ChainHeight{}
    64  
    65  	data, err := m.List(d, m.ColChainHeightID().In(ids))
    66  	if err != nil {
    67  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    68  	}
    69  	return data, nil
    70  }
    71  
    72  func RemoveChainHeightBySFID(ctx context.Context, id types.SFID) error {
    73  	d := types.MustMonitorDBExecutorFromContext(ctx)
    74  
    75  	m := &models.ChainHeight{RelChainHeight: models.RelChainHeight{ChainHeightID: id}}
    76  	if err := m.DeleteByChainHeightID(d); err != nil {
    77  		return status.DatabaseError.StatusErr().WithDesc(err.Error())
    78  	}
    79  	return nil
    80  }
    81  
    82  func BatchUpdateChainHeightPausedBySFIDs(ctx context.Context, ids []types.SFID, s datatypes.Bool) error {
    83  	d := types.MustMonitorDBExecutorFromContext(ctx)
    84  	m := &models.ChainHeight{
    85  		ChainHeightData: models.ChainHeightData{
    86  			ChainHeightInfo: models.ChainHeightInfo{
    87  				Paused: s,
    88  			},
    89  		},
    90  	}
    91  
    92  	expr := builder.Update(d.T(m)).Set(
    93  		m.ColPaused().ValueBy(s),
    94  		m.ColUpdatedAt().ValueBy(types.Timestamp{Time: time.Now()}),
    95  	).Where(m.ColChainHeightID().In(ids))
    96  
    97  	if _, err := d.Exec(expr); err != nil {
    98  		return status.DatabaseError.StatusErr().WithDesc(err.Error())
    99  	}
   100  	return nil
   101  }
   102  
   103  func checkChain(ctx context.Context, chainID uint64, chainName enums.ChainName) error {
   104  	cs := types.MustChainConfigFromContext(ctx)
   105  	if _, ok := cs.GetChain(chainID, chainName); !ok {
   106  		return status.BlockchainNotFound
   107  	}
   108  	return nil
   109  }