Store registrar for ibc permissions in db
This commit is contained in:
parent
485ec80cca
commit
7d3c0cd3e7
|
@ -29,25 +29,23 @@ func AllowIBC(app string) basecoin.Actor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler allows us to update the chain state or create a packet
|
// Handler allows us to update the chain state or create a packet
|
||||||
type Handler struct {
|
type Handler struct{}
|
||||||
Registrar basecoin.Actor
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ basecoin.Handler = &Handler{}
|
var _ basecoin.Handler = Handler{}
|
||||||
|
|
||||||
// NewHandler makes a Handler that allows all chains to connect via IBC.
|
// NewHandler makes a Handler that allows all chains to connect via IBC.
|
||||||
// Set a Registrar via SetOption to restrict it.
|
// Set a Registrar via SetOption to restrict it.
|
||||||
func NewHandler() *Handler {
|
func NewHandler() Handler {
|
||||||
return new(Handler)
|
return Handler{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name - return name space
|
// Name - return name space
|
||||||
func (*Handler) Name() string {
|
func (Handler) Name() string {
|
||||||
return NameIBC
|
return NameIBC
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetOption - sets the registrar for IBC
|
// SetOption - sets the registrar for IBC
|
||||||
func (h *Handler) SetOption(l log.Logger, store state.KVStore, module, key, value string) (log string, err error) {
|
func (h Handler) SetOption(l log.Logger, store state.KVStore, module, key, value string) (log string, err error) {
|
||||||
if module != NameIBC {
|
if module != NameIBC {
|
||||||
return "", errors.ErrUnknownModule(module)
|
return "", errors.ErrUnknownModule(module)
|
||||||
}
|
}
|
||||||
|
@ -57,8 +55,9 @@ func (h *Handler) SetOption(l log.Logger, store state.KVStore, module, key, valu
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
h.Registrar = act
|
// Save the data
|
||||||
// TODO: save/load from disk!
|
info := HandlerInfo{act}
|
||||||
|
info.Save(store)
|
||||||
return "Success", nil
|
return "Success", nil
|
||||||
}
|
}
|
||||||
return "", errors.ErrUnknownKey(key)
|
return "", errors.ErrUnknownKey(key)
|
||||||
|
@ -66,7 +65,7 @@ func (h *Handler) SetOption(l log.Logger, store state.KVStore, module, key, valu
|
||||||
|
|
||||||
// CheckTx verifies the packet is formated correctly, and has the proper sequence
|
// CheckTx verifies the packet is formated correctly, and has the proper sequence
|
||||||
// for a registered chain
|
// for a registered chain
|
||||||
func (h *Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
|
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
|
||||||
err = tx.ValidateBasic()
|
err = tx.ValidateBasic()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
|
@ -85,7 +84,7 @@ func (h *Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin
|
||||||
|
|
||||||
// DeliverTx verifies all signatures on the tx and updated the chain state
|
// DeliverTx verifies all signatures on the tx and updated the chain state
|
||||||
// apropriately
|
// apropriately
|
||||||
func (h *Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
|
func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
|
||||||
err = tx.ValidateBasic()
|
err = tx.ValidateBasic()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
|
@ -106,12 +105,13 @@ func (h *Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx baseco
|
||||||
// accepts it as the root of trust.
|
// accepts it as the root of trust.
|
||||||
//
|
//
|
||||||
// only the registrar, if set, is allowed to do this
|
// only the registrar, if set, is allowed to do this
|
||||||
func (h *Handler) initSeed(ctx basecoin.Context, store state.KVStore,
|
func (h Handler) initSeed(ctx basecoin.Context, store state.KVStore,
|
||||||
t RegisterChainTx) (res basecoin.Result, err error) {
|
t RegisterChainTx) (res basecoin.Result, err error) {
|
||||||
|
|
||||||
// check permission to attach
|
// check permission to attach
|
||||||
// nothing set, means anyone can connect
|
// nothing set, means anyone can connect
|
||||||
if !h.Registrar.Empty() && !ctx.HasPermission(h.Registrar) {
|
info := LoadInfo(store)
|
||||||
|
if !info.Registrar.Empty() && !ctx.HasPermission(info.Registrar) {
|
||||||
return res, errors.ErrUnauthorized()
|
return res, errors.ErrUnauthorized()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ func (h *Handler) initSeed(ctx basecoin.Context, store state.KVStore,
|
||||||
|
|
||||||
// updateSeed checks the seed against the existing chain data and rejects it if it
|
// updateSeed checks the seed against the existing chain data and rejects it if it
|
||||||
// doesn't fit (or no chain data)
|
// doesn't fit (or no chain data)
|
||||||
func (h *Handler) updateSeed(ctx basecoin.Context, store state.KVStore,
|
func (h Handler) updateSeed(ctx basecoin.Context, store state.KVStore,
|
||||||
t UpdateChainTx) (res basecoin.Result, err error) {
|
t UpdateChainTx) (res basecoin.Result, err error) {
|
||||||
|
|
||||||
chainID := t.ChainID()
|
chainID := t.ChainID()
|
||||||
|
@ -153,7 +153,7 @@ func (h *Handler) updateSeed(ctx basecoin.Context, store state.KVStore,
|
||||||
|
|
||||||
// createPacket makes sure all permissions are good and the destination
|
// createPacket makes sure all permissions are good and the destination
|
||||||
// chain is registed. If so, it appends it to the outgoing queue
|
// chain is registed. If so, it appends it to the outgoing queue
|
||||||
func (h *Handler) createPacket(ctx basecoin.Context, store state.KVStore,
|
func (h Handler) createPacket(ctx basecoin.Context, store state.KVStore,
|
||||||
t CreatePacketTx) (res basecoin.Result, err error) {
|
t CreatePacketTx) (res basecoin.Result, err error) {
|
||||||
|
|
||||||
// make sure the chain is registed
|
// make sure the chain is registed
|
||||||
|
|
|
@ -14,6 +14,30 @@ const (
|
||||||
prefixChains = "**"
|
prefixChains = "**"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
handlerKey = []byte{0x2}
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandlerInfo is the global state of the ibc.Handler
|
||||||
|
type HandlerInfo struct {
|
||||||
|
Registrar basecoin.Actor `json:"registrar"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the HandlerInfo to the store
|
||||||
|
func (h HandlerInfo) Save(store state.KVStore) {
|
||||||
|
b := wire.BinaryBytes(h)
|
||||||
|
store.Set(handlerKey, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadInfo loads the HandlerInfo from the data store
|
||||||
|
func LoadInfo(store state.KVStore) (h HandlerInfo) {
|
||||||
|
b := store.Get(handlerKey)
|
||||||
|
if len(b) > 0 {
|
||||||
|
wire.ReadBinaryBytes(b, &h)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// ChainInfo is the global info we store for each registered chain,
|
// ChainInfo is the global info we store for each registered chain,
|
||||||
// besides the headers, proofs, and packets
|
// besides the headers, proofs, and packets
|
||||||
type ChainInfo struct {
|
type ChainInfo struct {
|
||||||
|
|
Loading…
Reference in New Issue