public Account Get(Address address, Keccak rootHash = null) { byte[] bytes = Get(ValueKeccak.Compute(address.Bytes).BytesAsSpan, rootHash); if (bytes == null) { return(null); } return(_decoder.Decode(bytes.AsRlpStream())); }
public static Address From(Address deployingAddress, UInt256 nonce) { ValueKeccak contractAddressKeccak = ValueKeccak.Compute( Rlp.Encode( Rlp.Encode(deployingAddress), Rlp.Encode(nonce)).Bytes); return(new Address(in contractAddressKeccak)); }
public override bool Accepts(ref ValueKeccak topic) { for (int i = 0; i < _subexpression.Length; i++) { if (_subexpression[i].Accepts(ref topic)) { return(true); } } return(false); }
public static Span <byte> GetKey(UInt256 index) { if (index < CacheSize) { return(Cache[index]); } Span <byte> span = stackalloc byte[32]; index.ToBigEndian(span); return(ValueKeccak.Compute(span).BytesAsSpan.ToArray()); }
private static Bloom.BloomExtract GetExtract(Span <byte> sequence) { int GetIndex(Span <byte> bytes, int index1, int index2) { return(2047 - ((bytes[index1] << 8) + bytes[index2]) % 2048); } var keccakBytes = ValueKeccak.Compute(sequence).BytesAsSpan; var indexes = new Bloom.BloomExtract(GetIndex(keccakBytes, 0, 1), GetIndex(keccakBytes, 2, 3), GetIndex(keccakBytes, 4, 5)); return(indexes); }
public bool TryGetNext(out ValueKeccak current) { if (_decoderContext.Position < _length) { _decoderContext.DecodeValueKeccak(out current); Index++; return(true); } else { current = new ValueKeccak(); return(false); } }
public static Span <byte> GetKey(UInt256 index) { if (index < CacheSize) { return(Cache[index]); } Span <byte> span = stackalloc byte[32]; index.ToBigEndian(span); // (1% allocations on archive sync) this ToArray can be pooled or just directly converted to nibbles return(ValueKeccak.Compute(span).BytesAsSpan.ToArray()); }
public static Address From(Address deployingAddress, Span <byte> salt, Span <byte> initCode) { // sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))) Span <byte> bytes = new byte[1 + Address.ByteLength + 32 + salt.Length]; bytes[0] = 0xff; deployingAddress.Bytes.CopyTo(bytes.Slice(1, 20)); salt.CopyTo(bytes.Slice(21, salt.Length)); ValueKeccak.Compute(initCode).BytesAsSpan.CopyTo(bytes.Slice(21 + salt.Length, 32)); ValueKeccak contractAddressKeccak = ValueKeccak.Compute(bytes); return(new Address(in contractAddressKeccak)); }
public static void SetSecrets(EncryptionHandshake handshake, HandshakeRole handshakeRole) { Span <byte> tempConcat = stackalloc byte[64]; Span <byte> ephemeralSharedSecret = Proxy.EcdhSerialized(handshake.RemoteEphemeralPublicKey.Bytes, handshake.EphemeralPrivateKey.KeyBytes); Span <byte> nonceHash = ValueKeccak.Compute(Bytes.Concat(handshake.RecipientNonce, handshake.InitiatorNonce)).BytesAsSpan; ephemeralSharedSecret.CopyTo(tempConcat.Slice(0, 32)); nonceHash.CopyTo(tempConcat.Slice(32, 32)); Span <byte> sharedSecret = ValueKeccak.Compute(tempConcat).BytesAsSpan; // byte[] token = Keccak.Compute(sharedSecret).Bytes; sharedSecret.CopyTo(tempConcat.Slice(32, 32)); byte[] aesSecret = Keccak.Compute(tempConcat).Bytes; sharedSecret.Clear(); aesSecret.CopyTo(tempConcat.Slice(32, 32)); byte[] macSecret = Keccak.Compute(tempConcat).Bytes; ephemeralSharedSecret.Clear(); handshake.Secrets = new EncryptionSecrets(); // handshake.Secrets.Token = token; handshake.Secrets.AesSecret = aesSecret; handshake.Secrets.MacSecret = macSecret; KeccakDigest mac1 = new KeccakDigest(MacBitsSize); mac1.BlockUpdate(macSecret.Xor(handshake.RecipientNonce), 0, macSecret.Length); mac1.BlockUpdate(handshake.AuthPacket.Data, 0, handshake.AuthPacket.Data.Length); KeccakDigest mac2 = new KeccakDigest(MacBitsSize); mac2.BlockUpdate(macSecret.Xor(handshake.InitiatorNonce), 0, macSecret.Length); mac2.BlockUpdate(handshake.AckPacket.Data, 0, handshake.AckPacket.Data.Length); if (handshakeRole == HandshakeRole.Initiator) { handshake.Secrets.EgressMac = mac1; handshake.Secrets.IngressMac = mac2; } else { handshake.Secrets.EgressMac = mac2; handshake.Secrets.IngressMac = mac1; } }
public TxReceiptStructRef(TxReceipt receipt) { StatusCode = receipt.StatusCode; BlockNumber = receipt.BlockNumber; BlockHash = (receipt.BlockHash ?? Keccak.Zero).ToStructRef(); TxHash = (receipt.TxHash ?? Keccak.Zero).ToStructRef(); Index = receipt.Index; GasUsed = receipt.GasUsed; GasUsedTotal = receipt.GasUsedTotal; Sender = (receipt.Sender ?? Address.Zero).ToStructRef(); ContractAddress = (receipt.ContractAddress ?? Address.Zero).ToStructRef(); Recipient = (receipt.Recipient ?? Address.Zero).ToStructRef(); ReturnValue = receipt.ReturnValue; PostTransactionState = (receipt.PostTransactionState ?? Keccak.Zero).ToStructRef(); Bloom = (receipt.Bloom ?? Core.Bloom.Empty).ToStructRef(); Logs = receipt.Logs; LogsRlp = Span <byte> .Empty; Error = receipt.Error; }
protected byte[] Serialize(byte type, Span <byte> data) { byte[] result = new byte[32 + 1 + data.Length + 64 + 1]; Span <byte> resultSpan = result.AsSpan(); resultSpan[32 + 65] = type; data.CopyTo(resultSpan.Slice(32 + 65 + 1, data.Length)); Span <byte> payload = resultSpan.Slice(32 + 65); Keccak toSign = Keccak.Compute(payload); Signature signature = _ecdsa.Sign(_privateKey, toSign); signature.Bytes.AsSpan().CopyTo(resultSpan.Slice(32, 64)); resultSpan[32 + 64] = signature.RecoveryId; Span <byte> forMdc = resultSpan.Slice(32); ValueKeccak mdc = ValueKeccak.Compute(forMdc); mdc.BytesAsSpan.CopyTo(resultSpan.Slice(0, 32)); return(result); }
protected (PublicKey FarPublicKey, byte[] Mdc, byte[] Data) PrepareForDeserialization(byte[] msg) { if (msg.Length < 98) { throw new NetworkingException("Incorrect message", NetworkExceptionType.Validation); } byte[] mdc = msg.Slice(0, 32); Span <byte> signature = msg.AsSpan(32, 65); // var type = new[] { msg[97] }; byte[] data = msg.Slice(98, msg.Length - 98); Span <byte> computedMdc = ValueKeccak.Compute(msg.AsSpan(32)).BytesAsSpan; if (!Bytes.AreEqual(mdc, computedMdc)) { throw new NetworkingException("Invalid MDC", NetworkExceptionType.Validation); } PublicKey nodeId = _nodeIdResolver.GetNodeId(signature.Slice(0, 64).ToArray(), signature[64], msg.AsSpan(97, msg.Length - 97)); return(nodeId, mdc, data); }
public static Address ToKvmAddress(this byte[] publicKey) { return(new Address(ValueKeccak.Compute(publicKey).BytesAsSpan.SliceWithZeroPadding(0, 20).ToArray())); }
private Address ComputeAddress() { Span <byte> hash = ValueKeccak.Compute(Bytes).BytesAsSpan; return(new Address(hash.Slice(12).ToArray())); }
public override bool Accepts(ref ValueKeccak topic) => Bytes.AreEqual(topic.BytesAsSpan, _topic.Bytes);
public abstract bool Accepts(ref ValueKeccak topic);
public override SyncResponseHandlingResult HandleResponse(StateSyncBatch batch) { if (batch == EmptyBatch) { _logger.Error("Received empty batch as a response"); } if (!_pendingRequests.TryRemove(batch, out _)) { if (_logger.IsDebug) { _logger.Debug($"Cannot remove pending request {batch}"); } return(SyncResponseHandlingResult.OK); } else { if (_logger.IsTrace) { _logger.Trace($"Removing pending request {batch}"); } } int requestLength = batch.RequestedNodes?.Length ?? 0; int responseLength = batch.Responses?.Length ?? 0; void AddAgainAllItems() { for (int i = 0; i < requestLength; i++) { AddNodeToPending(batch.RequestedNodes[i], null, "missing", true); } } try { lock (_handleWatch) { if (DateTime.UtcNow - _lastReview > TimeSpan.FromSeconds(60)) { _lastReview = DateTime.UtcNow; string reviewMessage = _pendingItems.RecalculatePriorities(); if (_logger.IsInfo) { _logger.Info(reviewMessage); } } _handleWatch.Restart(); bool requestWasMade = batch.Responses != null; if (!requestWasMade) { AddAgainAllItems(); if (_logger.IsTrace) { _logger.Trace($"Batch was not assigned to any peer."); } Interlocked.Increment(ref _data.NotAssignedCount); return(SyncResponseHandlingResult.NotAssigned); } bool isMissingRequestData = batch.RequestedNodes == null; bool isMissingResponseData = batch.Responses == null; bool hasValidFormat = !isMissingRequestData && !isMissingResponseData; if (!hasValidFormat) { _hintsToResetRoot++; AddAgainAllItems(); if (_logger.IsWarn) { _logger.Warn($"Batch response had invalid format"); } Interlocked.Increment(ref _data.InvalidFormatCount); return(isMissingRequestData ? SyncResponseHandlingResult.InternalError : SyncResponseHandlingResult.NotAssigned); } if (_logger.IsTrace) { _logger.Trace($"Received node data - {responseLength} items in response to {requestLength}"); } int nonEmptyResponses = 0; int invalidNodes = 0; for (int i = 0; i < batch.RequestedNodes.Length; i++) { StateSyncItem currentStateSyncItem = batch.RequestedNodes[i]; /* if the peer has limit on number of requests in a batch then the response will possibly be * shorter than the request */ if (batch.Responses.Length < i + 1) { AddNodeToPending(currentStateSyncItem, null, "missing", true); continue; } /* if the peer does not have details of this particular node */ byte[] currentResponseItem = batch.Responses[i]; if (currentResponseItem == null) { AddNodeToPending(batch.RequestedNodes[i], null, "missing", true); continue; } /* node sent data that is not consistent with its hash - it happens surprisingly often */ if (!ValueKeccak.Compute(currentResponseItem).BytesAsSpan.SequenceEqual(currentStateSyncItem.Hash.Bytes)) { AddNodeToPending(currentStateSyncItem, null, "missing", true); if (_logger.IsTrace) { _logger.Trace($"Peer sent invalid data (batch {requestLength}->{responseLength}) of length {batch.Responses[i]?.Length} of type {batch.RequestedNodes[i].NodeDataType} at level {batch.RequestedNodes[i].Level} of type {batch.RequestedNodes[i].NodeDataType} Keccak({batch.Responses[i].ToHexString()}) != {batch.RequestedNodes[i].Hash}"); } invalidNodes++; continue; } nonEmptyResponses++; NodeDataType nodeDataType = currentStateSyncItem.NodeDataType; if (nodeDataType == NodeDataType.Code) { SaveNode(currentStateSyncItem, currentResponseItem); continue; } HandleTrieNode(currentStateSyncItem, currentResponseItem, ref invalidNodes); } Interlocked.Add(ref _data.ConsumedNodesCount, nonEmptyResponses); StoreProgressInDb(); if (_logger.IsTrace) { _logger.Trace($"After handling response (non-empty responses {nonEmptyResponses}) of {batch.RequestedNodes.Length} from ({_pendingItems.Description}) nodes"); } /* magic formula is ratio of our desired batch size - 1024 to Geth max batch size 384 times some missing nodes ratio */ bool isEmptish = (decimal)nonEmptyResponses / Math.Max(requestLength, 1) < 384m / 1024m * 0.75m; if (isEmptish) { Interlocked.Increment(ref _hintsToResetRoot); Interlocked.Increment(ref _data.EmptishCount); } else { Interlocked.Exchange(ref _hintsToResetRoot, 0); } /* here we are very forgiving for Geth nodes that send bad data fast */ bool isBadQuality = nonEmptyResponses > 64 && (decimal)invalidNodes / Math.Max(requestLength, 1) > 0.50m; if (isBadQuality) { Interlocked.Increment(ref _data.BadQualityCount); } bool isEmpty = nonEmptyResponses == 0 && !isBadQuality; if (isEmpty) { if (_logger.IsDebug) { _logger.Debug($"Peer sent no data in response to a request of length {batch.RequestedNodes.Length}"); } return(SyncResponseHandlingResult.NoProgress); } if (!isEmptish && !isBadQuality) { Interlocked.Increment(ref _data.OkCount); } SyncResponseHandlingResult result = isEmptish ? SyncResponseHandlingResult.Emptish : isBadQuality ? SyncResponseHandlingResult.LesserQuality : SyncResponseHandlingResult.OK; _data.DisplayProgressReport(_pendingRequests.Count, _logger); long total = _handleWatch.ElapsedMilliseconds + _networkWatch.ElapsedMilliseconds; if (total != 0) { // calculate averages if (_logger.IsTrace) { _logger.Trace($"Prepare batch {_networkWatch.ElapsedMilliseconds}ms ({(decimal) _networkWatch.ElapsedMilliseconds / total:P0}) - Handle {_handleWatch.ElapsedMilliseconds}ms ({(decimal) _handleWatch.ElapsedMilliseconds / total:P0})"); } } if (_handleWatch.ElapsedMilliseconds > 250) { if (_logger.IsDebug) { _logger.Debug($"Handle watch {_handleWatch.ElapsedMilliseconds}, DB reads {_data.DbChecks - _data.LastDbReads}, ratio {(decimal) _handleWatch.ElapsedMilliseconds / Math.Max(1, _data.DbChecks - _data.LastDbReads)}"); } } _data.LastDbReads = _data.DbChecks; _data.AverageTimeInHandler = (_data.AverageTimeInHandler * (_data.ProcessedRequestsCount - 1) + _handleWatch.ElapsedMilliseconds) / _data.ProcessedRequestsCount; Interlocked.Add(ref _data.HandledNodesCount, nonEmptyResponses); return(result); } } catch (Exception e) { _logger.Error("Error when handling state sync response", e); return(SyncResponseHandlingResult.InternalError); } finally { _handleWatch.Stop(); } }
public Span <byte> Baseline() { return(ValueKeccak.Compute(Input.Bytes).BytesAsSpan); }
public AddressStructRef(ValueKeccak keccak) : this(keccak.BytesAsSpan.Slice(12, ByteLength)) { }
public void Set(Address address, Account account) { ValueKeccak keccak = ValueKeccak.Compute(address.Bytes); Set(keccak.BytesAsSpan, account == null ? null : account.IsTotallyEmpty?EmptyAccountRlp: Rlp.Encode(account)); }
public override bool Accepts(ref ValueKeccak topic) => true;