Skip to content
Merged
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
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ status:


builder: status
# alpine image libmusl is not compatible with golang race detector
# also alpine libmusl is required for building static binaries to avoid glibc getaddrinfo panic
docker build \
--tag $(BUILDER):$(VERSION) \
--tag $(BUILDER):latest \
--build-arg BUILD_ARG=use_all_cores \
--build-arg BUILD_ARG=release \
-f docker/builder.Dockerfile \
.

Expand Down Expand Up @@ -183,6 +185,11 @@ client: bin/cql bin/cql.test bin/cql-fuse bin/cql-mysql-adapter bin/cql-faucet

all: bp miner client

build-release: bin/cqld bin/cql-minerd bin/cql bin/cql-fuse bin/cql-mysql-adapter bin/cql-faucet

release:
make -j$(JOBS) build-release

clean:
rm -rf bin/cql*
rm -f *.cover.out
Expand Down
56 changes: 56 additions & 0 deletions blockproducer/chain_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package blockproducer

import (
"database/sql"

pi "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/proto"
Expand Down Expand Up @@ -142,6 +144,60 @@ func (c *Chain) queryTxState(hash hash.Hash) (state pi.TransactionState, err err
return pi.TransactionStateNotFound, nil
}

func (c *Chain) queryAccountSQLChainProfiles(account proto.AccountAddress) (profiles []*types.SQLChainProfile, err error) {
var dbs []proto.DatabaseID

dbs, err = func() (dbs []proto.DatabaseID, err error) {
c.RLock()
defer c.RUnlock()

var (
id string
rows *sql.Rows
querySQL = `SELECT "id" FROM "indexed_shardChains" WHERE "account" = ?`
)

rows, err = c.storage.Reader().Query(querySQL, account.String())

if err != nil {
return
}

defer func() {
_ = rows.Close()
}()

for rows.Next() {
err = rows.Scan(&id)
if err != nil {
return
}

dbs = append(dbs, proto.DatabaseID(id))
}

return
}()

if err != nil {
return
}

var (
profile *types.SQLChainProfile
ok bool
)

for _, db := range dbs {
profile, ok = c.loadSQLChainProfile(db)
if ok {
profiles = append(profiles, profile)
}
}

return
}

func (c *Chain) immutableNextNonce(addr proto.AccountAddress) (n pi.AccountNonce, err error) {
c.RLock()
defer c.RUnlock()
Expand Down
13 changes: 12 additions & 1 deletion blockproducer/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,19 @@ func TestChain(t *testing.T) {
_, loaded = chain.immutable.loadOrStoreProviderObject(addr1, &types.ProviderProfile{})
So(loaded, ShouldBeFalse)
_, loaded = chain.immutable.loadOrStoreSQLChainObject(dbid1, &types.SQLChainProfile{
Miners: []*types.MinerInfo{&types.MinerInfo{Address: addr2}},
ID: dbid1,
Miners: []*types.MinerInfo{{Address: addr2}},
Users: []*types.SQLChainUser{{Address: addr1, Permission: &types.UserPermission{Role: types.Admin}}},
})
So(loaded, ShouldBeFalse)
_, loaded = chain.immutable.loadOrStoreAccountObject(addr2, &types.Account{
Address: addr2,
TokenBalance: [types.SupportTokenNumber]uint64{100, 100, 100, 100, 100},
})
So(loaded, ShouldBeFalse)

sps := chain.immutable.compileChanges(nil)
_ = store(chain.storage, sps, nil)
chain.immutable.commit()

err = rpcService.QuerySQLChainProfile(
Expand All @@ -237,6 +242,12 @@ func TestChain(t *testing.T) {
So(err, ShouldBeNil)
So(queryBalanceResp.OK, ShouldBeTrue)
So(queryBalanceResp.Balance, ShouldEqual, 100)

// query for account sqlchain profiles
var profilesResp = new(types.QueryAccountSQLChainProfilesResp)
_ = rpcService.QueryAccountSQLChainProfiles(&types.QueryAccountSQLChainProfilesReq{Addr: addr1}, profilesResp)
So(profilesResp.Profiles, ShouldNotBeEmpty)
So(profilesResp.Profiles[0].ID, ShouldEqual, dbid1)
})

Convey("Chain APIs should return correct result of tx state", func() {
Expand Down
13 changes: 13 additions & 0 deletions blockproducer/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,16 @@ func (s *ChainRPCService) QueryTxState(
resp.State = state
return
}

// QueryAccountSQLChainProfiles is the RPC method to query account sqlchain profiles.
func (s *ChainRPCService) QueryAccountSQLChainProfiles(
req *types.QueryAccountSQLChainProfilesReq, resp *types.QueryAccountSQLChainProfilesResp) (err error,
) {
var profiles []*types.SQLChainProfile
if profiles, err = s.chain.queryAccountSQLChainProfiles(req.Addr); err != nil {
return
}
resp.Addr = req.Addr
resp.Profiles = profiles
return
}
37 changes: 37 additions & 0 deletions blockproducer/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var (
UNIQUE ("address", "id")
);`,

`CREATE INDEX IF NOT EXISTS "idx__shardChain__id" ON "shardChain" ("id");`,

`CREATE TABLE IF NOT EXISTS "provider" (
"address" TEXT,
"encoded" BLOB,
Expand Down Expand Up @@ -108,6 +110,14 @@ var (
`CREATE INDEX IF NOT EXISTS "idx__indexed_transactions__timestamp" ON "indexed_transactions" ("timestamp" DESC);`,
`CREATE INDEX IF NOT EXISTS "idx__indexed_transactions__tx_type__timestamp" ON "indexed_transactions" ("tx_type", "timestamp" DESC);`,
`CREATE INDEX IF NOT EXISTS "idx__indexed_transactions__address__timestamp" ON "indexed_transactions" ("address", "timestamp" DESC);`,

`CREATE TABLE IF NOT EXISTS "indexed_shardChains" (
"account" TEXT,
"address" TEXT,
"id" TEXT,
UNIQUE("account", "address", "id")
);`,
`CREATE INDEX IF NOT EXISTS "idx__indexed_shardChains__id" ON "indexed_shardChains" ("id");`,
}
)

Expand Down Expand Up @@ -322,6 +332,29 @@ func updateShardChain(profile *types.SQLChainProfile) storageProcedure {
profile.Address.String(),
string(profile.ID),
enc.Bytes())
if err != nil {
return
}

for _, u := range profile.Users {
if u.Permission.Role == types.Void {
// remove index
_, err = tx.Exec(`DELETE FROM "indexed_shardChains" WHERE "account" = ? AND "address" = ?`,
u.Address.String(),
profile.Address.String())
} else {
_, err = tx.Exec(`INSERT OR REPLACE INTO "indexed_shardChains" ("account", "address", "id")
VALUES(?, ?, ?)`,
u.Address.String(),
profile.Address.String(),
profile.ID)
}

if err != nil {
return
}
}

return
}
}
Expand All @@ -332,6 +365,10 @@ func deleteShardChain(id proto.DatabaseID) storageProcedure {
"profile_database_id": id,
}).Debug("deleting profile")
_, err = tx.Exec(`DELETE FROM "shardChain" WHERE "id"=?`, id)
if err != nil {
return
}
_, err = tx.Exec(`DELETE FROM "indexed_shardChains" WHERE "id" = ?`, id)
return
}
}
Expand Down
Loading