Skip to content

Commit

Permalink
GetBFTAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jan 11, 2021
1 parent 7fc58eb commit 76e9f34
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
13 changes: 4 additions & 9 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu
PrevHash = UInt256.Zero,
Timestamp = (new DateTime(2016, 7, 15, 15, 8, 21, DateTimeKind.Utc)).ToTimestampMS(),
Index = 0,
NextConsensus = GetConsensusAddress(StandbyValidators),
NextConsensus = Contract.GetBFTAddress(StandbyValidators),
Witness = new Witness
{
InvocationScript = Array.Empty<byte>(),
Expand Down Expand Up @@ -129,11 +129,6 @@ private bool ContainsTransaction(UInt256 hash)
return NativeContract.Ledger.ContainsTransaction(View, hash);
}

public static UInt160 GetConsensusAddress(ECPoint[] validators)
{
return Contract.CreateMultiSigRedeemScript(validators.Length - (validators.Length - 1) / 3, validators).ToScriptHash();
}

public SnapshotCache GetSnapshot()
{
return new SnapshotCache(Store.GetSnapshot());
Expand Down Expand Up @@ -403,18 +398,18 @@ private void UpdateCurrentSnapshot()
var builder = ImmutableHashSet.CreateBuilder<UInt160>();
builder.Add(NativeContract.NEO.GetCommitteeAddress(currentSnapshot));
var validators = NativeContract.NEO.GetNextBlockValidators(currentSnapshot);
builder.Add(GetConsensusAddress(validators));
builder.Add(Contract.GetBFTAddress(validators));
builder.UnionWith(validators.Select(u => Contract.CreateSignatureRedeemScript(u).ToScriptHash()));
var oracles = NativeContract.RoleManagement.GetDesignatedByRole(currentSnapshot, Role.Oracle, Height);
if (oracles.Length > 0)
{
builder.Add(GetConsensusAddress(oracles));
builder.Add(Contract.GetBFTAddress(oracles));
builder.UnionWith(oracles.Select(u => Contract.CreateSignatureRedeemScript(u).ToScriptHash()));
}
var stateValidators = NativeContract.RoleManagement.GetDesignatedByRole(currentSnapshot, Role.StateValidator, Height);
if (stateValidators.Length > 0)
{
builder.Add(GetConsensusAddress(stateValidators));
builder.Add(Contract.GetBFTAddress(stateValidators));
builder.UnionWith(stateValidators.Select(u => Contract.CreateSignatureRedeemScript(u).ToScriptHash()));
}
extensibleWitnessWhiteList = builder.ToImmutable();
Expand Down
4 changes: 2 additions & 2 deletions src/neo/Network/P2P/Payloads/OracleResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
Expand Down Expand Up @@ -69,7 +69,7 @@ public override bool Verify(DataCache snapshot, Transaction tx)
OracleRequest request = NativeContract.Oracle.GetRequest(snapshot, Id);
if (request is null) return false;
if (tx.NetworkFee + tx.SystemFee != request.GasForResponse) return false;
UInt160 oracleAccount = Blockchain.GetConsensusAddress(NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.Oracle, NativeContract.Ledger.CurrentIndex(snapshot) + 1));
UInt160 oracleAccount = Contract.GetBFTAddress(NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.Oracle, NativeContract.Ledger.CurrentIndex(snapshot) + 1));
return tx.Signers.Any(p => p.Account.Equals(oracleAccount));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/neo/SmartContract/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,10 @@ public static byte[] CreateSignatureRedeemScript(ECPoint publicKey)
return sb.ToArray();
}
}

public static UInt160 GetBFTAddress(ECPoint[] pubkeys)
{
return CreateMultiSigRedeemScript(pubkeys.Length - (pubkeys.Length - 1) / 3, pubkeys).ToScriptHash();
}
}
}
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal GasToken()

internal override void Initialize(ApplicationEngine engine)
{
UInt160 account = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators);
UInt160 account = Contract.GetBFTAddress(Blockchain.StandbyValidators);
Mint(engine, account, 30_000_000 * Factor, false);
}

Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal override void Initialize(ApplicationEngine engine)
// Initialize economic parameters

engine.Snapshot.Add(CreateStorageKey(Prefix_GasPerBlock).AddBigEndian(0u), new StorageItem(5 * GAS.Factor));
Mint(engine, Blockchain.GetConsensusAddress(Blockchain.StandbyValidators), TotalAmount, false);
Mint(engine, Contract.GetBFTAddress(Blockchain.StandbyValidators), TotalAmount, false);
}

internal override void OnPersist(ApplicationEngine engine)
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/Native/UT_GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Check_BalanceOfTransferAndBurn()
{
var snapshot = _snapshot.CreateSnapshot();
var persistingBlock = new Block() { Index = 1000 };
byte[] from = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
byte[] from = Contract.GetBFTAddress(Blockchain.StandbyValidators).ToArray();
byte[] to = new byte[20];
var keyCount = snapshot.GetChangeSet().Count();
var supply = NativeContract.GAS.TotalSupply(snapshot);
Expand Down
10 changes: 5 additions & 5 deletions tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Check_Vote()
var snapshot = _snapshot.CreateSnapshot();
var persistingBlock = new Block() { Index = 1000 };

byte[] from = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
byte[] from = Contract.GetBFTAddress(Blockchain.StandbyValidators).ToArray();

// No signature

Expand Down Expand Up @@ -101,7 +101,7 @@ public void Check_Vote_Sameaccounts()
var snapshot = _snapshot.CreateSnapshot();
var persistingBlock = new Block() { Index = 1000 };

byte[] from = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
byte[] from = Contract.GetBFTAddress(Blockchain.StandbyValidators).ToArray();
var accountState = snapshot.TryGet(CreateStorageKey(20, from)).GetInteroperable<NeoAccountState>();
accountState.Balance = 100;
snapshot.Add(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray()), new StorageItem(new CandidateState()));
Expand Down Expand Up @@ -189,7 +189,7 @@ public void Check_UnclaimedGas()
var snapshot = _snapshot.CreateSnapshot();
var persistingBlock = new Block() { Index = 1000 };

byte[] from = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
byte[] from = Contract.GetBFTAddress(Blockchain.StandbyValidators).ToArray();

var unclaim = Check_UnclaimedGas(snapshot, from, persistingBlock);
unclaim.Value.Should().Be(new BigInteger(0.5 * 1000 * 100000000L));
Expand Down Expand Up @@ -344,7 +344,7 @@ public void Check_Transfer()
var snapshot = _snapshot.CreateSnapshot();
var persistingBlock = new Block() { Index = 1000 };

byte[] from = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
byte[] from = Contract.GetBFTAddress(Blockchain.StandbyValidators).ToArray();
byte[] to = new byte[20];

var keyCount = snapshot.GetChangeSet().Count();
Expand Down Expand Up @@ -393,7 +393,7 @@ public void Check_Transfer()
public void Check_BalanceOf()
{
var snapshot = _snapshot.CreateSnapshot();
byte[] account = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
byte[] account = Contract.GetBFTAddress(Blockchain.StandbyValidators).ToArray();

NativeContract.NEO.BalanceOf(snapshot, account).Should().Be(100_000_000);

Expand Down

1 comment on commit 76e9f34

@vncoelho
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good, @erikzhang! Perfect.

Please sign in to comment.