Beispiel #1
0
        public void Init()
        {
            if (Args.Instance.LocalWitness.GetPrivateKey().IsNullOrEmpty())
            {
                return;
            }

            byte[] privatekey         = Args.Instance.LocalWitness.GetPrivateKey();
            byte[] witness_address    = Args.Instance.LocalWitness.GetWitnessAccountAddress();
            byte[] privatekey_address = Wallet.PublickKeyToAddress(ECKey.FromPrivateKey(privatekey).PublicKey);

            WitnessCapsule witness = Manager.Instance.DBManager.Witness.Get(witness_address);

            if (witness == null)
            {
                Logger.Warning(
                    string.Format("WitnessCapsule[{0}] is not in witnessStore",
                                  witness_address));

                witness = new WitnessCapsule(ByteString.CopyFrom(witness_address));
            }

            this.privatekeys.Add(witness.Address, privatekey);
            this.local_witness_states.Add(witness.Address, witness);
            this.privatekey_addresses.Add(privatekey, privatekey_address);
        }
Beispiel #2
0
        private void CreateWitness(WitnessCreateContract contract)
        {
            WitnessCapsule witness = new WitnessCapsule(
                contract.OwnerAddress,
                0,
                contract.Url.ToStringUtf8());

            Logger.Debug(
                string.Format(
                    "CreateWitness, address[{0}]",
                    witness.ToHexString()));

            this.db_manager.Witness.Put(witness.CreateDatabaseKey(), witness);
            AccountCapsule account = this.db_manager.Account.Get(witness.CreateDatabaseKey());

            account.IsWitness = true;

            if (this.db_manager.DynamicProperties.GetAllowMultiSign() == 1)
            {
                account.SetDefaultWitnessPermission(this.db_manager);
            }
            this.db_manager.Account.Put(account.CreateDatabaseKey(), account);

            long cost = this.db_manager.DynamicProperties.GetAccountUpgradeCost();

            this.db_manager.AdjustBalance(contract.OwnerAddress.ToByteArray(), -cost);
            this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().CreateDatabaseKey(), +cost);
            this.db_manager.DynamicProperties.AddTotalCreateWitnessCost(cost);
        }
Beispiel #3
0
        public WitnessCapsule GetWitness(byte[] address)
        {
            WitnessCapsule result = null;
            Key            key    = new Key(address);

            if (this.witness_cache.ContainsKey(key))
            {
                result = this.witness_cache[key].ToCapsule <WitnessCapsule, Protocol.Witness>();
            }
            else
            {
                if (this.parent != null)
                {
                    result = this.parent.GetWitness(address);
                }
                else
                {
                    result = this.db_manager.Witness.Get(address);
                }

                if (result != null)
                {
                    this.witness_cache.Add(key, Value.Create(result.Data));
                }
            }

            return(result);
        }
Beispiel #4
0
        public void DumpParticipationLog()
        {
            StringBuilder builder = new StringBuilder();

            int[] block_filled_slots = this.db_manager.DynamicProperties.GetBlockFilledSlots();

            builder.Append("Dump articipation log \n ")
            .Append("block filled slots : ")
            .Append(string.Join("", block_filled_slots))
            .Append(",")
            .Append("\n")
            .Append(" Head slot:")
            .Append(GetHeadSlot())
            .Append(",");

            List <ByteString> active_witness = GetActiveWitnesses();

            active_witness.ForEach(active =>
            {
                WitnessCapsule witness = this.db_manager.Witness.Get(active.ToByteArray());
                builder.Append("\n")
                .Append(" Witness : ")
                .Append(witness.ToHexString())
                .Append(",")
                .Append("LatestBlockNum : ")
                .Append(witness.LatestBlockNum)
                .Append(",")
                .Append("LatestSlotNum : ")
                .Append(witness.LatestSlotNum)
                .Append(".");
            });
            Logger.Debug(builder.ToString());
        }
Beispiel #5
0
        private void UpdateWitness(WitnessUpdateContract contract)
        {
            WitnessCapsule witness = this.db_manager.Witness.Get(contract.OwnerAddress.ToByteArray());

            witness.Url = contract.UpdateUrl.ToStringUtf8();
            this.db_manager.Witness.Put(witness.CreateDatabaseKey(), witness);
        }
