simplify BlockId type parameter

This commit is contained in:
Mark Spanbroek 2024-12-09 11:14:39 +01:00
parent 183890e2ca
commit fc3bdde46e
9 changed files with 20 additions and 20 deletions

View File

@ -6,15 +6,15 @@ type
Block*[Dependencies] = ref object Block*[Dependencies] = ref object
author: CommitteeMember author: CommitteeMember
round: uint64 round: uint64
parents: ImmutableSeq[BlockId[Dependencies]] parents: ImmutableSeq[BlockId[Dependencies.Hash]]
transactions: ImmutableSeq[Dependencies.Transaction] transactions: ImmutableSeq[Dependencies.Transaction]
id: ?BlockId[Dependencies] id: ?BlockId[Dependencies.Hash]
func new*[Dependencies]( func new*[Dependencies](
_: type Block[Dependencies]; _: type Block[Dependencies];
author: CommitteeMember, author: CommitteeMember,
round: uint64, round: uint64,
parents: seq[BlockId[Dependencies]], parents: seq[BlockId[Dependencies.Hash]],
transactions: seq[Dependencies.Transaction] transactions: seq[Dependencies.Transaction]
): auto = ): auto =
Block[Dependencies]( Block[Dependencies](
@ -42,6 +42,6 @@ func id*(blck: Block): auto =
mixin hash mixin hash
let blockBytes = Dependencies.Serialization.toBytes(blck) let blockBytes = Dependencies.Serialization.toBytes(blck)
let blockHash = Dependencies.Hash.hash(blockBytes) let blockHash = Dependencies.Hash.hash(blockBytes)
id = BlockId[Dependencies].new(blck.author, blck.round, blockHash) id = BlockId.new(blck.author, blck.round, blockHash)
blck.id = some id blck.id = some id
id id

View File

@ -1,17 +1,17 @@
import ../committee import ../committee
type BlockId*[Dependencies] = object type BlockId*[Hash] = object
author: CommitteeMember author: CommitteeMember
round: uint64 round: uint64
hash: Dependencies.Hash hash: Hash
func new*[T: BlockId]( func new*[Hash](
_: type T, _: type BlockId,
author: CommitteeMember, author: CommitteeMember,
round: uint64, round: uint64,
hash: T.Dependencies.Hash hash: Hash
): auto = ): auto =
T( BlockId[Hash](
author: author, author: author,
round: round, round: round,
hash: hash hash: hash

View File

@ -89,7 +89,7 @@ proc propose*(validator: Validator, transactions: seq[Validator.Dependencies.Tra
let round = validator.rounds.latest let round = validator.rounds.latest
if round[validator.membership].proposals.len > 0: if round[validator.membership].proposals.len > 0:
return SignedBlock.failure "already proposed this round" return SignedBlock.failure "already proposed this round"
var parents: seq[BlockId[Validator.Dependencies]] var parents: seq[BlockId[Validator.Dependencies.Hash]]
var parentStake: Stake var parentStake: Stake
if previous =? round.previous: if previous =? round.previous:
for slot in previous.slots: for slot in previous.slots:
@ -112,7 +112,7 @@ proc propose*(validator: Validator, transactions: seq[Validator.Dependencies.Tra
func check*(validator: Validator, signed: SignedBlock): auto = func check*(validator: Validator, signed: SignedBlock): auto =
type BlockCheck = checks.BlockCheck[SignedBlock.Dependencies] type BlockCheck = checks.BlockCheck[SignedBlock.Dependencies]
type BlockId = blocks.BlockId[SignedBlock.Dependencies] type BlockId = blocks.BlockId[SignedBlock.Dependencies.Hash]
without member =? validator.committee.membership(signed.signer): without member =? validator.committee.membership(signed.signer):
return BlockCheck.invalid("block is not signed by a committee member") return BlockCheck.invalid("block is not signed by a committee member")
if member != signed.blck.author: if member != signed.blck.author:

View File

@ -11,7 +11,7 @@ type
of invalid: of invalid:
reason: string reason: string
of incomplete: of incomplete:
missing: seq[BlockId[Dependencies]] missing: seq[BlockId[Dependencies.Hash]]
of correct: of correct:
blck: CorrectBlock[Dependencies] blck: CorrectBlock[Dependencies]
CorrectBlock*[Dependencies] = distinct SignedBlock[Dependencies] CorrectBlock*[Dependencies] = distinct SignedBlock[Dependencies]

View File

@ -11,7 +11,7 @@ type
slot: ProposerSlot[Dependencies] slot: ProposerSlot[Dependencies]
signedBlock: SignedBlock[Dependencies] signedBlock: SignedBlock[Dependencies]
certifiedBy: Voting certifiedBy: Voting
certificates: seq[BlockId[Dependencies]] certificates: seq[BlockId[Dependencies.Hash]]
SlotStatus* {.pure.} = enum SlotStatus* {.pure.} = enum
undecided undecided
skip skip

View File

@ -13,8 +13,8 @@ proc example*(T: type CommitteeMember): T =
proc example*(T: type BlockId): T = proc example*(T: type BlockId): T =
let author = CommitteeMember.example let author = CommitteeMember.example
let round = uint64.example let round = uint64.example
let hash = T.Dependencies.Hash.example let hash = T.Hash.example
T.new(author, round, hash) BlockId.new(author, round, hash)
proc example*( proc example*(
T: type Block, T: type Block,
@ -22,7 +22,7 @@ proc example*(
round = uint64.example round = uint64.example
): T = ): T =
type Transaction = T.Dependencies.Transaction type Transaction = T.Dependencies.Transaction
let parents = seq[BlockId[T.Dependencies]].example let parents = seq[BlockId[T.Dependencies.Hash]].example
let transactions = seq[Transaction].example let transactions = seq[Transaction].example
T.new(author, round, parents, transactions) T.new(author, round, parents, transactions)

View File

@ -5,7 +5,7 @@ import mysticeti/blocks
suite "Blocks": suite "Blocks":
type Block = mysticeti.Block[MockDependencies] type Block = mysticeti.Block[MockDependencies]
type BlockId = mysticeti.BlockId[MockDependencies] type BlockId = mysticeti.BlockId[MockDependencies.Hash]
type Identity = MockDependencies.Identity type Identity = MockDependencies.Identity
type Transaction = MockDependencies.Transaction type Transaction = MockDependencies.Transaction
type Hash = MockDependencies.Hash type Hash = MockDependencies.Hash

View File

@ -4,7 +4,7 @@ import mysticeti/validator/slots
suite "Proposer Slots": suite "Proposer Slots":
type BlockId = mysticeti.BlockId[MockDependencies] type BlockId = mysticeti.BlockId[MockDependencies.Hash]
type SignedBlock = mysticeti.SignedBlock[MockDependencies] type SignedBlock = mysticeti.SignedBlock[MockDependencies]
type Proposal = slots.Proposal[MockDependencies] type Proposal = slots.Proposal[MockDependencies]
type ProposerSlot = slots.ProposerSlot[MockDependencies] type ProposerSlot = slots.ProposerSlot[MockDependencies]

View File

@ -11,7 +11,7 @@ suite "Validator Network":
type Identity = MockDependencies.Identity type Identity = MockDependencies.Identity
type Transaction = MockDependencies.Transaction type Transaction = MockDependencies.Transaction
type Block = blocks.Block[MockDependencies] type Block = blocks.Block[MockDependencies]
type BlockId = blocks.BlockId[MockDependencies] type BlockId = blocks.BlockId[MockDependencies.Hash]
type Hash = MockDependencies.Hash type Hash = MockDependencies.Hash
var simulator: NetworkSimulator var simulator: NetworkSimulator