Skip to content

added skippedblocksmith to snapshot (generate/apply) #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 30, 2020
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
81 changes: 45 additions & 36 deletions common/model/snapshot.pb.go

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

1 change: 1 addition & 0 deletions common/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func GetSnapshotQuery(ct chaintype.ChainType) (snapshotQuery map[string]Snapshot
"pendingTransaction": NewPendingTransactionQuery(),
"pendingSignature": NewPendingSignatureQuery(),
"multisignatureInfo": NewMultisignatureInfoQuery(),
"skippedBlocksmith": NewSkippedBlocksmithQuery(),
}
default:
snapshotQuery = map[string]SnapshotQuery{}
Expand Down
13 changes: 13 additions & 0 deletions common/query/skippedBlocksmithQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (*SkippedBlocksmithQuery) Scan(skippedBlocksmith *model.SkippedBlocksmith,
)
return err
}

func (sbq *SkippedBlocksmithQuery) Rollback(height uint32) (multiQueries [][]interface{}) {
return [][]interface{}{
{
Expand All @@ -113,3 +114,15 @@ func (sbq *SkippedBlocksmithQuery) Rollback(height uint32) (multiQueries [][]int
},
}
}

func (sbq *SkippedBlocksmithQuery) SelectDataForSnapshot(fromHeight, toHeight uint32) string {
return fmt.Sprintf("SELECT %s FROM %s WHERE block_height >= %d AND block_height <= %d ORDER BY block_height",
strings.Join(sbq.Fields, ", "),
sbq.getTableName(), fromHeight, toHeight)
}

// TrimDataBeforeSnapshot delete entries to assure there are no duplicates before applying a snapshot
func (sbq *SkippedBlocksmithQuery) TrimDataBeforeSnapshot(fromHeight, toHeight uint32) string {
return fmt.Sprintf(`DELETE FROM %s WHERE block_height >= %d AND block_height <= %d`,
sbq.getTableName(), fromHeight, toHeight)
}
98 changes: 98 additions & 0 deletions common/query/skippedBlocksmithQuery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package query

import (
"testing"

"github.com/zoobc/zoobc-core/common/chaintype"
)

func TestSkippedBlocksmithQuery_SelectDataForSnapshot(t *testing.T) {
qry := NewSkippedBlocksmithQuery()
type fields struct {
Fields []string
TableName string
ChainType chaintype.ChainType
}
type args struct {
fromHeight uint32
toHeight uint32
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "SelectDataForSnapshot",
fields: fields{
Fields: qry.Fields,
TableName: qry.TableName,
ChainType: &chaintype.MainChain{},
},
args: args{
fromHeight: 0,
toHeight: 10,
},
want: "SELECT blocksmith_public_key, pop_change, block_height, " +
"blocksmith_index FROM skipped_blocksmith WHERE block_height >= 0 AND block_height <= 10 ORDER BY block_height",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sbq := &SkippedBlocksmithQuery{
Fields: tt.fields.Fields,
TableName: tt.fields.TableName,
ChainType: tt.fields.ChainType,
}
if got := sbq.SelectDataForSnapshot(tt.args.fromHeight, tt.args.toHeight); got != tt.want {
t.Errorf("SkippedBlocksmithQuery.SelectDataForSnapshot() = %v, want %v", got, tt.want)
}
})
}
}

