Implement payload validation manager
This commit is contained in:
parent
1cc5657ed1
commit
d6ab3503bc
@ -1,13 +1,11 @@
|
||||
package consensus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/Secured-Finance/dione/consensus/validation"
|
||||
rtypes "github.com/Secured-Finance/dione/rpc/types"
|
||||
|
||||
ftypes "github.com/Secured-Finance/dione/rpc/filecoin/types"
|
||||
|
||||
oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
|
||||
|
||||
"github.com/Secured-Finance/dione/node"
|
||||
@ -167,23 +165,11 @@ func (ppp *PrePreparePool) IsValidPrePrepare(prePrepare *types2.Message) bool {
|
||||
}
|
||||
//////////////////////////////////////
|
||||
|
||||
// === verify filecoin message signature ===
|
||||
if consensusMsg.Task.OriginChain == rtypes.RPCTypeFilecoin && consensusMsg.Task.RequestType == "getTransaction" {
|
||||
var msg ftypes.SignedMessage
|
||||
if err := msg.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil {
|
||||
if err := msg.Message.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if msg.Type == ftypes.MessageTypeSecp256k1 {
|
||||
if err = sigs.Verify(msg.Signature, msg.Message.From.Bytes(), msg.Message.Cid().Bytes()); err != nil {
|
||||
logrus.Errorf("Couldn't verify transaction %v", err)
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
// TODO: BLS Signature verification
|
||||
return true
|
||||
// === validate payload by specific-chain checks ===
|
||||
if validationFunc := validation.GetValidationMethod(rtypes.RPCTypeFilecoin, consensusMsg.Task.RequestType); validationFunc != nil {
|
||||
err := validationFunc(consensusMsg.Task.Payload)
|
||||
if err != nil {
|
||||
logrus.Errorf("payload validation has failed: %v", err)
|
||||
}
|
||||
}
|
||||
/////////////////////////////////
|
||||
|
39
consensus/validation/filecoin/filecoin.go
Normal file
39
consensus/validation/filecoin/filecoin.go
Normal file
@ -0,0 +1,39 @@
|
||||
package filecoin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/Secured-Finance/dione/consensus/validation"
|
||||
rtypes "github.com/Secured-Finance/dione/rpc/types"
|
||||
|
||||
ftypes "github.com/Secured-Finance/dione/rpc/filecoin/types"
|
||||
"github.com/Secured-Finance/dione/sigs"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
func ValidateGetTransaction(payload []byte) error {
|
||||
var msg ftypes.SignedMessage
|
||||
if err := msg.UnmarshalCBOR(bytes.NewReader(payload)); err != nil {
|
||||
if err := msg.Message.UnmarshalCBOR(bytes.NewReader(payload)); err != nil {
|
||||
return xerrors.Errorf("cannot unmarshal payload")
|
||||
}
|
||||
}
|
||||
|
||||
if msg.Type == ftypes.MessageTypeSecp256k1 {
|
||||
if err := sigs.Verify(msg.Signature, msg.Message.From.Bytes(), msg.Message.Cid().Bytes()); err != nil {
|
||||
logrus.Errorf("Couldn't verify transaction %v", err)
|
||||
return xerrors.Errorf("Couldn't verify transaction: %v")
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
// TODO: BLS Signature verification
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
validation.RegisterValidation(rtypes.RPCTypeFilecoin, map[string]func([]byte) error{
|
||||
"getTransaction": ValidateGetTransaction,
|
||||
})
|
||||
}
|
19
consensus/validation/payload_validation_manager.go
Normal file
19
consensus/validation/payload_validation_manager.go
Normal file
@ -0,0 +1,19 @@
|
||||
package validation
|
||||
|
||||
var validations = map[uint8]map[string]func([]byte) error{} // rpcType -> {rpcMethodName -> actual func var}
|
||||
|
||||
func RegisterValidation(typ uint8, methods map[string]func([]byte) error) {
|
||||
validations[typ] = methods
|
||||
}
|
||||
|
||||
func GetValidationMethod(typ uint8, methodName string) func([]byte) error {
|
||||
rpcMethods, ok := validations[typ]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
actualMethod, ok := rpcMethods[methodName]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return actualMethod
|
||||
}
|
Loading…
Reference in New Issue
Block a user