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

Granite on mir #515

Draft
wants to merge 6 commits into
base: granite
Choose a base branch
from
Draft
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
120 changes: 120 additions & 0 deletions pkg/granite/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package common

import (
"github.com/filecoin-project/mir/pkg/crypto"
"github.com/filecoin-project/mir/pkg/granite"
"github.com/filecoin-project/mir/pkg/granite/internal/parts/consensustask"
granitepbtypes "github.com/filecoin-project/mir/pkg/pb/granitepb/types"
trantorpbtypes "github.com/filecoin-project/mir/pkg/pb/trantorpb/types"
t "github.com/filecoin-project/mir/pkg/types"
"github.com/filecoin-project/mir/pkg/util/maputil"
"github.com/filecoin-project/mir/pkg/util/membutil"
"reflect"
"time"
)

// ModuleConfig sets the module ids. All replicas are expected to use identical module configurations.
type ModuleConfig struct {
Self t.ModuleID // id of this module
App t.ModuleID
Ava t.ModuleID
Crypto t.ModuleID
Hasher t.ModuleID
Net t.ModuleID
Ord t.ModuleID
PPrepValidator t.ModuleID
Timer t.ModuleID
}

// ModuleParams sets the values for the parameters of an instance of the protocol.
// All replicas are expected to use identical module parameters.
type ModuleParams struct {

// InstanceUID is a unique identifier for this instance used to prevent replay attacks.
InstanceUID []byte `json:"-"`

// Membership defines the set of nodes participating in a particular instance of Granite.
Membership *trantorpbtypes.Membership `json:"-"`

Crypto crypto.Crypto

ConvergeDelay time.Duration
}

type Step = granite.MsgType
type State struct {
Round granite.RoundNr
Step Step

ValidatedMsgs *granite.MsgStore
UnvalidatedMsgs *granite.MsgStore

//Decision messages do not need to be stored per round
DecisionMessages map[t.NodeID]map[string]*granitepbtypes.Decision

Proposal []byte
Value []byte

Finished bool
}

func (st *State) IsValid(msg *granitepbtypes.ConsensusMsg, params *ModuleParams) bool {
switch msg.MsgType {
case granite.CONVERGE:
//TODO verify ticket and return false if not verified

if msg.Round == 1 {
return true
} else if value, ok := consensustask.GetValueWithStrongQuorum(params.Membership, st.ValidatedMsgs.Msgs[granite.COMMIT][granite.RoundNr(uint64(msg.Round)-1)]); ok && value == nil {
return true
} else {
return maputil.FindAny(st.ValidatedMsgs.Msgs[granite.COMMIT][granite.RoundNr(uint64(msg.Round)-1)], func(_msg *granitepbtypes.ConsensusMsg) bool {
return reflect.DeepEqual(_msg.Data, msg.Data)
})
}

case granite.PROPOSE:
if msg.Round == 1 {
return true
} else {
return maputil.FindAny(st.ValidatedMsgs.Msgs[granite.CONVERGE][msg.Round], func(_msg *granitepbtypes.ConsensusMsg) bool {
return reflect.DeepEqual(_msg.Data, msg.Data)
})
}

case granite.PREPARE:
//TODO COMPUTE whether the value is a mode for some subset efficiently (subsets with key's weights from membership)
// What is the most efficient way?
return true

case granite.COMMIT:
if msg.Data != nil {
keysWithVal := maputil.FindKeysWithFunc(st.ValidatedMsgs.Msgs[granite.PREPARE][msg.Round], func(_msg *granitepbtypes.ConsensusMsg) bool {
return reflect.DeepEqual(_msg.Data, msg.Data)
})
return membutil.HaveStrongQuorum(params.Membership, keysWithVal)
} else {
return maputil.HasTwoDistinctValues(st.ValidatedMsgs.Msgs[granite.PREPARE][msg.Round], func(msg, _msg *granitepbtypes.ConsensusMsg) bool {
return reflect.DeepEqual(_msg.Data, msg.Data)
})
}
}

return false
}

// TODO this (and also other functions in message validation etc.) can be optimized
func (st *State) FindNewValid(params *ModuleParams) (*granitepbtypes.ConsensusMsg, t.NodeID, bool) {
for _, store := range st.UnvalidatedMsgs.Msgs {
for _, nodeMap := range store {
for source, value := range nodeMap {
if st.IsValid(value, params) {
return value, source, true
}
}
}
}

var source t.NodeID
return nil, source, false
}
83 changes: 83 additions & 0 deletions pkg/granite/granite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package granite

import (
"github.com/filecoin-project/mir/pkg/dsl"
"github.com/filecoin-project/mir/pkg/factorymodule"
"github.com/filecoin-project/mir/pkg/granite/common"
"github.com/filecoin-project/mir/pkg/granite/internal/parts/consensustask"
"github.com/filecoin-project/mir/pkg/granite/internal/parts/messagehandlertask"
"github.com/filecoin-project/mir/pkg/logging"
"github.com/filecoin-project/mir/pkg/modules"
factorypbtypes "github.com/filecoin-project/mir/pkg/pb/factorypb/types"
granitepbtypes "github.com/filecoin-project/mir/pkg/pb/granitepb/types"
t "github.com/filecoin-project/mir/pkg/types"
)

// ModuleConfig sets the module ids. All replicas are expected to use identical module configurations.
type ModuleConfig = common.ModuleConfig

// ModuleParams sets the values for the parameters of an instance of the protocol.
// All replicas are expected to use identical module parameters.
type ModuleParams = common.ModuleParams

// NewModule creates a new instance of the Granite consensus protocol
func NewModule(mc *ModuleConfig, params *ModuleParams, logger logging.Logger) (modules.PassiveModule, error) {
m := dsl.NewModule(mc.Self)

state := &common.State{
Round: 1,
Step: PROPOSE, // First round skip CONVERGE step
ValidatedMsgs: NewMsgStore(),
UnvalidatedMsgs: NewMsgStore(),
DecisionMessages: make(map[t.NodeID]map[string]*granitepbtypes.Decision),
}

//init data structure to store all messages
//TODO init maps

// Include the core logic of the protocol.
consensustask.IncludeConsensusTask(m, mc, params, state, logger)
messagehandlertask.IncludeMessageHandlerTask(m, mc, params, state, logger)

return m, nil
}

func NewReconfigurableModule(mc ModuleConfig, paramsTemplate ModuleParams, logger logging.Logger) modules.PassiveModule {
if logger == nil {
logger = logging.ConsoleErrorLogger
}
return factorymodule.New(
mc.Self,
factorymodule.DefaultParams(

// This function will be called whenever the factory module
// is asked to create a new instance of granite.
func(graniteID t.ModuleID, params *factorypbtypes.GeneratorParams) (modules.PassiveModule, error) {

// Extract the IDs of the nodes in the membership associated with this instance
graniteParams := params.Type.(*factorypbtypes.GeneratorParams_Granite).Granite

// Create a copy of basic module config with an adapted ID for the submodule.
submc := mc
submc.Self = graniteID

// Fill in instance-specific parameters.
moduleParams := paramsTemplate
moduleParams.InstanceUID = []byte(graniteID)
moduleParams.Membership = graniteParams.Membership

// Create a new instance of granite.
granite, err := NewModule(
&submc,
&moduleParams,
logger,
)
if err != nil {
return nil, err
}
return granite, nil
},
),
logger,
)
}
Loading
Loading