Esempio n. 1
0
        public async Task UpdateAsync(SmartContractDto contract)
        {
            if (contract == null)
            {
                throw new NullReferenceException("Contract is null");
            }

            await _documentClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(_databaseName, _documentCollectionName, contract.Id), contract);
        }
        public async Task DeploySmartContractAsync(SmartContractDto smartContract)
        {
            if (smartContract == null)
            {
                throw new ArgumentNullException(nameof(smartContract));
            }

            // unlock the admin account first for 120 seconds
            var unlockResult =
                await _web3.Personal.UnlockAccount.SendRequestAsync(_blockchainAdminAccount, _blockchainAdminPassphrase,
                                                                    120);

            if (!unlockResult)
            {
                throw new Exception(
                          $"Failed to unlock account {_blockchainAdminAccount} check you have the correct passphrase in the Service Fabric config.");
            }

            string transactionsHash;

            try
            {
                transactionsHash =
                    await _web3.Eth.DeployContract.SendRequestAsync(smartContract.ByteCode, _blockchainAdminAccount,
                                                                    new HexBigInteger(900000));
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to deploy smart contract {smartContract.Name} version {smartContract.Version}", ex);
            }

            var receipt = await _web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionsHash);

            // wait for the transaction (smart contract deploy) to be mined.
            while (receipt == null)
            {
                Thread.Sleep(5000);
                receipt = await _web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionsHash);
            }

            // now we have the contract address we need to update the documentDB record
            var contractAddress = receipt.ContractAddress;

            smartContract.Address    = contractAddress;
            smartContract.IsDeployed = true;

            // now update the smart contract so we know it has been deployed along with the smart contract address.
            await _smartContractServiceAgent.UpdateAsync(smartContract);
        }
Esempio n. 3
0
        public void AddContract()
        {
            // add dummy smart contract

            SmartContractDto test = new SmartContractDto();

            test.Name       = SmartContractName.DeviceMovement;
            test.Id         = string.Format("{0}.{1}", SmartContractName.DeviceMovement.ToString(), "1");
            test.Version    = 1.0;
            test.IsDeployed = false;
            test.ByteCode   = "6060604052341561000c57fe5b5b6105218061001c6000396000f300606060405263ffffffff60e060020a6000350416637ebf139a811461002c5780638fc7a6921461015d575bfe5b341561003457fe5b610042600435602435610207565b60408051908101839052600160a060020a038216606082015260808082528554600260001961010060018416150201909116049082018190528190602082019060a0830190889080156100d65780601f106100ab576101008083540402835291602001916100d6565b820191906000526020600020905b8154815290600101906020018083116100b957829003601f168201915b505083810382528654600260001961010060018416150201909116048082526020909101908790801561014a5780601f1061011f5761010080835404028352916020019161014a565b820191906000526020600020905b81548152906001019060200180831161012d57829003601f168201915b5050965050505050505060405180910390f35b341561016557fe5b60408051602060046024803582810135601f81018590048502860185019096528585526101f3958335959394604494939290920191819084018382808284375050604080516020601f89358b01803591820183900483028401830190945280835297999881019791965091820194509250829150840183828082843750949650509335935061025492505050565b604080519115158252519081900360200190f35b60006020528160005260406000208181548110151561022257fe5b906000526020600020906004020160005b506002810154600382015491935060018401925090600160a060020a031684565b600061025e610336565b50604080516080810182528581526020808201869052818301859052600160a060020a0333166060830152600088815290819052919091208054600181016102a6838261036b565b916000526020600020906004020160005b50825180518492916102ce9183916020019061039d565b5060208281015180516102e7926001850192019061039d565b50604082015160028201556060909101516003909101805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0390921691909117905550600191505b50949350505050565b60806040519081016040528061034a61041c565b815260200161035761041c565b815260006020820181905260409091015290565b81548183558181151161039757600402816004028360005260206000209182019101610397919061042e565b5b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103de57805160ff191683800117855561040b565b8280016001018555821561040b579182015b8281111561040b5782518255916020019190600101906103f0565b5b5061041892915061048c565b5090565b60408051602081019091526000815290565b61048991905b8082111561041857600061044882826104ad565b6104566001830160006104ad565b506000600282015560038101805473ffffffffffffffffffffffffffffffffffffffff19169055600401610434565b5090565b90565b61048991905b808211156104185760008155600101610492565b5090565b90565b50805460018160011615610100020316600290046000825580601f106104d357506104f1565b601f0160209004906000526020600020908101906104f1919061048c565b5b505600a165627a7a723058208987c9adf30b9190ee099dd8cc9135ba6410847068a1e0b1b3d68f6a5b74c71d0029";
            test.Abi        = "[{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"bytes32\"},{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"telemetryCollection\",\"outputs\":[{\"name\":\"lat\",\"type\":\"string\"},{\"name\":\"long\",\"type\":\"string\"},{\"name\":\"temperatureInCelcius\",\"type\":\"int256\"},{\"name\":\"sender\",\"type\":\"address\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"key\",\"type\":\"bytes32\"},{\"name\":\"lat\",\"type\":\"string\"},{\"name\":\"long\",\"type\":\"string\"},{\"name\":\"temperatureInCelcius\",\"type\":\"int256\"}],\"name\":\"StoreMovement\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"}]";

            Task.Run(async() => await _documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_databaseName, _documentCollectionName), test)).GetAwaiter().GetResult();
        }
