예제 #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()));
        }
예제 #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));
                    }
                }
            }
        }
예제 #3
0
 protected override StorageItem TryGetInternal(StorageKey key)
 {
     byte[] value = store.TryGet(key.ToArray());
     if (value == null)
     {
         return(null);
     }
     return(new(value));
 }
예제 #4
0
 protected override StorageItem GetInternal(StorageKey key)
 {
     byte[] value = store.TryGet(key.ToArray());
     if (value == null)
     {
         throw new KeyNotFoundException();
     }
     return(new(value));
 }
예제 #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);
        }
예제 #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);
        }
예제 #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()));
        }
예제 #8
0
파일: SnapshotCache.cs 프로젝트: zhmkof/neo
 protected override void UpdateInternal(StorageKey key, StorageItem value)
 {
     snapshot?.Put(key.ToArray(), value.ToArray());
 }
예제 #9
0
파일: SnapshotCache.cs 프로젝트: zhmkof/neo
 protected override StorageItem TryGetInternal(StorageKey key)
 {
     return(store.TryGet(key.ToArray())?.AsSerializable <StorageItem>());
 }
예제 #10
0
파일: SnapshotCache.cs 프로젝트: zhmkof/neo
 protected override bool ContainsInternal(StorageKey key)
 {
     return(store.Contains(key.ToArray()));
 }
예제 #11
0
파일: SnapshotCache.cs 프로젝트: zhmkof/neo
 protected override void DeleteInternal(StorageKey key)
 {
     snapshot?.Delete(key.ToArray());
 }