micw/server/core/commit_block.go

48 lines
1.4 KiB
Go
Raw Normal View History

2024-11-17 21:24:19 +00:00
package core
import (
"context"
2024-11-24 14:04:52 +00:00
"github.com/matchsystems/werr"
"mic-wallet/common"
2024-11-17 21:24:19 +00:00
"mic-wallet/server/repository"
2024-11-24 14:04:52 +00:00
"mic-wallet/server/repository/entities"
2024-11-17 21:24:19 +00:00
)
2024-11-24 14:04:52 +00:00
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")
2024-11-17 21:24:19 +00:00
}