public SolidityExecutor Execute(IEnumerable <byte> scAddrPayload, IEnumerable <byte> addrPayload, IEnumerable <byte> data, bool addInTransaction = false) { if (scAddrPayload == null) { throw new ArgumentNullException(nameof(scAddrPayload)); } if (addrPayload == null) { addrPayload = new byte[0]; } var smartContract = _smartContractStore.GetSmartContracts().GetSmartContract(scAddrPayload); var defaultCallValue = new DataWord(new byte[] { 0x00 }); _smartContracts = _smartContractStore.GetSmartContracts(); var scode = smartContract.Code.ToHexString(); _solidityProgram = new SolidityProgram(smartContract.Code.ToList(), new SolidityProgramInvoke(data, smartContract.Address, new DataWord(addrPayload.ToArray()), defaultCallValue, _smartContracts, addInTransaction)); var vm = new SolidityVm(); while (!_solidityProgram.IsStopped()) { vm.Step(_solidityProgram); } return(this); }
private void Persist(Block block, WriteBatch writeBatch, bool addIntoCache = false) { if (writeBatch == null) { throw new ArgumentNullException(nameof(writeBatch)); } if (block == null) { throw new ArgumentNullException(nameof(block)); } var defaultCallValue = new DataWord(new byte[] { 0x00 }); foreach (var transaction in block.Transactions) { var smartContractTransaction = transaction as SmartContractTransaction; if (smartContractTransaction == null) { continue; } if (smartContractTransaction.To == null || !smartContractTransaction.To.Any()) // Create the contract. { var solidityVm = new SolidityVm(); var program = new SolidityProgram(smartContractTransaction.Data.ToList(), new SolidityProgramInvoke(new byte[0], new DataWord(smartContractTransaction.From.ToArray()), defaultCallValue, this)); while (!program.IsStopped()) { solidityVm.Step(program); } var contractCode = program.GetResult().GetHReturn(); var txId = transaction.GetTxId(); var smartContractAdr = smartContractTransaction.GetSmartContractAddress(); var hex = contractCode.ToHexString(); writeBatch.Put(string.Format(SMART_CONTRACT_ELT, smartContractAdr.ToHexString()), hex); writeBatch.Put(string.Format(SMART_CONTRACT_TX_ELT, txId.ToHexString()), smartContractAdr.ToHexString()); if (addIntoCache) { _cacheSmartContracts.Add(smartContractAdr.ToHexString(), new SmartContract { Address = smartContractAdr, Code = contractCode }); _cacheTxSmartContracts.Add(txId.ToHexString(), new SmartContract { Address = smartContractAdr, Code = contractCode }); } } else if (smartContractTransaction.To != null) { var sc = GetSmartContract(smartContractTransaction.To); if (sc == null) { return; } var from = smartContractTransaction.From; if (from == null) { from = new byte[0]; } var solidityVm = new SolidityVm(); var program = new SolidityProgram(sc.Code.ToList(), new SolidityProgramInvoke(smartContractTransaction.Data, smartContractTransaction.To, new DataWord(from.ToArray()), defaultCallValue, this)); while (!program.IsStopped()) { solidityVm.Step(program); } var logs = program.GetResult().GetLogs(); if (logs != null && logs.Any()) { AddLogs(smartContractTransaction.To, block.GetHashHeader(), logs, addIntoCache); } } } }