Esempio n. 1
0
        /**
         * @TODO this should extend file system based tests and resolve tests via autofac container
         */
        public DeltaExecutorTests()
        {
            _specProvider  = new CatalystSpecProvider();
            _stateProvider = new StateProvider(new StateDb(), new StateDb(), LimboLogs.Instance);
            var  storageProvider = new StorageProvider(new StateDb(), _stateProvider, LimboLogs.Instance);
            IKvm virtualMachine  = new KatVirtualMachine(_stateProvider, storageProvider, new StateUpdateHashProvider(),
                                                         _specProvider, LimboLogs.Instance);
            var logger = Substitute.For <ILogger>();

            logger.IsEnabled(Arg.Any <LogEventLevel>()).Returns(true);

            _senderPrivateKey = _cryptoContext.GeneratePrivateKey();
            _senderPublicKey  = _senderPrivateKey.GetPublicKey();

            _recipient  = _cryptoContext.GeneratePrivateKey().GetPublicKey();
            _poorSender = _cryptoContext.GeneratePrivateKey().GetPublicKey();

            _stateProvider.CreateAccount(_poorSender.ToKvmAddress(), 0.Kat());
            _stateProvider.CreateAccount(_senderPublicKey.ToKvmAddress(), 1000.Kat());
            _stateProvider.CreateAccount(Address.Zero, 1000.Kat());
            _stateProvider.Commit(_specProvider.GenesisSpec);

            _executor = new DeltaExecutor(_specProvider, _stateProvider, storageProvider, virtualMachine,
                                          new FfiWrapper(), logger);

            _signingContext = new SigningContext
            {
                NetworkType   = NetworkType.Devnet,
                SignatureType = SignatureType.TransactionPublic
            };
        }
        /*
         * 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);
        }
Esempio n. 3
0
        public void Init()
        {
            _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));

            _random = new Random(1);

            _randomFactory = Substitute.For <IDeterministicRandomFactory>();
            _randomFactory.GetDeterministicRandomFromSeed(Arg.Any <byte[]>())
            .Returns(ci => new IsaacRandom(((byte[])ci[0]).ToHex()));

            _producerId   = PeerIdHelper.GetPeerId("producer");
            _peerSettings = _producerId.ToSubstitutedPeerSettings();

            _previousDeltaHash = _hashProvider.ComputeUtf8MultiHash("previousDelta").ToCid();
            _zeroCoinbaseEntry = new CoinbaseEntry
            {
                Amount            = UInt256.Zero.ToUint256ByteString(),
                ReceiverPublicKey = _producerId.PublicKey.ToByteString()
            };

            _logger = Substitute.For <ILogger>();

            _cache = Substitute.For <IDeltaCache>();

            Delta previousDelta = new Delta();

            previousDelta.StateRoot = ByteString.CopyFrom(Keccak.EmptyTreeHash.Bytes);
            _cache.TryGetOrAddConfirmedDelta(Arg.Any <Cid>(), out Arg.Any <Delta>()).Returns(x =>
            {
                x[1] = previousDelta;
                return(true);
            });

            _dateTimeProvider = new DateTimeProvider();

            IDb             codeDb       = new MemDb();
            ISnapshotableDb stateDb      = new StateDb();
            ISpecProvider   specProvider = new CatalystSpecProvider();

            _cryptoContext = new FfiWrapper();
            _stateProvider = new StateProvider(stateDb, codeDb, LimboLogs.Instance);
            IStorageProvider  storageProvider = new StorageProvider(stateDb, _stateProvider, LimboLogs.Instance);
            KatVirtualMachine virtualMachine  = new KatVirtualMachine(_stateProvider,
                                                                      storageProvider,
                                                                      new StateUpdateHashProvider(),
                                                                      specProvider,
                                                                      new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256")),
                                                                      new FfiWrapper(),
                                                                      LimboLogs.Instance);

            _deltaExecutor = new DeltaExecutor(specProvider,
                                               _stateProvider,
                                               storageProvider,
                                               virtualMachine,
                                               _cryptoContext,
                                               _logger);
        }