Beispiel #6
0
        public void TryRemovePowerOfGr()
        {
            if (this.db_manager.DynamicProperties.GetRemoveThePowerOfTheGr() == 1)
            {
                WitnessStore witness_store = this.db_manager.Witness;

                Args.Instance.GenesisBlock.Witnesses.ForEach(witness =>
                {
                    WitnessCapsule witness_capsule = witness_store.Get(witness.Address);
                    witness_capsule.VoteCount      = witness_capsule.VoteCount - witness.VoteCount;

                    witness_store.Put(witness_capsule.CreateDatabaseKey(), witness_capsule);
                });

                this.db_manager.DynamicProperties.PutRemoveThePowerOfTheGr(-1);
            }
        }
Beispiel #7
0
        public void UpdateWitness()
        {
            TryRemovePowerOfGr();
            Dictionary <ByteString, long> count_witness = GetVoteCount(this.db_manager.Votes);

            if (count_witness.IsNullOrEmpty())
            {
                Logger.Info("No vote, no change to witness.");
            }
            else
            {
                List <ByteString> active_witness  = GetActiveWitnesses();
                List <ByteString> witness_address = new List <ByteString>();
                this.db_manager.Witness.AllWitnesses.ForEach(witness =>
                {
                    witness_address.Add(witness.Address);
                });

                foreach (KeyValuePair <ByteString, long> pair in count_witness)
                {
                    WitnessCapsule witness = this.db_manager.Witness.Get(pair.Key.ToByteArray());
                    if (witness == null)
                    {
                        Logger.Warning(
                            string.Format("WitnessCapsule is null address is {0}", Wallet.AddressToBase58(pair.Key.ToByteArray())));

                        return;
                    }

                    AccountCapsule account = this.db_manager.Account.Get(pair.Key.ToByteArray());
                    if (account == null)
                    {
                        Logger.Warning("Witness account[" + Wallet.AddressToBase58(pair.Key.ToByteArray()) + "] not exists");
                    }
                    else
                    {
                        witness.VoteCount += pair.Value;
                        this.db_manager.Witness.Put(witness.CreateDatabaseKey(), witness);
                        Logger.Info(
                            string.Format(
                                "Address is {0}  ,count vote is {1}",
                                Wallet.AddressToBase58(witness.Address.ToByteArray()),
                                witness.VoteCount));
                    }
                }

                SortWitness(ref witness_address);
                if (witness_address.Count > Parameter.ChainParameters.MAX_ACTIVE_WITNESS_NUM)
                {
                    SetActiveWitnesses(witness_address.GetRange(0, Parameter.ChainParameters.MAX_ACTIVE_WITNESS_NUM));
                }
                else
                {
                    SetActiveWitnesses(witness_address);
                }

                if (witness_address.Count > Parameter.ChainParameters.WITNESS_STANDBY_LENGTH)
                {
                    PayStandbyWitness(witness_address.GetRange(0, Parameter.ChainParameters.WITNESS_STANDBY_LENGTH));
                }
                else
                {
                    PayStandbyWitness(witness_address);
                }

                List <ByteString> new_active_witness = GetActiveWitnesses();
                if (WitnessSetChanged(active_witness, new_active_witness))
                {
                    active_witness.ForEach(address =>
                    {
                        WitnessCapsule witness = GetWitnessesByAddress(address);
                        witness.IsJobs         = false;
                        this.db_manager.Witness.Put(witness.CreateDatabaseKey(), witness);
                    });

                    new_active_witness.ForEach(address =>
                    {
                        WitnessCapsule witness = GetWitnessesByAddress(address);
                        witness.IsJobs         = true;
                        this.db_manager.Witness.Put(witness.CreateDatabaseKey(), witness);
                    });
                }

                Logger.Info(
                    string.Format("Update Witness, Before:{0},\nAfter:{1}  ",
                                  string.Join(", ", active_witness.Select(x => Wallet.AddressToBase58(x.ToByteArray())).ToList()),
                                  string.Join(", ", new_active_witness.Select(x => Wallet.AddressToBase58(x.ToByteArray())).ToList()))
                    );
            }
        }