|
| 1 | +package gnosisaccessnode |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math" |
| 6 | + |
| 7 | + pubsub "github.com/libp2p/go-libp2p-pubsub" |
| 8 | + "github.com/pkg/errors" |
| 9 | + |
| 10 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // TODO: pull these from keyper set manager |
| 15 | + n = 7 |
| 16 | + threshold = 5 |
| 17 | +) |
| 18 | + |
| 19 | +type DecryptionKeysHandler struct { |
| 20 | + config *Config |
| 21 | +} |
| 22 | + |
| 23 | +func NewDecryptionKeysHandler(config *Config) *DecryptionKeysHandler { |
| 24 | + return &DecryptionKeysHandler{ |
| 25 | + config: config, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (*DecryptionKeysHandler) MessagePrototypes() []p2pmsg.Message { |
| 30 | + return []p2pmsg.Message{&p2pmsg.DecryptionKeys{}} |
| 31 | +} |
| 32 | + |
| 33 | +func (handler *DecryptionKeysHandler) ValidateMessage(_ context.Context, msg p2pmsg.Message) (pubsub.ValidationResult, error) { |
| 34 | + key := msg.(*p2pmsg.DecryptionKeys) |
| 35 | + result, err := handler.validateCommonFields(key) |
| 36 | + if result != pubsub.ValidationAccept || err != nil { |
| 37 | + return result, err |
| 38 | + } |
| 39 | + result, err = handler.validateGnosisFields(key) |
| 40 | + if result != pubsub.ValidationAccept || err != nil { |
| 41 | + return result, err |
| 42 | + } |
| 43 | + return pubsub.ValidationAccept, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (handler *DecryptionKeysHandler) validateCommonFields(key *p2pmsg.DecryptionKeys) (pubsub.ValidationResult, error) { |
| 47 | + if key.InstanceID != handler.config.InstanceID { |
| 48 | + return pubsub.ValidationReject, |
| 49 | + errors.Errorf("instance ID mismatch (want=%d, have=%d)", handler.config.InstanceID, key.GetInstanceID()) |
| 50 | + } |
| 51 | + if key.Eon > math.MaxInt64 { |
| 52 | + return pubsub.ValidationReject, errors.Errorf("eon %d overflows int64", key.Eon) |
| 53 | + } |
| 54 | + |
| 55 | + if len(key.Keys) == 0 { |
| 56 | + return pubsub.ValidationReject, errors.New("no keys in message") |
| 57 | + } |
| 58 | + if len(key.Keys) > int(handler.config.MaxNumKeysPerMessage) { |
| 59 | + return pubsub.ValidationReject, errors.Errorf( |
| 60 | + "too many keys in message (%d > %d)", |
| 61 | + len(key.Keys), |
| 62 | + handler.config.MaxNumKeysPerMessage, |
| 63 | + ) |
| 64 | + } |
| 65 | + return pubsub.ValidationAccept, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (handler *DecryptionKeysHandler) validateGnosisFields(keys *p2pmsg.DecryptionKeys) (pubsub.ValidationResult, error) { |
| 69 | + extraWrapped, ok := keys.Extra.(*p2pmsg.DecryptionKeys_Gnosis) |
| 70 | + if !ok { |
| 71 | + return pubsub.ValidationReject, errors.Errorf("unexpected extra type %T, expected Gnosis", keys.Extra) |
| 72 | + } |
| 73 | + extra := extraWrapped.Gnosis |
| 74 | + if extra == nil { |
| 75 | + return pubsub.ValidationReject, errors.New("missing extra Gnosis data") |
| 76 | + } |
| 77 | + |
| 78 | + return pubsub.ValidationAccept, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (handler *DecryptionKeysHandler) HandleMessage(_ context.Context, _ p2pmsg.Message) ([]p2pmsg.Message, error) { |
| 82 | + return nil, nil |
| 83 | +} |
0 commit comments