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

[StateService] Add sender check #575

Merged
merged 3 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/StateService/Verification/VerificationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class VerificationContext
public int MyIndex => myIndex;
public uint RootIndex => rootIndex;
public ECPoint[] Verifiers => verifiers;
public int Sender
{
get
{
int p = ((int)rootIndex - Retries) % verifiers.Length;
return p >= 0 ? p : p + verifiers.Length;
}
}
public bool IsSender => myIndex == Sender;
public ICancelable Timer;
public StateRoot StateRoot
{
Expand Down Expand Up @@ -111,17 +120,21 @@ public bool CheckSignatures()
{
if (StateRoot is null) return false;
if (signatures.Count < M) return false;
if (StateRoot.Witness != null) return true;
Contract contract = Contract.CreateMultiSigContract(M, verifiers);
ContractParametersContext sc = new ContractParametersContext(StatePlugin.System.StoreView, StateRoot);
for (int i = 0, j = 0; i < verifiers.Length && j < M; i++)
if (StateRoot.Witness is null)
{
if (!signatures.TryGetValue(i, out byte[] sig)) continue;
sc.AddSignature(contract, verifiers[i], sig);
j++;
Contract contract = Contract.CreateMultiSigContract(M, verifiers);
ContractParametersContext sc = new(StatePlugin.System.StoreView, StateRoot);
for (int i = 0, j = 0; i < verifiers.Length && j < M; i++)
{
if (!signatures.TryGetValue(i, out byte[] sig)) continue;
sc.AddSignature(contract, verifiers[i], sig);
j++;
}
if (!sc.Completed) return false;
StateRoot.Witness = sc.GetWitnesses()[0];
}
StateRoot.Witness = sc.GetWitnesses()[0];
rootPayload = CreatePayload(MessageType.StateRoot, StateRoot, MaxValidUntilBlockIncrement);
if (IsSender)
rootPayload = CreatePayload(MessageType.StateRoot, StateRoot, MaxValidUntilBlockIncrement);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/StateService/Verification/VerificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void OnStateRootVote(Vote vote)

private void CheckVotes(VerificationContext context)
{
if (context.CheckSignatures())
if (context.IsSender && context.CheckSignatures())
{
if (context.StateRootMessage is null) return;
Utility.Log(nameof(VerificationService), LogLevel.Info, $"relay state root, height={context.StateRoot.Index}, root={context.StateRoot.RootHash}");
Expand Down