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
|
|
|
|
2023-01-13 15:51:27 +00:00
|
|
|
def __init__(self, blockSize):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""Initialize the block with a data array of blocksize^2 zeros."""
|
2023-01-13 15:51:27 +00:00
|
|
|
self.blockSize = blockSize
|
2022-12-07 14:25:48 +00:00
|
|
|
self.data = zeros(self.blockSize*self.blockSize)
|
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"""
|
2023-02-14 01:11:44 +00:00
|
|
|
return self.data[rowID*self.blockSize + columnID]
|
|
|
|
|
2023-03-01 21:21:31 +00:00
|
|
|
def setSegment(self, rowID, columnID, value = 1):
|
|
|
|
"""Set value for a segment (default 1)"""
|
|
|
|
self.data[rowID*self.blockSize + 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."""
|
2022-12-07 14:25:48 +00:00
|
|
|
return self.data[columnID::self.blockSize]
|
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."""
|
2022-12-20 09:09:54 +00:00
|
|
|
self.data[columnID::self.blockSize] |= column
|
|
|
|
|
|
|
|
def repairColumn(self, id):
|
2023-02-15 02:22:37 +00:00
|
|
|
"""It repairs the entire column if it has at least blockSize/2 ones.
|
|
|
|
Returns: list of repaired segments
|
|
|
|
"""
|
|
|
|
line = self.data[id::self.blockSize]
|
|
|
|
success = line.count(1)
|
2022-12-20 09:09:54 +00:00
|
|
|
if success >= self.blockSize/2:
|
2023-02-15 02:22:37 +00:00
|
|
|
ret = ~line
|
2022-12-20 09:09:54 +00:00
|
|
|
self.data[id::self.blockSize] = 1
|
2023-02-15 02:22:37 +00:00
|
|
|
else:
|
|
|
|
ret = zeros(self.blockSize)
|
|
|
|
return ret
|
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."""
|
2022-12-07 14:25:48 +00:00
|
|
|
return self.data[rowID*self.blockSize:(rowID+1)*self.blockSize]
|
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."""
|
2022-12-20 09:09:54 +00:00
|
|
|
self.data[rowID*self.blockSize:(rowID+1)*self.blockSize] |= row
|
|
|
|
|
|
|
|
def repairRow(self, id):
|
2023-02-15 02:22:37 +00:00
|
|
|
"""It repairs the entire row if it has at least blockSize/2 ones.
|
|
|
|
Returns: list of repaired segments
|
|
|
|
"""
|
|
|
|
line = self.data[id*self.blockSize:(id+1)*self.blockSize]
|
|
|
|
success = line.count(1)
|
2022-12-20 09:09:54 +00:00
|
|
|
if success >= self.blockSize/2:
|
2023-02-15 02:22:37 +00:00
|
|
|
ret = ~line
|
2022-12-20 09:09:54 +00:00
|
|
|
self.data[id*self.blockSize:(id+1)*self.blockSize] = 1
|
2023-02-15 02:22:37 +00:00
|
|
|
else:
|
|
|
|
ret = zeros(self.blockSize)
|
|
|
|
return ret
|
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))."""
|
2022-11-30 14:28:27 +00:00
|
|
|
dash = "-" * (self.blockSize+2)
|
|
|
|
print(dash)
|
|
|
|
for i in range(self.blockSize):
|
|
|
|
line = "|"
|
|
|
|
for j in range(self.blockSize):
|
|
|
|
line += "%i" % self.data[(i*self.blockSize)+j]
|
|
|
|
print(line+"|")
|
|
|
|
print(dash)
|
|
|
|
|