-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/development' into test/new-stake
- Loading branch information
Showing
88 changed files
with
790 additions
and
782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
68 changes: 68 additions & 0 deletions
68
.Lib9c.ActionEvaluatorCommonComponents/ActionContextMarshaller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Security.Cryptography; | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Common; | ||
using Libplanet.Crypto; | ||
using Libplanet.Types.Tx; | ||
using Boolean = Bencodex.Types.Boolean; | ||
|
||
namespace Lib9c.ActionEvaluatorCommonComponents | ||
{ | ||
public static class ActionContextMarshaller | ||
{ | ||
private static readonly Codec Codec = new Codec(); | ||
|
||
public static byte[] Serialize(this ICommittedActionContext actionContext) | ||
{ | ||
return Codec.Encode(Marshal(actionContext)); | ||
} | ||
|
||
public static Dictionary Marshal(this ICommittedActionContext actionContext) | ||
{ | ||
var dictionary = Bencodex.Types.Dictionary.Empty | ||
.Add("signer", actionContext.Signer.ToHex()) | ||
.Add("miner", actionContext.Miner.ToHex()) | ||
.Add("block_index", actionContext.BlockIndex) | ||
.Add("block_protocol_version", actionContext.BlockProtocolVersion) | ||
.Add("previous_states", actionContext.PreviousState.ByteArray) | ||
.Add("random_seed", actionContext.RandomSeed) | ||
.Add("block_action", actionContext.IsPolicyAction); | ||
|
||
if (actionContext.TxId is { } txId) | ||
{ | ||
dictionary = dictionary.Add("tx_id", txId.ByteArray); | ||
} | ||
|
||
return dictionary; | ||
} | ||
|
||
public static ICommittedActionContext Unmarshal(Dictionary dictionary) | ||
{ | ||
return new CommittedActionContext( | ||
signer: new Address(((Text)dictionary["signer"]).Value), | ||
txId: dictionary.TryGetValue((Text)"tx_id", out IValue txIdValue) && | ||
txIdValue is Binary txIdBinaryValue | ||
? new TxId(txIdBinaryValue.ByteArray) | ||
: null, | ||
miner: new Address(((Text)dictionary["miner"]).Value), | ||
blockIndex: (Integer)dictionary["block_index"], | ||
blockProtocolVersion: (Integer)dictionary["block_protocol_version"], | ||
previousState: new HashDigest<SHA256>(dictionary["previous_states"]), | ||
randomSeed: (Integer)dictionary["random_seed"], | ||
isPolicyAction: (Boolean)dictionary["block_action"] | ||
); | ||
} | ||
|
||
public static ICommittedActionContext Deserialize(byte[] serialized) | ||
{ | ||
var decoded = Codec.Decode(serialized); | ||
if (!(decoded is Dictionary dictionary)) | ||
{ | ||
throw new ArgumentException($"Expected 'Dictionary' but {decoded.GetType().Name}", nameof(serialized)); | ||
} | ||
|
||
return Unmarshal(dictionary); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.Lib9c.ActionEvaluatorCommonComponents/ActionEvaluationMarshaller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Security.Cryptography; | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Common; | ||
|
||
namespace Lib9c.ActionEvaluatorCommonComponents | ||
{ | ||
public static class ActionEvaluationMarshaller | ||
{ | ||
private static readonly Codec Codec = new Codec(); | ||
|
||
public static byte[] Serialize(this ICommittedActionEvaluation actionEvaluation) | ||
{ | ||
return Codec.Encode(Marshal(actionEvaluation)); | ||
} | ||
|
||
public static Dictionary Marshal(this ICommittedActionEvaluation actionEvaluation) => | ||
Dictionary.Empty | ||
.Add("action", actionEvaluation.Action) | ||
.Add("output_states", actionEvaluation.OutputState.ByteArray) | ||
.Add("input_context", actionEvaluation.InputContext.Marshal()) | ||
.Add("exception", actionEvaluation.Exception?.GetType().FullName is { } typeName ? (Text)typeName : Null.Value); | ||
|
||
public static ICommittedActionEvaluation Unmarshal(IValue value) | ||
{ | ||
if (value is not Dictionary dictionary) | ||
{ | ||
throw new ArgumentException(nameof(value)); | ||
} | ||
|
||
return new CommittedActionEvaluation( | ||
dictionary["action"], | ||
ActionContextMarshaller.Unmarshal((Dictionary)dictionary["input_context"]), | ||
new HashDigest<SHA256>(dictionary["output_states"]), | ||
dictionary["exception"] is Text typeName ? new Exception(typeName) : null | ||
); | ||
} | ||
|
||
public static ICommittedActionEvaluation Deserialize(byte[] serialized) | ||
{ | ||
var decoded = Codec.Decode(serialized); | ||
return Unmarshal(decoded); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
.Lib9c.ActionEvaluatorCommonComponents/PreEvaluationBlockMarshaller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using Libplanet.Types.Blocks; | ||
|
||
namespace Lib9c.ActionEvaluatorCommonComponents | ||
{ | ||
public static class PreEvaluationBlockMarshaller | ||
{ | ||
private static readonly Codec Codec = new Codec(); | ||
|
||
// Copied form Libplanet/Blocks/BlockMarshaler.cs | ||
// Block fields: | ||
private static readonly byte[] HeaderKey = { 0x48 }; // 'H' | ||
private static readonly byte[] TransactionsKey = { 0x54 }; // 'T' | ||
|
||
public static Dictionary Marshal(this IPreEvaluationBlock block) | ||
{ | ||
return Dictionary.Empty | ||
.Add(HeaderKey, BlockMarshaler.MarshalPreEvaluationBlockHeader(block)) | ||
.Add(TransactionsKey, new List(block.Transactions.Select(TransactionMarshaller.Serialize))); | ||
} | ||
|
||
public static IPreEvaluationBlock Unmarshal(Dictionary dictionary) | ||
{ | ||
return new PreEvaluationBlock( | ||
BlockMarshaler.UnmarshalPreEvaluationBlockHeader((Dictionary)dictionary[HeaderKey]), | ||
BlockMarshaler.UnmarshalBlockTransactions(dictionary), | ||
BlockMarshaler.UnmarshalBlockEvidence(dictionary)); | ||
} | ||
|
||
public static byte[] Serialize(this IPreEvaluationBlock block) | ||
{ | ||
return Codec.Encode(Marshal(block)); | ||
} | ||
|
||
public static IPreEvaluationBlock Deserialize(byte[] serialized) | ||
{ | ||
return Unmarshal((Dictionary)Codec.Decode(serialized)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Libplanet.Action; | ||
|
||
namespace Lib9c.ActionEvaluatorCommonComponents | ||
{ | ||
public class Random : System.Random, IRandom | ||
{ | ||
public Random(int seed) | ||
: base(seed) | ||
{ | ||
Seed = seed; | ||
} | ||
|
||
public int Seed { get; private set; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.Lib9c.ActionEvaluatorCommonComponents/TransactionMarshaller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using Libplanet.Types.Tx; | ||
|
||
namespace Lib9c.ActionEvaluatorCommonComponents | ||
{ | ||
public static class TransactionMarshaller | ||
{ | ||
private static readonly Codec Codec = new Codec(); | ||
|
||
// Copied from Libplanet/Tx/TxMarshaler.cs | ||
private static readonly Binary SignatureKey = new Binary(new byte[] { 0x53 }); // 'S' | ||
|
||
public static Dictionary Marshal(this ITransaction transaction) | ||
{ | ||
return transaction.MarshalUnsignedTx().Add(SignatureKey, transaction.Signature); | ||
} | ||
|
||
public static byte[] Serialize(this ITransaction transaction) | ||
{ | ||
return Codec.Encode(Marshal(transaction)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.