예제 #1
0
        public void Can_provide_a_latest_state_update_hash()
        {
            var stateUpdate = new StateUpdate();

            var stateUpdateHashProvider = new StateUpdateHashProvider();

            stateUpdateHashProvider.GetHash(stateUpdate, 1);
        }
        /*
         * also need to test:
         * BALANCE
         * EXTCODECOPY
         * CALLDATACOPY
         * GASPRICE
         * CREATE
         */

        private static GethLikeTxTracer RunVirtualMachine(byte[] code)
        {
            var txTracer = new GethLikeTxTracer(GethTraceOptions.Default);

            IDb stateDbDevice = new MemDb();
            IDb codeDbDevice  = new MemDb();

            ISnapshotableDb stateDb = new StateDb(stateDbDevice);
            ISnapshotableDb codeDb  = new StateDb(codeDbDevice);

            IStateProvider   stateProvider   = new StateProvider(stateDb, codeDb, LimboLogs.Instance);
            IStorageProvider storageProvider = new StorageProvider(stateDb, stateProvider, LimboLogs.Instance);

            IStateUpdateHashProvider stateUpdateHashProvider = new StateUpdateHashProvider();
            ISpecProvider            specProvider            = new CatalystSpecProvider();

            // these values will be set by the tx processor within the state update logic
            var env = new ExecutionEnvironment();

            env.Originator       = Address.Zero; // tx sender
            env.Sender           = Address.Zero; // sender of this call for a given call depth
            env.ExecutingAccount =
                Address.Zero;                    // account in which context the code is executed, it may be different from the code source when invoking a lib
            env.Value = 1
                        .Kat();                  // sometimes the value is just a description of an upper level call to be used by a an invoke library method
            env.TransferValue = 1.Kat();         // this is the actual value transferred from sender to recipient
            env.GasPrice      = 0;               // conversion from gas units to FULs
            env.InputData     =
                new byte[0];                     // only needed for contracts requiring input (ensure that this is not limited to 60bytes)
            env.CallDepth = 0;                   // zero when starting tx

            var stateUpdate = new StateUpdate(); // Catalyst single state update context (all phases together)

            stateUpdate.Difficulty =
                1;                                     // some metric describing the state update that is relevant for consensus
            stateUpdate.Number         = 1;            // state update sequence number
            stateUpdate.Timestamp      = 1;            // state update T0
            stateUpdate.GasLimit       = 1_000_000;    // max gas units to be available for this tx inside the kvm
            stateUpdate.GasBeneficiary = Address.Zero; // will get all the gas fees
            stateUpdate.GasUsed        =
                0L;                                    // zero if this is the first transaction in the update (accumulator over txs)
            env.CurrentBlock = stateUpdate;

            // this would be loaded by the tx processor from the recipient's code storage
            var codeInfo = new CodeInfo(code);

            env.CodeInfo   = codeInfo;
            env.CodeSource = Address.Zero;

            // this would be set by the tx processor that understands the type of transaction
            var vmState = new VmState(1_000_000L, env, ExecutionType.Transaction, false, true, false);

            var virtualMachine = new KatVirtualMachine(stateProvider, storageProvider, stateUpdateHashProvider,
                                                       specProvider, LimboLogs.Instance);

            virtualMachine.Run(vmState, txTracer);
            return(txTracer);
        }