Reject out of date blocks submitted via RPC (#1914)

* Disallow by default RPC submission of out of date blocks (blocks which are out of virtual DAA window)

* go fmt

* Better condition test

* Make allowNonDAABlocks an RPC field and not a command line flag

* go fmt

* one more go fmt
This commit is contained in:
Michael Sutton 2021-12-29 20:07:45 +02:00 committed by GitHub
parent 8282fb486e
commit 064b0454e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 513 additions and 482 deletions

View File

@ -4,7 +4,8 @@ package appmessage
// its respective RPC message
type SubmitBlockRequestMessage struct {
baseMessage
Block *RPCBlock
Block *RPCBlock
AllowNonDAABlocks bool
}
// Command returns the protocol command string for the message

View File

@ -34,6 +34,23 @@ func HandleSubmitBlock(context *rpccontext.Context, _ *router.Router, request ap
}, nil
}
if !submitBlockRequest.AllowNonDAABlocks {
virtualDAAScore, err := context.Domain.Consensus().GetVirtualDAAScore()
if err != nil {
return nil, err
}
// A simple heuristic check which signals that the mined block is out of date
// and should not be accepted unless user explicitly requests
daaWindowSize := uint64(context.Config.NetParams().DifficultyAdjustmentWindowSize)
if virtualDAAScore > daaWindowSize && domainBlock.Header.DAAScore() < virtualDAAScore-daaWindowSize {
return &appmessage.SubmitBlockResponseMessage{
Error: appmessage.RPCErrorf("Block rejected. Reason: block DAA score %d is too far "+
"behind virtual's DAA score %d", domainBlock.Header.DAAScore(), virtualDAAScore),
RejectReason: appmessage.RejectReasonBlockInvalid,
}, nil
}
}
err = context.ProtocolManager.AddBlock(domainBlock)
if err != nil {
isProtocolOrRuleError := errors.As(err, &ruleerrors.RuleError{}) || errors.As(err, &protocolerrors.ProtocolError{})

View File

@ -129,6 +129,7 @@ message GetCurrentNetworkResponseMessage{
// See: GetBlockTemplateRequestMessage
message SubmitBlockRequestMessage{
RpcBlock block = 2;
bool allowNonDAABlocks = 3;
}
message SubmitBlockResponseMessage{

View File

@ -27,7 +27,8 @@ func (x *SubmitBlockRequestMessage) toAppMessage() (appmessage.Message, error) {
return nil, err
}
return &appmessage.SubmitBlockRequestMessage{
Block: blockAppMessage,
Block: blockAppMessage,
AllowNonDAABlocks: x.GetAllowNonDAABlocks(),
}, nil
}