From 1cbcb82f07c1323fba0883d06c3f96b83ce73ec2 Mon Sep 17 00:00:00 2001 From: leventeliu Date: Sat, 29 Dec 2018 18:36:57 +0800 Subject: [PATCH 1/2] Prune unused codes --- blockproducer/errors.go | 21 -- blockproducer/xxx_test.go | 432 +---------------------------- client/clientbench_test.go | 1 - cmd/cql-adapter/config/errors.go | 2 - cmd/cql-minerd/integration_test.go | 28 +- cmd/cqld/main.go | 1 - crypto/asymmetric/signature.go | 2 - crypto/kms/privatekeystore.go | 2 - crypto/kms/pubkeystore.go | 2 - kayak/types/errors.go | 2 - kayak/wal/leveldb_wal.go | 11 - rpc/client_test.go | 2 - rpc/rpcutil_test.go | 1 - sqlchain/chain.go | 241 ---------------- sqlchain/errors.go | 64 +---- sqlchain/mux.go | 52 ---- sqlchain/rpc.go | 35 --- sqlchain/runtime.go | 26 +- sqlchain/xxx_test.go | 129 --------- twopc/twopc_test.go | 3 - types/errors.go | 10 - types/xxx_test.go | 85 ------ utils/big.go | 9 +- utils/errors.go | 8 - utils/integer.go | 21 +- worker/errors.go | 12 - xenomint/chain.go | 22 -- xenomint/errors.go | 6 - xenomint/xxx_test.go | 15 - 29 files changed, 24 insertions(+), 1221 deletions(-) delete mode 100644 utils/errors.go diff --git a/blockproducer/errors.go b/blockproducer/errors.go index 29409bcd6..fbea655a6 100644 --- a/blockproducer/errors.go +++ b/blockproducer/errors.go @@ -21,15 +21,6 @@ import "errors" var ( // ErrNoSuchDatabase defines database meta not exists error. ErrNoSuchDatabase = errors.New("no such database") - // ErrDatabaseAllocation defines database allocation failure error. - ErrDatabaseAllocation = errors.New("allocate database failed") - // ErrMetricNotCollected defines errors collected. - ErrMetricNotCollected = errors.New("metric not collected") - - // Errors on main chain - - // ErrCorruptedIndex defines index corrupted error. - ErrCorruptedIndex = errors.New("corrupted index item") // ErrParentNotFound defines that the parent block cannot be found. ErrParentNotFound = errors.New("previous block cannot be found") // ErrInvalidHash defines invalid hash error. @@ -42,12 +33,6 @@ var ( ErrParentNotMatch = errors.New("Block's parent hash cannot match best block") // ErrNoSuchBlock defines no such block error. ErrNoSuchBlock = errors.New("Cannot find such block") - // ErrNoSuchTxBilling defines no such txbilling error. - ErrNoSuchTxBilling = errors.New("Cannot find such txbilling") - // ErrSmallerSequenceID defines that new sequence id is smaller the old one. - ErrSmallerSequenceID = errors.New("SequanceID should be bigger than the old one") - // ErrInvalidBillingRequest defines BillingRequest is invalid - ErrInvalidBillingRequest = errors.New("The BillingRequest is invalid") // ErrBalanceOverflow indicates that there will be an overflow after balance manipulation. ErrBalanceOverflow = errors.New("balance overflow") @@ -63,17 +48,11 @@ var ( ErrDatabaseExists = errors.New("database already exists") // ErrDatabaseUserExists indicates that the database user already exists. ErrDatabaseUserExists = errors.New("database user already exists") - // ErrDatabaseCannotCreate indicates that database cannot be created. - ErrDatabaseCannotCreate = errors.New("database cannot be created") // ErrInvalidAccountNonce indicates that a transaction has a invalid account nonce. ErrInvalidAccountNonce = errors.New("invalid account nonce") // ErrUnknownTransactionType indicates that a transaction has a unknown type and cannot be // further processed. ErrUnknownTransactionType = errors.New("unknown transaction type") - // ErrTransactionMismatch indicates that transactions to be committed mismatch the pool. - ErrTransactionMismatch = errors.New("transaction mismatch") - // ErrMetaStateNotFound indicates that meta state not found in db. - ErrMetaStateNotFound = errors.New("meta state not found in db") // ErrInvalidSender indicates that tx.Signee != tx.Sender. ErrInvalidSender = errors.New("invalid sender") // ErrNoSuchMiner indicates that this miner does not exist or register. diff --git a/blockproducer/xxx_test.go b/blockproducer/xxx_test.go index 2b5d919e1..f254d9d90 100644 --- a/blockproducer/xxx_test.go +++ b/blockproducer/xxx_test.go @@ -21,454 +21,30 @@ import ( "math/rand" "os" "path" - "sync" "testing" "time" - pi "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces" "github.com/CovenantSQL/CovenantSQL/conf" - "github.com/CovenantSQL/CovenantSQL/crypto" ca "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/route" - "github.com/CovenantSQL/CovenantSQL/types" "github.com/CovenantSQL/CovenantSQL/utils/log" ) var ( - genesisHash = hash.Hash{} - uuidLen = 32 - peerNum uint32 = 32 - - testAddress1 = proto.AccountAddress{0x0, 0x0, 0x0, 0x1} - testAddress2 = proto.AccountAddress{0x0, 0x0, 0x0, 0x2} - testInitBalance = uint64(10000) - testDifficulty = 4 - - testAddress1Nonce pi.AccountNonce + genesisHash = hash.Hash{} testingDataDir string - testingTraceFile *os.File testingConfigFile = "../test/node_standalone/config.yaml" testingPrivateKeyFile = "../test/node_standalone/private.key" testingPublicKeyStoreFile string testingNonceDifficulty int - - testingPrivateKey *ca.PrivateKey - testingPublicKey *ca.PublicKey - - testingMasterKey = []byte(`?08Rl%WUih4V0H+c`) + testingPrivateKey *ca.PrivateKey + testingPublicKey *ca.PublicKey ) -const ( - letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" -) - -func generateRandomBytes(n int32) []byte { - s := make([]byte, n) - for i := range s { - s[i] = byte(rand.Int31n(2)) - } - return s -} - -func generateRandomDatabaseID() *proto.DatabaseID { - id := proto.DatabaseID(randStringBytes(uuidLen)) - return &id -} - -func generateRandomDatabaseIDs(n int32) []proto.DatabaseID { - s := make([]proto.DatabaseID, n) - for i := range s { - s[i] = proto.DatabaseID(randStringBytes(uuidLen)) - } - return s -} - -func randStringBytes(n int) string { - b := make([]byte, n) - for i := range b { - b[i] = letterBytes[rand.Intn(len(letterBytes))] - } - return string(b) -} - -func generateRandomBlock(parent hash.Hash, isGenesis bool) (b *types.BPBlock, err error) { - // Generate key pair - priv, _, err := ca.GenSecp256k1KeyPair() - - if err != nil { - return - } - - h := hash.Hash{} - rand.Read(h[:]) - - b = &types.BPBlock{ - SignedHeader: types.BPSignedHeader{ - BPHeader: types.BPHeader{ - Version: 0x01000000, - Producer: proto.AccountAddress(h), - ParentHash: parent, - Timestamp: time.Now().UTC(), - }, - }, - } - - if !isGenesis { - for i, n := 0, rand.Intn(10)+10; i < n; i++ { - ba, tb, err := generateRandomBillingAndBaseAccount() - if err != nil { - return nil, err - } - b.Transactions = append(b.Transactions, ba, tb) - } - } else { - // Create base accounts - var ( - ba1 = types.NewBaseAccount( - &types.Account{ - Address: testAddress1, - TokenBalance: [types.SupportTokenNumber]uint64{testInitBalance, testInitBalance}, - }, - ) - ba2 = types.NewBaseAccount( - &types.Account{ - Address: testAddress2, - TokenBalance: [types.SupportTokenNumber]uint64{testInitBalance, testInitBalance}, - }, - ) - ) - if err = ba1.Sign(testingPrivateKey); err != nil { - return - } - if err = ba2.Sign(testingPrivateKey); err != nil { - return - } - b.Transactions = append(b.Transactions, ba1, ba2) - } - - err = b.PackAndSignBlock(priv) - return -} - -func generateRandomBlockWithTransactions(parent hash.Hash, tbs []pi.Transaction) (b *types.BPBlock, err error) { - // Generate key pair - priv, _, err := ca.GenSecp256k1KeyPair() - - if err != nil { - return - } - - h := hash.Hash{} - rand.Read(h[:]) - - b = &types.BPBlock{ - SignedHeader: types.BPSignedHeader{ - BPHeader: types.BPHeader{ - Version: 0x01000000, - Producer: proto.AccountAddress(h), - ParentHash: parent, - Timestamp: time.Now().UTC(), - }, - }, - } - - for _, tb := range tbs { - b.Transactions = append(b.Transactions, tb) - } - - testAddress1Nonce++ - var tr = types.NewTransfer( - &types.TransferHeader{ - Sender: testAddress1, - Receiver: testAddress2, - Nonce: testAddress1Nonce, - Amount: 1, - }, - ) - if err = tr.Sign(priv); err != nil { - return - } - b.Transactions = append(b.Transactions, tr) - - err = b.PackAndSignBlock(priv) - - return -} - -func generateRandomBillingRequestHeader() *types.BillingRequestHeader { - return &types.BillingRequestHeader{ - DatabaseID: *generateRandomDatabaseID(), - LowBlock: generateRandomHash(), - LowHeight: rand.Int31(), - HighBlock: generateRandomHash(), - HighHeight: rand.Int31(), - GasAmounts: generateRandomGasAmount(peerNum), - } -} - -func generateRandomBillingRequest() (*types.BillingRequest, error) { - reqHeader := generateRandomBillingRequestHeader() - req := types.BillingRequest{ - Header: *reqHeader, - } - h, err := req.PackRequestHeader() - if err != nil { - return nil, err - } - - signees := make([]*ca.PublicKey, peerNum) - signatures := make([]*ca.Signature, peerNum) - - for i := range signees { - // Generate key pair - priv, pub, err := ca.GenSecp256k1KeyPair() - if err != nil { - return nil, err - } - signees[i] = pub - signatures[i], err = priv.Sign(h[:]) - if err != nil { - return nil, err - } - } - req.RequestHash = *h - req.Signatures = signatures - req.Signees = signees - - return &req, nil -} - -func generateRandomBillingHeader() (tc *types.BillingHeader, err error) { - var req *types.BillingRequest - if req, err = generateRandomBillingRequest(); err != nil { - return - } - - var priv *ca.PrivateKey - if priv, _, err = ca.GenSecp256k1KeyPair(); err != nil { - return - } - - if _, _, err = req.SignRequestHeader(priv, false); err != nil { - return - } - - receivers := make([]*proto.AccountAddress, peerNum) - fees := make([]uint64, peerNum) - rewards := make([]uint64, peerNum) - for i := range fees { - h := generateRandomHash() - accountAddress := proto.AccountAddress(h) - receivers[i] = &accountAddress - fees[i] = rand.Uint64() - rewards[i] = rand.Uint64() - } - producer := proto.AccountAddress(generateRandomHash()) - - tc = types.NewBillingHeader(pi.AccountNonce(rand.Uint32()), req, producer, receivers, fees, rewards) - return tc, nil -} - -func generateRandomBillingAndBaseAccount() (*types.BaseAccount, *types.Billing, error) { - header, err := generateRandomBillingHeader() - if err != nil { - return nil, nil, err - } - priv, _, err := ca.GenSecp256k1KeyPair() - header.Producer, _ = crypto.PubKeyHash(priv.PubKey()) - - txBilling := types.NewBilling(header) - - if err := txBilling.Sign(priv); err != nil { - return nil, nil, err - } - - txBaseAccount := types.NewBaseAccount( - &types.Account{ - Address: header.Producer, - TokenBalance: [types.SupportTokenNumber]uint64{testInitBalance, testInitBalance}, - }, - ) - - if err := txBaseAccount.Sign(priv); err != nil { - return nil, nil, err - } - - return txBaseAccount, txBilling, nil -} - -func generateRandomAccountBilling() (*types.Billing, error) { - header, err := generateRandomBillingHeader() - if err != nil { - return nil, err - } - header.Producer = testAddress1 - testAddress1Nonce++ - header.Nonce = testAddress1Nonce - txBilling := types.NewBilling(header) - - if err := txBilling.Sign(testingPrivateKey); err != nil { - return nil, err - } - - return txBilling, nil -} - -func generateRandomGasAmount(n uint32) []*proto.AddrAndGas { - gasAmount := make([]*proto.AddrAndGas, n) - - for i := range gasAmount { - gasAmount[i] = &proto.AddrAndGas{ - AccountAddress: proto.AccountAddress(generateRandomHash()), - RawNodeID: proto.RawNodeID{Hash: generateRandomHash()}, - GasAmount: rand.Uint64(), - } - } - - return gasAmount -} - -func generateRandomHash() hash.Hash { - h := hash.Hash{} - rand.Read(h[:]) - return h -} - -func registerNodesWithPublicKey(pub *ca.PublicKey, diff int, num int) ( - nis []cpuminer.NonceInfo, err error) { - nis = make([]cpuminer.NonceInfo, num) - - miner := cpuminer.NewCPUMiner(nil) - nCh := make(chan cpuminer.NonceInfo) - defer close(nCh) - block := cpuminer.MiningBlock{ - Data: pub.Serialize(), - NonceChan: nCh, - Stop: nil, - } - next := cpuminer.Uint256{} - wg := &sync.WaitGroup{} - - for i := range nis { - wg.Add(1) - go func() { - defer wg.Done() - miner.ComputeBlockNonce(block, next, diff) - }() - n := <-nCh - nis[i] = n - next = n.Nonce - next.Inc() - - if err = kms.SetPublicKey(proto.NodeID(n.Hash.String()), n.Nonce, pub); err != nil { - return - } - - wg.Wait() - } - - // Register a local nonce, don't know what is the matter though - kms.SetLocalNodeIDNonce(nis[0].Hash[:], &nis[0].Nonce) - return -} - -func createRandomString(offset, length int, s *string) { - buff := make([]byte, rand.Intn(length)+offset) - rand.Read(buff) - *s = string(buff) -} - -func createTestPeersWithPrivKeys(priv *ca.PrivateKey, num int) (nis []cpuminer.NonceInfo, p *proto.Peers, err error) { - if num <= 0 { - return - } - - pub := priv.PubKey() - - nis, err = registerNodesWithPublicKey(pub, testDifficulty, num) - - if err != nil { - return - } - - s := make([]proto.NodeID, num) - h := &hash.Hash{} - - for i := range s { - rand.Read(h[:]) - s[i] = proto.NodeID(nis[i].Hash.String()) - } - - p = &proto.Peers{ - PeersHeader: proto.PeersHeader{ - Term: 0, - Leader: s[0], - Servers: s, - }, - } - - if err = p.Sign(priv); err != nil { - return - } - - return -} - -func createTestPeers(num int) (nis []cpuminer.NonceInfo, p *proto.Peers, err error) { - if num <= 0 { - return - } - - // Use a same key pair for all the servers, so that we can run multiple instances of sql-chain - // locally without breaking the LocalKeyStore - pub, err := kms.GetLocalPublicKey() - - if err != nil { - return - } - - priv, err := kms.GetLocalPrivateKey() - - if err != nil { - return - } - - nis, err = registerNodesWithPublicKey(pub, testDifficulty, num) - - if err != nil { - return - } - - s := make([]proto.NodeID, num) - h := &hash.Hash{} - - for i := range s { - rand.Read(h[:]) - s[i] = proto.NodeID(nis[i].Hash.String()) - } - - p = &proto.Peers{ - PeersHeader: proto.PeersHeader{ - Term: 0, - Leader: s[0], - Servers: s, - }, - } - - if err = p.Sign(priv); err != nil { - return - } - - return -} - func setup() { var err error - rand.Seed(time.Now().UnixNano()) rand.Read(genesisHash[:]) @@ -495,7 +71,7 @@ func setup() { // Setup logging log.SetOutput(os.Stdout) - log.SetLevel(log.FatalLevel) + log.SetLevel(log.DebugLevel) } func teardown() { diff --git a/client/clientbench_test.go b/client/clientbench_test.go index e41208c15..7b5282eb9 100644 --- a/client/clientbench_test.go +++ b/client/clientbench_test.go @@ -30,7 +30,6 @@ import ( var ( baseDir = utils.GetProjectSrcDir() testWorkingDir = FJ(baseDir, "./test/GNTE/conf") - logDir = FJ(testWorkingDir, "./log/") once sync.Once ) diff --git a/cmd/cql-adapter/config/errors.go b/cmd/cql-adapter/config/errors.go index 1314f0d64..312887eda 100644 --- a/cmd/cql-adapter/config/errors.go +++ b/cmd/cql-adapter/config/errors.go @@ -21,8 +21,6 @@ import "github.com/pkg/errors" var ( // ErrEmptyAdapterConfig defines empty adapter config. ErrEmptyAdapterConfig = errors.New("empty adapter config") - // ErrRequireServerCertificate defines error of empty server certificate. - ErrRequireServerCertificate = errors.New("require server certificate") // ErrInvalidStorageConfig defines error on incomplete storage config. ErrInvalidStorageConfig = errors.New("invalid storage config") // ErrInvalidCertificateFile defines invalid certificate file error. diff --git a/cmd/cql-minerd/integration_test.go b/cmd/cql-minerd/integration_test.go index fdec6fe8c..48a8b92e1 100644 --- a/cmd/cql-minerd/integration_test.go +++ b/cmd/cql-minerd/integration_test.go @@ -35,7 +35,6 @@ import ( "time" bp "github.com/CovenantSQL/CovenantSQL/blockproducer" - "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces" "github.com/CovenantSQL/CovenantSQL/client" "github.com/CovenantSQL/CovenantSQL/conf" "github.com/CovenantSQL/CovenantSQL/crypto" @@ -52,12 +51,11 @@ import ( ) var ( - baseDir = utils.GetProjectSrcDir() - testWorkingDir = FJ(baseDir, "./test/") - logDir = FJ(testWorkingDir, "./log/") - testGasPrice uint64 = 1 - testInitTokenAmount uint64 = 1000000000 - testAdvancePayment uint64 = 20000000 + baseDir = utils.GetProjectSrcDir() + testWorkingDir = FJ(baseDir, "./test/") + logDir = FJ(testWorkingDir, "./log/") + testGasPrice uint64 = 1 + testAdvancePayment uint64 = 20000000 ) var nodeCmds []*utils.CMD @@ -898,19 +896,3 @@ func BenchmarkMinerGNTE8(b *testing.B) { benchGNTEMiner(b, 8, false) }) } - -func getNonce(addr proto.AccountAddress) (nonce interfaces.AccountNonce, err error) { - // allocate nonce - nonceReq := &types.NextAccountNonceReq{} - nonceResp := &types.NextAccountNonceResp{} - nonceReq.Addr = addr - - if err = rpc.RequestBP(route.MCCNextAccountNonce.String(), nonceReq, nonceResp); err != nil { - // allocate nonce failed - log.WithError(err).Warning("allocate nonce for transaction failed") - return - } - - nonce = nonceResp.Nonce - return -} diff --git a/cmd/cqld/main.go b/cmd/cqld/main.go index e0f3065f7..5164735d7 100644 --- a/cmd/cqld/main.go +++ b/cmd/cqld/main.go @@ -55,7 +55,6 @@ var ( noLogo bool showVersion bool configFile string - genKeyPair bool clientMode bool clientOperation string diff --git a/crypto/asymmetric/signature.go b/crypto/asymmetric/signature.go index ec7e59804..ee3a02b9a 100644 --- a/crypto/asymmetric/signature.go +++ b/crypto/asymmetric/signature.go @@ -20,7 +20,6 @@ import ( "crypto/elliptic" "errors" "math/big" - "sync" "github.com/CovenantSQL/CovenantSQL/crypto/secp256k1" "github.com/CovenantSQL/CovenantSQL/utils" @@ -32,7 +31,6 @@ var ( // BypassSignature is the flag indicate if bypassing signature sign & verify BypassSignature = false bypassS *Signature - mu sync.Mutex ) // For test Signature.Sign mock diff --git a/crypto/kms/privatekeystore.go b/crypto/kms/privatekeystore.go index 0e3aa01d8..43dd15d9b 100644 --- a/crypto/kms/privatekeystore.go +++ b/crypto/kms/privatekeystore.go @@ -37,8 +37,6 @@ var ( ErrHashNotMatch = errors.New("private key hash not match") // ErrInvalidBase58Version indicates specified key is not base58 version ErrInvalidBase58Version = errors.New("invalid base58 version") - // ErrInvalidBase58Checksum indicates specified key checksum is not base58 checksum - ErrInvalidBase58Checksum = errors.New("invalid base58 checksum") // PrivateKeyStoreVersion defines the private key version byte. PrivateKeyStoreVersion byte = 0x23 ) diff --git a/crypto/kms/pubkeystore.go b/crypto/kms/pubkeystore.go index 550882960..2719a707c 100644 --- a/crypto/kms/pubkeystore.go +++ b/crypto/kms/pubkeystore.go @@ -101,8 +101,6 @@ var ( ErrNilNode = errors.New("nil node") // ErrKeyNotFound indicates key not found ErrKeyNotFound = errors.New("key not found") - // ErrNotValidNodeID indicates that is not valid node id - ErrNotValidNodeID = errors.New("not valid node id") // ErrNodeIDKeyNonceNotMatch indicates node id, key, nonce not match ErrNodeIDKeyNonceNotMatch = errors.New("nodeID, key, nonce not match") ) diff --git a/kayak/types/errors.go b/kayak/types/errors.go index 6ceab9c9d..2912207d7 100644 --- a/kayak/types/errors.go +++ b/kayak/types/errors.go @@ -31,8 +31,6 @@ var ( ErrInvalidLog = errors.New("invalid log") // ErrNotInPeer represents current node does not exists in peer list. ErrNotInPeer = errors.New("node not in peer") - // ErrNeedRecovery represents current follower node needs recovery, back-off is required by leader. - ErrNeedRecovery = errors.New("need recovery") // ErrInvalidConfig represents invalid kayak runtime config. ErrInvalidConfig = errors.New("invalid runtime config") ) diff --git a/kayak/wal/leveldb_wal.go b/kayak/wal/leveldb_wal.go index 8382744de..374d1f45b 100644 --- a/kayak/wal/leveldb_wal.go +++ b/kayak/wal/leveldb_wal.go @@ -190,17 +190,6 @@ func (p *LevelDBWal) Close() { } } -// GetDB returns the leveldb for storage extensions. -func (p *LevelDBWal) GetDB() (d *leveldb.DB, err error) { - if atomic.LoadUint32(&p.closed) == 1 { - err = ErrWalClosed - return - } - - d = p.db - return -} - func (p *LevelDBWal) load(logHeader []byte) (l *kt.Log, err error) { l = new(kt.Log) diff --git a/rpc/client_test.go b/rpc/client_test.go index ecdf30f1b..c57e1dc0c 100644 --- a/rpc/client_test.go +++ b/rpc/client_test.go @@ -26,9 +26,7 @@ import ( ) const nodeID = "0000" -const privateKey = "test.private" const publicKeyStore = "./test.keystore" -const pass = "abc" func TestDial(t *testing.T) { Convey("dial error case", t, func() { diff --git a/rpc/rpcutil_test.go b/rpc/rpcutil_test.go index 13c786a28..00c51c87f 100644 --- a/rpc/rpcutil_test.go +++ b/rpc/rpcutil_test.go @@ -37,7 +37,6 @@ import ( ) const ( - DHTStorePath = "./DHTStore" RPCConcurrent = 10 RPCCount = 100 ) diff --git a/sqlchain/chain.go b/sqlchain/chain.go index 76e4f8a00..cd314ef99 100644 --- a/sqlchain/chain.go +++ b/sqlchain/chain.go @@ -52,7 +52,6 @@ const ( var ( metaState = [4]byte{'S', 'T', 'A', 'T'} metaBlockIndex = [4]byte{'B', 'L', 'C', 'K'} - metaRequestIndex = [4]byte{'R', 'E', 'Q', 'U'} metaResponseIndex = [4]byte{'R', 'E', 'S', 'P'} metaAckIndex = [4]byte{'Q', 'A', 'C', 'K'} leveldbConf = opt.Options{} @@ -79,11 +78,6 @@ func heightToKey(h int32) (key []byte) { return } -// keyToHeight converts a height back from a key in bytes. -func keyToHeight(k []byte) int32 { - return int32(binary.BigEndian.Uint32(k)) -} - // keyWithSymbolToHeight converts a height back from a key(ack/resp/req/block) in bytes. // ack key: // ['Q', 'A', 'C', 'K', height, hash] @@ -1108,241 +1102,6 @@ func (c *Chain) UpdatePeers(peers *proto.Peers) error { return c.rt.updatePeers(peers) } -// getBilling returns a billing request from the blocks within height range [low, high]. -func (c *Chain) getBilling(low, high int32) (req *types.BillingRequest, err error) { - // Height `n` is ensured (or skipped) if `Next Turn` > `n` + 1 - if c.rt.getNextTurn() <= high+1 { - err = ErrUnavailableBillingRang - return - } - - var ( - n *blockNode - addr proto.AccountAddress - lowBlock, highBlock *types.Block - billings = make(map[proto.AccountAddress]*proto.AddrAndGas) - ) - - if head := c.rt.getHead(); head != nil { - n = head.node - } - - for ; n != nil && n.height > high; n = n.parent { - } - - for ; n != nil && n.height >= low; n = n.parent { - // TODO(leventeliu): block maybe released, use persistence version in this case. - if n.block == nil { - continue - } - - if lowBlock == nil { - lowBlock = n.block - } - - highBlock = n.block - - if addr, err = crypto.PubKeyHash(n.block.Signee()); err != nil { - return - } - - if billing, ok := billings[addr]; ok { - billing.GasAmount = c.rt.producingReward - } else { - producer := n.block.Producer() - billings[addr] = &proto.AddrAndGas{ - AccountAddress: addr, - RawNodeID: *producer.ToRawNodeID(), - GasAmount: c.rt.producingReward, - } - } - - for _, v := range n.block.Acks { - if addr, err = crypto.PubKeyHash(v.SignedResponseHeader().Signee); err != nil { - return - } - - if billing, ok := billings[addr]; ok { - billing.GasAmount += c.rt.price[v.SignedRequestHeader().QueryType] * - v.SignedRequestHeader().BatchCount - } else { - billings[addr] = &proto.AddrAndGas{ - AccountAddress: addr, - RawNodeID: *v.SignedResponseHeader().NodeID.ToRawNodeID(), - GasAmount: c.rt.producingReward, - } - } - } - } - - if lowBlock == nil || highBlock == nil { - err = ErrUnavailableBillingRang - return - } - - // Make request - gasAmounts := make([]*proto.AddrAndGas, 0, len(billings)) - - for _, v := range billings { - gasAmounts = append(gasAmounts, v) - } - - req = &types.BillingRequest{ - Header: types.BillingRequestHeader{ - DatabaseID: c.databaseID, - LowBlock: *lowBlock.BlockHash(), - LowHeight: low, - HighBlock: *highBlock.BlockHash(), - HighHeight: high, - GasAmounts: gasAmounts, - }, - } - return -} - -func (c *Chain) collectBillingSignatures(ctx context.Context, billings *types.BillingRequest) { - // Process sign billing responses, note that range iterating over channel will only break if - // the channel is closed - req := &MuxSignBillingReq{ - Envelope: proto.Envelope{ - // TODO(leventeliu): Add fields. - }, - DatabaseID: c.databaseID, - SignBillingReq: SignBillingReq{ - BillingRequest: *billings, - }, - } - proWG := &sync.WaitGroup{} - respC := make(chan *SignBillingResp) - defer proWG.Wait() - proWG.Add(1) - go func() { - defer proWG.Done() - - bpReq := &types.AdviseBillingReq{ - Req: billings, - } - - var ( - bpNodeID proto.NodeID - err error - ) - - for resp := range respC { - if err = bpReq.Req.AddSignature(resp.Signee, resp.Signature, false); err != nil { - // consume all rpc result - for range respC { - } - - return - } - } - - defer log.WithFields(log.Fields{ - "peer": c.rt.getPeerInfoString(), - "time": c.rt.getChainTimeString(), - "signees_count": len(req.Signees), - "signatures_count": len(req.Signatures), - "bp": bpNodeID, - }).WithError(err).Debug( - "Sent billing request") - - if bpNodeID, err = rpc.GetCurrentBP(); err != nil { - return - } - - var resp interface{} - if err = c.cl.CallNodeWithContext( - ctx, bpNodeID, route.MCCAdviseBillingRequest.String(), bpReq, resp, - ); err != nil { - return - } - }() - - // Send requests and push responses to response channel - peers := c.rt.getPeers() - rpcWG := &sync.WaitGroup{} - defer func() { - rpcWG.Wait() - close(respC) - }() - - for _, s := range peers.Servers { - if s != c.rt.getServer() { - rpcWG.Add(1) - go func(id proto.NodeID) { - defer rpcWG.Done() - resp := &MuxSignBillingResp{} - - if err := c.cl.CallNodeWithContext( - ctx, id, route.SQLCSignBilling.String(), req, resp, - ); err != nil { - log.WithFields(log.Fields{ - "peer": c.rt.getPeerInfoString(), - "time": c.rt.getChainTimeString(), - }).WithError(err).Error("failed to send sign billing request") - } - - respC <- &resp.SignBillingResp - }(s) - } - } -} - -// LaunchBilling launches a new billing process for the blocks within height range [low, high] -// (inclusive). -func (c *Chain) LaunchBilling(low, high int32) (err error) { - var ( - req *types.BillingRequest - ) - - defer log.WithFields(log.Fields{ - "peer": c.rt.getPeerInfoString(), - "time": c.rt.getChainTimeString(), - "low": low, - "high": high, - }).WithError(err).Debug("launched billing process") - - if req, err = c.getBilling(low, high); err != nil { - return - } - - if _, err = req.PackRequestHeader(); err != nil { - return - } - - c.rt.goFunc(func(ctx context.Context) { c.collectBillingSignatures(ctx, req) }) - return -} - -// SignBilling signs a billing request. -func (c *Chain) SignBilling(req *types.BillingRequest) ( - pub *asymmetric.PublicKey, sig *asymmetric.Signature, err error, -) { - var ( - loc *types.BillingRequest - ) - defer log.WithFields(log.Fields{ - "peer": c.rt.getPeerInfoString(), - "time": c.rt.getChainTimeString(), - "low": req.Header.LowHeight, - "high": req.Header.HighHeight, - }).WithError(err).Debug("processing sign billing request") - - // Verify billing results - if err = req.VerifySignatures(); err != nil { - return - } - if loc, err = c.getBilling(req.Header.LowHeight, req.Header.HighHeight); err != nil { - return - } - if err = req.Compare(loc); err != nil { - return - } - pub, sig, err = req.SignRequestHeader(c.pk, false) - return -} - // AddSubscription is used by dbms to add an observer. func (c *Chain) AddSubscription(nodeID proto.NodeID, startHeight int32) (err error) { // send previous height and transactions using AdviseAckedQuery/AdviseNewBlock RPC method diff --git a/sqlchain/errors.go b/sqlchain/errors.go index 7ed223777..bdb3c1a0f 100644 --- a/sqlchain/errors.go +++ b/sqlchain/errors.go @@ -21,85 +21,25 @@ import ( ) var ( - // ErrFieldConversion indicates an incompatible field conversion during data unmarshalling from - // a protobuf message, such as converting a non-digital string to a BigInt. - ErrFieldConversion = errors.New("incompatible field type conversion") - - // ErrFieldLength indicates an unexpected array field length during data unmarshalling from a - // protobuf message. Since protobuf doesn't have fixed size array, we need to check the slice - // length before read it back from a slice field to a fixed size array field. - ErrFieldLength = errors.New("unexpected slice field length") - - // ErrNilValue indicates that an unexpected but not fatal nil value is detected , hence return - // it as an error. - ErrNilValue = errors.New("unexpected nil value") - // ErrParentNotFound indicates an error failing to find parent node during a chain reloading. ErrParentNotFound = errors.New("could not find parent node") - - // ErrBlockExists indicates that a received block is already in the indexed. - ErrBlockExists = errors.New("block already exists") - // ErrInvalidBlock indicates an invalid block which does not extend the best chain while // pushing new blocks. ErrInvalidBlock = errors.New("invalid block") - - // ErrBlockTimestampOutOfPeriod indicates a block producing timestamp verification failure. - ErrBlockTimestampOutOfPeriod = errors.New("block timestamp is out of producing period") - - // ErrMultipleAckOfResponse indicates that multiple acknowledgements for a same query response - // is detected. - ErrMultipleAckOfResponse = errors.New("multiple acknowledgements of same response") - // ErrMultipleAckOfSeqNo indicates that multiple acknowledgements for a same sequence number is // detected. ErrMultipleAckOfSeqNo = errors.New("multiple acknowledgements of same sequence number") - - // ErrQueryExpired indicates that a received query Response/Ack has expired. - ErrQueryExpired = errors.New("query has expired") - - // ErrQueryNotCached indicates that a wanted query is not cached locally. - ErrQueryNotCached = errors.New("query is not cached locally") - - // ErrQuerySignedByAnotherBlock indicates that a query is already signed in a known block - // during the verification of a new introduced block. - ErrQuerySignedByAnotherBlock = errors.New("query has been packed by another block") - - // ErrCorruptedIndex indicates that a corrupted index item is detected. - ErrCorruptedIndex = errors.New("corrupted index item") - // ErrUnknownMuxRequest indicates that the multiplexing request endpoint is not found. ErrUnknownMuxRequest = errors.New("unknown multiplexing request") - + // ErrQueryExpired indicates that a received query Response/Ack has expired. + ErrQueryExpired = errors.New("query has expired") // ErrUnknownProducer indicates that the block has an unknown producer. ErrUnknownProducer = errors.New("unknown block producer") - // ErrInvalidProducer indicates that the block has an invalid producer. ErrInvalidProducer = errors.New("invalid block producer") - - // ErrUnavailableBillingRang indicates that the billing range is not available now. - ErrUnavailableBillingRang = errors.New("unavailable billing range") - - // ErrHashNotMatch indicates that a message hash value doesn't match the original hash value - // given in its hash field. - ErrHashNotMatch = errors.New("hash value doesn't match") - - // ErrMetaStateNotFound indicates that meta state not found in db. - ErrMetaStateNotFound = errors.New("meta state not found in db") - - // ErrAckQueryNotFound indicates that an acknowledged query record is not found. - ErrAckQueryNotFound = errors.New("acknowledged query not found") - // ErrQueryNotFound indicates that a query is not found in the index. ErrQueryNotFound = errors.New("query not found") - - // ErrInvalidRequest indicates the query is invalid. - ErrInvalidRequest = errors.New("invalid request") - // ErrResponseSeqNotMatch indicates that a response sequence id doesn't match the original one // in the index. ErrResponseSeqNotMatch = errors.New("response sequence id doesn't match") - - // ErrInvalidTransactionType indicates that the transaction type is invalid. - ErrInvalidTransactionType = errors.New("invalid transaction type") ) diff --git a/sqlchain/mux.go b/sqlchain/mux.go index 7dea6dfea..836f038c9 100644 --- a/sqlchain/mux.go +++ b/sqlchain/mux.go @@ -103,34 +103,6 @@ type MuxFetchBlockResp struct { FetchBlockResp } -// MuxSignBillingReq defines a request of the SignBilling RPC method. -type MuxSignBillingReq struct { - proto.Envelope - proto.DatabaseID - SignBillingReq -} - -// MuxSignBillingResp defines a response of the SignBilling RPC method. -type MuxSignBillingResp struct { - proto.Envelope - proto.DatabaseID - SignBillingResp -} - -// MuxLaunchBillingReq defines a request of the LaunchBilling RPC method. -type MuxLaunchBillingReq struct { - proto.Envelope - proto.DatabaseID - LaunchBillingReq -} - -// MuxLaunchBillingResp defines a response of the LaunchBilling RPC method. -type MuxLaunchBillingResp struct { - proto.Envelope - proto.DatabaseID - LaunchBillingResp -} - // MuxSubscribeTransactionsReq defines a request of the SubscribeTransactions RPC method. type MuxSubscribeTransactionsReq struct { proto.Envelope @@ -205,30 +177,6 @@ func (s *MuxService) FetchBlock(req *MuxFetchBlockReq, resp *MuxFetchBlockResp) return ErrUnknownMuxRequest } -// SignBilling is the RPC method to get signature for a billing request from the target server. -func (s *MuxService) SignBilling(req *MuxSignBillingReq, resp *MuxSignBillingResp) (err error) { - if v, ok := s.serviceMap.Load(req.DatabaseID); ok { - resp.Envelope = req.Envelope - resp.DatabaseID = req.DatabaseID - return v.(*ChainRPCService).SignBilling(&req.SignBillingReq, &resp.SignBillingResp) - } - - return ErrUnknownMuxRequest -} - -// LaunchBilling is the RPC method to launch a new billing process in the target server. -func (s *MuxService) LaunchBilling(req *MuxLaunchBillingReq, resp *MuxLaunchBillingResp) ( - err error, -) { - if v, ok := s.serviceMap.Load(req.DatabaseID); ok { - resp.Envelope = req.Envelope - resp.DatabaseID = req.DatabaseID - return v.(*ChainRPCService).LaunchBilling(&req.LaunchBillingReq, &resp.LaunchBillingResp) - } - - return ErrUnknownMuxRequest -} - // SubscribeTransactions is the RPC method to subscribe transactions from the target server. func (s *MuxService) SubscribeTransactions(req *MuxSubscribeTransactionsReq, resp *MuxSubscribeTransactionsResp) (err error) { if v, ok := s.serviceMap.Load(req.DatabaseID); ok { diff --git a/sqlchain/rpc.go b/sqlchain/rpc.go index e9e8ff217..e9691d668 100644 --- a/sqlchain/rpc.go +++ b/sqlchain/rpc.go @@ -17,8 +17,6 @@ package sqlchain import ( - "github.com/CovenantSQL/CovenantSQL/crypto/asymmetric" - "github.com/CovenantSQL/CovenantSQL/crypto/hash" "github.com/CovenantSQL/CovenantSQL/proto" "github.com/CovenantSQL/CovenantSQL/types" ) @@ -66,27 +64,6 @@ type FetchBlockResp struct { Block *types.Block } -// SignBillingReq defines a request of the SignBilling RPC method. -type SignBillingReq struct { - types.BillingRequest -} - -// SignBillingResp defines a response of the SignBilling RPC method. -type SignBillingResp struct { - HeaderHash hash.Hash - Signee *asymmetric.PublicKey - Signature *asymmetric.Signature -} - -// LaunchBillingReq defines a request of LaunchBilling RPC method. -type LaunchBillingReq struct { - Low, High int32 -} - -// LaunchBillingResp defines a response of LaunchBilling RPC method. -type LaunchBillingResp struct { -} - // SubscribeTransactionsReq defines a request of SubscribeTransaction RPC method. type SubscribeTransactionsReq struct { SubscriberID proto.NodeID @@ -131,18 +108,6 @@ func (s *ChainRPCService) FetchBlock(req *FetchBlockReq, resp *FetchBlockResp) ( return } -// SignBilling is the RPC method to get signature for a billing request from the target server. -func (s *ChainRPCService) SignBilling(req *SignBillingReq, resp *SignBillingResp) (err error) { - resp.HeaderHash = req.BillingRequest.RequestHash - resp.Signee, resp.Signature, err = s.chain.SignBilling(&req.BillingRequest) - return -} - -// LaunchBilling is the RPC method to launch a new billing process in the target server. -func (s *ChainRPCService) LaunchBilling(req *LaunchBillingReq, _ *LaunchBillingResp) error { - return s.chain.LaunchBilling(req.Low, req.High) -} - // SubscribeTransactions is the RPC method to fetch subscribe new packed and confirmed transactions from the target server. func (s *ChainRPCService) SubscribeTransactions(req *SubscribeTransactionsReq, _ *SubscribeTransactionsResp) error { return s.chain.AddSubscription(req.SubscriberID, req.Height) diff --git a/sqlchain/runtime.go b/sqlchain/runtime.go index 8c1800982..88c680d7e 100644 --- a/sqlchain/runtime.go +++ b/sqlchain/runtime.go @@ -51,10 +51,6 @@ type runtime struct { blockCacheTTL int32 // muxServer is the multiplexing service of sql-chain PRC. muxService *MuxService - // price sets query price in gases. - price map[types.QueryType]uint64 - producingReward uint64 - billingPeriods int32 // peersMutex protects following peers-relative fields. peersMutex sync.Mutex @@ -104,16 +100,13 @@ func newRunTime(ctx context.Context, c *Config) (r *runtime) { ctx: cld, cancel: ccl, - period: c.Period, - tick: c.Tick, - queryTTL: c.QueryTTL, - blockCacheTTL: blockCacheTTLRequired(c), - muxService: c.MuxService, - price: c.Price, - producingReward: c.ProducingReward, - billingPeriods: c.BillingPeriods, - peers: c.Peers, - server: c.Server, + period: c.Period, + tick: c.Tick, + queryTTL: c.QueryTTL, + blockCacheTTL: blockCacheTTLRequired(c), + muxService: c.MuxService, + peers: c.Peers, + server: c.Server, index: func() int32 { if index, found := c.Peers.Find(c.Server); found { return index @@ -206,11 +199,6 @@ func (r *runtime) setNextTurn() { r.nextTurn++ } -// getQueryGas gets the consumption of gas for a specified query type. -func (r *runtime) getQueryGas(t types.QueryType) uint64 { - return r.price[t] -} - // stop sends a signal to the Runtime stop channel by closing it. func (r *runtime) stop(dbID proto.DatabaseID) { r.stopService(dbID) diff --git a/sqlchain/xxx_test.go b/sqlchain/xxx_test.go index 3e45a73b3..80b5719cb 100644 --- a/sqlchain/xxx_test.go +++ b/sqlchain/xxx_test.go @@ -72,18 +72,6 @@ func newRandomNode() (node *nodeProfile, err error) { return } -func newRandomNodes(n int) (nodes []*nodeProfile, err error) { - nodes = make([]*nodeProfile, n) - - for i := range nodes { - if nodes[i], err = newRandomNode(); err != nil { - return - } - } - - return -} - func createRandomString(offset, length int, s *string) { buff := make([]byte, rand.Intn(length)+offset) rand.Read(buff) @@ -186,40 +174,6 @@ func createRandomQueryResponse(cli, worker *nodeProfile) ( return } -func createRandomQueryResponseWithRequest(req *types.SignedRequestHeader, worker *nodeProfile) ( - r *types.SignedResponseHeader, err error, -) { - resp := &types.Response{ - Header: types.SignedResponseHeader{ - ResponseHeader: types.ResponseHeader{ - Request: *req, - NodeID: worker.NodeID, - Timestamp: createRandomTimeAfter(req.Timestamp, 100), - }, - }, - Payload: types.ResponsePayload{ - Columns: createRandomStrings(10, 10, 10, 10), - DeclTypes: createRandomStrings(10, 10, 10, 10), - Rows: make([]types.ResponseRow, rand.Intn(10)+10), - }, - } - - for i := range resp.Payload.Rows { - s := createRandomStrings(10, 10, 10, 10) - resp.Payload.Rows[i].Values = make([]interface{}, len(s)) - for j := range resp.Payload.Rows[i].Values { - resp.Payload.Rows[i].Values[j] = s[j] - } - } - - if err = resp.Sign(worker.PrivateKey); err != nil { - return - } - - r = &resp.Header - return -} - func createRandomQueryAckWithResponse(resp *types.SignedResponseHeader, cli *nodeProfile) ( r *types.SignedAckHeader, err error, ) { @@ -241,47 +195,6 @@ func createRandomQueryAckWithResponse(resp *types.SignedResponseHeader, cli *nod return } -func createRandomQueryAck(cli, worker *nodeProfile) (r *types.SignedAckHeader, err error) { - resp, err := createRandomQueryResponse(cli, worker) - - if err != nil { - return - } - - ack := &types.Ack{ - Header: types.SignedAckHeader{ - AckHeader: types.AckHeader{ - Response: *resp, - NodeID: cli.NodeID, - Timestamp: createRandomTimeAfter(resp.Timestamp, 100), - }, - }, - } - - if err = ack.Sign(cli.PrivateKey, true); err != nil { - return - } - - r = &ack.Header - return -} - -func createRandomNodesAndAck() (r *types.SignedAckHeader, err error) { - cli, err := newRandomNode() - - if err != nil { - return - } - - worker, err := newRandomNode() - - if err != nil { - return - } - - return createRandomQueryAck(cli, worker) -} - func registerNodesWithPublicKey(pub *asymmetric.PublicKey, diff int, num int) ( nis []cpuminer.NonceInfo, err error) { nis = make([]cpuminer.NonceInfo, num) @@ -372,48 +285,6 @@ func createRandomBlock(parent hash.Hash, isGenesis bool) (b *types.Block, err er return } -func createRandomQueries(x int) (acks []*types.SignedAckHeader, err error) { - n := rand.Intn(x) - acks = make([]*types.SignedAckHeader, n) - - for i := range acks { - if acks[i], err = createRandomNodesAndAck(); err != nil { - return - } - } - - return -} - -func createRandomBlockWithQueries(genesis, parent hash.Hash, acks []*types.SignedAckHeader) ( - b *types.Block, err error, -) { - // Generate key pair - priv, _, 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: genesis, - ParentHash: parent, - Timestamp: time.Now().UTC(), - }, - }, - } - - err = b.PackAndSignBlock(priv) - return -} - func createTestPeers(num int) (nis []cpuminer.NonceInfo, p *proto.Peers, err error) { if num <= 0 { return diff --git a/twopc/twopc_test.go b/twopc/twopc_test.go index e8e26d5dd..fa0accf04 100644 --- a/twopc/twopc_test.go +++ b/twopc/twopc_test.go @@ -67,9 +67,6 @@ type RaftNodeRPCServer struct { type RaftNode struct { RaftNodeRPCServer - - conn *etls.CryptoConn - client *rpc.Client } type RaftWriteBatchReq struct { diff --git a/types/errors.go b/types/errors.go index 882074aa9..86ea4154d 100644 --- a/types/errors.go +++ b/types/errors.go @@ -26,20 +26,10 @@ var ( // ErrNodePublicKeyNotMatch indicates that the public key given with a node does not match the // one in the key store. ErrNodePublicKeyNotMatch = errors.New("node publick key doesn't match") - // ErrSignRequest indicates a failed signature compute operation. - ErrSignRequest = errors.New("signature compute failed") // ErrSignVerification indicates a failed signature verification. ErrSignVerification = errors.New("signature verification failed") // ErrBillingNotMatch indicates that the billing request doesn't match the local result. ErrBillingNotMatch = errors.New("billing request doesn't match") // ErrHashVerification indicates a failed hash verification. ErrHashVerification = errors.New("hash verification failed") - // ErrNotExists defines errors on manipulating a non-exists database instance. - ErrNotExists = errors.New("database instance not exists") - // ErrNoSuchUser indicates that user do not exist. - ErrNoSuchUser = errors.New("user not exist") - // ErrOverflow indicates computing overflow. - ErrOverflow = errors.New("computing overflow") - // ErrInvalidHeight indicates that received height is smaller than current height. - ErrInvalidHeight = errors.New("received height is smaller than current height") ) diff --git a/types/xxx_test.go b/types/xxx_test.go index 7e5f92fd0..ab9d8f77f 100644 --- a/types/xxx_test.go +++ b/types/xxx_test.go @@ -43,82 +43,14 @@ const ( letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ) -func generateRandomSQLChainUser() *SQLChainUser { - return &SQLChainUser{ - Address: proto.AccountAddress(generateRandomHash()), - Permission: UserPermission(rand.Int31n(int32(NumberOfUserPermission))), - } - -} - -func generateRandomSQLChainUsers(n int) (users []*SQLChainUser) { - users = make([]*SQLChainUser, n) - for i := range users { - users[i] = generateRandomSQLChainUser() - - } - return - -} - -func generateRandomAccountAddresses(n int) (s []proto.AccountAddress) { - s = make([]proto.AccountAddress, n) - for i := range s { - s[i] = proto.AccountAddress(generateRandomHash()) - - } - return - -} - -func generateRandomProfile() *SQLChainProfile { - return &SQLChainProfile{ - ID: generateRandomDatabaseID(), - Owner: proto.AccountAddress(generateRandomHash()), - Users: generateRandomSQLChainUsers(rand.Intn(10) + 1), - } - -} - -func generateRandomAccount() *Account { - return &Account{ - Address: proto.AccountAddress(generateRandomHash()), - TokenBalance: [SupportTokenNumber]uint64{rand.Uint64(), rand.Uint64()}, - Rating: rand.Float64(), - } - -} - -func generateRandomBytes(n int32) []byte { - s := make([]byte, n) - for i := range s { - s[i] = byte(rand.Int31n(2)) - - } - return s - -} - func generateRandomHash() hash.Hash { h := hash.Hash{} rand.Read(h[:]) return h - } func generateRandomDatabaseID() proto.DatabaseID { return proto.DatabaseID(randStringBytes(uuidLen)) - -} - -func generateRandomDatabaseIDs(n int32) []proto.DatabaseID { - s := make([]proto.DatabaseID, n) - for i := range s { - s[i] = proto.DatabaseID(randStringBytes(uuidLen)) - - } - return s - } func randStringBytes(n int) string { @@ -128,7 +60,6 @@ func randStringBytes(n int) string { } return string(b) - } func generateRandomBlock(parent hash.Hash, isGenesis bool) (b *BPBlock, err error) { @@ -386,22 +317,6 @@ func setup() { log.SetLevel(log.DebugLevel) } -func createRandomString(offset, length int, s *string) { - buff := make([]byte, rand.Intn(length)+offset) - rand.Read(buff) - *s = string(buff) -} - -func createRandomStrings(offset, length, soffset, slength int) (s []string) { - s = make([]string, rand.Intn(length)+offset) - - for i := range s { - createRandomString(soffset, slength, &s[i]) - } - - return -} - func createRandomBlock(parent hash.Hash, isGenesis bool) (b *Block, err error) { // Generate key pair priv, pub, err := asymmetric.GenSecp256k1KeyPair() diff --git a/utils/big.go b/utils/big.go index 6568671f5..7c4556073 100644 --- a/utils/big.go +++ b/utils/big.go @@ -24,12 +24,9 @@ import ( // Various big integer limit values. var ( - tt255 = BigPow(2, 255) - tt256 = BigPow(2, 256) - tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1)) - tt63 = BigPow(2, 63) - MaxBig256 = new(big.Int).Set(tt256m1) - MaxBig63 = new(big.Int).Sub(tt63, big.NewInt(1)) + tt255 = BigPow(2, 255) + tt256 = BigPow(2, 256) + tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1)) ) const ( diff --git a/utils/errors.go b/utils/errors.go deleted file mode 100644 index 25a9a89d1..000000000 --- a/utils/errors.go +++ /dev/null @@ -1,8 +0,0 @@ -package utils - -import "errors" - -var ( - // ErrInvalidType defines invalid type. - ErrInvalidType = errors.New("invalid type") -) diff --git a/utils/integer.go b/utils/integer.go index 9d52f69c4..ca52b8889 100644 --- a/utils/integer.go +++ b/utils/integer.go @@ -18,25 +18,10 @@ package utils import ( "fmt" + "math" "strconv" ) -// Integer limit values. -const ( - MaxInt8 = 1<<7 - 1 - MinInt8 = -1 << 7 - MaxInt16 = 1<<15 - 1 - MinInt16 = -1 << 15 - MaxInt32 = 1<<31 - 1 - MinInt32 = -1 << 31 - MaxInt64 = 1<<63 - 1 - MinInt64 = -1 << 63 - MaxUint8 = 1<<8 - 1 - MaxUint16 = 1<<16 - 1 - MaxUint32 = 1<<32 - 1 - MaxUint64 = 1<<64 - 1 -) - // HexOrDecimal64 marshals uint64 as hex or decimal. type HexOrDecimal64 uint64 @@ -87,7 +72,7 @@ func SafeSub(x, y uint64) (uint64, bool) { // SafeAdd returns the result and whether overflow occurred. func SafeAdd(x, y uint64) (uint64, bool) { - return x + y, y > MaxUint64-x + return x + y, y > math.MaxUint64-x } // SafeMul returns multiplication result and whether overflow occurred. @@ -95,5 +80,5 @@ func SafeMul(x, y uint64) (uint64, bool) { if x == 0 || y == 0 { return 0, false } - return x * y, y > MaxUint64/x + return x * y, y > math.MaxUint64/x } diff --git a/worker/errors.go b/worker/errors.go index 56d6b7473..00677621d 100644 --- a/worker/errors.go +++ b/worker/errors.go @@ -21,34 +21,22 @@ import "errors" var ( // ErrInvalidRequest defines invalid request structure during request. ErrInvalidRequest = errors.New("invalid request supplied") - // ErrInvalidRequestSeq defines invalid sequence no of request. ErrInvalidRequestSeq = errors.New("invalid request sequence applied") - - // ErrMultipleQuery defines error on executing multiple select query in single request. - ErrMultipleQuery = errors.New("multiple query in single request") - // ErrAlreadyExists defines error on re-creating existing database instance. ErrAlreadyExists = errors.New("database instance already exists") - // ErrNotExists defines errors on manipulating a non-exists database instance. ErrNotExists = errors.New("database instance not exists") - // ErrInvalidDBConfig defines errors on received invalid db config from block producer. ErrInvalidDBConfig = errors.New("invalid database configuration") - // ErrSpaceLimitExceeded defines errors on disk space exceeding limit. ErrSpaceLimitExceeded = errors.New("space limit exceeded") - // ErrUnknownMuxRequest indicates that the a multiplexing request endpoint is not found. ErrUnknownMuxRequest = errors.New("unknown multiplexing request") - // ErrPermissionDeny indicates that the requester has no permission to send read or write query. ErrPermissionDeny = errors.New("permission deny") - // ErrInvalidPermission indicates that the requester sends a unrecognized permission. ErrInvalidPermission = errors.New("invalid permission") - // ErrInvalidTransactionType indicates that the transaction type is invalid. ErrInvalidTransactionType = errors.New("invalid transaction type") ) diff --git a/xenomint/chain.go b/xenomint/chain.go index 830ab2a0c..85193ce96 100644 --- a/xenomint/chain.go +++ b/xenomint/chain.go @@ -20,36 +20,14 @@ import ( "time" ca "github.com/CovenantSQL/CovenantSQL/crypto/asymmetric" - "github.com/CovenantSQL/CovenantSQL/crypto/hash" "github.com/CovenantSQL/CovenantSQL/crypto/kms" "github.com/CovenantSQL/CovenantSQL/proto" "github.com/CovenantSQL/CovenantSQL/types" "github.com/CovenantSQL/CovenantSQL/utils/log" xi "github.com/CovenantSQL/CovenantSQL/xenomint/interfaces" xs "github.com/CovenantSQL/CovenantSQL/xenomint/sqlite" - xt "github.com/CovenantSQL/CovenantSQL/xenomint/types" ) -const ( - inCommandBufferLength = 100000 - outCommandBufferLength = 100000 -) - -type applyRequest struct { - request *types.Request - response *types.Response -} - -type blockNode struct { - parent *blockNode - // Cached block fields - hash hash.Hash - count int32 - height int32 - // Cached block object, may be nil - block *xt.Block -} - // Chain defines the xenomint chain structure. type Chain struct { state *State diff --git a/xenomint/errors.go b/xenomint/errors.go index 9a8ec2dea..6d0ce64f9 100644 --- a/xenomint/errors.go +++ b/xenomint/errors.go @@ -25,14 +25,8 @@ var ( ErrMissingParent = errors.New("query missing parent") // ErrInvalidRequest indicates the query is invalid. ErrInvalidRequest = errors.New("invalid request") - // ErrQueryExists indicates the query already exists in pool. - ErrQueryExists = errors.New("query already exists") - // ErrStateClosed indicates the state is closed. - ErrStateClosed = errors.New("state is closed") // ErrQueryConflict indicates the there is a conflict on query replay. ErrQueryConflict = errors.New("query conflict") - // ErrLocalBehindRemote indicates the local state is behind the remote. - ErrLocalBehindRemote = errors.New("local state is behind the remote") // ErrMuxServiceNotFound indicates that the multiplexing service endpoint is not found. ErrMuxServiceNotFound = errors.New("mux service not found") // ErrStatefulQueryParts indicates query contains stateful query parts. diff --git a/xenomint/xxx_test.go b/xenomint/xxx_test.go index 13a973878..18596f8aa 100644 --- a/xenomint/xxx_test.go +++ b/xenomint/xxx_test.go @@ -156,21 +156,6 @@ type keygen interface { reset() } -type randKeygen struct { - offset int - length int -} - -func newRandKeygen(offset, length int) *randKeygen { - return &randKeygen{ - offset: offset, - length: length, - } -} - -func (k *randKeygen) next() int { return rand.Intn(k.length) + k.offset } -func (k *randKeygen) reset() {} - type permKeygen struct { offset int length int From 68ee164cfd9634ac4d7ba52c43276008c4efae4f Mon Sep 17 00:00:00 2001 From: leventeliu Date: Sat, 29 Dec 2018 20:42:45 +0800 Subject: [PATCH 2/2] Fix test case --- utils/integer_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/integer_test.go b/utils/integer_test.go index c63772875..3391e29d4 100644 --- a/utils/integer_test.go +++ b/utils/integer_test.go @@ -17,6 +17,7 @@ package utils import ( + "math" "testing" ) @@ -36,8 +37,8 @@ func TestOverflow(t *testing.T) { op operation }{ // add operations - {MaxUint64, 1, true, add}, - {MaxUint64 - 1, 1, false, add}, + {math.MaxUint64, 1, true, add}, + {math.MaxUint64 - 1, 1, false, add}, // sub operations {0, 1, true, sub}, @@ -46,8 +47,8 @@ func TestOverflow(t *testing.T) { // mul operations {0, 0, false, mul}, {10, 10, false, mul}, - {MaxUint64, 2, true, mul}, - {MaxUint64, 1, false, mul}, + {math.MaxUint64, 2, true, mul}, + {math.MaxUint64, 1, false, mul}, } { var overflows bool switch test.op {