package core import ( "context" "github.com/matchsystems/werr" "mic-wallet/common" "mic-wallet/server/repository" "mic-wallet/server/repository/entities" ) func NewTxOptsToRepoEntity(opts *common.NewTxOpts) *entities.NewTransactionOpts { return &entities.NewTransactionOpts{ Hash: opts.Hash, BlockId: opts.BlockId, SenderPublicKey: opts.SenderPublicKey, ReceiverPublicKey: opts.ReceiverPublicKey, IsReward: opts.IsReward, Amount: opts.Amount, AmountBurned: common.CalcBurning(opts.Amount), Message: opts.Message, Signature: opts.Signature, CreatedAt: opts.CreatedAt, CreatedAtNs: opts.CreatedAtNs, } } func CommitBlock(ctx context.Context, repo *repository.Repository, block *Block) (err error) { // If micw will be decentralized // this function will be sending blocks to other nodes // and then there will be block-fetcher to see which blocks were accepted to the network if err = repo.AddBLock(ctx, &entities.Block{ Id: block.Id, Hash: block.Hash, PrevHash: block.PrevHash, }); err != nil { return werr.Wrapf(err, "failed to commit block") } repoTxOpts := make([]*entities.NewTransactionOpts, len(block.TXs)) var i int for _, tx := range block.TXs { repoTxOpts[i] = NewTxOptsToRepoEntity(tx) i++ } return werr.Wrapf(repo.AddTransactions(ctx, repoTxOpts), "failed to commit transactions") }