mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-25 00:33:08 +00:00
blocks contain signed transactions
This commit is contained in:
parent
24948520cc
commit
d1dd428560
@ -2,6 +2,7 @@ from pkg/mysticeti import CommitteeMember, `==`, `$`
|
||||
import ../basics
|
||||
import ../transaction
|
||||
import ../hashing
|
||||
import ../signatures
|
||||
import ./blockid
|
||||
|
||||
export mysticeti.CommitteeMember
|
||||
@ -12,7 +13,7 @@ type Block* = ref object
|
||||
author*: CommitteeMember
|
||||
round*: uint64
|
||||
parents*: seq[BlockId]
|
||||
transactions*: seq[Transaction]
|
||||
transactions*: seq[Signed[Transaction]]
|
||||
hash: ?Hash
|
||||
|
||||
func `hash=`*(blck: Block, hash: Hash) =
|
||||
|
||||
@ -25,7 +25,7 @@ func init*(
|
||||
author: CommitteeMember(message.author),
|
||||
round: message.round,
|
||||
parents: message.parents.mapIt(? BlockId.init(it)),
|
||||
transactions: message.transactions.mapIt(? Transaction.init(it))
|
||||
transactions: message.transactions.mapIt(? Signed[Transaction].init(it))
|
||||
)
|
||||
|
||||
func init*(
|
||||
|
||||
@ -28,14 +28,14 @@ type BlockMessage* {.proto3.} = object
|
||||
author* {.fieldNumber: 1, pint.}: uint32
|
||||
round* {.fieldNumber: 2, pint.}: uint64
|
||||
parents* {.fieldNumber: 3.}: seq[BlockIdMessage]
|
||||
transactions* {.fieldNumber: 4.}: seq[TransactionMessage]
|
||||
transactions* {.fieldNumber: 4.}: seq[SignedTransactionMessage]
|
||||
|
||||
func init*(_: type BlockMessage, blck: Block): BlockMessage =
|
||||
BlockMessage(
|
||||
author: blck.author.uint32,
|
||||
round: blck.round,
|
||||
parents: blck.parents.mapIt(BlockIdMessage.init(it)),
|
||||
transactions: blck.transactions.mapIt(TransactionMessage.init(it))
|
||||
transactions: blck.transactions.mapIt(SignedTransactionMessage.init(it))
|
||||
)
|
||||
|
||||
func toBytes*(blck: Block): seq[byte] =
|
||||
|
||||
@ -55,7 +55,7 @@ func init(_: type Groth16Proof, message: Groth16ProofMessage): Groth16Proof =
|
||||
)
|
||||
)
|
||||
|
||||
func init*(_: type Transaction, message: TransactionMessage): ?!Transaction =
|
||||
func init(_: type Transaction, message: TransactionMessage): ?!Transaction =
|
||||
if message.version != TransactionVersion.version0.uint32:
|
||||
return failure "unsupported transaction version: " & $message.version
|
||||
let proofInput = ? StorageProofInput.init(message.proofInput)
|
||||
@ -68,7 +68,7 @@ func init*(_: type Transaction, message: TransactionMessage): ?!Transaction =
|
||||
else:
|
||||
failure "invalid transaction kind: " & $message.kind
|
||||
|
||||
func init(
|
||||
func init*(
|
||||
_: type Signed[Transaction],
|
||||
message: SignedTransactionMessage
|
||||
): ?!Signed[Transaction] =
|
||||
|
||||
@ -25,7 +25,7 @@ suite "Block serialization":
|
||||
check protobuf.author == blck.author.uint32
|
||||
check protobuf.round == blck.round
|
||||
check protobuf.parents == blck.parents.mapIt(BlockIdMessage.init(it))
|
||||
check protobuf.transactions == blck.transactions.mapIt(TransactionMessage.init(it))
|
||||
check protobuf.transactions == blck.transactions.mapIt(SignedTransactionMessage.init(it))
|
||||
|
||||
test "serializes a signed block with protobuf":
|
||||
let blck = Block.example
|
||||
@ -79,8 +79,8 @@ suite "Block serialization":
|
||||
test "deserialization fails when transaction is invalid":
|
||||
let signed = Signed[Block].example
|
||||
var message = SignedBlockMessage.init(signed)
|
||||
var transaction = TransactionMessage.init(Transaction.example)
|
||||
transaction.version = 42'u8
|
||||
var transaction = SignedTransactionMessage.init(Signed[Transaction].example)
|
||||
transaction.transaction.version = 42'u8
|
||||
message.blck.transactions &= transaction
|
||||
let invalid = Protobuf.encode(message)
|
||||
let deserialized = Signed[Block].fromBytes(invalid)
|
||||
|
||||
@ -94,5 +94,5 @@ proc example*(_: type Block): Block =
|
||||
author: CommitteeMember.example,
|
||||
round: uint64.example,
|
||||
parents: seq[BlockId].example,
|
||||
transactions: seq[Transaction].example
|
||||
transactions: seq[Signed[Transaction]].example
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user