1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 8 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let INT_MAX = 9223372036854775807 |
---|
| 5 | + | |
---|
| 6 | + | let WAVES = 100000000 |
---|
| 7 | + | |
---|
| 8 | + | let MIN_BALANCE = (2 * WAVES) |
---|
| 9 | + | |
---|
| 10 | + | let SEP = "," |
---|
| 11 | + | |
---|
| 12 | + | let BLOCK_HASH_SIZE = 32 |
---|
| 13 | + | |
---|
| 14 | + | let PUBLIC_KEY_HASH_SIZE = 20 |
---|
| 15 | + | |
---|
| 16 | + | let ROOT_HASH_SIZE = 32 |
---|
| 17 | + | |
---|
| 18 | + | let WITHDRAW_PROOFS_SIZE = 10 |
---|
| 19 | + | |
---|
| 20 | + | let ETH_ADDRESS_STR_SIZE = 40 |
---|
| 21 | + | |
---|
| 22 | + | let MAX_CL_TO_EL_TRANSFERS = 16 |
---|
| 23 | + | |
---|
| 24 | + | let zeroesStr = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" |
---|
| 25 | + | |
---|
| 26 | + | let allMinersKey = "allMiners" |
---|
| 27 | + | |
---|
| 28 | + | let mainChainIdKey = "mainChainId" |
---|
| 29 | + | |
---|
| 30 | + | let lastChainIdKey = "lastChainId" |
---|
| 31 | + | |
---|
| 32 | + | let firstValidAltChainIdKey = "firstValidAltChainId" |
---|
| 33 | + | |
---|
| 34 | + | let minerRewardKey = "minerReward" |
---|
| 35 | + | |
---|
| 36 | + | let blockMetaK = "block_0x" |
---|
| 37 | + | |
---|
| 38 | + | let finalizedBlockKey = "finalizedBlock" |
---|
| 39 | + | |
---|
| 40 | + | let tokenIdKey = "tokenId" |
---|
| 41 | + | |
---|
| 42 | + | let elBridgeAddressKey = "elBridgeAddress" |
---|
| 43 | + | |
---|
| 44 | + | let nativeTransfersCountKey = "nativeTransfersCount" |
---|
| 45 | + | |
---|
| 46 | + | func pad (i) = { |
---|
| 47 | + | let s = toString(i) |
---|
| 48 | + | match size(s) { |
---|
| 49 | + | case _ => |
---|
| 50 | + | if ((1 == $match0)) |
---|
| 51 | + | then ("0000000" + s) |
---|
| 52 | + | else if ((2 == $match0)) |
---|
| 53 | + | then ("000000" + s) |
---|
| 54 | + | else if ((3 == $match0)) |
---|
| 55 | + | then ("00000" + s) |
---|
| 56 | + | else if ((4 == $match0)) |
---|
| 57 | + | then ("0000" + s) |
---|
| 58 | + | else if ((5 == $match0)) |
---|
| 59 | + | then ("000" + s) |
---|
| 60 | + | else if ((6 == $match0)) |
---|
| 61 | + | then ("00" + s) |
---|
| 62 | + | else if ((7 == $match0)) |
---|
| 63 | + | then ("0" + s) |
---|
| 64 | + | else s |
---|
| 65 | + | } |
---|
| 66 | + | } |
---|
| 67 | + | |
---|
| 68 | + | |
---|
| 69 | + | func blockE2CTransfersKey (blockHashHex) = ("elToClTransfers_0x" + blockHashHex) |
---|
| 70 | + | |
---|
| 71 | + | |
---|
| 72 | + | func epochMetaKey (epoch) = ("epoch_" + pad(epoch)) |
---|
| 73 | + | |
---|
| 74 | + | |
---|
| 75 | + | func chainFirstBlockIdKey (chainId) = (("chain" + toString(chainId)) + "FirstBlock") |
---|
| 76 | + | |
---|
| 77 | + | |
---|
| 78 | + | func chainMetaKey (chainId) = ("chain_" + pad(chainId)) |
---|
| 79 | + | |
---|
| 80 | + | |
---|
| 81 | + | func chainLastHeightKey (chainId,miner) = ((("chain_" + pad(chainId)) + "_") + toString(miner)) |
---|
| 82 | + | |
---|
| 83 | + | |
---|
| 84 | + | func chainForkedHeightKey (chainId) = (("chain_" + pad(chainId)) + "ForkedHeight") |
---|
| 85 | + | |
---|
| 86 | + | |
---|
| 87 | + | func supportersKey (chainId) = (("chain" + toString(chainId)) + "Supporters") |
---|
| 88 | + | |
---|
| 89 | + | |
---|
| 90 | + | func minerRewardAddressKey (minerAddr) = (("miner_" + minerAddr) + "_RewardAddress") |
---|
| 91 | + | |
---|
| 92 | + | |
---|
| 93 | + | func minerPkKey (rewardAddress) = (("miner_0x" + rewardAddress) + "_PK") |
---|
| 94 | + | |
---|
| 95 | + | |
---|
| 96 | + | func minerChainIdKey (miner) = (("miner_" + toString(miner)) + "_ChainId") |
---|
| 97 | + | |
---|
| 98 | + | |
---|
| 99 | + | let nativeTransfersCount = valueOrElse(getInteger(this, nativeTransfersCountKey), 0) |
---|
| 100 | + | |
---|
| 101 | + | func nativeTransferKey (index) = ("nativeTransfer_" + toString(index)) |
---|
| 102 | + | |
---|
| 103 | + | |
---|
| 104 | + | func mkNativeTransferEntry (index,destElAddressHex,amount) = StringEntry(nativeTransferKey(index), ((("0x" + destElAddressHex) + SEP) + toString(amount))) |
---|
| 105 | + | |
---|
| 106 | + | |
---|
| 107 | + | func ensureCorrectTransfers (refTransferIndex,transferIndex,expectReward) = { |
---|
| 108 | + | let maxTransfers = if (expectReward) |
---|
| 109 | + | then (MAX_CL_TO_EL_TRANSFERS - 1) |
---|
| 110 | + | else MAX_CL_TO_EL_TRANSFERS |
---|
| 111 | + | let actualTransfers = (transferIndex - refTransferIndex) |
---|
| 112 | + | let checkNumber = if ((actualTransfers > maxTransfers)) |
---|
| 113 | + | then throw(((("Allowed only " + toString(maxTransfers)) + " transfers, got ") + toString(actualTransfers))) |
---|
| 114 | + | else true |
---|
| 115 | + | if ((checkNumber == checkNumber)) |
---|
| 116 | + | then if ((transferIndex >= nativeTransfersCount)) |
---|
| 117 | + | then throw(((("Attempt to transfer #" + toString(transferIndex)) + ". Available transfers: ") + toString(nativeTransfersCount))) |
---|
| 118 | + | else true |
---|
| 119 | + | else throw("Strict value is not equal to itself.") |
---|
| 120 | + | } |
---|
| 121 | + | |
---|
| 122 | + | |
---|
| 123 | + | func amountGtEq (t,gtEq,queueSize) = if ((t.amount >= gtEq)) |
---|
| 124 | + | then true |
---|
| 125 | + | else throw((((((("Transferring amount " + toString(t.amount)) + " should be >= ") + toString(gtEq)) + " for queue size of ") + toString(queueSize)) + ". Transfer more or wait")) |
---|
| 126 | + | |
---|
| 127 | + | |
---|
| 128 | + | func generatingBalance (address) = wavesBalance(address).generating |
---|
| 129 | + | |
---|
| 130 | + | |
---|
| 131 | + | func chainMeta (chainId) = { |
---|
| 132 | + | let s = getStringValue(chainMetaKey(chainId)) |
---|
| 133 | + | let items = split(s, SEP) |
---|
| 134 | + | $Tuple2(parseIntValue(items[0]), items[1]) |
---|
| 135 | + | } |
---|
| 136 | + | |
---|
| 137 | + | |
---|
| 138 | + | func mkChainMetaEntry (chainId,newChainHeight,blockHashHex) = StringEntry(chainMetaKey(chainId), ((toString(newChainHeight) + SEP) + blockHashHex)) |
---|
| 139 | + | |
---|
| 140 | + | |
---|
| 141 | + | let mainChainId = valueOrElse(getInteger(mainChainIdKey), 0) |
---|
| 142 | + | |
---|
| 143 | + | let $t050245090 = chainMeta(mainChainId) |
---|
| 144 | + | |
---|
| 145 | + | let mainChainHeight = $t050245090._1 |
---|
| 146 | + | |
---|
| 147 | + | let mainChainLastBlock = $t050245090._2 |
---|
| 148 | + | |
---|
| 149 | + | func epochMeta (epoch) = match getString(epochMetaKey(epoch)) { |
---|
| 150 | + | case s: String => |
---|
| 151 | + | let fragments = split(s, SEP) |
---|
| 152 | + | $Tuple3(addressFromStringValue(fragments[0]), parseIntValue(fragments[1]), fragments[2]) |
---|
| 153 | + | case _ => |
---|
| 154 | + | unit |
---|
| 155 | + | } |
---|
| 156 | + | |
---|
| 157 | + | |
---|
| 158 | + | let $t053215445 = match epochMeta(height) { |
---|
| 159 | + | case m: (Address, Int, String) => |
---|
| 160 | + | m |
---|
| 161 | + | case _ => |
---|
| 162 | + | $Tuple2(unit, 0) |
---|
| 163 | + | } |
---|
| 164 | + | |
---|
| 165 | + | let thisEpochMiner = $t053215445._1 |
---|
| 166 | + | |
---|
| 167 | + | let thisEpochRef = $t053215445._2 |
---|
| 168 | + | |
---|
| 169 | + | let allMinersStr = valueOrElse(getString(allMinersKey), "") |
---|
| 170 | + | |
---|
| 171 | + | let allMiners = match allMinersStr { |
---|
| 172 | + | case _ => |
---|
| 173 | + | if (("" == $match0)) |
---|
| 174 | + | then nil |
---|
| 175 | + | else if ($isInstanceOf($match0, "String")) |
---|
| 176 | + | then { |
---|
| 177 | + | let raw = $match0 |
---|
| 178 | + | split_4C(raw, SEP) |
---|
| 179 | + | } |
---|
| 180 | + | else throw("Match error") |
---|
| 181 | + | } |
---|
| 182 | + | |
---|
| 183 | + | func blockMeta (blockId) = { |
---|
| 184 | + | let meta = getBinaryValue((blockMetaK + blockId)) |
---|
| 185 | + | let metaSize = size(meta) |
---|
| 186 | + | let blockHeight = toInt(meta) |
---|
| 187 | + | let blockEpoch = toInt(meta, 8) |
---|
| 188 | + | let blockParent = take(drop(meta, 16), BLOCK_HASH_SIZE) |
---|
| 189 | + | let chainId = toInt(meta, (16 + BLOCK_HASH_SIZE)) |
---|
| 190 | + | let baseOffset = (24 + BLOCK_HASH_SIZE) |
---|
| 191 | + | let remainingBytes = (metaSize - baseOffset) |
---|
| 192 | + | let e2cTransfersRootHash = if ((remainingBytes >= ROOT_HASH_SIZE)) |
---|
| 193 | + | then take(drop(meta, baseOffset), ROOT_HASH_SIZE) |
---|
| 194 | + | else base58'' |
---|
| 195 | + | let lastC2ETransferIndex = if (if ((remainingBytes == 8)) |
---|
| 196 | + | then true |
---|
| 197 | + | else (remainingBytes > ROOT_HASH_SIZE)) |
---|
| 198 | + | then toInt(meta, (baseOffset + size(e2cTransfersRootHash))) |
---|
| 199 | + | else -1 |
---|
| 200 | + | $Tuple6(blockHeight, blockEpoch, blockParent, chainId, e2cTransfersRootHash, lastC2ETransferIndex) |
---|
| 201 | + | } |
---|
| 202 | + | |
---|
| 203 | + | |
---|
| 204 | + | func mkBlockMetaEntry (blockHashHex,blockHeight,blockParentHex,chainId,e2cTransfersRootHashHex,lastC2ETransferIndex) = { |
---|
| 205 | + | let e2cTransfersRootHashBytes = fromBase16String(e2cTransfersRootHashHex) |
---|
| 206 | + | let rootHashBytesSize = size(e2cTransfersRootHashBytes) |
---|
| 207 | + | let checkRootHash = if (if ((rootHashBytesSize == 0)) |
---|
| 208 | + | then true |
---|
| 209 | + | else (rootHashBytesSize == ROOT_HASH_SIZE)) |
---|
| 210 | + | then true |
---|
| 211 | + | else throw(((("Transfers root hash should have 0 or " + toString(ROOT_HASH_SIZE)) + " bytes, got ") + toString(rootHashBytesSize))) |
---|
| 212 | + | if ((checkRootHash == checkRootHash)) |
---|
| 213 | + | then { |
---|
| 214 | + | let blockMetaBytes = (((((toBytes(blockHeight) + toBytes(height)) + fromBase16String(blockParentHex)) + toBytes(chainId)) + e2cTransfersRootHashBytes) + toBytes(lastC2ETransferIndex)) |
---|
| 215 | + | BinaryEntry((blockMetaK + blockHashHex), blockMetaBytes) |
---|
| 216 | + | } |
---|
| 217 | + | else throw("Strict value is not equal to itself.") |
---|
| 218 | + | } |
---|
| 219 | + | |
---|
| 220 | + | |
---|
| 221 | + | func lastHeightBy (miner,chainId) = match getInteger(chainLastHeightKey(chainId, miner)) { |
---|
| 222 | + | case h: Int => |
---|
| 223 | + | h |
---|
| 224 | + | case _ => |
---|
| 225 | + | let blockHash = getStringValue(((("chain" + toString(chainId)) + "LastMinedBy") + toString(miner))) |
---|
| 226 | + | blockMeta(blockHash)._1 |
---|
| 227 | + | } |
---|
| 228 | + | |
---|
| 229 | + | |
---|
| 230 | + | let $t074858420 = { |
---|
| 231 | + | let hitSource = match lastBlock.vrf { |
---|
| 232 | + | case vrf: ByteVector => |
---|
| 233 | + | vrf |
---|
| 234 | + | case _ => |
---|
| 235 | + | lastBlock.generationSignature |
---|
| 236 | + | } |
---|
| 237 | + | func processMiner (prev,miner) = { |
---|
| 238 | + | let $t077527815 = prev |
---|
| 239 | + | let prevMiner = $t077527815._1 |
---|
| 240 | + | let prevTotalBalance = $t077527815._2 |
---|
| 241 | + | let prevDelay = $t077527815._3 |
---|
| 242 | + | let prevMiners = $t077527815._4 |
---|
| 243 | + | let minerAddress = addressFromStringValue(miner) |
---|
| 244 | + | let wavesGenBalance = wavesBalance(minerAddress).generating |
---|
| 245 | + | let minerBalance = generatingBalance(minerAddress) |
---|
| 246 | + | if (if ((MIN_BALANCE > wavesGenBalance)) |
---|
| 247 | + | then true |
---|
| 248 | + | else (0 >= minerBalance)) |
---|
| 249 | + | then prev |
---|
| 250 | + | else { |
---|
| 251 | + | let nextDelay = calculateDelay(minerAddress, minerBalance) |
---|
| 252 | + | if ((prevDelay > nextDelay)) |
---|
| 253 | + | then $Tuple4(miner, (prevTotalBalance + minerBalance), nextDelay, (prevMiners :+ miner)) |
---|
| 254 | + | else $Tuple4(prevMiner, (prevTotalBalance + minerBalance), prevDelay, (prevMiners :+ miner)) |
---|
| 255 | + | } |
---|
| 256 | + | } |
---|
| 257 | + | |
---|
| 258 | + | let $l = allMiners |
---|
| 259 | + | let $s = size($l) |
---|
| 260 | + | let $acc0 = $Tuple4("", 0, INT_MAX, nil) |
---|
| 261 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 262 | + | then $a |
---|
| 263 | + | else processMiner($a, $l[$i]) |
---|
| 264 | + | |
---|
| 265 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 266 | + | then $a |
---|
| 267 | + | else throw("List size exceeds 50") |
---|
| 268 | + | |
---|
| 269 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20), 21), 22), 23), 24), 25), 26), 27), 28), 29), 30), 31), 32), 33), 34), 35), 36), 37), 38), 39), 40), 41), 42), 43), 44), 45), 46), 47), 48), 49), 50) |
---|
| 270 | + | } |
---|
| 271 | + | |
---|
| 272 | + | let computedGenerator = $t074858420._1 |
---|
| 273 | + | |
---|
| 274 | + | let computedTotalBalance = $t074858420._2 |
---|
| 275 | + | |
---|
| 276 | + | let $t084228488 = blockMeta(mainChainLastBlock) |
---|
| 277 | + | |
---|
| 278 | + | let mclbIgnored1 = $t084228488._1 |
---|
| 279 | + | |
---|
| 280 | + | let mainChainEpoch = $t084228488._2 |
---|
| 281 | + | |
---|
| 282 | + | func calculateFinalizedBlockHash (curMiner,curPrevEpoch,curLastBlockHash) = { |
---|
| 283 | + | let offsets_100 = split_4C("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::", "") |
---|
| 284 | + | let halfBalance = (computedTotalBalance / 2) |
---|
| 285 | + | func step (prev,next) = { |
---|
| 286 | + | let $t088548918 = prev |
---|
| 287 | + | let thisEpoch = $t088548918._1 |
---|
| 288 | + | let maybeSafeEpoch = $t088548918._2 |
---|
| 289 | + | let totalBalance = $t088548918._3 |
---|
| 290 | + | let prevMiners = $t088548918._4 |
---|
| 291 | + | match maybeSafeEpoch { |
---|
| 292 | + | case _: Unit => |
---|
| 293 | + | let $t089769142 = if ((thisEpoch == height)) |
---|
| 294 | + | then $Tuple3(curMiner, curPrevEpoch, curLastBlockHash) |
---|
| 295 | + | else value(epochMeta(thisEpoch)) |
---|
| 296 | + | let miner = $t089769142._1 |
---|
| 297 | + | let prevEpoch = $t089769142._2 |
---|
| 298 | + | let lastBlockHash = $t089769142._3 |
---|
| 299 | + | if (if ((prevEpoch == 0)) |
---|
| 300 | + | then true |
---|
| 301 | + | else ((height - thisEpoch) >= 100)) |
---|
| 302 | + | then $Tuple4(thisEpoch, lastBlockHash, totalBalance, allMiners) |
---|
| 303 | + | else { |
---|
| 304 | + | let $t092969498 = if (containsElement(prevMiners, miner)) |
---|
| 305 | + | then $Tuple2(totalBalance, prevMiners) |
---|
| 306 | + | else $Tuple2((totalBalance + generatingBalance(miner)), miner :: prevMiners) |
---|
| 307 | + | let newTotalBalance = $t092969498._1 |
---|
| 308 | + | let newMiners = $t092969498._2 |
---|
| 309 | + | if ((newTotalBalance > halfBalance)) |
---|
| 310 | + | then $Tuple4(thisEpoch, lastBlockHash, newTotalBalance, allMiners) |
---|
| 311 | + | else $Tuple4(prevEpoch, unit, newTotalBalance, newMiners) |
---|
| 312 | + | } |
---|
| 313 | + | case _ => |
---|
| 314 | + | prev |
---|
| 315 | + | } |
---|
| 316 | + | } |
---|
| 317 | + | |
---|
| 318 | + | let $t097319827 = { |
---|
| 319 | + | let $l = offsets_100 |
---|
| 320 | + | let $s = size($l) |
---|
| 321 | + | let $acc0 = $Tuple4(height, unit, 0, nil) |
---|
| 322 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 323 | + | then $a |
---|
| 324 | + | else step($a, $l[$i]) |
---|
| 325 | + | |
---|
| 326 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 327 | + | then $a |
---|
| 328 | + | else throw("List size exceeds 100") |
---|
| 329 | + | |
---|
| 330 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20), 21), 22), 23), 24), 25), 26), 27), 28), 29), 30), 31), 32), 33), 34), 35), 36), 37), 38), 39), 40), 41), 42), 43), 44), 45), 46), 47), 48), 49), 50), 51), 52), 53), 54), 55), 56), 57), 58), 59), 60), 61), 62), 63), 64), 65), 66), 67), 68), 69), 70), 71), 72), 73), 74), 75), 76), 77), 78), 79), 80), 81), 82), 83), 84), 85), 86), 87), 88), 89), 90), 91), 92), 93), 94), 95), 96), 97), 98), 99), 100) |
---|
| 331 | + | } |
---|
| 332 | + | let fallbackEpoch = $t097319827._1 |
---|
| 333 | + | let finalizedBlockHashOpt = $t097319827._2 |
---|
| 334 | + | match finalizedBlockHashOpt { |
---|
| 335 | + | case finalizedBlockHash: String => |
---|
| 336 | + | finalizedBlockHash |
---|
| 337 | + | case _ => |
---|
| 338 | + | value(epochMeta(fallbackEpoch))._3 |
---|
| 339 | + | } |
---|
| 340 | + | } |
---|
| 341 | + | |
---|
| 342 | + | |
---|
| 343 | + | func supportingBalance (chainId) = { |
---|
| 344 | + | func addBalance (acc,generatorStr) = { |
---|
| 345 | + | let $t01009710133 = acc |
---|
| 346 | + | let totalBalance = $t01009710133._1 |
---|
| 347 | + | let generators = $t01009710133._2 |
---|
| 348 | + | let generator = addressFromStringValue(generatorStr) |
---|
| 349 | + | if (containsElement(generators, generator)) |
---|
| 350 | + | then acc |
---|
| 351 | + | else { |
---|
| 352 | + | let balance = generatingBalance(generator) |
---|
| 353 | + | $Tuple2((totalBalance + balance), (generators :+ generator)) |
---|
| 354 | + | } |
---|
| 355 | + | } |
---|
| 356 | + | |
---|
| 357 | + | let allGenerators = split_4C(getStringValue(supportersKey(chainId)), SEP) |
---|
| 358 | + | let $t01045510520 = { |
---|
| 359 | + | let $l = allGenerators |
---|
| 360 | + | let $s = size($l) |
---|
| 361 | + | let $acc0 = $Tuple2(0, nil) |
---|
| 362 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 363 | + | then $a |
---|
| 364 | + | else addBalance($a, $l[$i]) |
---|
| 365 | + | |
---|
| 366 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 367 | + | then $a |
---|
| 368 | + | else throw("List size exceeds 100") |
---|
| 369 | + | |
---|
| 370 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20), 21), 22), 23), 24), 25), 26), 27), 28), 29), 30), 31), 32), 33), 34), 35), 36), 37), 38), 39), 40), 41), 42), 43), 44), 45), 46), 47), 48), 49), 50), 51), 52), 53), 54), 55), 56), 57), 58), 59), 60), 61), 62), 63), 64), 65), 66), 67), 68), 69), 70), 71), 72), 73), 74), 75), 76), 77), 78), 79), 80), 81), 82), 83), 84), 85), 86), 87), 88), 89), 90), 91), 92), 93), 94), 95), 96), 97), 98), 99), 100) |
---|
| 371 | + | } |
---|
| 372 | + | let balance = $t01045510520._1 |
---|
| 373 | + | let _g = $t01045510520._2 |
---|
| 374 | + | balance |
---|
| 375 | + | } |
---|
| 376 | + | |
---|
| 377 | + | |
---|
| 378 | + | func isContractSetup () = isDefined(getInteger(minerRewardKey)) |
---|
| 379 | + | |
---|
| 380 | + | |
---|
| 381 | + | func ensureMiningEpoch (generator) = if ((toString(generator) != computedGenerator)) |
---|
| 382 | + | then throw(((((toBase58String(generator.bytes) + " is not allowed to mine in ") + toString(height)) + " epoch. Expected ") + computedGenerator)) |
---|
| 383 | + | else unit |
---|
| 384 | + | |
---|
| 385 | + | |
---|
| 386 | + | func isReferenceCorrect (reference,lastBlock) = if ((reference == lastBlock)) |
---|
| 387 | + | then unit |
---|
| 388 | + | else throw(((("Expected a reference to the chain last block: 0x" + lastBlock) + ". Got: 0x") + reference)) |
---|
| 389 | + | |
---|
| 390 | + | |
---|
| 391 | + | func chainIsInactive (chainId) = { |
---|
| 392 | + | let firstBlockId = getStringValue(chainFirstBlockIdKey(chainId)) |
---|
| 393 | + | let firstValidAltChainId = valueOrElse(getInteger(firstValidAltChainIdKey), 0) |
---|
| 394 | + | if ((firstValidAltChainId > chainId)) |
---|
| 395 | + | then true |
---|
| 396 | + | else (blockMeta(getStringValue(finalizedBlockKey))._1 > blockMeta(firstBlockId)._1) |
---|
| 397 | + | } |
---|
| 398 | + | |
---|
| 399 | + | |
---|
| 400 | + | func minerChainId (miner) = valueOrElse(getInteger(minerChainIdKey(miner)), getInteger(("chainIdOf" + toString(miner)))) |
---|
| 401 | + | |
---|
| 402 | + | |
---|
| 403 | + | func ensureExpectedOrInactiveChain (generator,expectedChainId,checkHeightBlock) = { |
---|
| 404 | + | let heightIsCorrect = match checkHeightBlock { |
---|
| 405 | + | case blockHash: String => |
---|
| 406 | + | let lastMinedBlockHeight = lastHeightBy(generator, mainChainId) |
---|
| 407 | + | ((blockMeta(blockHash)._1 + 1) > lastMinedBlockHeight) |
---|
| 408 | + | case _ => |
---|
| 409 | + | true |
---|
| 410 | + | } |
---|
| 411 | + | match minerChainId(generator) { |
---|
| 412 | + | case currentId: Int => |
---|
| 413 | + | if (if ((currentId == expectedChainId)) |
---|
| 414 | + | then true |
---|
| 415 | + | else if (chainIsInactive(currentId)) |
---|
| 416 | + | then heightIsCorrect |
---|
| 417 | + | else false) |
---|
| 418 | + | then unit |
---|
| 419 | + | else throw(("miner is mining other chain " + toString(currentId))) |
---|
| 420 | + | case _ => |
---|
| 421 | + | unit |
---|
| 422 | + | } |
---|
| 423 | + | } |
---|
| 424 | + | |
---|
| 425 | + | |
---|
| 426 | + | let heightString = (" at height " + toString(height)) |
---|
| 427 | + | |
---|
| 428 | + | func vrfAt (height) = valueOrErrorMessage(valueOrErrorMessage(blockInfoByHeight(height), ("last block is not available" + heightString)).vrf, ("VRF is not available" + heightString)) |
---|
| 429 | + | |
---|
| 430 | + | |
---|
| 431 | + | func ensureCorrectEpoch (expectedVRF) = { |
---|
| 432 | + | let actualVRF = vrfAt(height) |
---|
| 433 | + | if ((expectedVRF == actualVRF)) |
---|
| 434 | + | then unit |
---|
| 435 | + | else throw((((("Expected VRF " + toBase58String(expectedVRF)) + " does not match actual ") + toBase58String(actualVRF)) + heightString)) |
---|
| 436 | + | } |
---|
| 437 | + | |
---|
| 438 | + | |
---|
| 439 | + | func addSupporter (chainId,generator) = { |
---|
| 440 | + | let supportersStr = getStringValue(supportersKey(chainId)) |
---|
| 441 | + | let supporters = split_4C(supportersStr, SEP) |
---|
| 442 | + | if (containsElement(supporters, toString(generator))) |
---|
| 443 | + | then nil |
---|
| 444 | + | else [StringEntry(supportersKey(chainId), ((supportersStr + SEP) + toString(generator)))] |
---|
| 445 | + | } |
---|
| 446 | + | |
---|
| 447 | + | |
---|
| 448 | + | func setOrFail (flags,index) = if ((0 > index)) |
---|
| 449 | + | then throw(("Can't withdraw at negative index: " + toString(index))) |
---|
| 450 | + | else { |
---|
| 451 | + | let flagsSize = size(flags) |
---|
| 452 | + | if ((index >= flagsSize)) |
---|
| 453 | + | then { |
---|
| 454 | + | let addZeroes = (index - flagsSize) |
---|
| 455 | + | if ((addZeroes > size(zeroesStr))) |
---|
| 456 | + | then throw((("Can't add " + toString(addZeroes)) + " empty flags. Contact with developers")) |
---|
| 457 | + | else ((flags + take(zeroesStr, addZeroes)) + "1") |
---|
| 458 | + | } |
---|
| 459 | + | else { |
---|
| 460 | + | let tail = drop(flags, index) |
---|
| 461 | + | let atIndex = take(tail, 1) |
---|
| 462 | + | if ((atIndex == "0")) |
---|
| 463 | + | then ((take(flags, index) + "1") + drop(tail, 1)) |
---|
| 464 | + | else throw((("Transfer #" + toString(index)) + " has been already taken")) |
---|
| 465 | + | } |
---|
| 466 | + | } |
---|
| 467 | + | |
---|
| 468 | + | |
---|
| 469 | + | func validateBlockHash (hexStr) = { |
---|
| 470 | + | let decodedBytes = fromBase16String(hexStr) |
---|
| 471 | + | if ((size(decodedBytes) != BLOCK_HASH_SIZE)) |
---|
| 472 | + | then throw("invalid block id length") |
---|
| 473 | + | else hexStr |
---|
| 474 | + | } |
---|
| 475 | + | |
---|
| 476 | + | |
---|
| 477 | + | func getUpdateFinalizedBlockAction (caller,newBlockHashHex,prevEpoch) = { |
---|
| 478 | + | let curFinalizedBlockHeight = blockMeta(getStringValue(finalizedBlockKey))._1 |
---|
| 479 | + | let newFinalizedBlockHash = calculateFinalizedBlockHash(caller, prevEpoch, newBlockHashHex) |
---|
| 480 | + | if (if ((newFinalizedBlockHash == newBlockHashHex)) |
---|
| 481 | + | then true |
---|
| 482 | + | else (blockMeta(newFinalizedBlockHash)._1 > curFinalizedBlockHeight)) |
---|
| 483 | + | then [StringEntry(finalizedBlockKey, newFinalizedBlockHash)] |
---|
| 484 | + | else nil |
---|
| 485 | + | } |
---|
| 486 | + | |
---|
| 487 | + | |
---|
| 488 | + | @Callable(i) |
---|
| 489 | + | func extendMainChain (blockHashHex,referenceHex,vrf,e2cTransfersRootHashHex,lastC2ETransferIndex) = { |
---|
| 490 | + | let checkBlockHash = validateBlockHash(blockHashHex) |
---|
| 491 | + | if ((checkBlockHash == checkBlockHash)) |
---|
| 492 | + | then { |
---|
| 493 | + | let checkEpoch = ensureCorrectEpoch(vrf) |
---|
| 494 | + | if ((checkEpoch == checkEpoch)) |
---|
| 495 | + | then { |
---|
| 496 | + | let checkChain = ensureExpectedOrInactiveChain(i.originCaller, mainChainId, unit) |
---|
| 497 | + | if ((checkChain == checkChain)) |
---|
| 498 | + | then { |
---|
| 499 | + | let checkReference = isReferenceCorrect(referenceHex, mainChainLastBlock) |
---|
| 500 | + | if ((checkReference == checkReference)) |
---|
| 501 | + | then { |
---|
| 502 | + | let checkTransfers = ensureCorrectTransfers(blockMeta(referenceHex)._6, lastC2ETransferIndex, true) |
---|
| 503 | + | if ((checkTransfers == checkTransfers)) |
---|
| 504 | + | then { |
---|
| 505 | + | let thisEpochMeta = match epochMeta(height) { |
---|
| 506 | + | case _: Unit => |
---|
| 507 | + | StringEntry(epochMetaKey(height), ((((toString(i.originCaller) + SEP) + toString(mainChainEpoch)) + SEP) + blockHashHex)) |
---|
| 508 | + | case other => |
---|
| 509 | + | throw("Epoch already started") |
---|
| 510 | + | } |
---|
| 511 | + | if ((thisEpochMeta == thisEpochMeta)) |
---|
| 512 | + | then { |
---|
| 513 | + | let checkGenerator = ensureMiningEpoch(i.originCaller) |
---|
| 514 | + | if ((checkGenerator == checkGenerator)) |
---|
| 515 | + | then { |
---|
| 516 | + | let updateFinalizedBlock = getUpdateFinalizedBlockAction(i.originCaller, blockHashHex, mainChainEpoch) |
---|
| 517 | + | let newChainHeight = (mainChainHeight + 1) |
---|
| 518 | + | ([mkBlockMetaEntry(blockHashHex, newChainHeight, mainChainLastBlock, mainChainId, e2cTransfersRootHashHex, lastC2ETransferIndex), mkChainMetaEntry(mainChainId, newChainHeight, blockHashHex), IntegerEntry(minerChainIdKey(i.originCaller), mainChainId), IntegerEntry(chainLastHeightKey(mainChainId, i.originCaller), newChainHeight), thisEpochMeta] ++ updateFinalizedBlock) |
---|
| 519 | + | } |
---|
| 520 | + | else throw("Strict value is not equal to itself.") |
---|
| 521 | + | } |
---|
| 522 | + | else throw("Strict value is not equal to itself.") |
---|
| 523 | + | } |
---|
| 524 | + | else throw("Strict value is not equal to itself.") |
---|
| 525 | + | } |
---|
| 526 | + | else throw("Strict value is not equal to itself.") |
---|
| 527 | + | } |
---|
| 528 | + | else throw("Strict value is not equal to itself.") |
---|
| 529 | + | } |
---|
| 530 | + | else throw("Strict value is not equal to itself.") |
---|
| 531 | + | } |
---|
| 532 | + | else throw("Strict value is not equal to itself.") |
---|
| 533 | + | } |
---|
| 534 | + | |
---|
| 535 | + | |
---|
| 536 | + | |
---|
| 537 | + | @Callable(i) |
---|
| 538 | + | func startAltChain (blockHashHex,referenceHex,vrf,e2cTransfersRootHashHex,lastC2ETransferIndex) = { |
---|
| 539 | + | let checkBlockHash = validateBlockHash(blockHashHex) |
---|
| 540 | + | if ((checkBlockHash == checkBlockHash)) |
---|
| 541 | + | then { |
---|
| 542 | + | let checkEpoch = ensureCorrectEpoch(vrf) |
---|
| 543 | + | if ((checkEpoch == checkEpoch)) |
---|
| 544 | + | then { |
---|
| 545 | + | let $t01612716240 = blockMeta(referenceHex) |
---|
| 546 | + | let refChainHeight = $t01612716240._1 |
---|
| 547 | + | let refEpoch = $t01612716240._2 |
---|
| 548 | + | let refIgnored1 = $t01612716240._3 |
---|
| 549 | + | let refIgnored2 = $t01612716240._4 |
---|
| 550 | + | let refIgnored3 = $t01612716240._5 |
---|
| 551 | + | let refTransferIndex = $t01612716240._6 |
---|
| 552 | + | let finalizedEpoch = blockMeta(getStringValue(finalizedBlockKey))._2 |
---|
| 553 | + | let epochRef = if ((refEpoch >= finalizedEpoch)) |
---|
| 554 | + | then refEpoch |
---|
| 555 | + | else throw((((("Can not start alt chain from epoch " + toString(refEpoch)) + ", epoch ") + toString(finalizedEpoch)) + " is finalized")) |
---|
| 556 | + | let checkChain = ensureExpectedOrInactiveChain(i.originCaller, mainChainId, referenceHex) |
---|
| 557 | + | if ((checkChain == checkChain)) |
---|
| 558 | + | then { |
---|
| 559 | + | let checkTransfers = ensureCorrectTransfers(refTransferIndex, lastC2ETransferIndex, true) |
---|
| 560 | + | if ((checkTransfers == checkTransfers)) |
---|
| 561 | + | then { |
---|
| 562 | + | let newChainId = (valueOrElse(getInteger(lastChainIdKey), 0) + 1) |
---|
| 563 | + | let newChainHeight = (refChainHeight + 1) |
---|
| 564 | + | let thisEpochMeta = match epochMeta(height) { |
---|
| 565 | + | case _: Unit => |
---|
| 566 | + | StringEntry(epochMetaKey(height), ((((toString(i.originCaller) + SEP) + toString(epochRef)) + SEP) + blockHashHex)) |
---|
| 567 | + | case other => |
---|
| 568 | + | throw("Epoch already started") |
---|
| 569 | + | } |
---|
| 570 | + | let checkGenerator = ensureMiningEpoch(i.originCaller) |
---|
| 571 | + | if ((checkGenerator == checkGenerator)) |
---|
| 572 | + | then [thisEpochMeta, mkBlockMetaEntry(blockHashHex, newChainHeight, referenceHex, newChainId, e2cTransfersRootHashHex, lastC2ETransferIndex), StringEntry(chainFirstBlockIdKey(newChainId), blockHashHex), mkChainMetaEntry(newChainId, newChainHeight, blockHashHex), IntegerEntry(minerChainIdKey(i.originCaller), newChainId), IntegerEntry(chainLastHeightKey(newChainId, i.originCaller), newChainHeight), IntegerEntry(chainLastHeightKey(mainChainId, i.originCaller), newChainHeight), StringEntry(supportersKey(newChainId), toString(i.originCaller)), IntegerEntry(lastChainIdKey, newChainId)] |
---|
| 573 | + | else throw("Strict value is not equal to itself.") |
---|
| 574 | + | } |
---|
| 575 | + | else throw("Strict value is not equal to itself.") |
---|
| 576 | + | } |
---|
| 577 | + | else throw("Strict value is not equal to itself.") |
---|
| 578 | + | } |
---|
| 579 | + | else throw("Strict value is not equal to itself.") |
---|
| 580 | + | } |
---|
| 581 | + | else throw("Strict value is not equal to itself.") |
---|
| 582 | + | } |
---|
| 583 | + | |
---|
| 584 | + | |
---|
| 585 | + | |
---|
| 586 | + | @Callable(i) |
---|
| 587 | + | func extendAltChain (blockHashHex,referenceHex,vrf,chainId,e2cTransfersRootHashHex,lastC2ETransferIndex) = { |
---|
| 588 | + | let checkBlockHash = validateBlockHash(blockHashHex) |
---|
| 589 | + | if ((checkBlockHash == checkBlockHash)) |
---|
| 590 | + | then { |
---|
| 591 | + | let checkEpoch = ensureCorrectEpoch(vrf) |
---|
| 592 | + | if ((checkEpoch == checkEpoch)) |
---|
| 593 | + | then { |
---|
| 594 | + | let chainFirstBlockMeta = blockMeta(getStringValue(chainFirstBlockIdKey(chainId))) |
---|
| 595 | + | let checkChain = ensureExpectedOrInactiveChain(i.originCaller, chainId, toBase16String(chainFirstBlockMeta._3)) |
---|
| 596 | + | if ((checkChain == checkChain)) |
---|
| 597 | + | then { |
---|
| 598 | + | let $t01855318607 = chainMeta(chainId) |
---|
| 599 | + | let chainHeight = $t01855318607._1 |
---|
| 600 | + | let chainLastBlock = $t01855318607._2 |
---|
| 601 | + | let checkReference = isReferenceCorrect(referenceHex, chainLastBlock) |
---|
| 602 | + | if ((checkReference == checkReference)) |
---|
| 603 | + | then { |
---|
| 604 | + | let checkTransfers = ensureCorrectTransfers(blockMeta(referenceHex)._6, lastC2ETransferIndex, true) |
---|
| 605 | + | if ((checkTransfers == checkTransfers)) |
---|
| 606 | + | then { |
---|
| 607 | + | let newChainHeight = (chainHeight + 1) |
---|
| 608 | + | let prevEpoch = blockMeta(referenceHex)._2 |
---|
| 609 | + | let updateMainChainData = if ((supportingBalance(chainId) > (computedTotalBalance / 2))) |
---|
| 610 | + | then { |
---|
| 611 | + | let lastChainId = valueOrElse(getInteger(lastChainIdKey), 0) |
---|
| 612 | + | let updateFinalizedBlock = getUpdateFinalizedBlockAction(i.originCaller, blockHashHex, prevEpoch) |
---|
| 613 | + | ([IntegerEntry(chainForkedHeightKey(mainChainId), chainFirstBlockMeta._1), IntegerEntry(mainChainIdKey, chainId), IntegerEntry(firstValidAltChainIdKey, (lastChainId + 1))] ++ updateFinalizedBlock) |
---|
| 614 | + | } |
---|
| 615 | + | else nil |
---|
| 616 | + | let thisEpochMeta = match epochMeta(height) { |
---|
| 617 | + | case _: Unit => |
---|
| 618 | + | StringEntry(epochMetaKey(height), ((((toString(i.originCaller) + SEP) + toString(prevEpoch)) + SEP) + blockHashHex)) |
---|
| 619 | + | case other => |
---|
| 620 | + | throw("Epoch already started") |
---|
| 621 | + | } |
---|
| 622 | + | if ((thisEpochMeta == thisEpochMeta)) |
---|
| 623 | + | then { |
---|
| 624 | + | let checkGenerator = ensureMiningEpoch(i.originCaller) |
---|
| 625 | + | if ((checkGenerator == checkGenerator)) |
---|
| 626 | + | then { |
---|
| 627 | + | let updateMainChainLastMinedBlock = if (if ((updateMainChainData == nil)) |
---|
| 628 | + | then (valueOrElse(minerChainId(i.originCaller), 0) != chainId) |
---|
| 629 | + | else false) |
---|
| 630 | + | then [IntegerEntry(chainLastHeightKey(mainChainId, i.originCaller), chainFirstBlockMeta._1)] |
---|
| 631 | + | else nil |
---|
| 632 | + | ((([mkBlockMetaEntry(blockHashHex, newChainHeight, referenceHex, chainId, e2cTransfersRootHashHex, lastC2ETransferIndex), mkChainMetaEntry(chainId, newChainHeight, blockHashHex), thisEpochMeta, IntegerEntry(minerChainIdKey(i.originCaller), chainId), IntegerEntry(chainLastHeightKey(chainId, i.originCaller), newChainHeight)] ++ updateMainChainData) ++ addSupporter(chainId, i.originCaller)) ++ updateMainChainLastMinedBlock) |
---|
| 633 | + | } |
---|
| 634 | + | else throw("Strict value is not equal to itself.") |
---|
| 635 | + | } |
---|
| 636 | + | else throw("Strict value is not equal to itself.") |
---|
| 637 | + | } |
---|
| 638 | + | else throw("Strict value is not equal to itself.") |
---|
| 639 | + | } |
---|
| 640 | + | else throw("Strict value is not equal to itself.") |
---|
| 641 | + | } |
---|
| 642 | + | else throw("Strict value is not equal to itself.") |
---|
| 643 | + | } |
---|
| 644 | + | else throw("Strict value is not equal to itself.") |
---|
| 645 | + | } |
---|
| 646 | + | else throw("Strict value is not equal to itself.") |
---|
| 647 | + | } |
---|
| 648 | + | |
---|
| 649 | + | |
---|
| 650 | + | |
---|
| 651 | + | @Callable(i) |
---|
| 652 | + | func appendBlock (blockHashHex,referenceHex,e2cTransfersRootHashHex,lastC2ETransferIndex) = { |
---|
| 653 | + | let checkCaller = if ((thisEpochMiner == i.originCaller)) |
---|
| 654 | + | then true |
---|
| 655 | + | else match thisEpochMiner { |
---|
| 656 | + | case epochMiner: Address => |
---|
| 657 | + | throw(("not allowed to forge blocks in this epoch, expected from " + toString(epochMiner))) |
---|
| 658 | + | case _ => |
---|
| 659 | + | throw("not allowed to forge blocks in this epoch, epoch miner is absent") |
---|
| 660 | + | } |
---|
| 661 | + | if ((checkCaller == checkCaller)) |
---|
| 662 | + | then { |
---|
| 663 | + | let chainId = valueOrElse(minerChainId(i.originCaller), mainChainId) |
---|
| 664 | + | let $t02095621007 = chainMeta(chainId) |
---|
| 665 | + | let chainHeight = $t02095621007._1 |
---|
| 666 | + | let lastBlockId = $t02095621007._2 |
---|
| 667 | + | let checkReference = isReferenceCorrect(referenceHex, lastBlockId) |
---|
| 668 | + | if ((checkReference == checkReference)) |
---|
| 669 | + | then { |
---|
| 670 | + | let checkTransfers = ensureCorrectTransfers(blockMeta(referenceHex)._6, lastC2ETransferIndex, false) |
---|
| 671 | + | if ((checkTransfers == checkTransfers)) |
---|
| 672 | + | then { |
---|
| 673 | + | let newChainHeight = (chainHeight + 1) |
---|
| 674 | + | let checkBlockHash = validateBlockHash(blockHashHex) |
---|
| 675 | + | if ((checkBlockHash == checkBlockHash)) |
---|
| 676 | + | then [mkBlockMetaEntry(blockHashHex, newChainHeight, lastBlockId, chainId, e2cTransfersRootHashHex, lastC2ETransferIndex), IntegerEntry(chainLastHeightKey(chainId, i.originCaller), newChainHeight), mkChainMetaEntry(chainId, newChainHeight, blockHashHex), StringEntry(epochMetaKey(height), ((((toString(value(thisEpochMiner)) + SEP) + toString(thisEpochRef)) + SEP) + blockHashHex))] |
---|
| 677 | + | else throw("Strict value is not equal to itself.") |
---|
| 678 | + | } |
---|
| 679 | + | else throw("Strict value is not equal to itself.") |
---|
| 680 | + | } |
---|
| 681 | + | else throw("Strict value is not equal to itself.") |
---|
| 682 | + | } |
---|
| 683 | + | else throw("Strict value is not equal to itself.") |
---|
| 684 | + | } |
---|
| 685 | + | |
---|
| 686 | + | |
---|
| 687 | + | |
---|
| 688 | + | @Callable(i) |
---|
| 689 | + | func join (rewardAddressHex) = { |
---|
| 690 | + | func ensureNotOverrideOtherMinerPk (elAddressHex) = match getBinary(minerPkKey(elAddressHex)) { |
---|
| 691 | + | case pk: ByteVector => |
---|
| 692 | + | if ((pk == i.originCallerPublicKey)) |
---|
| 693 | + | then unit |
---|
| 694 | + | else throw(((("EL miner address " + elAddressHex) + " is already linked with ") + toBase58String(pk))) |
---|
| 695 | + | case _ => |
---|
| 696 | + | unit |
---|
| 697 | + | } |
---|
| 698 | + | |
---|
| 699 | + | let parts = split(rewardAddressHex, "0x") |
---|
| 700 | + | let rewardAddress = fromBase16String(parts[(size(parts) - 1)]) |
---|
| 701 | + | if (!(isContractSetup())) |
---|
| 702 | + | then throw("The contract has not yet set up") |
---|
| 703 | + | else if ((MIN_BALANCE > wavesBalance(i.originCaller).generating)) |
---|
| 704 | + | then throw(((("Insufficient generating balance: " + toString(wavesBalance(i.originCaller).generating)) + ". Required: ") + toString(MIN_BALANCE))) |
---|
| 705 | + | else if ((size(rewardAddress) != 20)) |
---|
| 706 | + | then throw("rewardAddress should be an L2 address") |
---|
| 707 | + | else if ((size(allMiners) >= 50)) |
---|
| 708 | + | then throw("too many miners") |
---|
| 709 | + | else { |
---|
| 710 | + | func checkExistence (exists,miner) = if (exists) |
---|
| 711 | + | then true |
---|
| 712 | + | else (miner == toString(i.originCaller)) |
---|
| 713 | + | |
---|
| 714 | + | let alreadyExists = { |
---|
| 715 | + | let $l = allMiners |
---|
| 716 | + | let $s = size($l) |
---|
| 717 | + | let $acc0 = false |
---|
| 718 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 719 | + | then $a |
---|
| 720 | + | else checkExistence($a, $l[$i]) |
---|
| 721 | + | |
---|
| 722 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 723 | + | then $a |
---|
| 724 | + | else throw("List size exceeds 50") |
---|
| 725 | + | |
---|
| 726 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20), 21), 22), 23), 24), 25), 26), 27), 28), 29), 30), 31), 32), 33), 34), 35), 36), 37), 38), 39), 40), 41), 42), 43), 44), 45), 46), 47), 48), 49), 50) |
---|
| 727 | + | } |
---|
| 728 | + | if (alreadyExists) |
---|
| 729 | + | then nil |
---|
| 730 | + | else { |
---|
| 731 | + | let newMiner = toString(i.originCaller) |
---|
| 732 | + | let lowercaseRewardAddress = toBase16String(rewardAddress) |
---|
| 733 | + | let check = ensureNotOverrideOtherMinerPk(lowercaseRewardAddress) |
---|
| 734 | + | if ((check == check)) |
---|
| 735 | + | then { |
---|
| 736 | + | let newMiners = if ((size(allMiners) == 0)) |
---|
| 737 | + | then newMiner |
---|
| 738 | + | else ((allMinersStr + SEP) + newMiner) |
---|
| 739 | + | let deletePrevRewardAddressPk = match getString(minerRewardAddressKey(newMiner)) { |
---|
| 740 | + | case prevAddress: String => |
---|
| 741 | + | if ((prevAddress == toBase16String(rewardAddress))) |
---|
| 742 | + | then nil |
---|
| 743 | + | else [DeleteEntry(minerPkKey(prevAddress))] |
---|
| 744 | + | case _ => |
---|
| 745 | + | nil |
---|
| 746 | + | } |
---|
| 747 | + | ([StringEntry(allMinersKey, newMiners), StringEntry(minerRewardAddressKey(newMiner), ("0x" + lowercaseRewardAddress)), BinaryEntry(minerPkKey(lowercaseRewardAddress), i.originCallerPublicKey)] ++ deletePrevRewardAddressPk) |
---|
| 748 | + | } |
---|
| 749 | + | else throw("Strict value is not equal to itself.") |
---|
| 750 | + | } |
---|
| 751 | + | } |
---|
| 752 | + | } |
---|
| 753 | + | |
---|
| 754 | + | |
---|
| 755 | + | |
---|
| 756 | + | @Callable(i) |
---|
| 757 | + | func leave () = { |
---|
| 758 | + | let leavingMiner = toString(i.originCaller) |
---|
| 759 | + | func skipLeavingMiner (acc,miner) = if ((miner == leavingMiner)) |
---|
| 760 | + | then acc |
---|
| 761 | + | else (acc :+ miner) |
---|
| 762 | + | |
---|
| 763 | + | let remainingMiners = { |
---|
| 764 | + | let $l = allMiners |
---|
| 765 | + | let $s = size($l) |
---|
| 766 | + | let $acc0 = nil |
---|
| 767 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 768 | + | then $a |
---|
| 769 | + | else skipLeavingMiner($a, $l[$i]) |
---|
| 770 | + | |
---|
| 771 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 772 | + | then $a |
---|
| 773 | + | else throw("List size exceeds 50") |
---|
| 774 | + | |
---|
| 775 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20), 21), 22), 23), 24), 25), 26), 27), 28), 29), 30), 31), 32), 33), 34), 35), 36), 37), 38), 39), 40), 41), 42), 43), 44), 45), 46), 47), 48), 49), 50) |
---|
| 776 | + | } |
---|
| 777 | + | let rewardAddrKey = minerRewardAddressKey(leavingMiner) |
---|
| 778 | + | let prevRewardAddress = valueOrErrorMessage(getString(this, rewardAddrKey), "miner has never joined") |
---|
| 779 | + | if ((prevRewardAddress == prevRewardAddress)) |
---|
| 780 | + | then if ((thisEpochMiner == i.originCaller)) |
---|
| 781 | + | then throw("designated miner can't leave") |
---|
| 782 | + | else [StringEntry(allMinersKey, makeString_2C(remainingMiners, SEP))] |
---|
| 783 | + | else throw("Strict value is not equal to itself.") |
---|
| 784 | + | } |
---|
| 785 | + | |
---|
| 786 | + | |
---|
| 787 | + | |
---|
| 788 | + | @Callable(i) |
---|
| 789 | + | func transfer (destElAddressHex) = { |
---|
| 790 | + | let checkAddress = if ((size(destElAddressHex) == ETH_ADDRESS_STR_SIZE)) |
---|
| 791 | + | then fromBase16String(destElAddressHex) |
---|
| 792 | + | else throw(((("Address should have " + toString(ETH_ADDRESS_STR_SIZE)) + " characters, got ") + toString(size(destElAddressHex)))) |
---|
| 793 | + | if ((checkAddress == checkAddress)) |
---|
| 794 | + | then { |
---|
| 795 | + | let checkPayment = if ((size(i.payments) == 1)) |
---|
| 796 | + | then true |
---|
| 797 | + | else throw(("Expected one payment as a transfer amount, got " + toString(size(i.payments)))) |
---|
| 798 | + | if ((checkPayment == checkPayment)) |
---|
| 799 | + | then { |
---|
| 800 | + | let tokenIdStr = getStringValue(tokenIdKey) |
---|
| 801 | + | let tokenId = fromBase58String(tokenIdStr) |
---|
| 802 | + | let t = i.payments[0] |
---|
| 803 | + | let checkAssetId = match t.assetId { |
---|
| 804 | + | case assetId: ByteVector => |
---|
| 805 | + | if ((assetId == tokenId)) |
---|
| 806 | + | then true |
---|
| 807 | + | else throw(((("Expected " + tokenIdStr) + " in the payment, got ") + toBase58String(assetId))) |
---|
| 808 | + | case _ => |
---|
| 809 | + | throw((("Expected " + tokenIdStr) + " in the payment, got Waves")) |
---|
| 810 | + | } |
---|
| 811 | + | if ((checkAssetId == checkAssetId)) |
---|
| 812 | + | then { |
---|
| 813 | + | let lastIndex = blockMeta(mainChainLastBlock)._6 |
---|
| 814 | + | let queueSize = (nativeTransfersCount - lastIndex) |
---|
| 815 | + | let checkQueueLimit = if ((160 > queueSize)) |
---|
| 816 | + | then amountGtEq(t, 1000000, queueSize) |
---|
| 817 | + | else if ((1600 > queueSize)) |
---|
| 818 | + | then amountGtEq(t, 10000000, queueSize) |
---|
| 819 | + | else if ((3200 > queueSize)) |
---|
| 820 | + | then amountGtEq(t, 100000000, queueSize) |
---|
| 821 | + | else if ((6400 > queueSize)) |
---|
| 822 | + | then amountGtEq(t, 1000000000, queueSize) |
---|
| 823 | + | else throw((("Transfers denied for queue size of " + toString(queueSize)) + ". Wait until current transfers processed")) |
---|
| 824 | + | if ((checkQueueLimit == checkQueueLimit)) |
---|
| 825 | + | then [IntegerEntry(nativeTransfersCountKey, (nativeTransfersCount + 1)), mkNativeTransferEntry(nativeTransfersCount, destElAddressHex, t.amount), Burn(tokenId, t.amount)] |
---|
| 826 | + | else throw("Strict value is not equal to itself.") |
---|
| 827 | + | } |
---|
| 828 | + | else throw("Strict value is not equal to itself.") |
---|
| 829 | + | } |
---|
| 830 | + | else throw("Strict value is not equal to itself.") |
---|
| 831 | + | } |
---|
| 832 | + | else throw("Strict value is not equal to itself.") |
---|
| 833 | + | } |
---|
| 834 | + | |
---|
| 835 | + | |
---|
| 836 | + | |
---|
| 837 | + | @Callable(i) |
---|
| 838 | + | func withdraw (blockHashHex,merkleProof,transferIndexInBlock,amount) = { |
---|
| 839 | + | let withdrawBlockMeta = blockMeta(blockHashHex) |
---|
| 840 | + | let withdrawBlockHeight = withdrawBlockMeta._1 |
---|
| 841 | + | let finalizedBlockHeight = blockMeta(getStringValue(finalizedBlockKey))._1 |
---|
| 842 | + | let mainChainLastBlockHeight = blockMeta(mainChainLastBlock)._1 |
---|
| 843 | + | if ((withdrawBlockHeight > finalizedBlockHeight)) |
---|
| 844 | + | then throw(((("EL block #" + toString(withdrawBlockHeight)) + " is not finalized. The current finalized is #") + toString(finalizedBlockHeight))) |
---|
| 845 | + | else if ((size(merkleProof) != WITHDRAW_PROOFS_SIZE)) |
---|
| 846 | + | then throw(((("Expected " + toString(WITHDRAW_PROOFS_SIZE)) + " proofs, got ") + toString(size(merkleProof)))) |
---|
| 847 | + | else if ((0 > transferIndexInBlock)) |
---|
| 848 | + | then throw(("Transfer index in block should be nonnegative, got " + toString(transferIndexInBlock))) |
---|
| 849 | + | else if ((0 >= amount)) |
---|
| 850 | + | then throw(("Amount should be positive, got " + toString(amount))) |
---|
| 851 | + | else { |
---|
| 852 | + | let withdrawBlockChainId = withdrawBlockMeta._4 |
---|
| 853 | + | let isMainChain = (withdrawBlockChainId == mainChainId) |
---|
| 854 | + | let relatesToMainChain = match getInteger(chainForkedHeightKey(withdrawBlockChainId)) { |
---|
| 855 | + | case forkedHeight: Int => |
---|
| 856 | + | (forkedHeight > withdrawBlockHeight) |
---|
| 857 | + | case _ => |
---|
| 858 | + | throw((((blockHashHex + " is on an alternative chain #") + toString(withdrawBlockChainId)) + " that was not approved by majority. Wait for some blocks")) |
---|
| 859 | + | } |
---|
| 860 | + | if (if (isMainChain) |
---|
| 861 | + | then true |
---|
| 862 | + | else relatesToMainChain) |
---|
| 863 | + | then { |
---|
| 864 | + | let recipient = i.originCaller |
---|
| 865 | + | let recipientPkHash = take(drop(recipient.bytes, 2), PUBLIC_KEY_HASH_SIZE) |
---|
| 866 | + | let zeroAmountBytes = base58'11111111111111111111111111111111111111111111' |
---|
| 867 | + | let amountBytes = toBytes(amount) |
---|
| 868 | + | let elEventData = ((recipientPkHash + take(zeroAmountBytes, (size(zeroAmountBytes) - size(amountBytes)))) + amountBytes) |
---|
| 869 | + | let elEventDataDigest = blake2b256_16Kb(elEventData) |
---|
| 870 | + | let calculatedRootHash = createMerkleRoot(merkleProof, elEventDataDigest, transferIndexInBlock) |
---|
| 871 | + | let expectedRootHash = withdrawBlockMeta._5 |
---|
| 872 | + | if ((calculatedRootHash == expectedRootHash)) |
---|
| 873 | + | then { |
---|
| 874 | + | let tokenId = fromBase58String(getStringValue(tokenIdKey)) |
---|
| 875 | + | let transfersKey = blockE2CTransfersKey(blockHashHex) |
---|
| 876 | + | [Reissue(tokenId, amount, true), ScriptTransfer(recipient, amount, tokenId), StringEntry(transfersKey, setOrFail(valueOrElse(getString(transfersKey), ""), transferIndexInBlock))] |
---|
| 877 | + | } |
---|
| 878 | + | else throw((((((("Expected root hash: " + toBase16String(expectedRootHash)) + ", got: ") + toBase16String(calculatedRootHash)) + ". Event data digest: ") + toBase64String(elEventDataDigest)) + ". Check your withdraw arguments")) |
---|
| 879 | + | } |
---|
| 880 | + | else throw((("Expected " + blockHashHex) + " to be either on the main chain or relate to it")) |
---|
| 881 | + | } |
---|
| 882 | + | } |
---|
| 883 | + | |
---|
| 884 | + | |
---|
| 885 | + | |
---|
| 886 | + | @Callable(i) |
---|
| 887 | + | func setup (genesisBlockHashHex,minerRewardInGwei) = if (isContractSetup()) |
---|
| 888 | + | then throw("The contract has been already set up") |
---|
| 889 | + | else if ((0 > minerRewardInGwei)) |
---|
| 890 | + | then throw("The miner reward must be nonnegative") |
---|
| 891 | + | else { |
---|
| 892 | + | let genesisBlockHash = fromBase16String(genesisBlockHashHex) |
---|
| 893 | + | let checkGenesisBlockHashSize = if ((size(genesisBlockHash) == BLOCK_HASH_SIZE)) |
---|
| 894 | + | then true |
---|
| 895 | + | else throw("Wrong genesis block hash") |
---|
| 896 | + | if ((checkGenesisBlockHashSize == checkGenesisBlockHashSize)) |
---|
| 897 | + | then { |
---|
| 898 | + | let emptyPk = base58'11111111111111111111111111111111' |
---|
| 899 | + | let genesisMinerAddress = addressFromPublicKey(emptyPk) |
---|
| 900 | + | let genesisEthRewardAddress = base58'11111111111111111111' |
---|
| 901 | + | let genesisBlockReferenceHash = "0000000000000000000000000000000000000000000000000000000000000000" |
---|
| 902 | + | let issue = Issue("UNIT0", "Native token", 0, 8, true) |
---|
| 903 | + | let tokenId = calculateAssetId(issue) |
---|
| 904 | + | let genesisBlockMeta = mkBlockMetaEntry(genesisBlockHashHex, 0, genesisBlockReferenceHash, 0, toBase16String(base58''), -1) |
---|
| 905 | + | [genesisBlockMeta, StringEntry(chainFirstBlockIdKey(0), genesisBlockHashHex), mkChainMetaEntry(0, 0, genesisBlockHashHex), IntegerEntry(minerRewardKey, minerRewardInGwei), StringEntry(epochMetaKey(height), ((toString(genesisMinerAddress) + ",0,") + genesisBlockHashHex)), StringEntry(finalizedBlockKey, genesisBlockHashHex), issue, StringEntry(tokenIdKey, toBase58String(tokenId)), StringEntry(elBridgeAddressKey, "0x0000000000000000000000000000000000006a7e")] |
---|
| 906 | + | } |
---|
| 907 | + | else throw("Strict value is not equal to itself.") |
---|
| 908 | + | } |
---|
| 909 | + | |
---|
| 910 | + | |
---|