Skip to content

Expire musig pending transaction #828

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 9 commits into from
May 14, 2020
Merged
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ accounts.txt
.editorconfig
.manual
release/
github.token
github.token
*.back
*.bak
10 changes: 4 additions & 6 deletions api/handler/multisigHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package handler
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/zoobc/zoobc-core/api/service"

"github.com/zoobc/zoobc-core/common/model"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type (
Expand All @@ -28,8 +26,8 @@ func (msh *MultisigHandler) GetPendingTransactions(
req.Pagination.OrderField = "block_height"
req.Pagination.OrderBy = model.OrderBy_DESC
}
result, err := msh.MultisigService.GetPendingTransactions(req)
return result, err

return msh.MultisigService.GetPendingTransactions(req)
}

func (msh *MultisigHandler) GetPendingTransactionDetailByTransactionHash(
Expand Down
4 changes: 4 additions & 0 deletions cmd/block/blockGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,13 @@ func initialize(
receiptUtil,
publishedReceiptUtil,
service.NewTransactionCoreService(
nil,
queryExecutor,
nil,
nil,
query.NewTransactionQuery(chainType),
nil,
nil,
),
nil,
nil,
Expand Down
44 changes: 33 additions & 11 deletions cmd/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ Command line interface to as a utility tools to develop the zoobc system.
- `go run main {command} --help` to see to see available subcommands and flags
- `go run main {command} {subcommand} --help` to see to see available subcommands and flags of subcommand

## Transaction Commands
### Transaction general flag

- `--output` to provide generated ouput type. Example: `--ouput bytes`
- `--version` to provide version of transaction. Example: `--version 1`
- `--timestamp` to provide timestamp of trasaction. Example: `--timestamp 1234567`
- `--sender-seed` to provide the seed of sender transaction. Example: `--sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved"`
- `--sender-address` transaction's sender address
- `--recipient` provide recepient transaction. Example `--recipient VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM`
- `--fee` to provide fee transaction, Example `--fee 1`
- `--post` to define automate post transaction or not. Example: `-post true`
Expand All @@ -40,7 +42,7 @@ Command line interface to as a utility tools to develop the zoobc system.
go run main.go generate transaction send-money --timestamp 1257894000 --sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved" --recipient VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM --amount 5000000000
```

#### Transaction send money escrow, set flag `--escrow true` and 3 more fields: `--approver-address`, `--timeout`, `--commission` and `--instruction`
### Transaction send money escrow, set flag `--escrow true` and 3 more fields: `--approver-address`, `--timeout`, `--commission` and `--instruction`

```bash
go run main.go generate transaction send-money --escrow true --approver-address BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE --timeout 200 --sender-seed "execute beach inflict session course dance vanish cover lawsuit earth casino fringe waste warfare also habit skull donate window cannon scene salute dawn good" --amount 1111 --commission 111 --instruction "Check amount should be 111" --recipient nK_ouxdDDwuJiogiDAi_zs1LqeN7f5ZsXbFtXGqGc0Pd
Expand Down Expand Up @@ -88,12 +90,34 @@ go run main.go generate transaction remove-account-dataset --timestamp 125789400
go run main.go generate transaction escrow-approval --transaction-id -2546596465476625657 --approval true --sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved" --fee 111
```

### Transaction Multi Signatures
```bash
Flags:
--address-signatures stringToString address:signature list --address1='signature1' --address2='signature2' (default [])
--addresses strings list of participants --addresses='address1,address2'
-h, --help help for multi-signature
--min-signature uint32 minimum number of signature required for the transaction to be valid
--nonce int random number / access code for the multisig info
--transaction-hash string hash of transaction being signed by address-signature list hex
--unsigned-transaction string hex string of the unsigned transaction bytes
```
For the multi signature transaction let say want to send money with multisig account, need to do this steps:
1. Generate transaction send money, make sure with argument `--hash`. It will be `--unsigned-transaction` value on multi signature generator.
2. Sign the transaction to get the transaction hash, and it will be `--transcation-has` and the last the `signature-hex` will be as `--address-signatures` value on multi signature generator. <br>

So the completed comment it will be:
```bash
go run main.go generate transaction multi-signature --sender-seed="execute beach inflict session course dance vanish cover lawsuit earth casino fringe waste warfare also habit skull donate window cannon scene salute dawn good" --unsigned-transaction="01000000012ba5ba5e000000002c000000486c5a4c683356636e4e6c764279576f417a584f51326a416c77464f69794f395f6e6a49336f7135596768612c000000486c38393154655446784767574f57664f4f464b59725f586468584e784f384a4b38576e4d4a56366738614c41420f0000000000080000000600000000000000000000000000000000000000000000000000000000000000" --transaction-hash="21ddbdada9903da81bf17dba6569ff7e2665fec38760c7f6636419ee30da65b0" --address-signatures="HlZLh3VcnNlvByWoAzXOQ2jAlwFOiyO9_njI3oq5Ygha=00000000b4efe21822c9d63818d8d19f6c608d917b2237426d1157b4e6689b22ce6c256ccf8ec8e2c1016ab09dd4ef2b01191fe2df70b7a123fec7115d7afd5a938f9b0a"
```

## Block Commands
### Block Generating Fake Blocks

```bash
go run main.go generate block fake-blocks --numberOfBlocks=1000 --blocksmithSecretPhrase='sprinkled sneak species pork outpost thrift unwind cheesy vexingly dizzy neurology neatness' --out='../resource/zoobc.db'
```

## Account Commands
### Account Generate Using Ed25519 Algorithm

```bash
Expand All @@ -114,30 +138,28 @@ go run main.go generate account bitcoin --seed "concur vocalist rotten busload g
go run main.go generate account multisig --addresses "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN" --addresses "BCZD_VxfO2S9aziIL3cn_cXW7uPDVPOrnXuP98GEAUC7" --addresses "BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J" —-min-sigs=2 --nonce=3
```

## Other Commands
```bash
go run main.go genesis generate
```
outputs cmd/genesis.go.new and cmd/cluster_config.json

```bash

### Genesis Generate From cmd/genesisblock/preRegisteredNodes.json and resource/zoobc.db

```

```bash
go run main.go genesis generate -w
```
outputs cmd/genesis.go.new and cmd/cluster_config.json

```bash

### Genesis Generate From cmd/genesisblock/preRegisteredNodes.json and resource/zoobc.db, plus n random nodes registrations

```

### Genesis Generate From cmd/genesisblock/preRegisteredNodes.json and resource/zoobc.db, plus n random nodes registrations
```bash
go run main.go genesis generate -w -n 10
outputs cmd/genesis.go.new and cmd/cluster_config.json

```

```
outputs cmd/genesis.go.new and cmd/cluster_config.json

### Generate Proof of Ownership Node Registry

Expand Down
2 changes: 1 addition & 1 deletion common/query/multisignatureInfoQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (msi *MultisignatureInfoQuery) GetMultisignatureInfoByAddress(
}
}

// InsertPendingSignature inserts a new pending transaction into DB
// InsertMultisignatureInfo inserts a new pending transaction into DB
func (msi *MultisignatureInfoQuery) InsertMultisignatureInfo(multisigInfo *model.MultiSignatureInfo) [][]interface{} {
var queries [][]interface{}
insertQuery := fmt.Sprintf("INSERT OR REPLACE INTO %s (%s) VALUES(%s)",
Expand Down
17 changes: 17 additions & 0 deletions common/query/pendingTransactionQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"

"github.com/zoobc/zoobc-core/common/constant"

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

Expand All @@ -22,6 +24,7 @@ type (
) (
str string, args []interface{},
)
GetPendingTransactionsExpireByHeight(blockHeight uint32) (str string, args []interface{})
InsertPendingTransaction(pendingTx *model.PendingTransaction) [][]interface{}
Scan(pendingTx *model.PendingTransaction, row *sql.Row) error
ExtractModel(pendingTx *model.PendingTransaction) []interface{}
Expand Down Expand Up @@ -103,6 +106,20 @@ func (ptq *PendingTransactionQuery) GetPendingTransactionsBySenderAddress(
}
}

// GetPendingTransactionsExpireByHeight presents query to get pending_transactions that was expire by block_height
func (ptq *PendingTransactionQuery) GetPendingTransactionsExpireByHeight(currentHeight uint32) (str string, args []interface{}) {
return fmt.Sprintf(
"SELECT %s FROM %s WHERE block_height = ? AND status = ? AND latest = ?",
strings.Join(ptq.Fields, ", "),
ptq.getTableName(),
),
[]interface{}{
currentHeight - constant.MinRollbackBlocks,
model.PendingTransactionStatus_PendingTransactionPending,
true,
}
}

// InsertPendingTransaction inserts a new pending transaction into DB
func (ptq *PendingTransactionQuery) InsertPendingTransaction(pendingTx *model.PendingTransaction) [][]interface{} {
var queries [][]interface{}
Expand Down
48 changes: 48 additions & 0 deletions common/query/pendingTransactionQuery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,51 @@ func TestPendingTransactionQuery_TrimDataBeforeSnapshot(t *testing.T) {
})
}
}

