remove Identity type from Dependencies

This commit is contained in:
Mark Spanbroek 2024-12-09 16:46:49 +01:00
parent 8caa6e04b8
commit 416d43bed1
7 changed files with 31 additions and 18 deletions

View File

@ -6,9 +6,11 @@ type SignedBlock*[Dependencies] = object
blck: Block[Dependencies]
signature: Dependencies.Signature
func sign*(blck: Block, signer: Block.Dependencies.Identity): auto =
mixin sign
let signature = signer.sign(blck.id.hash)
func init*(
_: type SignedBlock,
blck: Block,
signature: Block.Dependencies.Signature
): auto =
SignedBlock[Block.Dependencies](blck: blck, signature: signature)
func blck*(signed: SignedBlock): auto =

View File

@ -2,7 +2,6 @@ type Dependencies*[
Transaction,
Serialization,
Hash,
Identity,
Identifier,
Signature
] = object

View File

@ -31,9 +31,9 @@ proc example*(
author = CommitteeMember.example,
round = uint64.example
): T =
let identity = T.Dependencies.Identity.example
let blck = Block[T.Dependencies].example(author = author, round = round)
blck.sign(identity)
let signature = T.Dependencies.Signature.example
SignedBlock.init(blck, signature)
proc example*[T](_: type seq[T], length=0..10): seq[T] =
let size = rand(length)
@ -51,3 +51,6 @@ proc example*(T: type MockIdentity): T =
proc example*(T: type MockIdentifier): T =
MockIdentity.example.identifier
proc example*(T: type MockSignature): T =
MockIdentity.example.sign(MockHash.example)

View File

@ -13,7 +13,6 @@ type MockDependencies* = Dependencies[
MockTransaction,
MockSerialization,
MockHash,
MockIdentity,
MockIdentifier,
MockSignature
]

View File

@ -4,7 +4,7 @@ import mysticeti/blocks
type Validator = mysticeti.Validator[MockDependencies]
type Committee = mysticeti.Committee[MockDependencies]
type Identity = MockDependencies.Identity
type Identity = MockIdentity
type Transaction = MockDependencies.Transaction
type Block = blocks.Block[MockDependencies]
type SignedBlock = blocks.SignedBlock[MockDependencies]
@ -38,7 +38,8 @@ proc propose*(simulator: NetworkSimulator, validatorIndex: int): SignedBlock =
let parents = validator.parentBlocks()
let transactions = seq[Transaction].example
let blck = Block.new(author, round, parents, transactions)
let signed = blck.sign(identity)
let signature = identity.sign(blck.id.hash)
let signed = SignedBlock.init(blck, signature)
let checked = validator.check(signed)
validator.receive(checked.blck)
signed

View File

@ -6,7 +6,7 @@ suite "Blocks":
type Block = mysticeti.Block[MockDependencies]
type BlockId = mysticeti.BlockId[MockDependencies.Hash]
type Identity = MockDependencies.Identity
type Identity = MockIdentity
type Transaction = MockDependencies.Transaction
type Hash = MockDependencies.Hash
type Serialization = MockDependencies.Serialization
@ -32,6 +32,7 @@ suite "Blocks":
test "blocks can be signed":
let signer = Identity.init
let blck = Block.example
let signed = blck.sign(signer)
let signature = signer.sign(blck.id.hash)
let signed = SignedBlock.init(blck, signature)
check signed.blck == blck
check signed.signer == signer.identifier

View File

@ -56,8 +56,9 @@ suite "Validator Network":
test "refuses proposals that are not signed by the author":
let proposal = simulator.propose(1)
let signedByOther = proposal.blck.sign(simulator.identities[2])
let checked = simulator.validators[0].check(signedByOther)
let wrongSignature = simulator.identities[2].sign(proposal.blck.id.hash)
let wrongSigned = SignedBlock.init(proposal.blck, wrongSignature)
let checked = simulator.validators[0].check(wrongSigned)
check checked.verdict == BlockVerdict.invalid
check checked.reason == "block is not signed by its author"
@ -79,7 +80,8 @@ suite "Validator Network":
parents & badparent,
seq[Transaction].example
)
let proposal = blck.sign(simulator.identities[0])
let signature = simulator.identities[0].sign(blck.id.hash)
let proposal = SignedBlock.init(blck, signature)
let checked = simulator.validators[1].check(proposal)
check checked.verdict == BlockVerdict.invalid
check checked.reason == "block has a parent from an invalid round"
@ -94,7 +96,8 @@ suite "Validator Network":
parents & badparent,
seq[Transaction].example
)
let proposal = blck.sign(simulator.identities[0])
let signature = simulator.identities[0].sign(blck.id.hash)
let proposal = SignedBlock.init(blck, signature)
let checked = simulator.validators[1].check(proposal)
check checked.verdict == BlockVerdict.invalid
check checked.reason == "block includes a parent more than once"
@ -108,7 +111,8 @@ suite "Validator Network":
parents[0..<2],
seq[Transaction].example
)
let proposal = blck.sign(simulator.identities[0])
let signature = simulator.identities[0].sign(blck.id.hash)
let proposal = SignedBlock.init(blck, signature)
let checked = simulator.validators[1].check(proposal)
check checked.verdict == BlockVerdict.invalid
check checked.reason ==
@ -305,12 +309,16 @@ suite "Validator Network":
3: @[0, 1, 2, 3]
})
# validator 0 creates two different proposals
let proposalA, proposalB = Block.new(
let blockA, blockB = Block.new(
author = CommitteeMember(0),
round = 0,
parents = @[],
transactions = seq[Transaction].example(length = 1..10)
).sign(simulator.identities[0])
)
let signatureA = simulator.identities[0].sign(blockA.id.hash)
let signatureB = simulator.identities[0].sign(blockB.id.hash)
let proposalA = SignedBlock.init(blockA, signatureA)
let proposalB = SignedBlock.init(blockB, signatureB)
# validator 0 sends different proposals to different parts of the network
!exchangeBlock(simulator.validators[0], simulator.validators[0], proposalA)
!exchangeBlock(simulator.validators[0], simulator.validators[1], proposalA)