Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add incentive for committee #1847

Merged
merged 14 commits into from
Aug 22, 2020
13 changes: 12 additions & 1 deletion src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,19 @@ internal override void Initialize(ApplicationEngine engine)
protected override void OnPersist(ApplicationEngine engine)
{
base.OnPersist(engine);

// Distribute GAS for committee

var gasPerBlock = GetGasPerBlock(engine.Snapshot);
var committee = GetCommitteeMembers(engine.Snapshot).ToArray();
var pubkey = committee[engine.Snapshot.PersistingBlock.Index % ProtocolSettings.Default.CommitteeMembersCount];
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 = GetValidators(engine.Snapshot).ToByteArray();
storage.Value = committee[..ProtocolSettings.Default.ValidatorsCount].ToByteArray();
}

[ContractMethod(0_05000000, CallFlags.AllowModifyStates)]
Expand Down
21 changes: 21 additions & 0 deletions tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,27 @@ public void Check_BalanceOf()
NativeContract.NEO.BalanceOf(snapshot, account).Should().Be(0);
}

[TestMethod]
public void Check_CommitteeBonus()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.PersistingBlock = new Block { Index = 0 };

using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitAppCall(NativeContract.NEO.Hash, "onPersist");
sb.Emit(OpCode.RET);
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.System, null, snapshot, (long)(20 * NativeContract.GAS.Factor));
engine.LoadScript(sb.ToArray());
engine.Execute();
engine.State.Should().Be(VM.VMState.HALT);

var committee = NativeContract.NEO.GetCommittee(snapshot);
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);
}
}

[TestMethod]
public void Check_Initialize()
{
Expand Down