create method Null safety

BlockModel create(
  1. Uint8List transactionRoot
)

Creates a new block to be commited later.

It gets the last created block from the BlockRepository to extract BlockModel.previousHash from it. If there are no blocks it means that it is the genesis block, i.e, the first block created by the chain and the BlockModel.previousHash will be 0.

This method returns the BlockModel created in-memory. Its return should be used to commit the TransactionModel. After committing all the transactions, commit needs to be called to persist the block into BlockRepository.

Implementation

BlockModel create(Uint8List transactionRoot) {
  BlockModel? lastBlock = _repository.getLast();
  BlockModel block = BlockModel(
      previousHash: lastBlock == null
          ? Uint8List(1)
          : Digest("SHA3-256").process(lastBlock.serialize()),
      transactionRoot: transactionRoot);
  block.id = Digest("SHA3-256").process(block.serialize());
  return block;
}