Ich erstelle ein Anmeldeformular für Studenten und benötige eine eindeutige Kennung für sie. Gibt es Methoden in Solidity, die jedem Schüler, der sich anmeldet, eine eindeutige Nummer geben?
In Solidity gibt es keinen integrierten eindeutigen ID-Generator.
2 Ideen:
uint
und stellen Sie sicher, dass Sie ihn nur erhöhenkeccak256
Hash-Funktion für eindeutige Daten über den Schüler, wie z. B. seinen Benutzernamen oder seine E-Mail-AdresseSehen Sie sich die create()
und createWithNonce()
Methoden in diesem Vertrag an:
https://github.com/link-blockchain/blobstore/blob/master/blobstore.sol
create()
nimmt den Blockhash des vorherigen Blocks mit der Absenderadresse und Hashes (mit keccak256), um eine eindeutige Kennung zu erstellen:
/**
* @dev Creates a new blob. It is guaranteed that different users will never receive the same blobId, even before consensus has been reached. This prevents blobId sniping. Consider createWithNonce() if not calling from another contract.
* @param flags Packed blob settings.
* @param contents Contents of the blob to be stored.
* @return blobId Id of the blob.
*/
function create(bytes4 flags, bytes contents) external returns (bytes20 blobId) {
// Generate the blobId.
blobId = bytes20(keccak256(msg.sender, block.blockhash(block.number - 1)));
// Make sure this blobId has not been used before (could be in the same block).
while (blobInfo[blobId].blockNumber != 0) {
blobId = bytes20(keccak256(blobId));
}
// Store blob info in state.
blobInfo[blobId] = BlobInfo({
flags: flags,
revisionCount: 1,
blockNumber: uint32(block.number),
owner: (flags & ANONYMOUS != 0) ? 0 : msg.sender,
});
// Store the first revision in a log in the current block.
Store(blobId, 0, contents);
}
createWithNonce
:
/**
* @dev Creates a new blob using provided nonce. It is guaranteed that different users will never receive the same blobId, even before consensus has been reached. This prevents blobId sniping. This method is cheaper than create(), especially if multiple blobs from the same account end up in the same block. However, it is not suitable for calling from other contracts because it will throw if a unique nonce is not provided.
* @param flagsNonce First 4 bytes: Packed blob settings. The parameter as a whole must never have been passed to this function from the same account, or it will throw.
* @param contents Contents of the blob to be stored.
* @return blobId Id of the blob.
*/
function createWithNonce(bytes32 flagsNonce, bytes contents) external returns (bytes20 blobId) {
// Generate the blobId.
blobId = bytes20(keccak256(msg.sender, flagsNonce));
// Make sure this blobId has not been used before.
if (blobInfo[blobId].blockNumber != 0) {
throw;
}
// Store blob info in state.
blobInfo[blobId] = BlobInfo({
flags: bytes4(flagsNonce),
revisionCount: 1,
blockNumber: uint32(block.number),
owner: (bytes4(flagsNonce) & ANONYMOUS != 0) ? 0 : msg.sender,
});
// Store the first revision in a log in the current block.
Store(blobId, 0, contents);
}
Sie können auf dieser Seite https://ethfiddle.com/mn8VfzWoN9 fummeln
contract Student {
uint public studentId;
function getStudentId() public returns (uint) {
return studentId++;
}
}
getStudentId
Jedes Mal, wenn Sie die Funktion aufrufen , studentId
erhöht sich der Wert um 1.
public
Vielleicht müssen Sie to durch internal
eine Validierungsanweisung ersetzen .
11t
eth
function getID() returns(uint) { return ++counter; }
Contract hat eine Zustandsvariable namenscounter
.