Exemple #1
0
        public static ApplicationEngine Run(byte[] script, UInt256 ChainHash, IScriptContainer container = null, Block persisting_block = null)
        {
            BlockchainBase Chain = BlockchainBase.GetBlockchain(ChainHash);

            if (persisting_block == null)
            {
                persisting_block = new Block(ChainHash)
                {
                    Version       = 0,
                    PrevHash      = Chain.CurrentBlockHash,
                    MerkleRoot    = new UInt256(),
                    Timestamp     = Chain.GetHeader(Chain.Height).Timestamp + Chain.SecondsPerBlock,
                    Index         = Chain.Height + 1,
                    ConsensusData = 0,
                    NextConsensus = Chain.GetHeader(Chain.Height).NextConsensus,
                    Script        = new Witness
                    {
                        InvocationScript   = new byte[0],
                        VerificationScript = new byte[0]
                    },
                    Transactions = new Transaction[0]
                }
            }
            ;
            DataCache <UInt160, AccountState>   accounts  = Chain.GetStates <UInt160, AccountState>();
            DataCache <UInt256, AssetState>     assets    = Chain.GetStates <UInt256, AssetState>();
            DataCache <UInt160, ContractState>  contracts = Chain.GetStates <UInt160, ContractState>();
            DataCache <StorageKey, StorageItem> storages  = Chain.GetStates <StorageKey, StorageItem>();
            CachedScriptTable script_table = new CachedScriptTable(contracts);

            using (StateMachine service = new StateMachine(persisting_block, accounts, assets, contracts, storages))
            {
                ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, container, script_table, service, Fixed8.Zero, true);
                engine.LoadScript(script, false);
                engine.Execute();
                return(engine);
            }
        }
    }
Exemple #2
0
        private void OnGetBlocksMessageReceived(GetBlocksPayload payload)
        {
            if (!localNode.ServiceEnabled)
            {
                return;
            }

            BlockchainBase Chain = BlockchainBase.GetBlockchain(payload.ChainHash);

            if (Chain == null)
            {
                return;
            }

            UInt256 hash = payload.HashStart.Select(p => Chain.GetHeader(p)).Where(p => p != null).OrderBy(p => p.Index).Select(p => p.Hash).FirstOrDefault();

            if (hash == null || hash == payload.HashStop)
            {
                return;
            }
            List <UInt256> hashes = new List <UInt256>();

            do
            {
                hash = Chain.GetNextBlockHash(hash);
                if (hash == null)
                {
                    break;
                }
                hashes.Add(hash);
            } while (hash != payload.HashStop && hashes.Count < 500);
            if (hashes.Count > 0)
            {
                EnqueueMessage("inv", InvPayload.Create(InventoryType.Block, hashes.ToArray()));
            }
        }