public AddBlockResult Insert(Block block) { if (!CanAcceptNewBlocks) { return(AddBlockResult.CannotAccept); } if (block.Number == 0) { throw new InvalidOperationException("Genesis block should not be inserted."); } Rlp newRlp = _blockDecoder.Encode(block); _blockDb.Set(block.Hash, newRlp.Bytes); long expectedNumber = (LowestInsertedBody?.Number - 1 ?? LongConverter.FromString(_syncConfig.PivotNumber ?? "0")); if (block.Number != expectedNumber) { throw new InvalidOperationException($"Trying to insert out of order block {block.Number} when expected number was {expectedNumber}"); } if (block.Number < (LowestInsertedBody?.Number ?? long.MaxValue)) { LowestInsertedBody = block; } return(AddBlockResult.Added); }
public void Can_do_roundtrip_scenarios() { BlockDecoder decoder = new BlockDecoder(); foreach (Block block in _scenarios) { Rlp encoded = decoder.Encode(block); Block decoded = decoder.Decode(new RlpStream(encoded.Bytes)); Rlp encoded2 = decoder.Encode(decoded); Assert.AreEqual(encoded.Bytes.ToHexString(), encoded2.Bytes.ToHexString()); } }
public void Can_do_roundtrip_scenarios([Values(true, false)] bool valueDecoder) { BlockDecoder decoder = new BlockDecoder(); foreach (Block block in _scenarios) { Rlp encoded = decoder.Encode(block); var valueDecoderContext = new Rlp.ValueDecoderContext(encoded.Bytes); Block decoded = valueDecoder ? decoder.Decode(ref valueDecoderContext) : decoder.Decode(new RlpStream(encoded.Bytes)); Rlp encoded2 = decoder.Encode(decoded); Assert.AreEqual(encoded.Bytes.ToHexString(), encoded2.Bytes.ToHexString()); } }
public void Can_do_roundtrip_null() { BlockDecoder decoder = new BlockDecoder(); Rlp result = decoder.Encode((Block)null); Block decoded = Rlp.Decode <Block>(result); Assert.IsNull(decoded); }
public void Can_do_roundtrip_null([Values(true, false)] bool valueDecoder) { BlockDecoder decoder = new BlockDecoder(); Rlp result = decoder.Encode((Block)null); Block decoded = valueDecoder ? Rlp.Decode <Block>(result.Bytes.AsSpan()) : Rlp.Decode <Block>(result); Assert.IsNull(decoded); }
public void Can_do_roundtrip_regression() { BlockDecoder decoder = new BlockDecoder(); Block decoded = decoder.Decode(new RlpStream(Bytes.FromHexString(regression5644))); Rlp encoded = decoder.Encode(decoded); Assert.AreEqual(encoded.Bytes.ToHexString(), encoded.Bytes.ToHexString()); }
public void Can_do_roundtrip_regression([Values(true, false)] bool valueDecoder) { BlockDecoder decoder = new BlockDecoder(); var bytes = Bytes.FromHexString(regression5644); var valueDecoderContext = new Rlp.ValueDecoderContext(bytes); Block decoded = valueDecoder ? decoder.Decode(ref valueDecoderContext) : decoder.Decode(new RlpStream(bytes)); Rlp encoded = decoder.Encode(decoded); Assert.AreEqual(encoded.Bytes.ToHexString(), encoded.Bytes.ToHexString()); }
public void Get_block_rlp_by_hash() { BlockDecoder decoder = new BlockDecoder(); Rlp rlp = decoder.Encode(Build.A.Block.WithNumber(1).TestObject); debugBridge.GetBlockRlp(Keccak.Zero).Returns(rlp.Bytes); DebugRpcModule rpcModule = new DebugRpcModule(LimboLogs.Instance, debugBridge, jsonRpcConfig); JsonRpcSuccessResponse response = RpcTest.TestRequest <IDebugRpcModule>(rpcModule, "debug_getBlockRlpByHash", $"{Keccak.Zero.Bytes.ToHexString()}") as JsonRpcSuccessResponse; Assert.AreEqual(rlp.Bytes, (byte[])response?.Result); }
public void Get_block_rlp_by_hash_when_missing() { BlockDecoder decoder = new BlockDecoder(); Rlp rlp = decoder.Encode(Build.A.Block.WithNumber(1).TestObject); debugBridge.GetBlockRlp(Keccak.Zero).Returns((byte[])null); DebugModule module = new DebugModule(LimboLogs.Instance, debugBridge, jsonRpcConfig); JsonRpcErrorResponse response = RpcTest.TestRequest <IDebugModule>(module, "debug_getBlockRlpByHash", $"{Keccak.Zero.Bytes.ToHexString()}") as JsonRpcErrorResponse; Assert.AreEqual(-32001, response.Error.Code); }
public void Get_block_rlp() { BlockDecoder decoder = new BlockDecoder(); IDebugBridge debugBridge = Substitute.For <IDebugBridge>(); Rlp rlp = decoder.Encode(Build.A.Block.WithNumber(1).TestObject); debugBridge.GetBlockRlp(1).Returns(rlp.Bytes); DebugRpcModule rpcModule = new DebugRpcModule(LimboLogs.Instance, debugBridge, jsonRpcConfig); JsonRpcSuccessResponse response = RpcTest.TestRequest <IDebugRpcModule>(rpcModule, "debug_getBlockRlp", "1") as JsonRpcSuccessResponse; Assert.AreEqual(rlp.Bytes, (byte[])response?.Result); }
public void Get_block_rlp_by_hash() { BlockDecoder decoder = new BlockDecoder(); IDebugBridge debugBridge = Substitute.For <IDebugBridge>(); Rlp rlp = decoder.Encode(Build.A.Block.WithNumber(1).TestObject); debugBridge.GetBlockRlp(Keccak.Zero).Returns(rlp.Bytes); DebugModule module = new DebugModule(NullLogManager.Instance, debugBridge); JsonRpcSuccessResponse response = RpcTest.TestRequest <IDebugModule>(module, "debug_getBlockRlpByHash", $"{Keccak.Zero.Bytes.ToHexString()}") as JsonRpcSuccessResponse; Assert.AreEqual(rlp.Bytes, (byte[])response?.Result); }
public void Get_block_rlp_by_hash_when_missing() { BlockDecoder decoder = new BlockDecoder(); IDebugBridge debugBridge = Substitute.For <IDebugBridge>(); Rlp rlp = decoder.Encode(Build.A.Block.WithNumber(1).TestObject); debugBridge.GetBlockRlp(Keccak.Zero).Returns((byte[])null); DebugModule module = new DebugModule(NullLogManager.Instance, debugBridge); JsonRpcResponse response = RpcTest.TestRequest <IDebugModule>(module, "debug_getBlockRlpByHash", $"{Keccak.Zero.Bytes.ToHexString()}"); Assert.AreEqual(-32601, response.Error.Code); }
public void Get_block_rlp() { BlockDecoder decoder = new BlockDecoder(); IDebugBridge debugBridge = Substitute.For <IDebugBridge>(); Rlp rlp = decoder.Encode(Build.A.Block.WithNumber(1).TestObject); debugBridge.GetBlockRlp(1).Returns(rlp.Bytes); DebugModule module = new DebugModule(NullLogManager.Instance, debugBridge); JsonRpcResponse response = RpcTest.TestRequest <IDebugModule>(module, "debug_getBlockRlp", "1"); Assert.IsNotInstanceOf <JsonRpcErrorResponse>(response); Assert.AreEqual(rlp.Bytes, (byte[])response.Result); }
public byte[] Improved2() { return(_blockDecoder.Encode(_block).Bytes); }