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())); }
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)); } } } }
protected override StorageItem TryGetInternal(StorageKey key) { byte[] value = store.TryGet(key.ToArray()); if (value == null) { return(null); } return(new(value)); }
protected override StorageItem GetInternal(StorageKey key) { byte[] value = store.TryGet(key.ToArray()); if (value == null) { throw new KeyNotFoundException(); } return(new(value)); }
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); }
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); }
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())); }
protected override void UpdateInternal(StorageKey key, StorageItem value) { snapshot?.Put(key.ToArray(), value.ToArray()); }
protected override StorageItem TryGetInternal(StorageKey key) { return(store.TryGet(key.ToArray())?.AsSerializable <StorageItem>()); }
protected override bool ContainsInternal(StorageKey key) { return(store.Contains(key.ToArray())); }
protected override void DeleteInternal(StorageKey key) { snapshot?.Delete(key.ToArray()); }