Implement payload validation manager

This commit is contained in:
ChronosX88 2020-12-03 01:18:47 +04:00
parent 1cc5657ed1
commit d6ab3503bc
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 64 additions and 20 deletions

View File

@ -1,13 +1,11 @@
package consensus package consensus
import ( import (
"bytes"
"fmt" "fmt"
"github.com/Secured-Finance/dione/consensus/validation"
rtypes "github.com/Secured-Finance/dione/rpc/types" 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" oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
"github.com/Secured-Finance/dione/node" "github.com/Secured-Finance/dione/node"
@ -167,23 +165,11 @@ func (ppp *PrePreparePool) IsValidPrePrepare(prePrepare *types2.Message) bool {
} }
////////////////////////////////////// //////////////////////////////////////
// === verify filecoin message signature === // === validate payload by specific-chain checks ===
if consensusMsg.Task.OriginChain == rtypes.RPCTypeFilecoin && consensusMsg.Task.RequestType == "getTransaction" { if validationFunc := validation.GetValidationMethod(rtypes.RPCTypeFilecoin, consensusMsg.Task.RequestType); validationFunc != nil {
var msg ftypes.SignedMessage err := validationFunc(consensusMsg.Task.Payload)
if err := msg.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil { if err != nil {
if err := msg.Message.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil { logrus.Errorf("payload validation has failed: %v", err)
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
} }
} }
///////////////////////////////// /////////////////////////////////

View 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,
})
}

View 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
}