Tengo el siguiente contrato que maneja matrices de direcciones:
contract C {
address[] goodAddr;
address[] badAddr;
function initAddr() constant {
if(goodAddr.length == 0) {
goodAddr.push(0x029ddf2082bc96d321c9769ec8e27e10b3cb16ee);
}
if(badAddr.length == 0) {
badAddr.push(0x115744603fedb255e5ab4765cc1dc6c832639fd5);
}
if(goodAddr.length == 1 && badAddr.length == 1) {
return;
}
for(uint256 i = 0; i < goodAddr.length; i++) {
if(goodAddr[i] == 0x029ddf2082bc96d321c9769ec8e27e10b3cb16ee)
return;
}
for(i = 0; i < badAddr.length; i++) {
if(badAddr[i] == 0x115744603fedb255e5ab4765cc1dc6c832639fd5)
return;
}
goodAddr.push(0x029ddf2082bc96d321c9769ec8e27e10b3cb16ee);
badAddr.push(0x115744603fedb255e5ab4765cc1dc6c832639fd5);
}
function getGoodAddr(uint256 i) constant returns (address) {
if(i < 0 || i >= goodAddr.length) {
return 0x0000000000000000000000000000000000000000;
} else {
return goodAddr[i];
}
}
function getBadAddr(uint256 i) constant returns (address) {
if(i < 0 || i >= badAddr.length) {
return 0x0000000000000000000000000000000000000000;
} else {
return badAddr[i];
}
}
}
El código funciona bien con la solidez del navegador, pero no inicializa las matrices de direcciones cuando se ejecuta localmente (en una red de prueba privada):
> eth.accounts
["0x029ddf2082bc96d321c9769ec8e27e10b3cb16ee", "0x115744603fedb255e5ab4765cc1dc6c832639fd5"]
> checkAllBalances();
eth.accounts[0]: 0x029ddf2082bc96d321c9769ec8e27e10b3cb16ee balance: 55922.95 ether
eth.accounts[1]: 0x115744603fedb255e5ab4765cc1dc6c832639fd5 balance: 1337 ether
undefined
> var c = cContract.new(
.. {
...... from: web3.eth.accounts[0],
...... {0600101610568565b5090565b5b5050509190906000526020600020900160005b73115744603fedb255e5ab4765cc1dc6c832639fd5909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690830217905550505b5056',
...... gas: 4700000
...... }, function (e, contract){
...... console.log(e, contract);
...... if (typeof contract.address !== 'undefined') {
.......... console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
.......... }
...... });
Unlock account 029ddf2082bc96d321c9769ec8e27e10b3cb16ee
Passphrase:
null [object Object]
undefined
> null [object Object]
Contract mined! address: 0xb43c8d6c6ee366358f170553602f2a0fc8ebe1d6 transactionHash: 0x8741c2ccae448a1cc647bdc03883e9136dadcec7adf942832f46bdcbe02e2eae
> c
{
address: "0xb43c8d6c6ee366358f170553602f2a0fc8ebe1d6",
transactionHash: "0x8741c2ccae448a1cc647bdc03883e9136dadcec7adf942832f46bdcbe02e2eae",
allEvents: function(),
getBadAddr: function(),
getGoodAddr: function(),
initAddr: function()
}
> c.initAddr.call();
[]
> c.getBadAddr(0);
"0x0000000000000000000000000000000000000000"
> c.getGoodAddr(0);
"0x0000000000000000000000000000000000000000"
>
¿Es esto un error en geth ? Estoy usando la versión 1.3.5.
Actualicé a la versión 1.4 pero ahora todos los argumentos de la línea de comandos no son válidos. Cuando ejecuto la siguiente instancia:
geth --datadir "./datadir" --networkid 13 --rpcapi eth,web3,personal --rpc --maxpeers 0 init "gen.json" --etherbase "0x0000000000000000000000000000000000000000" --mine --minerthreads 2
me sale el error:
Incorrect Usage.
init [arguments...]
The init command initialises a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
participating.
flag provided but not defined: -etherbase
Editar:
¿Alguna razón en particular para el voto negativo?
No creo que esto sea un error. Estas líneas adentro initAddr
significan que está realizando una operación de escritura (cambio de estado). goodAddr.push(0x029ddf2082bc96d321c9769ec8e27e10b3cb16ee); badAddr.push(0x115744603fedb255e5ab4765cc1dc6c832639fd5);
Probablemente necesite usar .sendTransaction()
para llamar a initAddr. Y elimine el constant
de esta función, solo entonces los cambios se reflejarán en matrices. El código devuelve el valor inicial predeterminado del tipo de datos de dirección. Déjame saber el resultado después de implementar esto.
> c.getBadAddr(0); "0x0000000000000000000000000000000000000000"
initAddr()
no debería ser constant
ya que está cambiando de estado. El código funciona en Browser Solidity ya que el compilador de solidity aún no aplica la constante, por lo que se usa una transacción cuando initAddr
se invoca.
roland kofler
Sebí
Sebí
ética
q9f