mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-05 06:53:08 +00:00
serialize signed block
This commit is contained in:
parent
0717382480
commit
2a4cef2d77
@ -2,12 +2,13 @@ import ./blocks/blockid
|
||||
import ./blocks/blck
|
||||
|
||||
export blockid
|
||||
export blck
|
||||
export blck except hash
|
||||
|
||||
import ./blocks/serialization
|
||||
|
||||
export serialization.toBytes
|
||||
|
||||
import ./blocks/blockhash
|
||||
import ./blocks/hashing
|
||||
|
||||
export blockhash.id
|
||||
export hashing.hash
|
||||
export hashing.id
|
||||
|
||||
@ -12,4 +12,10 @@ type Block* = ref object
|
||||
round*: uint64
|
||||
parents*: seq[BlockId]
|
||||
transactions*: seq[Transaction]
|
||||
hash*: ?Hash
|
||||
hash: ?Hash
|
||||
|
||||
func `hash=`*(blck: Block, hash: Hash) =
|
||||
blck.hash = some hash
|
||||
|
||||
func hash*(blck: Block): ?Hash =
|
||||
blck.hash
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
import ../basics
|
||||
import ./blck
|
||||
import ./blockid
|
||||
import ./serialization
|
||||
import ../hashing
|
||||
|
||||
func id*(blck: Block): BlockId =
|
||||
without var hash =? blck.hash:
|
||||
hash = Hash.hash(blck.toBytes())
|
||||
blck.hash = some hash
|
||||
BlockId.init(
|
||||
blck.author,
|
||||
blck.round,
|
||||
hash
|
||||
)
|
||||
18
codexvalidator/blocks/hashing.nim
Normal file
18
codexvalidator/blocks/hashing.nim
Normal file
@ -0,0 +1,18 @@
|
||||
import ../basics
|
||||
import ../hashing
|
||||
import ./blck
|
||||
import ./blockid
|
||||
import ./serialization
|
||||
|
||||
func hash*(b: Block): Hash =
|
||||
without var hash =? blck.hash(b):
|
||||
hash = Hash.hash(b.toBytes())
|
||||
b.hash = hash
|
||||
hash
|
||||
|
||||
func id*(b: Block): BlockId =
|
||||
BlockId.init(
|
||||
b.author,
|
||||
b.round,
|
||||
hash(b)
|
||||
)
|
||||
@ -3,6 +3,7 @@ import ../basics
|
||||
import ../hashing
|
||||
import ../transaction
|
||||
import ../transaction/serialization
|
||||
import ../signatures
|
||||
import ./blockid
|
||||
import ./blck
|
||||
|
||||
@ -39,3 +40,21 @@ func init*(_: type BlockMessage, blck: Block): BlockMessage =
|
||||
|
||||
func toBytes*(blck: Block): seq[byte] =
|
||||
Protobuf.encode(BlockMessage.init(blck))
|
||||
|
||||
type SignedBlockMessage* {.proto3.} = object
|
||||
blck* {.fieldNumber: 1.}: BlockMessage
|
||||
signer* {.fieldNumber: 2.}: seq[byte]
|
||||
signature* {.fieldNumber: 3.}: seq[byte]
|
||||
|
||||
func init*(
|
||||
_: type SignedBlockMessage,
|
||||
signed: Signed[Block]
|
||||
): SignedBlockMessage =
|
||||
SignedBlockMessage(
|
||||
blck: BlockMessage.init(signed.value),
|
||||
signer: signed.signer.toBytes(),
|
||||
signature: signed.signature.toBytes()
|
||||
)
|
||||
|
||||
func toBytes*(signed: Signed[Block]): seq[byte] =
|
||||
Protobuf.encode(SignedBlockMessage.init(signed))
|
||||
|
||||
@ -3,6 +3,7 @@ import codexvalidator/blocks
|
||||
import codexvalidator/blocks/serialization
|
||||
import codexvalidator/transaction/serialization
|
||||
import codexvalidator/hashing
|
||||
import codexvalidator/signatures
|
||||
|
||||
suite "Block serialization":
|
||||
|
||||
@ -26,3 +27,13 @@ suite "Block serialization":
|
||||
check protobuf.round == blck.round
|
||||
check protobuf.parents == blck.parents.mapIt(BlockIdMessage.init(it))
|
||||
check protobuf.transactions == blck.transactions.mapIt(TransactionMessage.init(it))
|
||||
|
||||
test "serializes a signed block with protobuf":
|
||||
let blck = Block.example
|
||||
let identity = Identity.example
|
||||
let signed = Signed.sign(identity, blck)
|
||||
let serialized = signed.toBytes()
|
||||
let protobuf = Protobuf.decode(serialized, SignedBlockMessage)
|
||||
check protobuf.blck == BlockMessage.init(blck)
|
||||
check protobuf.signer == signed.signer.toBytes()
|
||||
check protobuf.signature == signed.signature.toBytes()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user