Skip to content

Commit

Permalink
add Slashing
Browse files Browse the repository at this point in the history
  • Loading branch information
igroman787 committed Feb 16, 2021
1 parent a346d88 commit beef1d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.DS_Store
.idea/
__pycache__/
test.py
56 changes: 43 additions & 13 deletions mytoncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def GetConfig(self, configId):
config = configs.get("configId")
if config:
diffTime = timestamp - config.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config
#end if

Expand All @@ -794,7 +794,7 @@ def GetConfig12(self):
config12 = local.buffer.get("config12")
if config12:
diffTime = timestamp - config12.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config12
#end if

Expand All @@ -817,7 +817,7 @@ def GetConfig15(self):
config15 = local.buffer.get("config15")
if config15:
diffTime = timestamp - config15.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config15
#end if

Expand All @@ -839,7 +839,7 @@ def GetConfig17(self):
config17 = local.buffer.get("config17")
if config17:
diffTime = timestamp - config17.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config17
#end if

Expand All @@ -865,7 +865,7 @@ def GetConfig32(self):
config32 = local.buffer.get("config32")
if config32:
diffTime = timestamp - config32.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config32
#end if

Expand Down Expand Up @@ -902,7 +902,7 @@ def GetConfig34(self):
config34 = local.buffer.get("config34")
if config34:
diffTime = timestamp - config34.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config34
#end if

Expand Down Expand Up @@ -939,7 +939,7 @@ def GetConfig36(self):
config36 = local.buffer.get("config36")
if config36:
diffTime = timestamp - config36.get("timestamp")
if diffTime < 10:
if diffTime < 60:
return config36
#end if

Expand Down Expand Up @@ -1748,7 +1748,7 @@ def VoteComplaint(self, complaintHash):
resultFilePath = self.SignComplaintVoteRequestWithValidator(complaintHash, electionId, validatorIndex, validatorPubkey_b64, validatorSignature)
resultFilePath = self.SignFileWithWallet(wallet, resultFilePath, fullElectorAddr, 1.5)
self.SendFile(resultFilePath, wallet)
self.AddSaveComplaint(complaintHash)
# self.AddSaveComplaint(complaintHash)
#end define

def CheckComplaints(self):
Expand Down Expand Up @@ -1776,7 +1776,7 @@ def CheckComplaints(self):
def CheckComplaint(self, filePath):
local.AddLog("start CheckComplaint function", "debug")
cmd = "loadproofcheck {filePath}".format(filePath=filePath)
result = self.liteClient.Run(cmd, timeout=10)
result = self.liteClient.Run(cmd, timeout=30)
lines = result.split('\n')
ok = False
for line in lines:
Expand Down Expand Up @@ -1817,7 +1817,7 @@ def GetValidatorsLoad(self, timeDiff=2000):
time2 = timeNow - timeDiff
filePrefix = self.tempDir + "check_{timeNow}".format(timeNow=timeNow)
cmd = "checkloadall {time2} {timeNow} {filePrefix}".format(timeNow=timeNow, time2=time2, filePrefix=filePrefix)
result = self.liteClient.Run(cmd, timeout=10)
result = self.liteClient.Run(cmd, timeout=30)
lines = result.split('\n')
vdata = dict()
compFiles = list()
Expand Down Expand Up @@ -1873,6 +1873,13 @@ def GetValidatorsLoad(self, timeDiff=2000):
validatorsLoad["vdata"] = vdata
validatorsLoad["compFiles"] = compFiles
local.buffer[bname] = validatorsLoad

# delete compFiles
if timeDiff == 2000:
for item in compFiles:
os.remove(item)
#end if

return vdata, compFiles
#end define

Expand All @@ -1889,7 +1896,7 @@ def GetValidatorsList(self):
return validators
#end define

def CheckValidators(self, timeDiff=2000):
def CheckValidators(self, timeDiff=3000):
local.AddLog("start CheckValidators function", "debug")
vdata, compFiles = self.GetValidatorsLoad(timeDiff)
fullElectorAddr = self.GetFullElectorAddr()
Expand Down Expand Up @@ -2654,16 +2661,38 @@ def SaveTransNumFromShard(ton, shard, buff):
#end define

def Complaints(ton):
saveComplaints = ton.GetSaveComplaints()
validatorIndex = self.GetValidatorIndex()
if validatorIndex < 0:
return
#end if

# saveComplaints = ton.GetSaveComplaints()
checkComplaints = ton.CheckComplaints()
complaints = ton.GetComplaints()
for complaint in complaints:
complaintHash = complaint.get("hash")
complaintHash_hex = dec2hex(complaintHash).upper()
if complaintHash_hex in checkComplaints and complaintHash not in saveComplaints:
# if complaintHash_hex in checkComplaints and complaintHash not in saveComplaints:
if complaintHash_hex in checkComplaints:
ton.VoteComplaint(complaintHash)
#end define

def Slashing(ton):
timeNow = GetTimestamp()
oldSlashTime = local.buffer.get("oldSlashTime")
config15 = ton.GetConfig15()
config34 = ton.GetConfig34()
startWorkTime = config34.get("startWorkTime")
validatorsElectedFor = config15.get("validatorsElectedFor")
timeDiff = validatorsElectedFor - 1000
slashTime = startWorkTime + validatorsElectedFor - 900
if oldSlashTime != slashTime and slashTime > timeNow:
text = "start Slashing function, {}, {}, {}".format(timeNow, slashTime, timeDiff)
local.AddLog(text)
ton.CheckValidators(timeDiff)
local.buffer["oldSlashTime"] = slashTime
#end define

def General():
local.AddLog("start General function", "debug")
ton = MyTonCore()
Expand All @@ -2673,6 +2702,7 @@ def General():
local.StartCycle(Statistics, sec=10, args=(ton, ))
local.StartCycle(Offers, sec=600, args=(ton, ))
local.StartCycle(Complaints, sec=600, args=(ton, ))
local.StartCycle(Slashing, sec=60, args=(ton, ))
local.StartCycle(Domains, sec=600, args=(ton, ))
local.StartCycle(Telemetry, sec=60, args=(ton, ))
local.StartCycle(Mining, sec=1, args=(ton, ))
Expand Down
2 changes: 1 addition & 1 deletion mytonctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def Test2(args):
timeDiff = args[0]
timeDiff = int(timeDiff)
except:
timeDiff = 2000
timeDiff = 3000
ton.CheckValidators(timeDiff)
#end define

Expand Down

0 comments on commit beef1d8

Please sign in to comment.