Commit 0cdf9a37 authored by suyanlong's avatar suyanlong

😘 Add rule Validation Engine function

parent f7fdb365
Pipeline #8156 failed with stages
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"github.com/link33/sidecar/cmd/sidecar/client" "github.com/link33/sidecar/cmd/sidecar/client"
"github.com/link33/sidecar/internal/peermgr" "github.com/link33/sidecar/internal/peermgr"
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/internal/rulemgr"
"github.com/link33/sidecar/model/pb" "github.com/link33/sidecar/model/pb"
) )
...@@ -25,13 +26,14 @@ type Server struct { ...@@ -25,13 +26,14 @@ type Server struct {
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
peerMgr peermgr.PeerManager peerMgr peermgr.PeerManager
rule *rulemgr.RuleMgr
} }
type response struct { type response struct {
Data []byte `json:"data"` Data []byte `json:"data"`
} }
func NewServer(config *repo.Config, peerMgr peermgr.PeerManager, logger logrus.FieldLogger) *Server { func NewServer(config *repo.Config, peerMgr peermgr.PeerManager, rule *rulemgr.RuleMgr, logger logrus.FieldLogger) *Server {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
router := gin.New() router := gin.New()
...@@ -42,6 +44,7 @@ func NewServer(config *repo.Config, peerMgr peermgr.PeerManager, logger logrus.F ...@@ -42,6 +44,7 @@ func NewServer(config *repo.Config, peerMgr peermgr.PeerManager, logger logrus.F
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
peerMgr: peerMgr, peerMgr: peerMgr,
rule: rule,
} }
} }
...@@ -63,7 +66,7 @@ func (g *Server) Start() error { ...@@ -63,7 +66,7 @@ func (g *Server) Start() error {
}() }()
<-g.ctx.Done() <-g.ctx.Done()
}() }()
return nil return g.peerMgr.RegisterMsgHandler(pb.Message_RULE_DEPLOY, g.rule.HandleRule)
} }
func (g *Server) Stop() error { func (g *Server) Stop() error {
......
...@@ -6,6 +6,7 @@ require ( ...@@ -6,6 +6,7 @@ require (
github.com/Rican7/retry v0.1.0 github.com/Rican7/retry v0.1.0
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/bits-and-blooms/bitset v1.2.1 github.com/bits-and-blooms/bitset v1.2.1
github.com/bits-and-blooms/bloom/v3 v3.1.0
github.com/btcsuite/btcd v0.21.0-beta github.com/btcsuite/btcd v0.21.0-beta
github.com/bxcodec/faker/v3 v3.6.0 github.com/bxcodec/faker/v3 v3.6.0
github.com/cbergoon/merkletree v0.2.0 github.com/cbergoon/merkletree v0.2.0
...@@ -37,6 +38,7 @@ require ( ...@@ -37,6 +38,7 @@ require (
github.com/juju/errors v0.0.0-20210818161939-5560c4c073ff github.com/juju/errors v0.0.0-20210818161939-5560c4c073ff
github.com/lestrrat-go/file-rotatelogs v2.2.0+incompatible github.com/lestrrat-go/file-rotatelogs v2.2.0+incompatible
github.com/libp2p/go-libp2p-core v0.6.1 github.com/libp2p/go-libp2p-core v0.6.1
github.com/magiconair/properties v1.8.1
github.com/meshplus/bitxhub-core v1.3.1-0.20210524071255-789fd9ab501c github.com/meshplus/bitxhub-core v1.3.1-0.20210524071255-789fd9ab501c
github.com/meshplus/bitxhub-kit v1.2.1-0.20210524063043-9afae78ac098 github.com/meshplus/bitxhub-kit v1.2.1-0.20210524063043-9afae78ac098
github.com/meshplus/bitxid v0.0.0-20210412025850-e0eaf0f9063a github.com/meshplus/bitxid v0.0.0-20210412025850-e0eaf0f9063a
...@@ -60,7 +62,7 @@ require ( ...@@ -60,7 +62,7 @@ require (
) )
require ( require (
github.com/bits-and-blooms/bloom/v3 v3.1.0 // indirect github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
...@@ -80,6 +82,9 @@ require ( ...@@ -80,6 +82,9 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/huin/goupnp v1.0.0 // indirect github.com/huin/goupnp v1.0.0 // indirect
github.com/hyperledger/fabric v2.0.1+incompatible // indirect
github.com/hyperledger/fabric-amcl v0.0.0-20200424173818-327c9e2cf77a // indirect
github.com/hyperledger/fabric-protos-go v0.0.0-20200330074707-cfe579e86986 // indirect
github.com/ipfs/go-datastore v0.4.4 // indirect github.com/ipfs/go-datastore v0.4.4 // indirect
github.com/ipfs/go-ipfs-util v0.0.1 // indirect github.com/ipfs/go-ipfs-util v0.0.1 // indirect
github.com/ipfs/go-ipns v0.0.2 // indirect github.com/ipfs/go-ipns v0.0.2 // indirect
...@@ -132,9 +137,10 @@ require ( ...@@ -132,9 +137,10 @@ require (
github.com/libp2p/go-ws-transport v0.3.1 // indirect github.com/libp2p/go-ws-transport v0.3.1 // indirect
github.com/libp2p/go-yamux v1.3.6 // indirect github.com/libp2p/go-yamux v1.3.6 // indirect
github.com/looplab/fsm v0.2.0 // indirect github.com/looplab/fsm v0.2.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/meshplus/bitxhub-model v1.2.1-0.20210524063354-5d48e2fee178 // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect github.com/minio/sha256-simd v0.1.1 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect github.com/mitchellh/go-testing-interface v1.0.0 // indirect
...@@ -164,11 +170,13 @@ require ( ...@@ -164,11 +170,13 @@ require (
github.com/spf13/jwalterweatherman v1.0.0 // indirect github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect
github.com/sykesm/zap-logfmt v0.0.3 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 // indirect
github.com/tidwall/match v1.0.3 // indirect github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.0.2 // indirect github.com/tidwall/pretty v1.0.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect github.com/ugorji/go/codec v1.1.7 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wasmerio/go-ext-wasm v0.3.1 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
go.opencensus.io v0.22.3 // indirect go.opencensus.io v0.22.3 // indirect
......
This diff is collapsed.
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"github.com/link33/sidecar/internal/peermgr" "github.com/link33/sidecar/internal/peermgr"
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/internal/router" "github.com/link33/sidecar/internal/router"
"github.com/link33/sidecar/internal/rulemgr"
"github.com/link33/sidecar/internal/txcrypto" "github.com/link33/sidecar/internal/txcrypto"
"github.com/link33/sidecar/pkg/plugins" "github.com/link33/sidecar/pkg/plugins"
"github.com/link33/sidecar/tool" "github.com/link33/sidecar/tool"
...@@ -65,7 +66,10 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) ...@@ -65,7 +66,10 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
r.Adds(clientPort) r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), pm, appchainMgr, loggers.Logger(loggers.Manger)) mg, err := manger.NewManager(addr.String(), pm, appchainMgr, loggers.Logger(loggers.Manger))
tool.Asset(err) tool.Asset(err)
apiServer := api.NewServer(config, pm, loggers.Logger(loggers.ApiServer))
rule, err := rulemgr.New(store, loggers.Logger(loggers.Rule))
tool.Asset(err)
apiServer := api.NewServer(config, pm, rule, loggers.Logger(loggers.ApiServer))
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
return &App{ return &App{
storage: store, storage: store,
......
package checker package checker
import "github.com/link33/sidecar/model/pb" import (
"fmt"
"sync"
"github.com/link33/sidecar/internal/rulemgr"
"github.com/link33/sidecar/model/pb"
)
type MockChecker struct{} type MockChecker struct{}
func (ck *MockChecker) Check(ibtp *pb.IBTP) error { func (ck *MockChecker) Check(ibtp *pb.IBTP) error {
return nil return nil
} }
type checker struct {
ruleMgr *rulemgr.RuleMgr
appchainCache sync.Map
}
type appchainRule struct {
codeAddress string
}
func NewChecker(ruleMgr *rulemgr.RuleMgr) Checker {
return &checker{
ruleMgr: ruleMgr,
}
}
func (c *checker) Check(ibtp *pb.IBTP) error {
// todo check err
// chainID := ibtp.From
var validatorAddr string
var trustRoot string
// todo
ok, err := c.ruleMgr.Validate(validatorAddr, ibtp.From, ibtp.Proof, ibtp.Payload, trustRoot)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("rule check failed")
}
return nil
}
...@@ -19,6 +19,7 @@ const ( ...@@ -19,6 +19,7 @@ const (
Swarm = "swarm" Swarm = "swarm"
Syncer = "syncer" Syncer = "syncer"
Manger = "manger" Manger = "manger"
Rule = "rule"
) )
var w *loggerWrapper var w *loggerWrapper
...@@ -49,9 +50,10 @@ func InitializeLogger(config *repo.Config) { ...@@ -49,9 +50,10 @@ func InitializeLogger(config *repo.Config) {
m[Syncer].Logger.SetLevel(log.ParseLevel(config.Log.Module.Syncer)) m[Syncer].Logger.SetLevel(log.ParseLevel(config.Log.Module.Syncer))
m[PeerMgr] = log.NewWithModule(PeerMgr) m[PeerMgr] = log.NewWithModule(PeerMgr)
m[PeerMgr].Logger.SetLevel(log.ParseLevel(config.Log.Module.PeerMgr)) m[PeerMgr].Logger.SetLevel(log.ParseLevel(config.Log.Module.PeerMgr))
m[Manger] = log.NewWithModule(Manger) m[Manger] = log.NewWithModule(Manger)
m[Manger].Logger.SetLevel(log.ParseLevel(config.Log.Module.Manger)) m[Manger].Logger.SetLevel(log.ParseLevel(config.Log.Module.Manger))
m[Rule] = log.NewWithModule(Rule)
m[Rule].Logger.SetLevel(log.ParseLevel(config.Log.Module.Rule))
w = &loggerWrapper{loggers: m} w = &loggerWrapper{loggers: m}
} }
......
...@@ -67,6 +67,7 @@ type LogModule struct { ...@@ -67,6 +67,7 @@ type LogModule struct {
Swarm string `toml:"swarm" json:"swarm" json:"swarm,omitempty"` Swarm string `toml:"swarm" json:"swarm" json:"swarm,omitempty"`
Syncer string `toml:"syncer" json:"syncer" json:"syncer,omitempty"` Syncer string `toml:"syncer" json:"syncer" json:"syncer,omitempty"`
Manger string `toml:"manger" json:"manger" json:"manger,omitempty"` Manger string `toml:"manger" json:"manger" json:"manger,omitempty"`
Rule string `toml:"rule" json:"rule" json:"rule,omitempty"`
} }
// Appchain are configs about appchain // Appchain are configs about appchain
......
package rulemgr
import (
"github.com/meshplus/bitxhub-kit/storage"
"github.com/meshplus/bitxhub-kit/types"
)
const rulePrefix = "validation-rule-"
type CodeLedger struct {
storage storage.Storage
}
func (l *CodeLedger) GetCode(address *types.Address) []byte {
key := rulePrefix + address.String()
code := l.storage.Get([]byte(key))
return code
}
func (l *CodeLedger) SetCode(address *types.Address, code []byte) error {
key := rulePrefix + address.String()
l.storage.Put([]byte(key), code)
return nil
}
package rulemgr
import (
"encoding/json"
"github.com/meshplus/bitxhub-kit/types"
"github.com/link33/sidecar/internal/port"
"github.com/link33/sidecar/model/pb"
)
func (rm *RuleMgr) HandleRule(p port.Port, msg *pb.Message) {
data := msg.Payload.Data
rule := &Rule{}
if err := json.Unmarshal(data, rule); err != nil {
rm.logger.Error(err)
return
}
ok := true
err := rm.Ledger.SetCode(types.NewAddressByStr(rule.Address), rule.Code)
if err != nil {
ok = false
rm.logger.Error(err)
}
res := RuleResponse{
Ok: ok,
}
ackData, err := json.Marshal(res)
if err != nil {
rm.logger.Error(err)
return
}
err = p.AsyncSend(pb.Msg(pb.Message_ACK, true, ackData))
if err != nil {
rm.logger.Error(err)
}
}
package rulemgr
import (
"sync"
"github.com/meshplus/bitxhub-core/validator"
"github.com/meshplus/bitxhub-kit/storage"
"github.com/sirupsen/logrus"
)
type Rule struct {
Code []byte `json:"code"`
Address string `json:"address"`
}
type RuleResponse struct {
Ok bool `json:"ok"`
Content string `json:"content"`
}
type RuleMgr struct {
Ledger *CodeLedger
Ve *validator.ValidationEngine
logger logrus.FieldLogger
}
func New(storage storage.Storage, logger logrus.FieldLogger) (*RuleMgr, error) {
ledger := &CodeLedger{
storage: storage,
}
instances := &sync.Map{}
ve := validator.NewValidationEngine(ledger, instances, logger)
rm := &RuleMgr{
Ledger: ledger,
Ve: ve,
logger: logger,
}
return rm, nil
}
func (rm *RuleMgr) Validate(address, from string, proof, payload []byte, validators string) (bool, error) {
ok, err := rm.Ve.Validate(address, from, proof, payload, validators)
return ok, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment