das-research/DAS/block.py

84 lines
2.8 KiB
Python
Raw Normal View History

2022-11-30 14:28:27 +00:00
#!/bin/python3
import random
from bitarray import bitarray
from bitarray.util import zeros
2022-11-30 14:28:27 +00:00
class Block:
"""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):
"""Initialize the block with a data array of blocksize^2 zeros."""
2023-01-13 15:51:27 +00:00
self.blockSize = blockSize
self.data = zeros(self.blockSize*self.blockSize)
2022-11-30 14:28:27 +00:00
def fill(self):
"""It fills the block data with ones."""
self.data.setall(1)
2022-11-30 14:28:27 +00:00
def merge(self, merged):
"""It merges (OR) the existing block with the received one."""
self.data |= merged.data
def getSegment(self, rowID, columnID):
"""Check whether a segment is included"""
return self.data[rowID*self.blockSize + columnID]
def setSegment(self, rowID, columnID, value = 1):
"""Set value for a segment (default 1)"""
self.data[rowID*self.blockSize + columnID] = value
def getColumn(self, columnID):
"""It returns the block column corresponding to columnID."""
return self.data[columnID::self.blockSize]
def mergeColumn(self, columnID, column):
"""It merges (OR) the existing column with the received one."""
self.data[columnID::self.blockSize] |= column
def repairColumn(self, id):
"""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)
if success >= self.blockSize/2:
ret = ~line
self.data[id::self.blockSize] = 1
else:
ret = zeros(self.blockSize)
return ret
def getRow(self, rowID):
"""It returns the block row corresponding to rowID."""
return self.data[rowID*self.blockSize:(rowID+1)*self.blockSize]
def mergeRow(self, rowID, row):
"""It merges (OR) the existing row with the received one."""
self.data[rowID*self.blockSize:(rowID+1)*self.blockSize] |= row
def repairRow(self, id):
"""It repairs the entire row if it has at least blockSize/2 ones.
2023-03-03 10:47:27 +00:00
Returns: list of repaired segments.
"""
line = self.data[id*self.blockSize:(id+1)*self.blockSize]
success = line.count(1)
if success >= self.blockSize/2:
ret = ~line
self.data[id*self.blockSize:(id+1)*self.blockSize] = 1
else:
ret = zeros(self.blockSize)
return ret
2022-11-30 14:28:27 +00:00
def print(self):
"""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)