Esempio n. 1
0
        private string GetProof(Trie trie, int contract_id, byte[] key)
        {
            StorageKey skey = new StorageKey
            {
                Id  = contract_id,
                Key = key,
            };
            var result = trie.TryGetProof(skey.ToArray(), out var proof);

            if (!result)
            {
                throw new KeyNotFoundException();
            }

            using MemoryStream ms     = new();
            using BinaryWriter writer = new(ms, Utility.StrictUTF8);

            writer.WriteVarBytes(skey.ToArray());
            writer.WriteVarInt(proof.Count);
            foreach (var item in proof)
            {
                writer.WriteVarBytes(item);
            }
            writer.Flush();

            return(Convert.ToBase64String(ms.ToArray()));
        }
Esempio n. 2
0
        internal override async ContractTask PostPersist(ApplicationEngine engine)
        {
            // Distribute GAS for committee

            int m           = engine.ProtocolSettings.CommitteeMembersCount;
            int n           = engine.ProtocolSettings.ValidatorsCount;
            int index       = (int)(engine.PersistingBlock.Index % (uint)m);
            var gasPerBlock = GetGasPerBlock(engine.Snapshot);
            var committee   = GetCommitteeFromCache(engine.Snapshot);
            var pubkey      = committee[index].PublicKey;
            var account     = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash();
            await GAS.Mint(engine, account, gasPerBlock *CommitteeRewardRatio / 100, false);

            // Record the cumulative reward of the voters of committee

            if (ShouldRefreshCommittee(engine.PersistingBlock.Index, m))
            {
                BigInteger voterRewardOfEachCommittee = gasPerBlock * VoterRewardRatio * 100000000L * m / (m + n) / 100; // Zoom in 100000000 times, and the final calculation should be divided 100000000L
                for (index = 0; index < committee.Count; index++)
                {
                    var member = committee[index];
                    var factor = index < n ? 2 : 1; // The `voter` rewards of validator will double than other committee's
                    if (member.Votes > 0)
                    {
                        BigInteger voterSumRewardPerNEO = factor * voterRewardOfEachCommittee / member.Votes;
                        StorageKey voterRewardKey       = CreateStorageKey(Prefix_VoterRewardPerCommittee).Add(member.PublicKey).AddBigEndian(engine.PersistingBlock.Index + 1);
                        byte[]     border = CreateStorageKey(Prefix_VoterRewardPerCommittee).Add(member.PublicKey).ToArray();
                        (_, var item)         = engine.Snapshot.FindRange(voterRewardKey.ToArray(), border, SeekDirection.Backward).FirstOrDefault();
                        voterSumRewardPerNEO += (item ?? BigInteger.Zero);
                        engine.Snapshot.Add(voterRewardKey, new StorageItem(voterSumRewardPerNEO));
                    }
                }
            }
        }
Esempio n. 3
0
 protected override StorageItem TryGetInternal(StorageKey key)
 {
     byte[] value = store.TryGet(key.ToArray());
     if (value == null)
     {
         return(null);
     }
     return(new(value));
 }
Esempio n. 4
0
 protected override StorageItem GetInternal(StorageKey key)
 {
     byte[] value = store.TryGet(key.ToArray());
     if (value == null)
     {
         throw new KeyNotFoundException();
     }
     return(new(value));
 }
Esempio n. 5
0
        public void Size()
        {
            var ut = new StorageKey()
            {
                Key = new byte[17], ScriptHash = UInt160.Zero
            };

            ut.ToArray().Length.Should().Be(((ISerializable)ut).Size);

            ut = new StorageKey()
            {
                Key = new byte[0], ScriptHash = UInt160.Zero
            };
            ut.ToArray().Length.Should().Be(((ISerializable)ut).Size);

            ut = new StorageKey()
            {
                Key = new byte[16], ScriptHash = UInt160.Zero
            };
            ut.ToArray().Length.Should().Be(((ISerializable)ut).Size);
        }
Esempio n. 6
0
        public void Size()
        {
            var ut = new StorageKey()
            {
                Key = new byte[17], Id = 0
            };

            ut.ToArray().Length.Should().Be(((ISerializable)ut).Size);

            ut = new StorageKey()
            {
                Key = new byte[0], Id = 0
            };
            ut.ToArray().Length.Should().Be(((ISerializable)ut).Size);

            ut = new StorageKey()
            {
                Key = new byte[16], Id = 0
            };
            ut.ToArray().Length.Should().Be(((ISerializable)ut).Size);
        }
Esempio n. 7
0
        private string GetProof(UInt256 root_hash, UInt160 script_hash, byte[] key)
        {
            if (!Settings.Default.FullState && StateStore.Singleton.CurrentLocalRootHash != root_hash)
            {
                throw new RpcException(-100, "Old state not supported");
            }
            var snapshot = System.StoreView;
            var contract = NativeContract.ContractManagement.GetContract(snapshot, script_hash);

            if (contract is null)
            {
                throw new RpcException(-100, "Unknown contract");
            }
            StorageKey skey = new StorageKey
            {
                Id  = contract.Id,
                Key = key,
            };
            HashSet <byte[]> proof = StateStore.Singleton.GetProof(root_hash, skey);

            if (proof is null)
            {
                throw new RpcException(-100, "Unknown value");
            }

            using MemoryStream ms     = new MemoryStream();
            using BinaryWriter writer = new BinaryWriter(ms, Utility.StrictUTF8);

            writer.WriteVarBytes(skey.ToArray());
            writer.WriteVarInt(proof.Count);
            foreach (var item in proof)
            {
                writer.WriteVarBytes(item);
            }
            writer.Flush();

            return(Convert.ToBase64String(ms.ToArray()));
        }
Esempio n. 8
0
 protected override void UpdateInternal(StorageKey key, StorageItem value)
 {
     snapshot?.Put(key.ToArray(), value.ToArray());
 }
Esempio n. 9
0
 protected override StorageItem TryGetInternal(StorageKey key)
 {
     return(store.TryGet(key.ToArray())?.AsSerializable <StorageItem>());
 }
Esempio n. 10
0
 protected override bool ContainsInternal(StorageKey key)
 {
     return(store.Contains(key.ToArray()));
 }
Esempio n. 11
0
 protected override void DeleteInternal(StorageKey key)
 {
     snapshot?.Delete(key.ToArray());
 }