Skip to content

Commit

Permalink
Fix incentive for committee (neo-project#1884)
Browse files Browse the repository at this point in the history
* fix incentive for committee

* fix

* Update Blockchain.cs

* Update NeoToken.cs

* fix ut

Co-authored-by: Tommo-L <luchuan@neo.org>
Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Erik Zhang <erik@neo.org>
  • Loading branch information
4 people authored and cloud8little committed Jan 24, 2021
1 parent a77cf72 commit 84583c5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static Blockchain()
}
using (ScriptBuilder sb = new ScriptBuilder())
{
foreach (NativeContract contract in new NativeContract[] { NativeContract.Oracle })
foreach (NativeContract contract in new NativeContract[] { NativeContract.NEO, NativeContract.Oracle })
{
sb.EmitAppCall(contract.Hash, "postPersist");
sb.Emit(OpCode.DROP);
Expand Down Expand Up @@ -477,9 +477,8 @@ private void Persist(Block block)
}
}
snapshot.BlockHashIndex.GetAndChange().Set(block);
if (block.Index > 0)
using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.System, null, snapshot))
{
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.System, null, snapshot);
engine.LoadScript(postPersistScript);
if (engine.Execute() != VMState.HALT) throw new InvalidOperationException();
ApplicationExecuted application_executed = new ApplicationExecuted(engine);
Expand Down
18 changes: 11 additions & 7 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,23 @@ protected override void OnPersist(ApplicationEngine engine)
{
base.OnPersist(engine);

// Set next validators

StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_NextValidators), () => new StorageItem());
storage.Value = GetValidators(engine.Snapshot).ToByteArray();
}

protected override void PostPersist(ApplicationEngine engine)
{
base.PostPersist(engine);

// Distribute GAS for committee

int index = (int)(engine.Snapshot.PersistingBlock.Index % (uint)ProtocolSettings.Default.CommitteeMembersCount);
var gasPerBlock = GetGasPerBlock(engine.Snapshot);
var committee = GetCommitteeMembers(engine.Snapshot).ToArray();
var pubkey = committee.OrderBy(p => p).ElementAt(index);
var pubkey = GetCommittee(engine.Snapshot)[index];
var account = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash();
GAS.Mint(engine, account, gasPerBlock * CommitteeRewardRatio / 100);

// Set next validators

StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_NextValidators), () => new StorageItem());
storage.Value = committee.Take(ProtocolSettings.Default.ValidatorsCount).OrderBy(p => p).ToArray().ToByteArray();
}

[ContractMethod(0_05000000, CallFlags.AllowModifyStates)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Check_BalanceOfTransferAndBurn()
var keyCount = snapshot.Storages.GetChangeSet().Count();

var supply = NativeContract.GAS.TotalSupply(snapshot);
supply.Should().Be(3000000000000000);
supply.Should().Be(3000000025000000); // 3000000000000000 + 25000000 (neo holder reward)

// Check unclaim

Expand All @@ -68,7 +68,7 @@ public void Check_BalanceOfTransferAndBurn()
unclaim.State.Should().BeTrue();

supply = NativeContract.GAS.TotalSupply(snapshot);
supply.Should().Be(3000050000000000);
supply.Should().Be(3000050025000000);

snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount + 3); // Gas

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,11 @@ public void Check_BalanceOf()
public void Check_CommitteeBonus()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.PersistingBlock = new Block { Index = 0 };
snapshot.PersistingBlock = new Block { Index = 1 };

using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitAppCall(NativeContract.NEO.Hash, "onPersist");
sb.EmitAppCall(NativeContract.NEO.Hash, "postPersist");
sb.Emit(OpCode.RET);
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.System, null, snapshot, (long)(20 * NativeContract.GAS.Factor));
engine.LoadScript(sb.ToArray());
Expand All @@ -406,7 +406,8 @@ public void Check_CommitteeBonus()

var committee = Blockchain.StandbyCommittee.OrderBy(p => p).ToArray();
NativeContract.GAS.BalanceOf(snapshot, Contract.CreateSignatureContract(committee[0]).ScriptHash.ToArray()).Should().Be(25000000);
NativeContract.GAS.BalanceOf(snapshot, Contract.CreateSignatureContract(committee[1]).ScriptHash.ToArray()).Should().Be(0);
NativeContract.GAS.BalanceOf(snapshot, Contract.CreateSignatureContract(committee[1]).ScriptHash.ToArray()).Should().Be(25000000);
NativeContract.GAS.BalanceOf(snapshot, Contract.CreateSignatureContract(committee[2]).ScriptHash.ToArray()).Should().Be(0);
}
}

Expand Down

0 comments on commit 84583c5

Please sign in to comment.