Skip to content

Commit

Permalink
more check
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Jul 17, 2024
1 parent 17d3cfc commit 6035c83
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
max_line_length=140
insert_final_newline=true

[*.{swift}]
[*.swift]
indent_style=space
tab_width=4
17 changes: 14 additions & 3 deletions Blockchain/Sources/Blockchain/Safrole.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,21 @@ extension Safrole {
ticketsAccumulator.array
}

newTicketsAccumulatorArr.insertSorted(newTickets)
// TODO: check for extrinsicsNotUnique
try newTicketsAccumulatorArr.insertSorted(newTickets) {
if $0 == $1 {
throw SafroleError.extrinsicsNotUnique
}
return $0 < $1
}

if newTicketsAccumulatorArr.count > config.value.epochLength {
// TODO: check for extrinsicsTooLow
let firstToBeRemoved = newTicketsAccumulatorArr[config.value.epochLength]
let highestTicket = newTickets.last! // newTickets must not be empty, otherwise we won't need to remove anything
guard highestTicket < firstToBeRemoved else {
// every tickets must be valid or this is an invalid block
// i.e. the block producer must not include invalid tickets
throw SafroleError.extrinsicsTooLow
}
newTicketsAccumulatorArr.removeLast(newTicketsAccumulatorArr.count - config.value.epochLength)
}

Expand Down
4 changes: 2 additions & 2 deletions Blockchain/Sources/Blockchain/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ extension Header: ScaleCodec.Encodable {
}

extension Header {
public var hash: Data32 {
Data32() // TODO: implement this
public func hash() throws -> Data32 {
try blake2b256(ScaleCodec.encode(self))
}
}
4 changes: 2 additions & 2 deletions Utils/Sources/Utils/Array+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ extension Array where Element: Comparable {
///
/// - Note: The elements of the sequence must be comparable.
/// - Invariant: The array and elements must be sorted according to the given comparison function.
public mutating func insertSorted(_ elements: any Sequence<Element>, by comparer: (Element, Element) -> Bool = { $0 < $1 }) {
public mutating func insertSorted(_ elements: any Sequence<Element>, by comparer: (Element, Element) throws -> Bool = { $0 < $1 }) rethrows {
reserveCapacity(count + elements.underestimatedCount)
var startIdx = 0
for element in elements {
if let idx = self[startIdx...].firstIndex(where: { !comparer($0, element) }) {
if let idx = try self[startIdx...].firstIndex(where: { try !comparer($0, element) }) {
insert(element, at: idx)
startIdx = idx + 1
} else {
Expand Down

0 comments on commit 6035c83

Please sign in to comment.