Skip to content

Commit

Permalink
few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcindsobczak committed May 2, 2023
1 parent 23a1005 commit 5335697
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ public void Bad_chain_id_is_not_valid()
}

[Test, Timeout(Timeout.MaxTestTime)]
public void No_chain_id_tx_is_valid()
public void No_chain_id_legacy_tx_is_valid()
{
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = 27; // correct v
Signature signature = new(sigData);
Transaction tx = Build.A.Transaction.WithSignature(signature).TestObject;

Expand Down Expand Up @@ -146,7 +147,7 @@ public bool Before_eip_2930_has_to_be_legacy_tx(TxType txType, bool eip2930)
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = CalculateV();
sigData[64] = 27; // correct v
Signature signature = new(sigData);
Transaction tx = Build.A.Transaction
.WithType(txType > TxType.AccessList ? TxType.Legacy : txType)
Expand Down Expand Up @@ -231,7 +232,7 @@ public bool MaxFeePerGas_is_required_to_be_greater_than_MaxPriorityFeePerGas(TxT
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = CalculateV();
sigData[64] = 27; // correct v
Signature signature = new(sigData);
Transaction tx = Build.A.Transaction
.WithType(txType > TxType.AccessList ? TxType.Legacy : txType)
Expand Down Expand Up @@ -286,7 +287,7 @@ public bool MaxFeePerDataGas_should_be_set_for_blob_tx_only(TxType txType, bool
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = 1 + TestBlockchainIds.ChainId * 2 + 35;
sigData[64] = 27; // correct v
Signature signature = new(sigData);
TransactionBuilder<Transaction> txBuilder = Build.A.Transaction
.WithType(txType)
Expand Down Expand Up @@ -315,7 +316,7 @@ public bool Blobs_count_should_be_within_constraints(int blobsCount)
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = 1 + TestBlockchainIds.ChainId * 2 + 35;
sigData[64] = 27; // correct v
Signature signature = new(sigData);
Transaction tx = Build.A.Transaction
.WithType(TxType.Blob)
Expand All @@ -338,7 +339,7 @@ public bool BlobVersionedHash_should_be_correct(byte[] hash)
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = 1 + TestBlockchainIds.ChainId * 2 + 35;
sigData[64] = 27; // correct v
Signature signature = new(sigData);
Transaction tx = Build.A.Transaction
.WithType(TxType.Blob)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Consensus/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ValueTask Sign(Transaction tx)
{
Keccak hash = Keccak.Compute(Rlp.Encode(tx, true, true, _chainId).Bytes);
tx.Signature = Sign(hash);
tx.Signature.V = tx.Signature.V + 8 + 2 * _chainId;
tx.Signature.V = tx.Type == TxType.Legacy ? tx.Signature.V + 8 + 2 * _chainId : (ulong)(tx.Signature.RecoveryId + 27);
return default;
}

Expand Down
17 changes: 12 additions & 5 deletions src/Nethermind/Nethermind.Consensus/Validators/TxValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public bool IsWellFormed(Transaction transaction, IReleaseSpec releaseSpec)
transaction.GasLimit >= IntrinsicGasCalculator.Calculate(transaction, releaseSpec) &&
/* if it is a call or a transfer then we require the 'To' field to have a value
while for an init it will be empty */
ValidateSignature(transaction.Signature, releaseSpec) &&
ValidateSignature(transaction, releaseSpec) &&
ValidateChainId(transaction) &&
Validate1559GasFields(transaction, releaseSpec) &&
Validate3860Rules(transaction, releaseSpec) &&
Expand Down Expand Up @@ -71,8 +71,10 @@ private bool ValidateChainId(Transaction transaction) =>
_ => transaction.ChainId == _chainIdValue
};

private bool ValidateSignature(Signature? signature, IReleaseSpec spec)
private bool ValidateSignature(Transaction tx, IReleaseSpec spec)
{
Signature? signature = tx.Signature;

if (signature is null)
{
return false;
Expand All @@ -91,12 +93,17 @@ private bool ValidateSignature(Signature? signature, IReleaseSpec spec)
return false;
}

if (spec.IsEip155Enabled)
if (signature.V is 27 or 28)
{
return true;
}

if (tx.Type == TxType.Legacy && spec.IsEip155Enabled && (signature.V == _chainIdValue * 2 + 35ul || signature.V == _chainIdValue * 2 + 36ul))
{
return (signature.ChainId ?? _chainIdValue) == _chainIdValue;
return true;
}

return !spec.ValidateChainId || signature.V is 27 or 28;
return !spec.ValidateChainId;
}

private static bool Validate4844Fields(Transaction transaction)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Hive/HiveRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private async Task InitializeChain(string chainFile)
while (rlpStream.PeekNumberOfItemsRemaining() > 0)
{
rlpStream.PeekNextItem();
Block block = Rlp.Decode<Block>(rlpStream);
Block block = Rlp.Decode<Block>(rlpStream, RlpBehaviors.AllowExtraBytes);
if (_logger.IsInfo)
_logger.Info($"HIVE Reading a chain.rlp block {block.ToString(Block.Format.Short)}");
blocks.Add(block);
Expand Down
1 change: 0 additions & 1 deletion src/Nethermind/Nethermind.Network/NetworkNodeDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public int GetLength(NetworkNode item, RlpBehaviors rlpBehaviors)
private int GetContentLength(NetworkNode item, RlpBehaviors rlpBehaviors)
{
return Rlp.LengthOf(item.NodeId.Bytes)
+ Rlp.LengthOf(item.NodeId.Bytes)
+ Rlp.LengthOf(item.Host)
+ Rlp.LengthOf(item.Port)
+ 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ChainLevelDecoder : IRlpStreamDecoder<ChainLevelInfo>, IRlpValueDec

if (rlpStream.IsNextItemNull())
{
rlpStream.ReadByte();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecode
return null;
}

int length = rlpStream.PeekNextRlpLength();
int length = rlpStream.ReadSequenceLength();
int check = rlpStream.Position + length;
rlpStream.SkipLength();

AccessListBuilder accessListBuilder = new();
while (rlpStream.Position < check)
{
rlpStream.SkipLength();
int accessListItemLength = rlpStream.ReadSequenceLength();
int accessListItemCheck = rlpStream.Position + accessListItemLength;
Address address = rlpStream.DecodeAddress();
if (address is null)
{
Expand All @@ -44,14 +44,27 @@ public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecode

if (rlpStream.Position < check)
{
int storageCheck = rlpStream.Position + rlpStream.PeekNextRlpLength();
rlpStream.SkipLength();
while (rlpStream.Position < storageCheck)
int storagesLength = rlpStream.ReadSequenceLength();
int storagesCheck = rlpStream.Position + storagesLength;
while (rlpStream.Position < storagesCheck)
{
int storageItemCheck = rlpStream.Position + 33;
UInt256 index = rlpStream.DecodeUInt256();
accessListBuilder.AddStorage(index);
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
rlpStream.Check(storageItemCheck);
}
}
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
rlpStream.Check(storagesCheck);
}
}
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
rlpStream.Check(accessListItemCheck);
}
}

if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
Expand All @@ -78,14 +91,14 @@ public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecode
return null;
}

int length = decoderContext.PeekNextRlpLength();
int length = decoderContext.ReadSequenceLength();
int check = decoderContext.Position + length;
decoderContext.SkipLength();

AccessListBuilder accessListBuilder = new();
while (decoderContext.Position < check)
{
decoderContext.SkipLength();
int accessListItemLength = decoderContext.ReadSequenceLength();
int accessListItemCheck = decoderContext.Position + accessListItemLength;
Address address = decoderContext.DecodeAddress();
if (address is null)
{
Expand All @@ -96,14 +109,27 @@ public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecode

if (decoderContext.Position < check)
{
int storageCheck = decoderContext.Position + decoderContext.PeekNextRlpLength();
decoderContext.SkipLength();
while (decoderContext.Position < storageCheck)
int storagesLength = decoderContext.ReadSequenceLength();
int storagesCheck = decoderContext.Position + storagesLength;
while (decoderContext.Position < storagesCheck)
{
int storageItemCheck = decoderContext.Position + 33;
UInt256 index = decoderContext.DecodeUInt256();
accessListBuilder.AddStorage(index);
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
decoderContext.Check(storageItemCheck);
}
}
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
decoderContext.Check(storagesCheck);
}
}
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
decoderContext.Check(accessListItemCheck);
}
}

if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
Expand Down
Loading

0 comments on commit 5335697

Please sign in to comment.