Estoy aprendiendo Solidity y me refiero al ejemplo dado de Votación en un documento . Pero no soy capaz de entender una cosa acerca de delegate
la función.
Aquí está el método Delegado:
/// Delegate your vote to the voter `to`.
function delegate(address to) public {
// assigns reference
Voter storage sender = voters[msg.sender];
require(!sender.voted, "You already voted.");
require(to != msg.sender, "Self-delegation is disallowed.");
// Forward the delegation as long as
// `to` also delegated.
// In general, such loops are very dangerous,
// because if they run too long, they might
// need more gas than is available in a block.
// In this case, the delegation will not be executed,
// but in other situations, such loops might
// cause a contract to get "stuck" completely.
while (voters[to].delegate != address(0)) {
to = voters[to].delegate;
// We found a loop in the delegation, not allowed.
require(to != msg.sender, "Found loop in delegation.");
}
}
¿Cuál es el uso de while loop aquí? ya comprobaron con autodelegación con
require(to != msg.sender, "Self-delegation is disallowed.");
Y podemos verificar la condición de la dirección inicial con if
la condición.
if (voters[to].delegate != address(0)) {
to = voters[to].delegate;
...
...
¿Qué tiene de especial el ciclo while aquí?
El while
bucle es para comprobar que no existe ningún bucle de delegación como este:
voter A === delegate ===> voter B === delegate ===> ... === delegate ===> voter A
Mientras que la línea require(to != msg.sender, "Self-delegation is disallowed.");
solo busca un autodelegado directo:
voter A === delegate ===> voter A