func TestPendingTransactionQuery_GetPendingTransactionsExpireByHeight(t *testing.T) {
type fields struct {
Fields []string
TableName string
}
type args struct {
currentHeight uint32
}
tests := []struct {
name string
fields fields
args args
wantStr string
wantArgs []interface{}
}{
{
name: "WantSuccess",
fields: fields(*NewPendingTransactionQuery()),
args: args{
currentHeight: 1000,
},
wantStr: "SELECT sender_address, transaction_hash, transaction_bytes, status, block_height, latest " +
"FROM pending_transaction WHERE block_height = ? AND status = ? AND latest = ?",
wantArgs: []interface{}{
uint32(1000) - constant.MinRollbackBlocks,
model.PendingTransactionStatus_PendingTransactionPending,
true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ptq := &PendingTransactionQuery{
Fields: tt.fields.Fields,
TableName: tt.fields.TableName,
}
gotStr, gotArgs := ptq.GetPendingTransactionsExpireByHeight(tt.args.currentHeight)
if gotStr != tt.wantStr {
t.Errorf("GetPendingTransactionsExpireByHeight() gotStr = %v, want %v", gotStr, tt.wantStr)
return
}
if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
t.Errorf("GetPendingTransactionsExpireByHeight() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
}
})
}
}
9 changes: 8 additions & 1 deletion core/service/blockMainService.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,18 @@ func (bs *BlockService) PushBlock(previousBlock, block *model.Block, broadcast,
return err
}

