예제 #1
0
        private Hash ComputeRootWithTransactionStatusMerklePath(Hash txId, MerklePath path)
        {
            var txResultStatusRawBytes =
                EncodingHelper.EncodeUtf8(TransactionResultStatus.Mined.ToString());
            var hash = HashHelper.ComputeFrom(ByteArrayHelper.ConcatArrays(txId.ToByteArray(), txResultStatusRawBytes));

            return(path.ComputeRootWithLeafNode(hash));
        }
        private Hash GetHashCombiningTransactionAndStatus(Hash txId,
                                                          TransactionResultStatus executionReturnStatus)
        {
            // combine tx result status
            var rawBytes = ByteArrayHelper.ConcatArrays(txId.ToByteArray(),
                                                        EncodingHelper.EncodeUtf8(executionReturnStatus.ToString()));

            return(HashHelper.ComputeFrom(rawBytes));
        }
        private Hash GenerateVoteId(VoteMinerInput voteMinerInput)
        {
            if (voteMinerInput.Token != null)
            {
                return(Context.GenerateId(Context.Self, voteMinerInput.Token));
            }

            var candidateVotesCount =
                State.CandidateVotes[voteMinerInput.CandidatePubkey]?.ObtainedActiveVotedVotesAmount ?? 0;

            return(Context.GenerateId(Context.Self,
                                      ByteArrayHelper.ConcatArrays(voteMinerInput.CandidatePubkey.GetBytes(),
                                                                   candidateVotesCount.ToBytes(false))));
        }
예제 #4
0
        public void ConcatArrays_Test()
        {
            var bytes1      = new byte[] { 1, 2, 3, 4 };
            var bytes2      = new byte[] { 5, 6, 7, 8 };
            var concatBytes = ByteArrayHelper.ConcatArrays(bytes1, bytes2);

            concatBytes.Length.ShouldBe(8);
            for (var i = 0; i < bytes1.Length; i++)
            {
                concatBytes[i].ShouldBe(bytes1[i]);
            }
            for (var i = bytes1.Length; i < concatBytes.Length; i++)
            {
                concatBytes[i].ShouldBe(bytes2[i - bytes1.Length]);
            }
        }
예제 #5
0
        public override Empty RegisterForProfits(RegisterForProfitsInput input)
        {
            Assert(State.LockIds[input.SchemeManager][Context.Sender] == null, "Already registered.");
            var scheme = GetValidScheme(input.SchemeManager);

            if (State.TokenContract.Value == null)
            {
                State.TokenContract.Value =
                    Context.GetContractAddressByName(SmartContractConstants.TokenContractSystemName);
            }

            var lockId = Context.GenerateId(Context.Self,
                                            ByteArrayHelper.ConcatArrays(input.SchemeManager.ToByteArray(), Context.Sender.ToByteArray()));

            State.TokenContract.Lock.Send(new LockInput
            {
                LockId  = lockId,
                Symbol  = scheme.Symbol,
                Address = Context.Sender,
                Amount  = input.Amount,
            });
            State.LockIds[input.SchemeManager][Context.Sender] = lockId;
            State.LockTimestamp[lockId] = Context.CurrentBlockTime;
            State.ProfitContract.AddBeneficiary.Send(new AddBeneficiaryInput
            {
                SchemeId         = scheme.SchemeId,
                BeneficiaryShare = new BeneficiaryShare
                {
                    Beneficiary = Context.Sender,
                    Shares      = input.Amount
                }
            });

            // Check auto-distribute threshold.
            if (scheme.AutoDistributeThreshold != null && scheme.AutoDistributeThreshold.Any())
            {
                var originScheme   = State.ProfitContract.GetScheme.Call(scheme.SchemeId);
                var virtualAddress = originScheme.VirtualAddress;
                Profit.DistributeProfitsInput distributedInput = null;
                foreach (var threshold in scheme.AutoDistributeThreshold)
                {
                    var balance = State.TokenContract.GetBalance.Call(new GetBalanceInput
                    {
                        Owner  = virtualAddress,
                        Symbol = threshold.Key
                    }).Balance;
                    if (balance < threshold.Value)
                    {
                        continue;
                    }
                    if (distributedInput == null)
                    {
                        distributedInput = new Profit.DistributeProfitsInput
                        {
                            SchemeId = scheme.SchemeId,
                            Period   = scheme.Period
                        }
                    }
                    ;
                    distributedInput.AmountsMap[threshold.Key] = 0;
                    break;
                }

                if (distributedInput == null)
                {
                    return(new Empty());
                }
                State.ProfitContract.DistributeProfits.Send(distributedInput);
                scheme.Period = scheme.Period.Add(1);
                State.TokenHolderProfitSchemes[input.SchemeManager] = scheme;
            }

            return(new Empty());
        }