2022-11-30 14:28:27 +00:00
|
|
|
#!/bin/python3
|
|
|
|
|
|
|
|
import random
|
2022-12-07 14:25:48 +00:00
|
|
|
from bitarray import bitarray
|
|
|
|
from bitarray.util import zeros
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
class Block:
|
2023-02-15 14:06:42 +00:00
|
|
|
"""This class represents a block in the Ethereum blockchain."""
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2024-02-15 18:18:30 +00:00
|
|
|
def __init__(self, blockSizeR, blockSizeRK=0, blockSizeC=0, blockSizeCK=0):
|
|
|
|
"""Initialize the block with a data array of blocksize^2 zeros.
|
|
|
|
BlockSizeR: row size
|
|
|
|
BlockSizeRK: original row size, before erasure coding to BlocksSizeR
|
|
|
|
BlockSizeC: column size (i.e. number of rows)
|
|
|
|
BlockSizeCK: original column size, before erasure coding to BlocksSizeR
|
|
|
|
"""
|
|
|
|
self.blockSizeR = blockSizeR
|
|
|
|
self.blockSizeRK = blockSizeRK if blockSizeRK else blockSizeR/2
|
|
|
|
self.blockSizeC = blockSizeC if blockSizeC else blockSizeR
|
|
|
|
self.blockSizeCK = blockSizeCK if blockSizeCK else blockSizeRK
|
|
|
|
self.data = zeros(self.blockSizeR*self.blockSizeC)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def fill(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It fills the block data with ones."""
|
2022-12-07 17:44:02 +00:00
|
|
|
self.data.setall(1)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 09:09:54 +00:00
|
|
|
def merge(self, merged):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It merges (OR) the existing block with the received one."""
|
2022-12-20 09:09:54 +00:00
|
|
|
self.data |= merged.data
|
|
|
|
|
2023-02-14 01:11:44 +00:00
|
|
|
def getSegment(self, rowID, columnID):
|
2023-03-01 21:21:31 +00:00
|
|
|
"""Check whether a segment is included"""
|
2024-02-15 18:18:30 +00:00
|
|
|
return self.data[rowID*self.blockSizeR + columnID]
|
2023-02-14 01:11:44 +00:00
|
|
|
|
2023-03-01 21:21:31 +00:00
|
|
|
def setSegment(self, rowID, columnID, value = 1):
|
|
|
|
"""Set value for a segment (default 1)"""
|
2024-02-15 18:18:30 +00:00
|
|
|
self.data[rowID*self.blockSizeR + columnID] = value
|
2023-02-14 01:11:44 +00:00
|
|
|
|
2022-12-07 14:10:35 +00:00
|
|
|
def getColumn(self, columnID):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It returns the block column corresponding to columnID."""
|
2024-02-15 18:18:30 +00:00
|
|
|
return self.data[columnID::self.blockSizeR]
|
2022-12-07 14:10:35 +00:00
|
|
|
|
2022-12-20 09:09:54 +00:00
|
|
|
def mergeColumn(self, columnID, column):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It merges (OR) the existing column with the received one."""
|
2024-02-15 18:18:30 +00:00
|
|
|
self.data[columnID::self.blockSizeR] |= column
|
2022-12-20 09:09:54 +00:00
|
|
|
|
|
|
|
def repairColumn(self, id):
|
2024-02-15 18:18:30 +00:00
|
|
|
"""It repairs the entire column if it has at least blockSizeCK ones.
|
2023-02-15 02:22:37 +00:00
|
|
|
Returns: list of repaired segments
|
|
|
|
"""
|
2024-02-15 18:18:30 +00:00
|
|
|
line = self.data[id::self.blockSizeR]
|
2023-02-15 02:22:37 +00:00
|
|
|
success = line.count(1)
|
2024-02-19 22:20:53 +00:00
|
|
|
repairedSamples = 0
|
2024-02-15 18:18:30 +00:00
|
|
|
if success >= self.blockSizeCK:
|
2023-02-15 02:22:37 +00:00
|
|
|
ret = ~line
|
2024-02-15 18:18:30 +00:00
|
|
|
self.data[id::self.blockSizeR] = 1
|
2024-02-19 22:20:53 +00:00
|
|
|
repairedSamples = len(line) - success
|
2023-02-15 02:22:37 +00:00
|
|
|
else:
|
2024-02-15 18:18:30 +00:00
|
|
|
ret = zeros(self.blockSizeC)
|
2024-02-19 22:20:53 +00:00
|
|
|
return ret, repairedSamples
|
2022-12-20 09:09:54 +00:00
|
|
|
|
2022-12-07 14:10:35 +00:00
|
|
|
def getRow(self, rowID):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It returns the block row corresponding to rowID."""
|
2024-02-15 18:18:30 +00:00
|
|
|
return self.data[rowID*self.blockSizeR:(rowID+1)*self.blockSizeR]
|
2022-12-07 14:10:35 +00:00
|
|
|
|
2022-12-20 09:09:54 +00:00
|
|
|
def mergeRow(self, rowID, row):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It merges (OR) the existing row with the received one."""
|
2024-02-15 18:18:30 +00:00
|
|
|
self.data[rowID*self.blockSizeR:(rowID+1)*self.blockSizeR] |= row
|
2022-12-20 09:09:54 +00:00
|
|
|
|
|
|
|
def repairRow(self, id):
|
2024-02-15 18:18:30 +00:00
|
|
|
"""It repairs the entire row if it has at least blockSizeRK ones.
|
2023-03-03 10:47:27 +00:00
|
|
|
Returns: list of repaired segments.
|
2023-02-15 02:22:37 +00:00
|
|
|
"""
|
2024-02-15 18:18:30 +00:00
|
|
|
line = self.data[id*self.blockSizeR:(id+1)*self.blockSizeR]
|
2023-02-15 02:22:37 +00:00
|
|
|
success = line.count(1)
|
2024-02-19 22:20:53 +00:00
|
|
|
repairedSamples = 0
|
2024-02-15 18:18:30 +00:00
|
|
|
if success >= self.blockSizeRK:
|
2023-02-15 02:22:37 +00:00
|
|
|
ret = ~line
|
2024-02-15 18:18:30 +00:00
|
|
|
self.data[id*self.blockSizeR:(id+1)*self.blockSizeR] = 1
|
2024-02-19 22:20:53 +00:00
|
|
|
repairedSamples = len(line) - success
|
2023-02-15 02:22:37 +00:00
|
|
|
else:
|
2024-02-15 18:18:30 +00:00
|
|
|
ret = zeros(self.blockSizeR)
|
2024-02-19 22:20:53 +00:00
|
|
|
return ret, repairedSamples
|
2022-12-20 09:09:54 +00:00
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
def print(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It prints the block in the terminal (outside of the logger rules))."""
|
2024-02-15 18:18:30 +00:00
|
|
|
dash = "-" * (self.blockSizeR+2)
|
2022-11-30 14:28:27 +00:00
|
|
|
print(dash)
|
2024-02-15 18:18:30 +00:00
|
|
|
for i in range(self.blockSizeC):
|
2022-11-30 14:28:27 +00:00
|
|
|
line = "|"
|
2024-02-15 18:18:30 +00:00
|
|
|
for j in range(self.blockSizeR):
|
|
|
|
line += "%i" % self.data[(i*self.blockSizeR)+j]
|
2022-11-30 14:28:27 +00:00
|
|
|
print(line+"|")
|
|
|
|
print(dash)
|