Esempio n. 4
0
 public async Task UpdateAsync(SmartContractDto contract)
 {
     try
     {
         await _smartContractStoreService.UpdateAsync(contract);
     }
     catch (FabricServiceNotFoundException notFoundex)
     {
         throw new Exception(
                   $"Unable to communicate with the SmartContractStoreService to update the Smart contract. Reason: {notFoundex.Message}");
     }
     catch (Exception ex)
     {
         throw new Exception($"Unable to communicate with the SmartContractStoreService reason: {ex} ");
     }
 }
        public async Task <List <TrackingDto> > GetTrackingUsingHashesAsync(List <TrackerHashDto> trackerHashCollection)
        {
            // get the latest smart contract version to invoke.
            _deviceMovementSmartContract =
                await _smartContractServiceAgent.GetLatestVersionSmartContractByName(SmartContractName.DeviceMovement);

            // if it's been removed since we bootstrapped the application, redeploy it.
            if (!_deviceMovementSmartContract.IsDeployed)
            {
                throw new Exception(
                          $"Smart contract {_deviceMovementSmartContract.Name} version {_deviceMovementSmartContract.Version} has not been deployed.");
            }

            // now load the contract using the contract address
            _contract = _web3.Eth.GetContract(_deviceMovementSmartContract.Abi,
                                              _deviceMovementSmartContract.Address);

            _telemetryCollectionFunction = _contract.GetFunction("telemetryCollection");

            // unlock the account.
            var unlockResult = await _web3.Personal.UnlockAccount.SendRequestAsync(_blockchainAdminAccount,
                                                                                   _blockchainAdminPassphrase, 1000);

            if (!unlockResult)
            {
                throw new Exception(
                          $"Unable to unlock account {_blockchainAdminAccount} check passphrase is correct and that the account is valid");
            }

            List <TrackingDto> trackingDtoCollection = new List <TrackingDto>();

            Task.WaitAll(trackerHashCollection.Select(t => ProcessTracker(t, trackingDtoCollection))
                         .ToArray());

            return(trackingDtoCollection.OrderByDescending(x => x.TimeStamp).ToList());
        }
 public Task UpdateAsync(SmartContractDto contract)
 {
     return(_smartContractsRepository.UpdateAsync(contract));
 }
        public async Task PublishAsync(SensorDto payload)
        {
            // publish the telemetry on the blockchain. Firstly check if we have a reference to the contract.
            if (_contract == null)
            {
                // get the latest smart contract version to invoke.
                _deviceMovementSmartContract = _smartContractServiceAgent.GetLatestVersionSmartContractByName(SmartContractName.DeviceMovement).Result;

                // if it's been removed since we bootstrapped the application, redeploy it.
                if (!_deviceMovementSmartContract.IsDeployed)
                {
                    await _blockchainServiceAgent.DeploySmartContractAsync(_deviceMovementSmartContract);
                }

                // now load the contract using the contract address

                _contract = _web3.Eth.GetContract(_deviceMovementSmartContract.Abi,
                                                  _deviceMovementSmartContract.Address);

                _storeMovementFunction = _contract.GetFunction("StoreTelemetry");
            }

            DeviceTwinTagsDto deviceTwin = null;

            if (!_deviceTwinFuncs.ContainsKey(payload.DeviceId))
            {
                deviceTwin = await _deviceStoreServiceAgent.GetDeviceTwinTagsByIdAsync(payload.DeviceId);

                _deviceTwinFuncs.Add(payload.DeviceId, () => deviceTwin);
            }
            else
            {
                deviceTwin = _deviceTwinFuncs[payload.DeviceId]();
            }

            // now to get the account and key for this blockchain user if we don't have it already.

            // unlock the account.
            var unlockResult = await _web3.Personal.UnlockAccount.SendRequestAsync(deviceTwin.BlockchainAccount, "Monday01", 1000);

            if (!unlockResult)
            {
                throw new Exception($"Unable to unlock account {deviceTwin.BlockchainAccount}");
            }

            var transactionsHash =
                await
                _storeMovementFunction.SendTransactionAsync(deviceTwin.BlockchainAccount, new HexBigInteger(900000), null,
                                                            payload.TransactionId, // this is the index for the record
                                                            payload.GpsLat,
                                                            payload.GpsLong,
                                                            payload.TemperatureInCelcius,
                                                            payload.DeviceId,
                                                            Convert.ToInt64(payload.Timestamp.Ticks));

            // check it has been mined.
            var receipt = await _web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionsHash);

            while (receipt == null)
            {
                Thread.Sleep(5000);
                receipt = await _web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionsHash);
            }

            // now pass the transaction hash and id of the record so we can find it within blockchain or the smart contract to the tracking service, no need to await this process.
            await _trackerStoreServiceAgent.PublishAsync(
                new TrackerHashDto(payload.TransactionId, receipt.TransactionHash, payload.DeviceId, payload.Timestamp, receipt.BlockNumber.HexValue, receipt.BlockHash, receipt.TransactionIndex.HexValue, _deviceMovementSmartContract.Address));
        }