func TestSkippedBlocksmithQuery_TrimDataBeforeSnapshot(t *testing.T) {
qry := NewSkippedBlocksmithQuery()
type fields struct {
Fields []string
TableName string
ChainType chaintype.ChainType
}
type args struct {
fromHeight uint32
toHeight uint32
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "TrimDataBeforeSnapshot",
fields: fields{
Fields: qry.Fields,
TableName: qry.TableName,
ChainType: &chaintype.MainChain{},
},
args: args{
fromHeight: 0,
toHeight: 10,
},
want: "DELETE FROM skipped_blocksmith WHERE block_height >= 0 AND block_height <= 10",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sbq := &SkippedBlocksmithQuery{
Fields: tt.fields.Fields,
TableName: tt.fields.TableName,
ChainType: tt.fields.ChainType,
}
if got := sbq.TrimDataBeforeSnapshot(tt.args.fromHeight, tt.args.toHeight); got != tt.want {
t.Errorf("SkippedBlocksmithQuery.TrimDataBeforeSnapshot() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion common/schema
13 changes: 13 additions & 0 deletions common/service/accountDataset.pb.go

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

14 changes: 10 additions & 4 deletions common/service/accountDataset.pb.gw.go

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

2 changes: 1 addition & 1 deletion core/service/blockSpineService.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ func (bs *BlockSpineService) BlockTransactionsRequestedListener() observer.Liste
func (bs *BlockSpineService) WillSmith(
blocksmith *model.Blocksmith,
blockchainProcessorLastBlockID int64,
) (lastBlockId, blocksmithIndex int64, err error) {
) (lastBlockID, blocksmithIndex int64, err error) {
lastBlock, err := bs.GetLastBlock()
if err != nil {
return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker(
Expand Down
13 changes: 13 additions & 0 deletions core/service/snapshotMainBlockService.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
PendingTransactionQuery query.PendingTransactionQueryInterface
PendingSignatureQuery query.PendingSignatureQueryInterface
MultisignatureInfoQuery query.MultisignatureInfoQueryInterface
SkippedBlocksmithQuery query.SkippedBlocksmithQueryInterface
BlockQuery query.BlockQueryInterface
SnapshotQueries map[string]query.SnapshotQuery
DerivedQueries []query.DerivedQuery
Expand All @@ -47,6 +48,7 @@ func NewSnapshotMainBlockService(
pendingTransactionQuery query.PendingTransactionQueryInterface,
pendingSignatureQuery query.PendingSignatureQueryInterface,
multisignatureInfoQuery query.MultisignatureInfoQueryInterface,
skippedBlocksmithQuery query.SkippedBlocksmithQueryInterface,
blockQuery query.BlockQueryInterface,
snapshotQueries map[string]query.SnapshotQuery,
derivedQueries []query.DerivedQuery,
Expand All @@ -66,6 +68,7 @@ func NewSnapshotMainBlockService(
PendingTransactionQuery: pendingTransactionQuery,
PendingSignatureQuery: pendingSignatureQuery,
MultisignatureInfoQuery: multisignatureInfoQuery,
SkippedBlocksmithQuery: skippedBlocksmithQuery,
BlockQuery: blockQuery,
SnapshotQueries: snapshotQueries,
DerivedQueries: derivedQueries,
Expand Down Expand Up @@ -130,6 +133,8 @@ func (ss *SnapshotMainBlockService) NewSnapshotFile(block *model.Block) (snapsho
snapshotPayload.PendingSignatures, err = ss.PendingSignatureQuery.BuildModel([]*model.PendingSignature{}, rows)
case "multisignatureInfo":
snapshotPayload.MultiSignatureInfos, err = ss.MultisignatureInfoQuery.BuildModel([]*model.MultiSignatureInfo{}, rows)
case "skippedBlocksmith":
snapshotPayload.SkippedBlocksmiths, err = ss.SkippedBlocksmithQuery.BuildModel([]*model.SkippedBlocksmith{}, rows)
default:
err = blocker.NewBlocker(blocker.ParserErr, fmt.Sprintf("Invalid Snapshot Query Repository: %s", qryRepoName))
}
Expand Down Expand Up @@ -264,6 +269,14 @@ func (ss *SnapshotMainBlockService) InsertSnapshotPayloadToDB(payload *model.Sna
qryArgs := ss.MultisignatureInfoQuery.InsertMultisignatureInfo(rec)
queries = append(queries, qryArgs...)
}
case "skippedBlocksmith":
for _, rec := range payload.SkippedBlocksmiths {
qry, args := ss.SkippedBlocksmithQuery.InsertSkippedBlocksmith(rec)
queries = append(queries,
append(
[]interface{}{qry}, args...),
)
}
default:
return blocker.NewBlocker(blocker.ParserErr, fmt.Sprintf("Invalid Snapshot Query Repository: %s", qryRepoName))
}
Expand Down
Loading