Ejemplo n.º 1
0
        public void TransferAsset(
            [Argument("SENDER", Description = "An address of sender.")] string sender,
            [Argument("RECIPIENT", Description = "An address of recipient.")] string recipient,
            [Argument("AMOUNT", Description = "An amount of gold to transfer.")] int goldAmount,
            [Argument("GENESIS-BLOCK", Description = "A genesis block containing InitializeStates.")] string genesisBlock
            )
        {
            byte[] genesisBytes = File.ReadAllBytes(genesisBlock);
            var    genesisDict  = (Bencodex.Types.Dictionary)_codec.Decode(genesisBytes);
            IReadOnlyList <Transaction <NCAction> > genesisTxs =
                BlockMarshaler.UnmarshalBlockTransactions <NCAction>(genesisDict);
            var      initStates = (InitializeStates)genesisTxs.Single().Actions.Single().InnerAction;
            Currency currency   = new GoldCurrencyState(initStates.GoldCurrency).Currency;

            var action = new TransferAsset(
                new Address(sender),
                new Address(recipient),
                currency * goldAmount
                );

            var bencoded = new List(
                (Text)nameof(TransferAsset),
                action.PlainValue
                );

            byte[] raw = _codec.Encode(bencoded);
            Console.Write(ByteUtil.Hex(raw));
        }
Ejemplo n.º 2
0
        internal Block <NullAction> GetGenesisBlock(IBlockPolicy <NullAction> policy)
        {
            var uri = new Uri(GenesisBlockPath);

            using (var client = new WebClient())
            {
                var serialized = client.DownloadData(uri);
                var dict       = (Bencodex.Types.Dictionary)Codec.Decode(serialized);
                return(BlockMarshaler.UnmarshalBlock <NullAction>(policy.GetHashAlgorithm, dict));
            }
        }
Ejemplo n.º 3
0
 public void UnmarshalBlock()
 {
     Assert.Equal(
         _fx.Genesis,
         BlockMarshaler.UnmarshalBlock <FxAction>(_fx.GetHashAlgorithm, _marshaledGenesis)
         );
     Assert.Equal(
         _fx.Next,
         BlockMarshaler.UnmarshalBlock <FxAction>(_fx.GetHashAlgorithm, _marshaledNext)
         );
     Assert.Equal(
         _fx.HasTx,
         BlockMarshaler.UnmarshalBlock <FxAction>(_fx.GetHashAlgorithm, _marshaledHasTx)
         );
 }
Ejemplo n.º 4
0
        public void CompareToOtherBlock()
        {
            Block <PolymorphicAction <BaseAction> > sameBlock1 = _fx.Genesis;
            var sameBlock2 = BlockMarshaler.UnmarshalBlock <PolymorphicAction <BaseAction> >(
                _ => _fx.Genesis.HashAlgorithm,
                _fx.Genesis.MarshalBlock()
                );
            Block <PolymorphicAction <BaseAction> > differentBlock = _fx.Next;

            Assert.Equal(sameBlock1, sameBlock2);
            Assert.NotEqual(sameBlock2, differentBlock);

            Assert.True(sameBlock1.Equals(sameBlock2));
            Assert.False(sameBlock2.Equals(differentBlock));
        }
Ejemplo n.º 5
0
        public BlockDigest(Bencodex.Types.Dictionary dict)
        {
            var headerDict = dict.GetValue <Bencodex.Types.Dictionary>(HeaderKey);
            var tuple      = BlockMarshaler.UnmarshalPreEvaluationBlockHeader(headerDict);

            _metadata          = tuple.Metadata;
            _nonce             = tuple.Nonce;
            _preEvaluationHash = tuple.PreEvaluationHash;
            StateRootHash      = BlockMarshaler.UnmarshalBlockHeaderStateRootHash(headerDict);
            Signature          = BlockMarshaler.UnmarshalBlockHeaderSignature(headerDict);
            Hash  = BlockMarshaler.UnmarshalBlockHeaderHash(headerDict);
            TxIds = dict.ContainsKey((IKey)(Binary)TransactionIdsKey)
                ? dict.GetValue <Bencodex.Types.List>(TransactionIdsKey)
                    .Select(txId => ((Binary)txId).ToImmutableArray()).ToImmutableArray()
                : ImmutableArray <ImmutableArray <byte> > .Empty;
        }
Ejemplo n.º 6
0
        public Bencodex.Types.Dictionary ToBencodex()
        {
            var preEvalHeaderDict = BlockMarshaler.MarshalPreEvaluationBlockHeader(
                BlockMarshaler.MarshalBlockMetadata(_metadata),
                _nonce,
                _preEvaluationHash ?? ImmutableArray <byte> .Empty
                );
            Dictionary headerDict = BlockMarshaler.MarshalBlockHeader(
                preEvalHeaderDict,
                StateRootHash,
                Signature,
                Hash
                );
            var dict = Bencodex.Types.Dictionary.Empty.Add(HeaderKey, headerDict);

            if (TxIds.Any())
            {
                dict = dict.Add(
                    TransactionIdsKey,
                    TxIds.Select(txId => (IValue)(Binary)txId.ToArray()));
            }

            return(dict);
        }
Ejemplo n.º 7
0
 public void MarshalBlockMetadata()
 {
     AssertBencodexEqual(
         _marshaledGenesisMetadata,
         BlockMarshaler.MarshalBlockMetadata(_fx.Genesis)
         );
     AssertBencodexEqual(
         _marshaledNextMetadata,
         BlockMarshaler.MarshalBlockMetadata(_fx.Next)
         );
     AssertBencodexEqual(
         _marshaledHasTxMetadata,
         BlockMarshaler.MarshalBlockMetadata(_fx.HasTx)
         );
     AssertBencodexEqual(
         Dictionary.Empty
         .Add(IndexKey, 0L)
         .Add(TimestampKey, "2021-09-06T04:46:39.123000Z")
         .Add(DifficultyKey, 0L)
         .Add(TotalDifficultyKey, 0)
         .Add(MinerKey, _content.BlockPv0.Miner.ByteArray),
         BlockMarshaler.MarshalBlockMetadata(_content.BlockPv0)
         );
     AssertBencodexEqual(
         Dictionary.Empty
         .Add(ProtocolVersionKey, 1)
         .Add(IndexKey, 1L)
         .Add(PreviousHashKey, _content.BlockPv1.PreviousHash?.ByteArray ?? default)
         .Add(TimestampKey, "2021-09-06T08:01:09.045000Z")
         .Add(DifficultyKey, 123L)
         .Add(TotalDifficultyKey, 123)
         .Add(MinerKey, _content.BlockPv1.Miner.ByteArray)
         .Add(TxHashKey, _content.BlockPv1.TxHash?.ByteArray ?? default),
         BlockMarshaler.MarshalBlockMetadata(_content.BlockPv1)
         );
 }
Ejemplo n.º 8
0
 public BlockHeader GetHeader(HashAlgorithmGetter hashAlgorithmGetter)
 {
     return(BlockMarshaler.UnmarshalBlockHeader(hashAlgorithmGetter, HeaderDictionary));
 }