// Respecting Expiring escrow before push block process
/*
Expiring Process: expiring the the transactions that affected by current block height.
Respecting Expiring escrow and multi signature transaction before push block process
*/
err = bs.TransactionCoreService.ExpiringEscrowTransactions(block.GetHeight(), true)
if err != nil {
return blocker.NewBlocker(blocker.BlockErr, err.Error())
}
err = bs.TransactionCoreService.ExpiringPendingTransactions(block.GetHeight(), true)
if err != nil {
return blocker.NewBlocker(blocker.BlockErr, err.Error())
}

blockInsertQuery, blockInsertValue := bs.BlockQuery.InsertBlock(block)
err = bs.QueryExecutor.ExecuteTransaction(blockInsertQuery, blockInsertValue...)
Expand Down
44 changes: 37 additions & 7 deletions core/service/blockMainService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ func (*mockQueryExecutorSuccess) BeginTx() error { return nil }

func (*mockQueryExecutorSuccess) RollbackTx() error { return nil }

func (*mockQueryExecutorSuccess) ExecuteTransaction(qStr string, args ...interface{}) error {
func (*mockQueryExecutorSuccess) ExecuteTransaction(string, ...interface{}) error {
return nil
}
func (*mockQueryExecutorSuccess) ExecuteTransactions(queries [][]interface{}) error {
func (*mockQueryExecutorSuccess) ExecuteTransactions([][]interface{}) error {
return nil
}
func (*mockQueryExecutorSuccess) CommitTx() error { return nil }
Expand Down Expand Up @@ -577,6 +577,9 @@ func (*mockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...inter
mockTransaction.GetSenderAccountAddress(),
mockTransaction.GetRecipientAccountAddress(),
))
case "SELECT sender_address, transaction_hash, transaction_bytes, status, block_height, latest " +
"FROM pending_transaction WHERE block_height = ? AND status = ? AND latest = ?":
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(mock.NewRows(query.NewPendingTransactionQuery().Fields))
// which is escrow expiration process
default:
mockRows := sqlmock.NewRows(query.NewEscrowTransactionQuery().Fields)
Expand Down Expand Up @@ -935,11 +938,7 @@ func (*mockPushBlockCoinbaseLotteryWinnersSuccess) CoinbaseLotteryWinners(blocks
return []string{}, nil
}

func (*mockPushBlockBlocksmithServiceSuccess) RewardBlocksmithAccountAddresses(
blocksmithAccountAddresses []string,
totalReward, blockTimestamp int64,
height uint32,
) error {
func (*mockPushBlockBlocksmithServiceSuccess) RewardBlocksmithAccountAddresses([]string, int64, int64, uint32) error {
return nil
}

Expand Down Expand Up @@ -1056,9 +1055,15 @@ func TestBlockService_PushBlock(t *testing.T) {
CoinbaseService: &mockPushBlockCoinbaseLotteryWinnersSuccess{},
BlocksmithService: &mockPushBlockBlocksmithServiceSuccess{},
TransactionCoreService: NewTransactionCoreService(
log.New(),
&mockQueryExecutorSuccess{},
&transaction.TypeSwitcher{
Executor: &mockQueryExecutorSuccess{},
},
&transaction.Util{},
query.NewTransactionQuery(&chaintype.MainChain{}),
query.NewEscrowTransactionQuery(),
query.NewPendingTransactionQuery(),
),
PublishedReceiptService: &mockPushBlockPublishedReceiptServiceSuccess{},
},
Expand Down Expand Up @@ -1116,9 +1121,13 @@ func TestBlockService_PushBlock(t *testing.T) {
CoinbaseService: &mockPushBlockCoinbaseLotteryWinnersSuccess{},
BlocksmithService: &mockPushBlockBlocksmithServiceSuccess{},
TransactionCoreService: NewTransactionCoreService(
log.New(),
&mockQueryExecutorSuccess{},
&transaction.TypeSwitcher{Executor: &mockQueryExecutorSuccess{}},
&transaction.Util{},
query.NewTransactionQuery(&chaintype.MainChain{}),
query.NewEscrowTransactionQuery(),
query.NewPendingTransactionQuery(),
),
PublishedReceiptService: &mockPushBlockPublishedReceiptServiceSuccess{},
},
Expand Down Expand Up @@ -1176,9 +1185,15 @@ func TestBlockService_PushBlock(t *testing.T) {
CoinbaseService: &mockPushBlockCoinbaseLotteryWinnersSuccess{},
BlocksmithService: &mockPushBlockBlocksmithServiceSuccess{},
TransactionCoreService: NewTransactionCoreService(
log.New(),
&mockQueryExecutorSuccess{},
&transaction.TypeSwitcher{
Executor: &mockQueryExecutorSuccess{},
},
&transaction.Util{},
query.NewTransactionQuery(&chaintype.MainChain{}),
query.NewEscrowTransactionQuery(),
query.NewPendingTransactionQuery(),
),
PublishedReceiptService: &mockPushBlockPublishedReceiptServiceSuccess{},
},
Expand Down Expand Up @@ -1236,9 +1251,15 @@ func TestBlockService_PushBlock(t *testing.T) {
CoinbaseService: &mockPushBlockCoinbaseLotteryWinnersSuccess{},
BlocksmithService: &mockPushBlockBlocksmithServiceSuccess{},
TransactionCoreService: NewTransactionCoreService(
log.New(),
&mockQueryExecutorSuccess{},
&transaction.TypeSwitcher{
Executor: &mockQueryExecutorSuccess{},
},
&transaction.Util{},
query.NewTransactionQuery(&chaintype.MainChain{}),
query.NewEscrowTransactionQuery(),
query.NewPendingTransactionQuery(),
),
PublishedReceiptService: &mockPushBlockPublishedReceiptServiceSuccess{},
},
Expand Down Expand Up @@ -1900,9 +1921,15 @@ func TestBlockService_AddGenesis(t *testing.T) {
BlockPoolService: &mockBlockPoolServiceNoDuplicate{},
Logger: log.New(),
TransactionCoreService: NewTransactionCoreService(
log.New(),
&mockQueryExecutorSuccess{},
&transaction.TypeSwitcher{
Executor: &mockQueryExecutorSuccess{},
},
&transaction.Util{},
query.NewTransactionQuery(&chaintype.MainChain{}),
query.NewEscrowTransactionQuery(),
query.NewPendingTransactionQuery(),
),
PublishedReceiptService: &mockAddGenesisPublishedReceiptServiceSuccess{},
},
Expand Down Expand Up @@ -4201,6 +4228,9 @@ func (*mockExecutorBlockPopSuccess) ExecuteSelect(qStr string, tx bool, args ...
"transaction_index FROM \"transaction\" WHERE block_id = ? ORDER BY transaction_index ASC":
mock.ExpectQuery(regexp.QuoteMeta(qStr)).WillReturnRows(
sqlmock.NewRows(transactionQ.Fields))
case "SELECT sender_address, transaction_hash, transaction_bytes, status, block_height, latest FROM pending_transaction " +
"WHERE (block_height+?) = ? AND status = ? AND latest = ?":
mock.ExpectQuery(regexp.QuoteMeta(qStr)).WillReturnRows(mock.NewRows(query.NewPendingTransactionQuery().Fields))
}

return db.Query(qStr)
Expand Down
Loading