|
| 1 | +package slashing |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | +) |
| 7 | + |
| 8 | +type StakeSource string |
| 9 | + |
| 10 | +const ( |
| 11 | + StakeSourceSlashable StakeSource = "slashable" |
| 12 | + StakeSourceNonSlashable StakeSource = "non-slashable" |
| 13 | + StakeSourceBoth StakeSource = "both" |
| 14 | +) |
| 15 | + |
| 16 | +type State struct { |
| 17 | + totalMagnitude float64 |
| 18 | + operatorSets []int |
| 19 | + slashableMagnitude []float64 |
| 20 | +} |
| 21 | + |
| 22 | +func CalculateNewState( |
| 23 | + oldState State, |
| 24 | + stakeSource StakeSource, |
| 25 | + operatorSet int, |
| 26 | + slashableProportion float64, |
| 27 | +) *State { |
| 28 | + |
| 29 | + if stakeSource == StakeSourceSlashable { |
| 30 | + // non slashable proportion |
| 31 | + nonSlashableProportion := (oldState.totalMagnitude - sumFloatArray(oldState.slashableMagnitude)) / oldState.totalMagnitude |
| 32 | + fmt.Println("nonSlashableProportion: ", nonSlashableProportion) |
| 33 | + |
| 34 | + allocatedMagnitude := sumFloatArray(oldState.slashableMagnitude) |
| 35 | + fmt.Println("allocatedMagnitude: ", allocatedMagnitude) |
| 36 | + |
| 37 | + totalMagnitudeNew := allocatedMagnitude / (1 - slashableProportion - nonSlashableProportion) |
| 38 | + fmt.Println("totalMagnitudeNew: ", totalMagnitudeNew) |
| 39 | + |
| 40 | + slashableMagnitude := slashableProportion * totalMagnitudeNew |
| 41 | + fmt.Println("slashableMagnitude: ", slashableMagnitude) |
| 42 | + |
| 43 | + nonSlashableMagnitude := nonSlashableProportion * totalMagnitudeNew |
| 44 | + fmt.Println("nonSlashableMagnitude: ", nonSlashableMagnitude) |
| 45 | + |
| 46 | + opSetToUpdate := []int{operatorSet} |
| 47 | + slashableMagnitudeSet := []float64{Round(slashableMagnitude, 10)} |
| 48 | + |
| 49 | + return &State{ |
| 50 | + totalMagnitude: Round(totalMagnitudeNew, 10), |
| 51 | + operatorSets: opSetToUpdate, |
| 52 | + slashableMagnitude: slashableMagnitudeSet, |
| 53 | + } |
| 54 | + } else if stakeSource == StakeSourceNonSlashable { |
| 55 | + // TODO: need to first verify if the operator set is already in the state |
| 56 | + // and also if there is enough non slashable stake to allocate |
| 57 | + |
| 58 | + return &State{ |
| 59 | + totalMagnitude: oldState.totalMagnitude, |
| 60 | + operatorSets: []int{operatorSet}, |
| 61 | + slashableMagnitude: []float64{Round(oldState.totalMagnitude*slashableProportion, 10)}, |
| 62 | + } |
| 63 | + } else { |
| 64 | + // Stake is sourced from both slashable and non-slashable proportionally |
| 65 | + } |
| 66 | + |
| 67 | + // non slashable proportion |
| 68 | + nonSlashableProportion := (oldState.totalMagnitude - sumFloatArray(oldState.slashableMagnitude)) / oldState.totalMagnitude |
| 69 | + fmt.Println("nonSlashableProportion: ", nonSlashableProportion) |
| 70 | + |
| 71 | + allocatedMagnitude := sumFloatArray(oldState.slashableMagnitude) |
| 72 | + fmt.Println("allocatedMagnitude: ", allocatedMagnitude) |
| 73 | + |
| 74 | + /* |
| 75 | + i = new operator set |
| 76 | + slashablePercentage = slashable magnitude (i) / total magnitude new |
| 77 | +
|
| 78 | + */ |
| 79 | + totalMagnitudeNew := allocatedMagnitude / (1 - slashableProportion - nonSlashableProportion) |
| 80 | + fmt.Println("totalMagnitudeNew: ", totalMagnitudeNew) |
| 81 | + |
| 82 | + slashableMagnitude := slashableProportion * totalMagnitudeNew |
| 83 | + fmt.Println("slashableMagnitude: ", slashableMagnitude) |
| 84 | + |
| 85 | + nonSlashableMagnitude := nonSlashableProportion * totalMagnitudeNew |
| 86 | + fmt.Println("nonSlashableMagnitude: ", nonSlashableMagnitude) |
| 87 | + |
| 88 | + opSetToUpdate := []int{operatorSet} |
| 89 | + slashableMagnitudeSet := []float64{Round(slashableMagnitude, 10)} |
| 90 | + |
| 91 | + return &State{ |
| 92 | + totalMagnitude: Round(totalMagnitudeNew, 10), |
| 93 | + operatorSets: opSetToUpdate, |
| 94 | + slashableMagnitude: slashableMagnitudeSet, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func sumFloatArray(arr []float64) float64 { |
| 99 | + sum := 0.0 |
| 100 | + for _, i := range arr { |
| 101 | + sum += i |
| 102 | + } |
| 103 | + return sum |
| 104 | +} |
| 105 | + |
| 106 | +// Round rounds the floating point number to the specified number of decimal places. |
| 107 | +func Round(val float64, places int) float64 { |
| 108 | + if places < 0 { |
| 109 | + return val |
| 110 | + } |
| 111 | + factor := math.Pow(10, float64(places)) |
| 112 | + return math.Round(val*factor) / factor |
| 113 | +} |
0 commit comments