Skip to content

Commit

Permalink
Add incentive for committee (#1847)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommo-L authored Aug 22, 2020
1 parent 35eea42 commit 765c43a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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 = Blockchain.StandbyCommittee;
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

0 comments on commit 765c43a

Please sign in to comment.