Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bf00b58
Embed shardchain-explorer in cql command
ggicci Jan 30, 2019
5be00bb
Remove explorer dist files
ggicci Jan 30, 2019
f8daf05
Update dep
ggicci Jan 30, 2019
faf6c26
Automate upgrade script for statik.go
ggicci Jan 30, 2019
d3d77dd
Add missing vendor: statik
ggicci Jan 30, 2019
a90f921
Make observer into cql
Feb 19, 2019
5fd2b78
Merge remote-tracking branch 'origin/develop' into feature/mergeObClient
Feb 19, 2019
582edf6
Delete cmd/cql-observer/worker.go
Feb 19, 2019
8dba222
Move statik generated file to sqlchain/observer
Feb 20, 2019
c04859b
Move upgrade-shardchain-explorer.sh
Feb 20, 2019
4b63a1f
Make observer and explorer work in one http server
Feb 20, 2019
c9687ae
Make explorer run in background
Feb 20, 2019
68f83cb
Lock github.com/rakyll/statik version
Feb 20, 2019
acccea8
Updata github.com/xtaci/smux for fixing writeFrame may block forever …
Feb 20, 2019
78fd395
Merge remote-tracking branch 'origin/develop' into feature/mergeObClient
Feb 20, 2019
64cb50b
Fix tests
Feb 20, 2019
239c69c
Fix test
Feb 20, 2019
75296c8
Not ignore cmd dir in codecov
Feb 22, 2019
75be482
Merge remote-tracking branch 'origin/develop' into feature/mergeObClient
Feb 22, 2019
bbe4739
Remove unused fixture test database in cql-minerd
Feb 22, 2019
fe4939b
Ignore cmd and sqlchain/observer codecov for temporary
Feb 25, 2019
7d25210
Merge branch 'feature/mergeObClient' of github.com:CovenantSQL/Covena…
Feb 25, 2019
a55c3e8
Fix call os.Exit(-1) in non main func
Feb 25, 2019
e6ae82d
Add comments for functions
Feb 25, 2019
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
13 changes: 11 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@
[[constraint]]
name = "github.com/davecgh/go-spew"
version = "1.1.1"

[[constraint]]
name = "github.com/rakyll/statik"
version = "0.1.5"
3 changes: 2 additions & 1 deletion api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"net/http"
"time"

"github.com/pkg/errors"

"github.com/CovenantSQL/CovenantSQL/api/models"
"github.com/CovenantSQL/CovenantSQL/rpc/jsonrpc"
"github.com/pkg/errors"
)

var (
Expand Down
4 changes: 3 additions & 1 deletion client/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func TestDefaultInit(t *testing.T) {

// check and prepare ~/.cql
homePath := utils.HomeDirExpand("~/.cql")
So(utils.Exist(homePath), ShouldEqual, false)
if utils.Exist(homePath) {
return
}

// no config err
err = defaultInit()
Expand Down
146 changes: 0 additions & 146 deletions cmd/cql-minerd/dbms.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,9 @@
package main

import (
"bytes"
"io/ioutil"
"math/rand"
"os"
"time"

"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/pow/cpuminer"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/rpc"
"github.com/CovenantSQL/CovenantSQL/types"
"github.com/CovenantSQL/CovenantSQL/utils"
"github.com/CovenantSQL/CovenantSQL/worker"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -61,139 +49,5 @@ func startDBMS(server *rpc.Server, onCreateDB func()) (dbms *worker.DBMS, err er
return
}

// add test fixture database
if conf.GConf.Miner.IsTestMode {
// in test mode
var privKey *asymmetric.PrivateKey

if privKey, err = kms.GetLocalPrivateKey(); err != nil {
err = errors.Wrap(err, "get local private key failed")
return
}

// add database to miner
for _, testFixture := range conf.GConf.Miner.TestFixtures {
// build test db instance configuration
dbPeers := &proto.Peers{
PeersHeader: proto.PeersHeader{
Term: testFixture.Term,
Leader: testFixture.Leader,
Servers: testFixture.Servers,
},
}

if err = dbPeers.Sign(privKey); err != nil {
err = errors.Wrap(err, "sign peers failed")
return
}

// load genesis block
var block *types.Block
if block, err = loadGenesisBlock(testFixture); err != nil {
err = errors.Wrap(err, "load genesis block failed")
return
}

// add to dbms
instance := &types.ServiceInstance{
DatabaseID: testFixture.DatabaseID,
Peers: dbPeers,
GenesisBlock: block,
}
if err = dbms.Create(instance, false); err != nil {
err = errors.Wrap(err, "add new DBMS failed")
return
}
}
}

return
}

func loadGenesisBlock(fixture *conf.MinerDatabaseFixture) (block *types.Block, err error) {
if fixture.GenesisBlockFile == "" {
err = os.ErrNotExist
return
}

var blockBytes []byte
if blockBytes, err = ioutil.ReadFile(fixture.GenesisBlockFile); err == nil {
err = errors.Wrap(err, "read block failed")
return
}

if os.IsNotExist(err) && fixture.AutoGenerateGenesisBlock {
// generate
if block, err = createRandomBlock(rootHash, true); err != nil {
err = errors.Wrap(err, "create random block failed")
return
}

// encode block
var bytesBuffer *bytes.Buffer
if bytesBuffer, err = utils.EncodeMsgPack(block); err != nil {
err = errors.Wrap(err, "encode block failed")
return
}

blockBytes = bytesBuffer.Bytes()

// write to file
err = ioutil.WriteFile(fixture.GenesisBlockFile, blockBytes, 0644)
} else {
err = utils.DecodeMsgPack(blockBytes, &block)
}

return
}

// copied from sqlchain.xxx_test.
func createRandomBlock(parent hash.Hash, isGenesis bool) (b *types.Block, err error) {
// Generate key pair
priv, pub, err := asymmetric.GenSecp256k1KeyPair()

if err != nil {
return
}

h := hash.Hash{}
rand.Read(h[:])

b = &types.Block{
SignedHeader: types.SignedHeader{
Header: types.Header{
Version: 0x01000000,
Producer: proto.NodeID(h.String()),
GenesisHash: rootHash,
ParentHash: parent,
Timestamp: time.Now().UTC(),
},
},
}

if isGenesis {
// Compute nonce with public key
nonceCh := make(chan cpuminer.NonceInfo)
quitCh := make(chan struct{})
miner := cpuminer.NewCPUMiner(quitCh)
go miner.ComputeBlockNonce(cpuminer.MiningBlock{
Data: pub.Serialize(),
NonceChan: nonceCh,
Stop: nil,
}, cpuminer.Uint256{}, 4)
nonce := <-nonceCh
close(quitCh)
close(nonceCh)
// Add public key to KMS
id := cpuminer.HashBlock(pub.Serialize(), nonce.Nonce)
b.SignedHeader.Header.Producer = proto.NodeID(id.String())
err = kms.SetPublicKey(proto.NodeID(id.String()), nonce.Nonce, pub)

if err != nil {
return nil, err
}
}

err = b.PackAndSignBlock(priv)
return
}
5 changes: 0 additions & 5 deletions cmd/cql-minerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@ func main() {
log.WithError(err).Fatal("init node failed")
}

if conf.GConf.Miner.IsTestMode {
// miner test mode enabled
log.Debug("miner test mode enabled")
}

// stop channel for all daemon routines
stopCh := make(chan struct{})
defer close(stopCh)
Expand Down
83 changes: 18 additions & 65 deletions cmd/cql-observer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/sqlchain/observer"
"github.com/CovenantSQL/CovenantSQL/utils"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
Expand All @@ -40,21 +40,17 @@ var (
version = "unknown"

// config
configFile string
dbID string
listenAddr string
resetPosition string
showVersion bool
logLevel string
configFile string
listenAddr string
showVersion bool
logLevel string
)

func init() {
flag.StringVar(&configFile, "config", "~/.cql/config.yaml", "Config file path")
flag.StringVar(&dbID, "database", "", "Database to listen for observation")
flag.BoolVar(&showVersion, "version", false, "Show version information and exit")
flag.BoolVar(&asymmetric.BypassSignature, "bypass-signature", false,
"Disable signature sign and verify, for testing")
flag.StringVar(&resetPosition, "reset", "", "Reset subscribe position")
flag.StringVar(&listenAddr, "listen", "127.0.0.1:4663", "Listen address for http explorer api")
flag.StringVar(&logLevel, "log-level", "", "Service log level")
}
Expand All @@ -70,6 +66,14 @@ func main() {
os.Exit(0)
}

signalCh := make(chan os.Signal, 1)
signal.Notify(
signalCh,
syscall.SIGINT,
syscall.SIGTERM,
)
signal.Ignore(syscall.SIGHUP, syscall.SIGTTIN, syscall.SIGTTOU)

configFile = utils.HomeDirExpand(configFile)

flag.Visit(func(f *flag.Flag) {
Expand All @@ -81,70 +85,19 @@ func main() {
if err != nil {
log.WithField("config", configFile).WithError(err).Fatal("load config failed")
}

kms.InitBP()

// init node
if err = initNode(); err != nil {
log.WithError(err).Fatal("init node failed")
}

// start service
var service *Service
if service, err = startService(); err != nil {
log.WithError(err).Fatal("start observation failed")
}
service, httpServer, err := observer.StartObserver(listenAddr, version)

// start explorer api
httpServer, err := startAPI(service, listenAddr)
if err != nil {
log.WithError(err).Fatal("start explorer api failed")
}

// register node
if err = registerNode(); err != nil {
log.WithError(err).Fatal("register node failed")
}

// start subscription
var cfg *Config
if cfg, err = loadConfig(configFile); err != nil {
log.WithError(err).Fatal("failed to load config")
}
if cfg != nil {
for _, v := range cfg.Databases {
if err = service.subscribe(proto.DatabaseID(v.ID), v.Position); err != nil {
log.WithError(err).Fatal("init subscription failed")
}
}
}
// Process command arguments after config file so that you can reset subscribe on startup
// without changing the config.
if dbID != "" {
if err = service.subscribe(proto.DatabaseID(dbID), resetPosition); err != nil {
log.WithError(err).Fatal("init subscription failed")
}
log.WithError(err).Fatal("start observer failed")
}

signalCh := make(chan os.Signal, 1)
signal.Notify(
signalCh,
syscall.SIGINT,
syscall.SIGTERM,
)
signal.Ignore(syscall.SIGHUP, syscall.SIGTTIN, syscall.SIGTTOU)

<-signalCh

// stop explorer api
if err = stopAPI(httpServer); err != nil {
log.WithError(err).Fatal("stop explorer api failed")
}

// stop subscriptions
if err = stopService(service); err != nil {
log.WithError(err).Fatal("stop service failed")
}
_ = observer.StopObserver(service, httpServer)

log.Info("observer stopped")

return
}
Loading