From 249cf7651fe029dc9efc2d96647a91a09db770aa Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 20 Nov 2018 14:43:34 +0900 Subject: [PATCH 01/25] Add BlockTransaction.Index and BlockOperation.Index --- lib/block/genesis.go | 2 +- lib/block/operation.go | 2 + lib/block/operation_test.go | 2 +- lib/block/transaction.go | 5 +- lib/block/transaction_test.go | 50 +++++++++---------- lib/common/constant.go | 3 ++ lib/node/runner/api/base_test.go | 10 ++-- lib/node/runner/api/resource/operation.go | 1 + lib/node/runner/api/resource/resource_test.go | 6 +-- lib/node/runner/api/resource/transaction.go | 1 + lib/node/runner/api_block_test.go | 4 +- lib/node/runner/api_transactions_test.go | 4 +- lib/node/runner/block_operations_test.go | 6 +-- lib/node/runner/finish_ballot.go | 8 +-- 14 files changed, 58 insertions(+), 46 deletions(-) diff --git a/lib/block/genesis.go b/lib/block/genesis.go index 761c9d84d..a941e57fb 100644 --- a/lib/block/genesis.go +++ b/lib/block/genesis.go @@ -117,7 +117,7 @@ func MakeGenesisBlock(st *storage.LevelDBBackend, genesisAccount BlockAccount, c return } - bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx) + bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, 1) // first tx (not proposer tx) if err = bt.Save(st); err != nil { return } diff --git a/lib/block/operation.go b/lib/block/operation.go index 9df36475a..4c5de63d8 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -29,6 +29,7 @@ type BlockOperation struct { Target string `json:"target"` Body []byte `json:"body"` Height uint64 `json:"block_height"` + Index uint64 `json:"index"` // bellows will be used only for `Save` time. transaction transaction.Transaction @@ -74,6 +75,7 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans Target: target, Body: body, Height: blockHeight, + Index: index, transaction: tx, operation: op, diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index 43f818f6d..924c84dac 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -103,7 +103,7 @@ func TestBlockOperationSaveByTransaction(t *testing.T) { _, tx := transaction.TestMakeTransaction(conf.NetworkID, 10) block := TestMakeNewBlockWithPrevBlock(GetLatestBlock(st), []string{tx.GetHash()}) - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, 0) err := bt.Save(st) require.NoError(t, err) diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 3473c1002..b780d2490 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -37,12 +37,14 @@ type BlockTransaction struct { Created string `json:"created"` Message []byte `json:"message"` + Index uint64 `json:"index"` + transaction transaction.Transaction isSaved bool blockHeight uint64 } -func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, confirmed string, tx transaction.Transaction) BlockTransaction { +func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, confirmed string, tx transaction.Transaction, index uint64) BlockTransaction { var opHashes []string for _, op := range tx.B.Operations { opHashes = append(opHashes, NewBlockOperationKey(op.MakeHashString(), tx.GetHash())) @@ -59,6 +61,7 @@ func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, co Amount: tx.TotalAmount(true), Confirmed: confirmed, Created: tx.H.Created, + Index: index, transaction: tx, blockHeight: blockHeight, diff --git a/lib/block/transaction_test.go b/lib/block/transaction_test.go index 2c45693f6..927f4395e 100644 --- a/lib/block/transaction_test.go +++ b/lib/block/transaction_test.go @@ -16,7 +16,7 @@ func TestNewBlockTransaction(t *testing.T) { conf := common.NewTestConfig() _, tx := transaction.TestMakeTransaction(conf.NetworkID, 1) block := TestMakeNewBlock([]string{tx.GetHash()}) - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, 0) require.Equal(t, bt.Hash, tx.H.Hash) require.Equal(t, bt.SequenceID, tx.B.SequenceID) @@ -39,7 +39,7 @@ func TestBlockTransactionSaveAndGet(t *testing.T) { conf := common.NewTestConfig() st := storage.NewTestStorage() - bt := makeNewBlockTransaction(conf.NetworkID, 1) + bt := makeNewBlockTransaction(conf.NetworkID, 1, 0) err := bt.Save(st) require.NoError(t, err) @@ -60,7 +60,7 @@ func TestBlockTransactionSaveExisting(t *testing.T) { conf := common.NewTestConfig() st := storage.NewTestStorage() - bt := makeNewBlockTransaction(conf.NetworkID, 1) + bt := makeNewBlockTransaction(conf.NetworkID, 1, 0) err := bt.Save(st) require.NoError(t, err) @@ -93,8 +93,8 @@ func TestMultipleBlockTransactionSource(t *testing.T) { } block := TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, uint64(i)) err := bt.Save(st) require.NoError(t, err) } @@ -109,8 +109,8 @@ func TestMultipleBlockTransactionSource(t *testing.T) { } block = TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, uint64(i)) err := bt.Save(st) require.NoError(t, err) } @@ -175,8 +175,8 @@ func TestMultipleBlockTransactionConfirmed(t *testing.T) { } block := TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, uint64(i)) err := bt.Save(st) require.NoError(t, err) } @@ -225,7 +225,7 @@ func TestBlockTransactionMultipleSave(t *testing.T) { conf := common.NewTestConfig() st := storage.NewTestStorage() - bt := makeNewBlockTransaction(conf.NetworkID, 1) + bt := makeNewBlockTransaction(conf.NetworkID, 1, 0) err := bt.Save(st) require.NoError(t, err) @@ -259,7 +259,7 @@ func TestMultipleBlockTransactionGetByAccount(t *testing.T) { blk = TestMakeNewBlock(txHashes) for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx) + bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, 0) bt.MustSave(st) err := bt.SaveBlockOperations(st) require.NoError(t, err) @@ -278,8 +278,8 @@ func TestMultipleBlockTransactionGetByAccount(t *testing.T) { } blk = TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, uint64(i)) bt.MustSave(st) err := bt.SaveBlockOperations(st) require.NoError(t, err) @@ -297,8 +297,8 @@ func TestMultipleBlockTransactionGetByAccount(t *testing.T) { } blk = TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, uint64(i)) bt.MustSave(st) err := bt.SaveBlockOperations(st) require.NoError(t, err) @@ -343,8 +343,8 @@ func TestMultipleBlockTransactionGetByBlock(t *testing.T) { } block0 := TestMakeNewBlock(txHashes0) - for _, tx := range txs0 { - bt := NewBlockTransactionFromTransaction(block0.Hash, block0.Height, block0.ProposedTime, tx) + for i, tx := range txs0 { + bt := NewBlockTransactionFromTransaction(block0.Hash, block0.Height, block0.ProposedTime, tx, uint64(i)) bt.MustSave(st) } @@ -359,8 +359,8 @@ func TestMultipleBlockTransactionGetByBlock(t *testing.T) { } block1 := TestMakeNewBlock(txHashes1) - for _, tx := range txs1 { - bt := NewBlockTransactionFromTransaction(block1.Hash, block1.Height, block1.ProposedTime, tx) + for i, tx := range txs1 { + bt := NewBlockTransactionFromTransaction(block1.Hash, block1.Height, block1.ProposedTime, tx, uint64(i)) bt.MustSave(st) } @@ -427,8 +427,8 @@ func TestMultipleBlockTransactionsOrderByBlockHeightAndCursor(t *testing.T) { block := TestMakeNewBlock(txHashes) block.Height++ - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, uint64(i)) bt.MustSave(st) } transactionOrder = append(transactionOrder, createdOrder...) @@ -448,8 +448,8 @@ func TestMultipleBlockTransactionsOrderByBlockHeightAndCursor(t *testing.T) { } block := TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, uint64(i)) bt.MustSave(st) } @@ -506,9 +506,9 @@ func TestMultipleBlockTransactionsOrderByBlockHeightAndCursor(t *testing.T) { } } -func makeNewBlockTransaction(networkID []byte, n int) BlockTransaction { +func makeNewBlockTransaction(networkID []byte, n int, index uint64) BlockTransaction { _, tx := transaction.TestMakeTransaction(networkID, n) block := TestMakeNewBlock([]string{tx.GetHash()}) - return NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx) + return NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, index) } diff --git a/lib/common/constant.go b/lib/common/constant.go index 49c386e65..ad3af83e9 100644 --- a/lib/common/constant.go +++ b/lib/common/constant.go @@ -67,6 +67,9 @@ const ( // DiscoveryMessageCreatedAllowDuration limit the `DiscoveryMessage.Created` // is allowed or not. DiscoveryMessageCreatedAllowDuration time.Duration = time.Second * 10 + + // ProposerTransactionIndex + ProposerTransactionIndex uint64 = 0 ) var ( diff --git a/lib/node/runner/api/base_test.go b/lib/node/runner/api/base_test.go index c0d124315..5e25eb2b1 100644 --- a/lib/node/runner/api/base_test.go +++ b/lib/node/runner/api/base_test.go @@ -127,8 +127,8 @@ func prepareTxsWithKeyPair(storage *storage.LevelDBBackend, source, target *keyp theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(storage), txHashes) theBlock.MustSave(storage) - for _, tx := range txs { - bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx) + for i, tx := range txs { + bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx, uint64(i)) bt.MustSave(storage) if err := bt.SaveBlockOperations(storage); err != nil { return nil, nil, nil @@ -170,8 +170,8 @@ func prepareTxsWithoutSave(count int, st *storage.LevelDBBackend) (*keypair.Full } theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(st), txHashes) - for _, tx := range txs { - bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx) + for i, tx := range txs { + bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx, uint64(i)) btList = append(btList, bt) } return kp, btList @@ -182,7 +182,7 @@ func prepareTxWithoutSave(st *storage.LevelDBBackend) (*keypair.Full, *transacti tx := transaction.TestMakeTransactionWithKeypair(networkID, 1, kp) theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(st), []string{tx.GetHash()}) - bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx) + bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx, 0) return kp, &tx, &bt } diff --git a/lib/node/runner/api/resource/operation.go b/lib/node/runner/api/resource/operation.go index 777df34e1..ffa77fab3 100644 --- a/lib/node/runner/api/resource/operation.go +++ b/lib/node/runner/api/resource/operation.go @@ -38,6 +38,7 @@ func (o Operation) GetMap() hal.Entry { "target": o.bo.Target, "type": o.bo.Type, "tx_hash": o.bo.TxHash, + "index": o.bo.Index, "body": body, "block_height": o.bo.Height, } diff --git a/lib/node/runner/api/resource/resource_test.go b/lib/node/runner/api/resource/resource_test.go index c301f3432..766db55de 100644 --- a/lib/node/runner/api/resource/resource_test.go +++ b/lib/node/runner/api/resource/resource_test.go @@ -47,7 +47,7 @@ func TestResourceAccount(t *testing.T) { // Transaction { _, tx := transaction.TestMakeTransaction([]byte{0x00}, 1) - bt := block.NewBlockTransactionFromTransaction("dummy", 0, common.NowISO8601(), tx) + bt := block.NewBlockTransactionFromTransaction("dummy", 0, common.NowISO8601(), tx, 0) bt.MustSave(storage) rt := NewTransaction(&bt) @@ -73,7 +73,7 @@ func TestResourceAccount(t *testing.T) { // Operation { _, tx := transaction.TestMakeTransaction([]byte{0x00}, 1) - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, common.NowISO8601(), tx) + bt := block.NewBlockTransactionFromTransaction(common.GetUniqueIDFromUUID(), 0, common.NowISO8601(), tx, 0) bt.MustSave(storage) err := bt.SaveBlockOperations(storage) @@ -104,7 +104,7 @@ func TestResourceAccount(t *testing.T) { { var err error _, tx := transaction.TestMakeTransaction([]byte{0x00}, 3) - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, common.NowISO8601(), tx) + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, common.NowISO8601(), tx, 0) bt.MustSave(storage) err = bt.SaveBlockOperations(storage) require.NoError(t, err) diff --git a/lib/node/runner/api/resource/transaction.go b/lib/node/runner/api/resource/transaction.go index 0fc302edf..ec3e93879 100644 --- a/lib/node/runner/api/resource/transaction.go +++ b/lib/node/runner/api/resource/transaction.go @@ -27,6 +27,7 @@ func (t Transaction) GetMap() hal.Entry { "sequence_id": t.bt.SequenceID, "created": t.bt.Created, "operation_count": len(t.bt.Operations), + "index": t.bt.Index, } } func (t Transaction) Resource() *hal.Resource { diff --git a/lib/node/runner/api_block_test.go b/lib/node/runner/api_block_test.go index e2a805a16..6e6f23f97 100644 --- a/lib/node/runner/api_block_test.go +++ b/lib/node/runner/api_block_test.go @@ -57,8 +57,8 @@ func (p *HelperTestGetBlocksHandler) createBlock() block.Block { bk := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(p.st), txHashes) bk.MustSave(p.st) - for _, tx := range txs { - btx := block.NewBlockTransactionFromTransaction(bk.Hash, bk.Height, bk.ProposedTime, tx) + for i, tx := range txs { + btx := block.NewBlockTransactionFromTransaction(bk.Hash, bk.Height, bk.ProposedTime, tx, uint64(i)) btx.MustSave(p.st) btx.SaveBlockOperations(p.st) block.SaveTransactionPool(p.st, tx) diff --git a/lib/node/runner/api_transactions_test.go b/lib/node/runner/api_transactions_test.go index ee9844e4b..da81dc6af 100644 --- a/lib/node/runner/api_transactions_test.go +++ b/lib/node/runner/api_transactions_test.go @@ -109,8 +109,8 @@ func (p *HelperTestGetNodeTransactionsHandler) createBlock() block.Block { bk.Height = uint64(height + 1) bk.MustSave(p.st) - for _, tx := range txs { - btx := block.NewBlockTransactionFromTransaction(bk.Hash, bk.Height, bk.ProposedTime, tx) + for i, tx := range txs { + btx := block.NewBlockTransactionFromTransaction(bk.Hash, bk.Height, bk.ProposedTime, tx, uint64(i)) if err := btx.Save(p.st); err != nil { panic(err) } diff --git a/lib/node/runner/block_operations_test.go b/lib/node/runner/block_operations_test.go index a72a27acf..f1bc041ee 100644 --- a/lib/node/runner/block_operations_test.go +++ b/lib/node/runner/block_operations_test.go @@ -54,7 +54,7 @@ func (p *TestSavingBlockOperationHelper) makeBlock(prevBlock block.Block, numTxs opi, _ := ballot.NewInflationFromBallot(*blt, block.CommonKP.Address(), common.BaseReserve) opc, _ := ballot.NewCollectTxFeeFromBallot(*blt, block.CommonKP.Address(), txs...) ptx, _ := ballot.NewProposerTransactionFromBallot(*blt, opc, opi) - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, ptx.Transaction) + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, ptx.Transaction, common.ProposerTransactionIndex) bt.MustSave(p.st) block.SaveTransactionPool(p.st, ptx.Transaction) @@ -62,8 +62,8 @@ func (p *TestSavingBlockOperationHelper) makeBlock(prevBlock block.Block, numTxs blk.MustSave(p.st) - for _, tx := range txs { - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx) + for i, tx := range txs { + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, uint64(i+1)) bt.MustSave(p.st) } diff --git a/lib/node/runner/finish_ballot.go b/lib/node/runner/finish_ballot.go index 3111dffef..b1cf3b96b 100644 --- a/lib/node/runner/finish_ballot.go +++ b/lib/node/runner/finish_ballot.go @@ -5,6 +5,7 @@ import ( "boscoin.io/sebak/lib/ballot" "boscoin.io/sebak/lib/block" + "boscoin.io/sebak/lib/common" "boscoin.io/sebak/lib/errors" "boscoin.io/sebak/lib/metrics" "boscoin.io/sebak/lib/storage" @@ -126,8 +127,9 @@ func getProposedTransactions(st *storage.LevelDBBackend, pTxHashes []string, tra } func FinishTransactions(blk block.Block, transactions []*transaction.Transaction, st *storage.LevelDBBackend) (err error) { - for _, tx := range transactions { - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, *tx) + for i, tx := range transactions { + // i = 0 is an index of proposerTransaction + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, *tx, uint64(i+1)) if err = bt.Save(st); err != nil { return } @@ -290,7 +292,7 @@ func FinishProposerTransaction(st *storage.LevelDBBackend, blk block.Block, ptx } } - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, ptx.Transaction) + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, ptx.Transaction, common.ProposerTransactionIndex) if err = bt.Save(st); err != nil { return } From a99fb08fba371b3be65f6513a133d7fe796ca8e4 Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 20 Nov 2018 16:43:53 +0900 Subject: [PATCH 02/25] Make BlockOperation with BlockTransaction.Index - For building blockoperation's indexes, tx.Index is needed. --- lib/block/operation.go | 9 +++++---- lib/block/operation_test.go | 2 +- lib/block/test.go | 2 +- lib/block/transaction.go | 2 +- lib/node/runner/api/base_test.go | 6 +++--- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index 4c5de63d8..ecd1cfcaf 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -29,13 +29,14 @@ type BlockOperation struct { Target string `json:"target"` Body []byte `json:"body"` Height uint64 `json:"block_height"` - Index uint64 `json:"index"` + Index int `json:"index"` // bellows will be used only for `Save` time. transaction transaction.Transaction operation operation.Operation linked string isSaved bool + txIndex int opIndex int } @@ -43,7 +44,7 @@ func NewBlockOperationKey(opHash, txHash string) string { return fmt.Sprintf("%s-%s", opHash, txHash) } -func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, opIndex int) (BlockOperation, error) { +func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, txIndex, opIndex int) (BlockOperation, error) { body, err := json.Marshal(op.B) if err != nil { return BlockOperation{}, err @@ -75,12 +76,12 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans Target: target, Body: body, Height: blockHeight, - Index: index, + Index: opIndex, transaction: tx, operation: op, linked: linked, - opIndex: opIndex, + txIndex: txIndex, }, nil } diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index 924c84dac..28ff78b75 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -16,7 +16,7 @@ func TestNewBlockOperationFromOperation(t *testing.T) { _, tx := transaction.TestMakeTransaction(conf.NetworkID, 1) op := tx.B.Operations[0] - bo, err := NewBlockOperationFromOperation(op, tx, 0, 0) + bo, err := NewBlockOperationFromOperation(op, tx, 0, 1, 0) require.NoError(t, err) require.Equal(t, bo.Type, op.H.Type) diff --git a/lib/block/test.go b/lib/block/test.go index 8725d92a6..5eb7f9b9a 100644 --- a/lib/block/test.go +++ b/lib/block/test.go @@ -133,7 +133,7 @@ func TestMakeNewBlockOperation(networkID []byte, n int) (bos []BlockOperation) { _, tx := transaction.TestMakeTransaction(networkID, n) for i, op := range tx.B.Operations { - bo, err := NewBlockOperationFromOperation(op, tx, 0, i) + bo, err := NewBlockOperationFromOperation(op, tx, 0, 1, i) if err != nil { panic(err) } diff --git a/lib/block/transaction.go b/lib/block/transaction.go index b780d2490..0dbb88de9 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -201,7 +201,7 @@ func (bt *BlockTransaction) SaveBlockOperation(st *storage.LevelDBBackend, op op } var bo BlockOperation - bo, err = NewBlockOperationFromOperation(op, bt.Transaction(), bt.blockHeight, opIndex) + bo, err = NewBlockOperationFromOperation(op, bt.Transaction(), bt.blockHeight, bt.Index, opIndex) if err != nil { return } diff --git a/lib/node/runner/api/base_test.go b/lib/node/runner/api/base_test.go index 5e25eb2b1..fe88e7148 100644 --- a/lib/node/runner/api/base_test.go +++ b/lib/node/runner/api/base_test.go @@ -80,9 +80,9 @@ func prepareOpsWithoutSave(count int, st *storage.LevelDBBackend) (*keypair.Full } theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(st), txHashes) - for _, tx := range txs { - for i, op := range tx.B.Operations { - bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, i) + for i, tx := range txs { + for j, op := range tx.B.Operations { + bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, uint64(i), uint64(j)) if err != nil { panic(err) } From 450eb1c036de1246b3dbc5002307f68716a1b1c6 Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 20 Nov 2018 18:25:24 +0900 Subject: [PATCH 03/25] Fill test codes for indexes --- lib/block/operation_test.go | 9 ++++++++- lib/block/transaction_test.go | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index 28ff78b75..157bb9ffb 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -15,8 +15,11 @@ func TestNewBlockOperationFromOperation(t *testing.T) { conf := common.NewTestConfig() _, tx := transaction.TestMakeTransaction(conf.NetworkID, 1) + var index uint64 = 0 + var txIndex uint64 = 1 + op := tx.B.Operations[0] - bo, err := NewBlockOperationFromOperation(op, tx, 0, 1, 0) + bo, err := NewBlockOperationFromOperation(op, tx, 0, txIndex, index) require.NoError(t, err) require.Equal(t, bo.Type, op.H.Type) @@ -24,6 +27,8 @@ func TestNewBlockOperationFromOperation(t *testing.T) { require.Equal(t, bo.Source, tx.B.Source) encoded := common.MustMarshalJSON(op.B) require.Equal(t, bo.Body, encoded) + require.Equal(t, bo.Index, index) + require.Equal(t, bo.txIndex, txIndex) } func TestBlockOperationSaveAndGet(t *testing.T) { @@ -41,6 +46,7 @@ func TestBlockOperationSaveAndGet(t *testing.T) { require.Equal(t, bo.Hash, fetched.Hash) require.Equal(t, bo.Source, fetched.Source) require.Equal(t, bo.Body, fetched.Body) + require.Equal(t, bo.Index, uint64(0)) } func TestBlockOperationSaveExisting(t *testing.T) { @@ -93,6 +99,7 @@ func TestGetSortedBlockOperationsByTxHash(t *testing.T) { for i, bo := range saved { require.Equal(t, bo.Hash, createdOrder[bo.TxHash][i]) + require.Equal(t, bo.Index, uint64(i)) } } } diff --git a/lib/block/transaction_test.go b/lib/block/transaction_test.go index 927f4395e..dc51cffb0 100644 --- a/lib/block/transaction_test.go +++ b/lib/block/transaction_test.go @@ -16,7 +16,7 @@ func TestNewBlockTransaction(t *testing.T) { conf := common.NewTestConfig() _, tx := transaction.TestMakeTransaction(conf.NetworkID, 1) block := TestMakeNewBlock([]string{tx.GetHash()}) - bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, 0) + bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, 1) require.Equal(t, bt.Hash, tx.H.Hash) require.Equal(t, bt.SequenceID, tx.B.SequenceID) @@ -33,6 +33,11 @@ func TestNewBlockTransaction(t *testing.T) { require.Equal(t, opHash, opHashes[i]) } require.Equal(t, bt.Amount, tx.TotalAmount(true)) + + for i, txHash := range block.Transactions { + require.Equal(t, txHash, bt.Hash) + require.Equal(t, uint64(i+1), bt.Index) + } } func TestBlockTransactionSaveAndGet(t *testing.T) { @@ -380,6 +385,7 @@ func TestMultipleBlockTransactionGetByBlock(t *testing.T) { require.Equal(t, len(saved), len(createdOrder0), "fetched records insufficient") for i, bt := range saved { require.Equal(t, bt.Hash, createdOrder0[i], "order mismatch") + require.Equal(t, bt.Index, uint64(i)) } } From 9364f13b5c9cf85d2bc39ab79a8e8b367e42d676 Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 20 Nov 2018 21:17:17 +0900 Subject: [PATCH 04/25] Change DB Index --- lib/block/operation.go | 47 +++++++++++++------------- lib/block/operation_test.go | 2 +- lib/block/transaction.go | 51 ++++++++++++++++++++++------- lib/block/transaction_test.go | 16 ++++----- lib/common/prefix.go | 1 + lib/node/runner/block_operations.go | 2 +- 6 files changed, 74 insertions(+), 45 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index ecd1cfcaf..1eec5947b 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -24,24 +24,23 @@ type BlockOperation struct { OpHash string `json:"op_hash"` TxHash string `json:"tx_hash"` - Type operation.OperationType `json:"type"` - Source string `json:"source"` - Target string `json:"target"` - Body []byte `json:"body"` - Height uint64 `json:"block_height"` - Index int `json:"index"` + Type operation.OperationType `json:"type"` + Source string `json:"source"` + Target string `json:"target"` + Body []byte `json:"body"` + Height uint64 `json:"block_height"` + Index uint64 `json:"index"` + TxIndex uint64 `json:"tx_index"` // bellows will be used only for `Save` time. transaction transaction.Transaction operation operation.Operation linked string isSaved bool - txIndex int - opIndex int } -func NewBlockOperationKey(opHash, txHash string) string { - return fmt.Sprintf("%s-%s", opHash, txHash) +func NewBlockOperationKey(opHash, txHash string, index uint64) string { + return fmt.Sprintf("%s-%s-%d", opHash, txHash, index) } func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, txIndex, opIndex int) (BlockOperation, error) { @@ -66,22 +65,22 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans } return BlockOperation{ - Hash: NewBlockOperationKey(opHash, txHash), + Hash: NewBlockOperationKey(opHash, txHash, uint64(opIndex)), OpHash: opHash, TxHash: txHash, - Type: op.H.Type, - Source: tx.B.Source, - Target: target, - Body: body, - Height: blockHeight, - Index: opIndex, + Type: op.H.Type, + Source: tx.B.Source, + Target: target, + Body: body, + Height: blockHeight, + Index: uint64(opIndex), + TxIndex: uint64(txIndex), transaction: tx, operation: op, linked: linked, - txIndex: txIndex, }, nil } @@ -223,8 +222,8 @@ func (bo BlockOperation) NewBlockOperationTxHashKey() string { "%s%s%s%s", keyPrefixTxHash(bo.TxHash), common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), + common.EncodeUint64ToByteSlice(bo.TxIndex), + common.EncodeUint64ToByteSlice(bo.Index), ) } @@ -233,8 +232,8 @@ func (bo BlockOperation) NewBlockOperationSourceKey() string { "%s%s%s%s", keyPrefixSource(bo.Source), common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), + common.EncodeUint64ToByteSlice(bo.TxIndex), + common.EncodeUint64ToByteSlice(bo.Index), ) } @@ -251,8 +250,8 @@ func (bo BlockOperation) NewBlockOperationSourceAndTypeKey() string { "%s%s%s%s", keyPrefixSourceAndType(bo.Source, bo.Type), common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), + common.EncodeUint64ToByteSlice(bo.TxIndex), + common.EncodeUint64ToByteSlice(bo.Index), ) } func (bo BlockOperation) NewBlockOperationTargetKey(target string) string { diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index 157bb9ffb..f14ca02c0 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -28,7 +28,7 @@ func TestNewBlockOperationFromOperation(t *testing.T) { encoded := common.MustMarshalJSON(op.B) require.Equal(t, bo.Body, encoded) require.Equal(t, bo.Index, index) - require.Equal(t, bo.txIndex, txIndex) + require.Equal(t, bo.TxIndex, txIndex) } func TestBlockOperationSaveAndGet(t *testing.T) { diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 0dbb88de9..56bd64811 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -46,8 +46,8 @@ type BlockTransaction struct { func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, confirmed string, tx transaction.Transaction, index uint64) BlockTransaction { var opHashes []string - for _, op := range tx.B.Operations { - opHashes = append(opHashes, NewBlockOperationKey(op.MakeHashString(), tx.GetHash())) + for i, op := range tx.B.Operations { + opHashes = append(opHashes, NewBlockOperationKey(op.MakeHashString(), tx.GetHash(), uint64(i))) } return BlockTransaction{ @@ -74,15 +74,24 @@ func (bt BlockTransaction) NewBlockTransactionKeySource() string { GetBlockTransactionKeyPrefixSource(bt.Source), common.EncodeUint64ToByteSlice(bt.blockHeight), common.EncodeUint64ToByteSlice(bt.SequenceID), - common.GetUniqueIDFromUUID(), + common.EncodeUint64ToByteSlice(bt.Index), ) } func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { return fmt.Sprintf( - "%s%s", + "%s%s%s", GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed), - common.GetUniqueIDFromUUID(), + common.EncodeUint64ToByteSlice(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.Index), + ) +} + +func (bt BlockTransaction) NewBlockTransactionKeyHeight() string { + return fmt.Sprintf( + "%s%s", + GetBlockTransactionKeyPrefixHeight(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.Index), ) } @@ -91,18 +100,17 @@ func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string "%s%s%s%s", GetBlockTransactionKeyPrefixAccount(accountAddress), common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.SequenceID), + common.EncodeUint64ToByteSlice(bt.Index), common.GetUniqueIDFromUUID(), ) } func (bt BlockTransaction) NewBlockTransactionKeyByBlock(hash string) string { return fmt.Sprintf( - "%s%s%s%s", + "%s%s%s", GetBlockTransactionKeyPrefixBlock(hash), common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.SequenceID), - common.GetUniqueIDFromUUID(), + common.EncodeUint64ToByteSlice(bt.Index), ) } @@ -124,6 +132,9 @@ func (bt *BlockTransaction) Save(st *storage.LevelDBBackend) (err error) { if err = st.New(GetBlockTransactionKey(bt.Hash), bt); err != nil { return } + if err = st.New(bt.NewBlockTransactionKeyHeight(), bt.Hash); err != nil { + return + } if err = st.New(bt.NewBlockTransactionKeySource(), bt.Hash); err != nil { return } @@ -249,7 +260,11 @@ func GetBlockTransactionKeyPrefixBlock(hash string) string { } func GetBlockTransactionKey(hash string) string { - return fmt.Sprintf("%s%s", common.BlockTransactionPrefixHash, hash) + return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHash, hash) +} + +func GetBlockTransactionKeyPrefixHeight(height uint64) string { + return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHeight, common.EncodeUint64ToByteSlice(height)) } func GetBlockTransaction(st *storage.LevelDBBackend, hash string) (bt BlockTransaction, err error) { @@ -328,4 +343,18 @@ func GetBlockTransactionsByBlock(st *storage.LevelDBBackend, hash string, option return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) } -var GetBlockTransactions = GetBlockTransactionsByConfirmed +func GetBlockTransactionsByHeight(st *storage.LevelDBBackend, height uint64, options storage.ListOptions) ( + func() (BlockTransaction, bool, []byte), + func(), +) { + iterFunc, closeFunc := st.GetIterator(GetBlockTransactionKeyPrefixHeight(height), options) + return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) +} + +func GetBlockTransactions(st *storage.LevelDBBackend, options storage.ListOptions) ( + func() (BlockTransaction, bool, []byte), + func(), +) { + iterFunc, closeFunc := st.GetIterator(common.BlockTransactionPrefixHeight, options) + return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) +} diff --git a/lib/block/transaction_test.go b/lib/block/transaction_test.go index dc51cffb0..273eaef9b 100644 --- a/lib/block/transaction_test.go +++ b/lib/block/transaction_test.go @@ -26,8 +26,8 @@ func TestNewBlockTransaction(t *testing.T) { require.Equal(t, bt.Created, tx.H.Created) var opHashes []string - for _, op := range tx.B.Operations { - opHashes = append(opHashes, NewBlockOperationKey(op.MakeHashString(), tx.GetHash())) + for i, op := range tx.B.Operations { + opHashes = append(opHashes, NewBlockOperationKey(op.MakeHashString(), tx.GetHash(), uint64(i))) } for i, opHash := range bt.Operations { require.Equal(t, opHash, opHashes[i]) @@ -113,7 +113,7 @@ func TestMultipleBlockTransactionSource(t *testing.T) { txHashes = append(txHashes, tx.GetHash()) } - block = TestMakeNewBlock(txHashes) + block = TestMakeNewBlockWithPrevBlock(block, txHashes) for i, tx := range txs { bt := NewBlockTransactionFromTransaction(block.Hash, block.Height, block.ProposedTime, tx, uint64(i)) err := bt.Save(st) @@ -263,8 +263,8 @@ func TestMultipleBlockTransactionGetByAccount(t *testing.T) { } blk = TestMakeNewBlock(txHashes) - for _, tx := range txs { - bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, 0) + for i, tx := range txs { + bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, uint64(i)) bt.MustSave(st) err := bt.SaveBlockOperations(st) require.NoError(t, err) @@ -282,7 +282,7 @@ func TestMultipleBlockTransactionGetByAccount(t *testing.T) { txHashes = append(txHashes, tx.GetHash()) } - blk = TestMakeNewBlock(txHashes) + blk = TestMakeNewBlockWithPrevBlock(blk, txHashes) for i, tx := range txs { bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, uint64(i)) bt.MustSave(st) @@ -301,7 +301,7 @@ func TestMultipleBlockTransactionGetByAccount(t *testing.T) { txHashes = append(txHashes, tx.GetHash()) } - blk = TestMakeNewBlock(txHashes) + blk = TestMakeNewBlockWithPrevBlock(blk, txHashes) for i, tx := range txs { bt := NewBlockTransactionFromTransaction(blk.Hash, blk.Height, blk.ProposedTime, tx, uint64(i)) bt.MustSave(st) @@ -363,7 +363,7 @@ func TestMultipleBlockTransactionGetByBlock(t *testing.T) { txHashes1 = append(txHashes1, tx.GetHash()) } - block1 := TestMakeNewBlock(txHashes1) + block1 := TestMakeNewBlockWithPrevBlock(block0, txHashes1) for i, tx := range txs1 { bt := NewBlockTransactionFromTransaction(block1.Hash, block1.Height, block1.ProposedTime, tx, uint64(i)) bt.MustSave(st) diff --git a/lib/common/prefix.go b/lib/common/prefix.go index 8457c96d1..55f0feeb5 100644 --- a/lib/common/prefix.go +++ b/lib/common/prefix.go @@ -10,6 +10,7 @@ const ( BlockTransactionPrefixConfirmed = string(0x12) BlockTransactionPrefixAccount = string(0x13) BlockTransactionPrefixBlock = string(0x14) + BlockTransactionPrefixHeight = string(0x15) BlockOperationPrefixHash = string(0x20) BlockOperationPrefixTxHash = string(0x21) BlockOperationPrefixSource = string(0x22) diff --git a/lib/node/runner/block_operations.go b/lib/node/runner/block_operations.go index fde530305..4d4e5e8b3 100644 --- a/lib/node/runner/block_operations.go +++ b/lib/node/runner/block_operations.go @@ -257,7 +257,7 @@ func (sb *SavingBlockOperations) CheckTransactionByBlock(st *storage.LevelDBBack } for i, op := range bt.Transaction().B.Operations { - opHash := block.NewBlockOperationKey(op.MakeHashString(), hash) + opHash := block.NewBlockOperationKey(op.MakeHashString(), hash, uint64(i)) var exists bool if exists, err = block.ExistsBlockOperation(st, opHash); err != nil { From e59d84a60af8705c586e020efd6507af9aeb6131 Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 20 Nov 2018 22:40:52 +0900 Subject: [PATCH 05/25] Fix test failure of TestResourceAccount --- lib/node/runner/api/resource/resource_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node/runner/api/resource/resource_test.go b/lib/node/runner/api/resource/resource_test.go index 766db55de..af1ed64bd 100644 --- a/lib/node/runner/api/resource/resource_test.go +++ b/lib/node/runner/api/resource/resource_test.go @@ -73,7 +73,7 @@ func TestResourceAccount(t *testing.T) { // Operation { _, tx := transaction.TestMakeTransaction([]byte{0x00}, 1) - bt := block.NewBlockTransactionFromTransaction(common.GetUniqueIDFromUUID(), 0, common.NowISO8601(), tx, 0) + bt := block.NewBlockTransactionFromTransaction(common.GetUniqueIDFromUUID(), 0, common.NowISO8601(), tx, 1) bt.MustSave(storage) err := bt.SaveBlockOperations(storage) @@ -104,7 +104,7 @@ func TestResourceAccount(t *testing.T) { { var err error _, tx := transaction.TestMakeTransaction([]byte{0x00}, 3) - bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, common.NowISO8601(), tx, 0) + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, common.NowISO8601(), tx, 2) bt.MustSave(storage) err = bt.SaveBlockOperations(storage) require.NoError(t, err) From 111bb0c63a746f2b1f78e6d107985cb2073fe6a2 Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 01:41:35 +0900 Subject: [PATCH 06/25] Add storage.Index and BlockTransaction.BlockHeight --- lib/block/operation.go | 2 +- lib/block/transaction.go | 163 ++++++++++++++------ lib/common/util.go | 5 + lib/node/runner/api/resource/transaction.go | 1 + lib/storage/index.go | 37 +++++ 5 files changed, 156 insertions(+), 52 deletions(-) create mode 100644 lib/storage/index.go diff --git a/lib/block/operation.go b/lib/block/operation.go index 1eec5947b..a593436b1 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -43,7 +43,7 @@ func NewBlockOperationKey(opHash, txHash string, index uint64) string { return fmt.Sprintf("%s-%s-%d", opHash, txHash, index) } -func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, txIndex, opIndex int) (BlockOperation, error) { +func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, txIndex uint64, opIndex int) (BlockOperation, error) { body, err := json.Marshal(op.B) if err != nil { return BlockOperation{}, err diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 56bd64811..f86038303 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -2,7 +2,6 @@ package block import ( "encoding/json" - "fmt" "boscoin.io/sebak/lib/common" "boscoin.io/sebak/lib/errors" @@ -37,11 +36,11 @@ type BlockTransaction struct { Created string `json:"created"` Message []byte `json:"message"` - Index uint64 `json:"index"` + Index uint64 `json:"index"` + BlockHeight uint64 `json:"block_height"` transaction transaction.Transaction isSaved bool - blockHeight uint64 } func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, confirmed string, tx transaction.Transaction, index uint64) BlockTransaction { @@ -51,67 +50,114 @@ func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, co } return BlockTransaction{ - Hash: tx.H.Hash, - Block: blockHash, - SequenceID: tx.B.SequenceID, - Signature: tx.H.Signature, - Source: tx.B.Source, - Fee: tx.B.Fee, - Operations: opHashes, - Amount: tx.TotalAmount(true), - Confirmed: confirmed, - Created: tx.H.Created, - Index: index, + Hash: tx.H.Hash, + Block: blockHash, + SequenceID: tx.B.SequenceID, + Signature: tx.H.Signature, + Source: tx.B.Source, + Fee: tx.B.Fee, + Operations: opHashes, + Amount: tx.TotalAmount(true), + Confirmed: confirmed, + Created: tx.H.Created, + Index: index, + BlockHeight: blockHeight, transaction: tx, - blockHeight: blockHeight, } } func (bt BlockTransaction) NewBlockTransactionKeySource() string { - return fmt.Sprintf( - "%s%s%s%s", - GetBlockTransactionKeyPrefixSource(bt.Source), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.SequenceID), - common.EncodeUint64ToByteSlice(bt.Index), + idx := storage.NewIndex() + idx.WritePrefix(GetBlockTransactionKeyPrefixSource(bt.Source)) + idx.WriteOrder( + common.EncodeUint64ToString(bt.BlockHeight), + common.EncodeUint64ToString(bt.Index), + common.EncodeUint64ToString(bt.SequenceID), ) + return idx.String() + + /* + return fmt.Sprintf( + "%s%s%s%s", + GetBlockTransactionKeyPrefixSource(bt.Source), + common.EncodeUint64ToByteSlice(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.SequenceID), + common.EncodeUint64ToByteSlice(bt.Index), + ) + */ } func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { - return fmt.Sprintf( - "%s%s%s", - GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), + idx := storage.NewIndex() + idx.WritePrefix(GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed)) + idx.WriteOrder( + common.EncodeUint64ToString(bt.BlockHeight), + common.EncodeUint64ToString(bt.Index), ) + return idx.String() + /* + return fmt.Sprintf( + "%s%s%s", + GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed), + common.EncodeUint64ToByteSlice(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.Index), + ) + */ } func (bt BlockTransaction) NewBlockTransactionKeyHeight() string { - return fmt.Sprintf( - "%s%s", - GetBlockTransactionKeyPrefixHeight(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), + idx := storage.NewIndex() + idx.WritePrefix(GetBlockTransactionKeyPrefixHeight(bt.BlockHeight)) + idx.WriteOrder( + common.EncodeUint64ToString(bt.Index), ) + return idx.String() + /* + return fmt.Sprintf( + "%s%s", + GetBlockTransactionKeyPrefixHeight(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.Index), + ) + */ } func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string) string { - return fmt.Sprintf( - "%s%s%s%s", - GetBlockTransactionKeyPrefixAccount(accountAddress), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), + idx := storage.NewIndex() + idx.WritePrefix(GetBlockTransactionKeyPrefixAccount(accountAddress)) + idx.WriteOrder( + common.EncodeUint64ToString(bt.BlockHeight), + common.EncodeUint64ToString(bt.Index), common.GetUniqueIDFromUUID(), ) + return idx.String() + /* + return fmt.Sprintf( + "%s%s%s%s", + GetBlockTransactionKeyPrefixAccount(accountAddress), + common.EncodeUint64ToByteSlice(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.Index), + common.GetUniqueIDFromUUID(), + ) + */ } func (bt BlockTransaction) NewBlockTransactionKeyByBlock(hash string) string { - return fmt.Sprintf( - "%s%s%s", - GetBlockTransactionKeyPrefixBlock(hash), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), + idx := storage.NewIndex() + idx.WritePrefix(GetBlockTransactionKeyPrefixBlock(hash)) + idx.WriteOrder( + common.EncodeUint64ToString(bt.BlockHeight), + common.EncodeUint64ToString(bt.Index), ) + return idx.String() + /* + return fmt.Sprintf( + "%s%s%s", + GetBlockTransactionKeyPrefixBlock(hash), + common.EncodeUint64ToByteSlice(bt.blockHeight), + common.EncodeUint64ToByteSlice(bt.Index), + ) + */ } func (bt *BlockTransaction) Save(st *storage.LevelDBBackend) (err error) { @@ -183,12 +229,12 @@ func (bt *BlockTransaction) SaveBlockOperations(st *storage.LevelDBBackend) (err return errors.FailedToSaveBlockOperaton } - if bt.blockHeight < 1 { + if bt.BlockHeight < 1 { var blk Block if blk, err = GetBlock(st, bt.Block); err != nil { return } else { - bt.blockHeight = blk.Height + bt.BlockHeight = blk.Height } } @@ -202,17 +248,17 @@ func (bt *BlockTransaction) SaveBlockOperations(st *storage.LevelDBBackend) (err } func (bt *BlockTransaction) SaveBlockOperation(st *storage.LevelDBBackend, op operation.Operation, opIndex int) (err error) { - if bt.blockHeight < 1 { + if bt.BlockHeight < 1 { var blk Block if blk, err = GetBlock(st, bt.Block); err != nil { return } else { - bt.blockHeight = blk.Height + bt.BlockHeight = blk.Height } } var bo BlockOperation - bo, err = NewBlockOperationFromOperation(op, bt.Transaction(), bt.blockHeight, bt.Index, opIndex) + bo, err = NewBlockOperationFromOperation(op, bt.Transaction(), bt.BlockHeight, bt.Index, opIndex) if err != nil { return } @@ -244,27 +290,42 @@ func (bt *BlockTransaction) GetOperationIndex(opHash string) (opIndex int, err e } func GetBlockTransactionKeyPrefixSource(source string) string { - return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixSource, source) + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockTransactionPrefixSource, source).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixSource, source) } func GetBlockTransactionKeyPrefixConfirmed(confirmed string) string { - return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixConfirmed, confirmed) + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockTransactionPrefixConfirmed, confirmed).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixConfirmed, confirmed) } func GetBlockTransactionKeyPrefixAccount(accountAddress string) string { - return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixAccount, accountAddress) + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockTransactionPrefixAccount, accountAddress).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixAccount, accountAddress) } func GetBlockTransactionKeyPrefixBlock(hash string) string { - return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixBlock, hash) + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockTransactionPrefixBlock, hash).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixBlock, hash) } func GetBlockTransactionKey(hash string) string { - return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHash, hash) + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockTransactionPrefixHash, hash).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHash, hash) } func GetBlockTransactionKeyPrefixHeight(height uint64) string { - return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHeight, common.EncodeUint64ToByteSlice(height)) + idx := storage.NewIndex() + return idx.WritePrefix( + common.BlockTransactionPrefixHeight, + common.EncodeUint64ToString(height), + ).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHeight, common.EncodeUint64ToByteSlice(height)) } func GetBlockTransaction(st *storage.LevelDBBackend, hash string) (bt BlockTransaction, err error) { diff --git a/lib/common/util.go b/lib/common/util.go index 8174c182a..888de2ad6 100644 --- a/lib/common/util.go +++ b/lib/common/util.go @@ -160,6 +160,11 @@ func EncodeUint64ToByteSlice(i uint64) [MaxUintEncodeByte]byte { return b } +func EncodeUint64ToString(i uint64) string { + bs := EncodeUint64ToByteSlice(i) + return string(bs[:]) +} + type KV struct { Key string Value uint64 diff --git a/lib/node/runner/api/resource/transaction.go b/lib/node/runner/api/resource/transaction.go index ec3e93879..bb183edd0 100644 --- a/lib/node/runner/api/resource/transaction.go +++ b/lib/node/runner/api/resource/transaction.go @@ -28,6 +28,7 @@ func (t Transaction) GetMap() hal.Entry { "created": t.bt.Created, "operation_count": len(t.bt.Operations), "index": t.bt.Index, + "block_height": t.bt.BlockHeight, } } func (t Transaction) Resource() *hal.Resource { diff --git a/lib/storage/index.go b/lib/storage/index.go new file mode 100644 index 000000000..cb6852a6f --- /dev/null +++ b/lib/storage/index.go @@ -0,0 +1,37 @@ +package storage + +import "strings" + +const ( + IndexPrefixOrderDelimiter = "/" + IndexElementDelimiter = "-" +) + +type Index struct { + prefix string + order string +} + +func NewIndex() *Index { + idx := &Index{} + return idx +} + +func (idx Index) Bytes() []byte { + return []byte(idx.String()) +} + +func (idx Index) String() string { + index := strings.Join([]string{idx.prefix, idx.order}, IndexPrefixOrderDelimiter) + return index +} + +func (idx *Index) WritePrefix(ss ...string) *Index { + idx.prefix = strings.Join(ss, IndexElementDelimiter) + return idx +} + +func (idx *Index) WriteOrder(ss ...string) *Index { + idx.order = strings.Join(ss, IndexElementDelimiter) + return idx +} From fd267e93f3ca628697ce0a43b1962f9dd971db26 Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 13:31:18 +0900 Subject: [PATCH 07/25] Add BlockOrder... --- lib/block/order.go | 49 ++++++++++++++++++++++++++++++++++++++++ lib/block/transaction.go | 40 ++++++++++++-------------------- lib/storage/index.go | 21 ++++++++++++----- 3 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 lib/block/order.go diff --git a/lib/block/order.go b/lib/block/order.go new file mode 100644 index 000000000..46542551d --- /dev/null +++ b/lib/block/order.go @@ -0,0 +1,49 @@ +package block + +import ( + "strconv" + "strings" + + "boscoin.io/sebak/lib/common" + "boscoin.io/sebak/lib/storage" +) + +type BlockOrder struct { + parts []uint64 // height,txindex,opindex +} + +func NewBlockOrder(height uint64) *BlockOrder { + b := &BlockOrder{ + parts: []uint64{height}, + } + return b +} + +func NewBlockTxOrder(height, index uint64) *BlockOrder { + b := &BlockOrder{ + parts: []uint64{height, index}, + } + return b +} + +func NewBlockOpOrder(height, txindex, index uint64) *BlockOrder { + b := &BlockOrder{ + parts: []uint64{height, txindex, index}, + } + return b +} + +func (o *BlockOrder) Index(idx *storage.Index) string { + for _, x := range o.parts { + idx.WriteOrder(common.EncodeUint64ToString(x)) + } + return idx.String() +} + +func (o *BlockOrder) String() string { + var ss []string + for _, p := range o.parts { + ss = append(ss, strconv.FormatUint(p, 10)) + } + return strings.Join(ss, "-") +} diff --git a/lib/block/transaction.go b/lib/block/transaction.go index f86038303..07c49d8ae 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -41,6 +41,7 @@ type BlockTransaction struct { transaction transaction.Transaction isSaved bool + order *BlockOrder } func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, confirmed string, tx transaction.Transaction, index uint64) BlockTransaction { @@ -49,6 +50,8 @@ func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, co opHashes = append(opHashes, NewBlockOperationKey(op.MakeHashString(), tx.GetHash(), uint64(i))) } + order := NewBlockTxOrder(blockHeight, index) + return BlockTransaction{ Hash: tx.H.Hash, Block: blockHash, @@ -64,17 +67,14 @@ func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, co BlockHeight: blockHeight, transaction: tx, + order: order, } } func (bt BlockTransaction) NewBlockTransactionKeySource() string { idx := storage.NewIndex() idx.WritePrefix(GetBlockTransactionKeyPrefixSource(bt.Source)) - idx.WriteOrder( - common.EncodeUint64ToString(bt.BlockHeight), - common.EncodeUint64ToString(bt.Index), - common.EncodeUint64ToString(bt.SequenceID), - ) + bt.order.Index(idx) return idx.String() /* @@ -91,10 +91,7 @@ func (bt BlockTransaction) NewBlockTransactionKeySource() string { func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { idx := storage.NewIndex() idx.WritePrefix(GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed)) - idx.WriteOrder( - common.EncodeUint64ToString(bt.BlockHeight), - common.EncodeUint64ToString(bt.Index), - ) + bt.order.Index(idx) return idx.String() /* return fmt.Sprintf( @@ -108,10 +105,8 @@ func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { func (bt BlockTransaction) NewBlockTransactionKeyHeight() string { idx := storage.NewIndex() - idx.WritePrefix(GetBlockTransactionKeyPrefixHeight(bt.BlockHeight)) - idx.WriteOrder( - common.EncodeUint64ToString(bt.Index), - ) + idx.WritePrefix(common.BlockTransactionPrefixHeight) + bt.order.Index(idx) return idx.String() /* return fmt.Sprintf( @@ -125,11 +120,8 @@ func (bt BlockTransaction) NewBlockTransactionKeyHeight() string { func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string) string { idx := storage.NewIndex() idx.WritePrefix(GetBlockTransactionKeyPrefixAccount(accountAddress)) - idx.WriteOrder( - common.EncodeUint64ToString(bt.BlockHeight), - common.EncodeUint64ToString(bt.Index), - common.GetUniqueIDFromUUID(), - ) + bt.order.Index(idx) + idx.WriteOrder(common.GetUniqueIDFromUUID()) return idx.String() /* return fmt.Sprintf( @@ -145,10 +137,7 @@ func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string func (bt BlockTransaction) NewBlockTransactionKeyByBlock(hash string) string { idx := storage.NewIndex() idx.WritePrefix(GetBlockTransactionKeyPrefixBlock(hash)) - idx.WriteOrder( - common.EncodeUint64ToString(bt.BlockHeight), - common.EncodeUint64ToString(bt.Index), - ) + bt.order.Index(idx) return idx.String() /* return fmt.Sprintf( @@ -321,10 +310,10 @@ func GetBlockTransactionKey(hash string) string { func GetBlockTransactionKeyPrefixHeight(height uint64) string { idx := storage.NewIndex() - return idx.WritePrefix( + idx.WritePrefix( common.BlockTransactionPrefixHeight, - common.EncodeUint64ToString(height), - ).String() + ) + return idx.WriteOrder(common.EncodeUint64ToString(height)).String() //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHeight, common.EncodeUint64ToByteSlice(height)) } @@ -334,6 +323,7 @@ func GetBlockTransaction(st *storage.LevelDBBackend, hash string) (bt BlockTrans } bt.isSaved = true + bt.order = NewBlockTxOrder(bt.BlockHeight, bt.Index) return } diff --git a/lib/storage/index.go b/lib/storage/index.go index cb6852a6f..def388fb0 100644 --- a/lib/storage/index.go +++ b/lib/storage/index.go @@ -8,12 +8,15 @@ const ( ) type Index struct { - prefix string - order string + prefix []string + order []string } func NewIndex() *Index { - idx := &Index{} + idx := &Index{ + prefix: make([]string, 0, 0), + order: make([]string, 0, 0), + } return idx } @@ -22,16 +25,22 @@ func (idx Index) Bytes() []byte { } func (idx Index) String() string { - index := strings.Join([]string{idx.prefix, idx.order}, IndexPrefixOrderDelimiter) + prefix := strings.Join(idx.prefix, IndexElementDelimiter) + order := strings.Join(idx.order, IndexElementDelimiter) + index := strings.Join([]string{prefix, order}, IndexPrefixOrderDelimiter) return index } func (idx *Index) WritePrefix(ss ...string) *Index { - idx.prefix = strings.Join(ss, IndexElementDelimiter) + for _, s := range ss { + idx.prefix = append(idx.prefix, s) + } return idx } func (idx *Index) WriteOrder(ss ...string) *Index { - idx.order = strings.Join(ss, IndexElementDelimiter) + for _, s := range ss { + idx.order = append(idx.order, s) + } return idx } From 7fa090f5333ead8986304ae981aa325c5fa0b49b Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 15:05:56 +0900 Subject: [PATCH 08/25] Add PageCursor --- lib/block/operation.go | 124 ++++++++++++++++------------- lib/block/transaction.go | 4 + lib/node/runner/api/pagecursor.go | 48 +++++++++++ lib/node/runner/api/pagequery.go | 9 +++ lib/node/runner/api/transaction.go | 23 ++++-- 5 files changed, 146 insertions(+), 62 deletions(-) create mode 100644 lib/node/runner/api/pagecursor.go diff --git a/lib/block/operation.go b/lib/block/operation.go index a593436b1..028e04177 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -37,6 +37,7 @@ type BlockOperation struct { operation operation.Operation linked string isSaved bool + order *BlockOrder } func NewBlockOperationKey(opHash, txHash string, index uint64) string { @@ -63,6 +64,7 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans linked = createAccount.Linked } } + order := NewBlockOpOrder(blockHeight, txIndex, index) return BlockOperation{ Hash: NewBlockOperationKey(opHash, txHash, uint64(opIndex)), @@ -81,6 +83,7 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans transaction: tx, operation: op, linked: linked, + order: order, }, nil } @@ -164,96 +167,104 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) { return nil } -func key(hash string) string { - return fmt.Sprintf("%s%s", common.BlockOperationPrefixHash, hash) +func GetBlockOperationKey(hash string) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixHash, hash) + return idx.String() } func GetBlockOperationCreateFrozenKey(hash string, height uint64) string { - return fmt.Sprintf( - "%s%s%s", - common.BlockOperationPrefixCreateFrozen, - common.EncodeUint64ToByteSlice(height), - hash, - ) + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixCreateFrozen) + idx.WritePrefix(common.EncodeUint64ToString(height)) + idx.WritePrefix(hash) + return idx.String() } -func keyPrefixFrozenLinked(hash string) string { - return fmt.Sprintf( - "%s%s", - common.BlockOperationPrefixFrozenLinked, - hash, - ) +func GetBlockOperationKeyPrefixFrozenLinked(hash string) string { + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockOperationPrefixFrozenLinked, hash).String() } -func keyPrefixTxHash(txHash string) string { - return fmt.Sprintf("%s%s-", common.BlockOperationPrefixTxHash, txHash) +func GetBlockOperationKeyPrefixTxHash(txHash string) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixTxHash, txHash) + return idx.String() } -func keyPrefixSource(source string) string { - return fmt.Sprintf("%s%s-", common.BlockOperationPrefixSource, source) +func GetBlockOperationKeyPrefixSource(source string) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixSource, source) + return idx.String() } -func keyPrefixSourceAndType(source string, ty operation.OperationType) string { - return fmt.Sprintf("%s%s%s-", common.BlockOperationPrefixTypeSource, string(ty), source) +func GetBlockOperationKeyPrefixSourceAndType(source string, ty operation.OperationType) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixSource, string(ty), source) + return idx.String() } -func keyPrefixBlockHeight(height uint64) string { - return fmt.Sprintf("%s%s-", common.BlockOperationPrefixBlockHeight, common.EncodeUint64ToByteSlice(height)) +func GetBlockOperationKeyPrefixBlockHeight(height uint64) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixBlockHeight) + idx.WritePrefix(common.EncodeUint64ToString(height)) + return idx.String() } -func keyPrefixTarget(target string) string { - return fmt.Sprintf("%s%s-", common.BlockOperationPrefixTarget, target) +func GetBlockOperationKeyPrefixTarget(target string) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixTarget, target) + return idx.String() } -func keyPrefixTargetAndType(target string, ty operation.OperationType) string { - return fmt.Sprintf("%s%s%s-", common.BlockOperationPrefixTypeTarget, string(ty), target) +func GetBlockOperationKeyPrefixTargetAndType(target string, ty operation.OperationType) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixTypeTarget) + idx.WritePrefix(string(ty), target) + return idx.String() } -func keyPrefixPeers(addr string) string { - return fmt.Sprintf("%s%s-", common.BlockOperationPrefixPeers, addr) +func GetBlockOperationKeyPrefixPeers(addr string) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixPeers, addr) + return idx.String() } -func keyPrefixPeersAndType(addr string, ty operation.OperationType) string { - return fmt.Sprintf("%s%s%s-", common.BlockOperationPrefixTypePeers, string(ty), addr) +func GetBlockOperationKeyPrefixPeersAndType(addr string, ty operation.OperationType) string { + idx := storage.NewIndex() + idx.WritePrefix(common.BlockOperationPrefixTypePeers) + idx.WritePrefix(string(ty), addr) + return idx.String() } func (bo BlockOperation) NewBlockOperationTxHashKey() string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixTxHash(bo.TxHash), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.TxIndex), - common.EncodeUint64ToByteSlice(bo.Index), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixTxHash(bo.TxHash)) + bo.order.Index(idx) + return idx.String() } func (bo BlockOperation) NewBlockOperationSourceKey() string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixSource(bo.Source), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.TxIndex), - common.EncodeUint64ToByteSlice(bo.Index), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixSource(bo.Source)) + bo.order.Index(idx) + return idx.String() } func (bo BlockOperation) NewBlockOperationFrozenLinkedKey(hash string) string { - return fmt.Sprintf( - "%s%s", - keyPrefixFrozenLinked(hash), - common.EncodeUint64ToByteSlice(bo.Height), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixFrozenLinked(hash)) + bo.order.Index(idx) + return idx.String() } func (bo BlockOperation) NewBlockOperationSourceAndTypeKey() string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixSourceAndType(bo.Source, bo.Type), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.TxIndex), - common.EncodeUint64ToByteSlice(bo.Index), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixSourceAndType(bo.Source, bo.Type)) + bo.order.Index(idx) + return idx.String() } + func (bo BlockOperation) NewBlockOperationTargetKey(target string) string { return fmt.Sprintf( "%s%s%s%s", @@ -312,6 +323,7 @@ func GetBlockOperation(st *storage.LevelDBBackend, hash string) (bo BlockOperati } bo.isSaved = true + bo.order = NewBlockOpOrder(bo.Height, bo.TxIndex, bo.Index) return } diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 07c49d8ae..b15cb5437 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -278,6 +278,10 @@ func (bt *BlockTransaction) GetOperationIndex(opHash string) (opIndex int, err e return } +func (bt BlockTransaction) BlockOrder() *BlockOrder { + return bt.order +} + func GetBlockTransactionKeyPrefixSource(source string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixSource, source).String() diff --git a/lib/node/runner/api/pagecursor.go b/lib/node/runner/api/pagecursor.go new file mode 100644 index 000000000..cb4b7fe9c --- /dev/null +++ b/lib/node/runner/api/pagecursor.go @@ -0,0 +1,48 @@ +package api + +import ( + "strconv" + "strings" + + "boscoin.io/sebak/lib/common" + "boscoin.io/sebak/lib/storage" +) + +type PageCursor struct { + input string + prefix string + index *storage.Index +} + +func NewPageCursor(input []byte, prefix string) *PageCursor { + p := &PageCursor{ + input: strings.TrimSpace(string(input)), + prefix: prefix, + index: storage.NewIndex(), + } + + return p +} + +func (p *PageCursor) IndexKey() ([]byte, error) { + p.index.WritePrefix(p.prefix) + err := p.indexOrder() + return p.index.Bytes(), err +} + +func (p *PageCursor) indexOrder() error { + parts := strings.Split(p.input, "-") + for i, part := range parts { + if part == "" { + continue + } + if i < 3 { + partInt, err := strconv.ParseUint(part, 10, 64) + if err != nil { + return err + } + p.index.WriteOrder(common.EncodeUint64ToString(partInt)) + } + } + return nil +} diff --git a/lib/node/runner/api/pagequery.go b/lib/node/runner/api/pagequery.go index d891fff70..964509ed6 100644 --- a/lib/node/runner/api/pagequery.go +++ b/lib/node/runner/api/pagequery.go @@ -88,6 +88,15 @@ func (p *PageQuery) ListOptions() storage.ListOptions { return storage.NewDefaultListOptions(p.Reverse(), p.Cursor(), p.Limit()) } +func (p *PageQuery) PageCursorListOptions(prefix string) (storage.ListOptions, error) { + c := NewPageCursor(p.Cursor(), prefix) + indexCursor, err := c.IndexKey() + if err != nil { + return nil, err + } + return storage.NewDefaultListOptions(p.Reverse(), indexCursor, p.Limit()), nil +} + func (p *PageQuery) WalkOption() *storage.WalkOption { return storage.NewWalkOption(string(p.Cursor()), p.Limit(), p.Reverse()) } diff --git a/lib/node/runner/api/transaction.go b/lib/node/runner/api/transaction.go index 3364bdff6..88aaf4ec5 100644 --- a/lib/node/runner/api/transaction.go +++ b/lib/node/runner/api/transaction.go @@ -9,6 +9,7 @@ import ( "boscoin.io/sebak/lib/block" o "boscoin.io/sebak/lib/common/observer" + "boscoin.io/sebak/lib/common" "boscoin.io/sebak/lib/errors" "boscoin.io/sebak/lib/network/httputils" "boscoin.io/sebak/lib/node/runner/api/resource" @@ -21,9 +22,18 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht return } - var options = p.ListOptions() - var firstCursor []byte - var cursor []byte + options, err := p.PageCursorListOptions(common.BlockTransactionPrefixHeight) + if err != nil { + //TODO: more correct err for it + httputils.WriteJSONError(w, err) + return + } + + var ( + prevCursor string + nextCursor + ) + readFunc := func() []resource.Resource { var txs []resource.Resource iterFunc, closeFunc := block.GetBlockTransactions(api.storage, options) @@ -33,9 +43,10 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht break } cursor = append([]byte{}, c...) - if len(firstCursor) == 0 { - firstCursor = append(firstCursor, c...) + if prevCursor == "" { + prevCursor = t.BlockOrder().String() } + cursor = t.BlockOrder().String() txs = append(txs, resource.NewTransaction(&t)) } closeFunc() @@ -44,7 +55,7 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht txs := readFunc() - list := p.ResourceList(txs, firstCursor, cursor) + list := p.ResourceList(txs, prevCursor, nextCursor) httputils.MustWriteJSON(w, 200, list) } From d37390d07552e7da0eec257875e8c89221b68f36 Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 17:59:18 +0900 Subject: [PATCH 09/25] Add BlockOrder.Prev and Next..(not finished) --- lib/block/order.go | 47 ++++++++++++++++++++++++++++-- lib/block/transaction.go | 12 +++++--- lib/common/prefix.go | 2 +- lib/node/runner/api/pagequery.go | 11 +++++++ lib/node/runner/api/transaction.go | 18 +++++------- lib/storage/index.go | 4 ++- 6 files changed, 76 insertions(+), 18 deletions(-) diff --git a/lib/block/order.go b/lib/block/order.go index 46542551d..8bbf85e92 100644 --- a/lib/block/order.go +++ b/lib/block/order.go @@ -40,10 +40,53 @@ func (o *BlockOrder) Index(idx *storage.Index) string { return idx.String() } -func (o *BlockOrder) String() string { +func (o *BlockOrder) formatText(xs []uint64) string { var ss []string - for _, p := range o.parts { + for _, p := range xs { ss = append(ss, strconv.FormatUint(p, 10)) } return strings.Join(ss, "-") } + +func (o *BlockOrder) String() string { + return o.formatText(o.parts) +} + +func (o *BlockOrder) NextString() string { + return o.formatText(o.NextOrder()) +} + +func (o *BlockOrder) PrevString() string { + return o.formatText(o.PrevOrder()) +} + +func (o *BlockOrder) NextOrder() (xs []uint64) { + if o == nil || len(o.parts) <= 0 { + return + } + for i, x := range o.parts { + if i == len(xs)-1 { + x++ + } + xs = append(xs, x) + } + return +} + +func (o *BlockOrder) PrevOrder() []uint64 { + if o == nil || len(o.parts) <= 0 { + return []uint64{} + } + return blockOrderPrevOrder(o.parts, len(o.parts)-1) +} + +func blockOrderPrevOrder(xs []uint64, pos int) []uint64 { + if pos == 0 || len(xs)-1 < pos { + return xs + } + if xs[pos] > 0 { + xs[pos]-- + return xs + } + return blockOrderPrevOrder(xs, pos-1) +} diff --git a/lib/block/transaction.go b/lib/block/transaction.go index b15cb5437..a3aefa7dd 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -103,9 +103,9 @@ func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { */ } -func (bt BlockTransaction) NewBlockTransactionKeyHeight() string { +func (bt BlockTransaction) NewBlockTransactionKeyAll() string { idx := storage.NewIndex() - idx.WritePrefix(common.BlockTransactionPrefixHeight) + idx.WritePrefix(common.BlockTransactionPrefixAll) bt.order.Index(idx) return idx.String() /* @@ -167,7 +167,7 @@ func (bt *BlockTransaction) Save(st *storage.LevelDBBackend) (err error) { if err = st.New(GetBlockTransactionKey(bt.Hash), bt); err != nil { return } - if err = st.New(bt.NewBlockTransactionKeyHeight(), bt.Hash); err != nil { + if err = st.New(bt.NewBlockTransactionKeyAll(), bt.Hash); err != nil { return } if err = st.New(bt.NewBlockTransactionKeySource(), bt.Hash); err != nil { @@ -312,6 +312,7 @@ func GetBlockTransactionKey(hash string) string { //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHash, hash) } +/* func GetBlockTransactionKeyPrefixHeight(height uint64) string { idx := storage.NewIndex() idx.WritePrefix( @@ -320,6 +321,7 @@ func GetBlockTransactionKeyPrefixHeight(height uint64) string { return idx.WriteOrder(common.EncodeUint64ToString(height)).String() //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHeight, common.EncodeUint64ToByteSlice(height)) } +*/ func GetBlockTransaction(st *storage.LevelDBBackend, hash string) (bt BlockTransaction, err error) { if err = st.Get(GetBlockTransactionKey(hash), &bt); err != nil { @@ -398,6 +400,7 @@ func GetBlockTransactionsByBlock(st *storage.LevelDBBackend, hash string, option return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) } +/* func GetBlockTransactionsByHeight(st *storage.LevelDBBackend, height uint64, options storage.ListOptions) ( func() (BlockTransaction, bool, []byte), func(), @@ -405,11 +408,12 @@ func GetBlockTransactionsByHeight(st *storage.LevelDBBackend, height uint64, opt iterFunc, closeFunc := st.GetIterator(GetBlockTransactionKeyPrefixHeight(height), options) return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) } +*/ func GetBlockTransactions(st *storage.LevelDBBackend, options storage.ListOptions) ( func() (BlockTransaction, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(common.BlockTransactionPrefixHeight, options) + iterFunc, closeFunc := st.GetIterator(common.BlockTransactionPrefixAll, options) return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) } diff --git a/lib/common/prefix.go b/lib/common/prefix.go index 55f0feeb5..b3a38059a 100644 --- a/lib/common/prefix.go +++ b/lib/common/prefix.go @@ -10,7 +10,7 @@ const ( BlockTransactionPrefixConfirmed = string(0x12) BlockTransactionPrefixAccount = string(0x13) BlockTransactionPrefixBlock = string(0x14) - BlockTransactionPrefixHeight = string(0x15) + BlockTransactionPrefixAll = string(0x15) BlockOperationPrefixHash = string(0x20) BlockOperationPrefixTxHash = string(0x21) BlockOperationPrefixSource = string(0x22) diff --git a/lib/node/runner/api/pagequery.go b/lib/node/runner/api/pagequery.go index 964509ed6..3253e2ddd 100644 --- a/lib/node/runner/api/pagequery.go +++ b/lib/node/runner/api/pagequery.go @@ -7,6 +7,7 @@ import ( "net/url" "strconv" + "boscoin.io/sebak/lib/block" "boscoin.io/sebak/lib/common" "boscoin.io/sebak/lib/errors" "boscoin.io/sebak/lib/node/runner/api/resource" @@ -109,6 +110,16 @@ func (p *PageQuery) ResourceList(rs []resource.Resource, firstCursor, lastCursor } } +func (p *PageQuery) ResourceListWithOrder(rs []resource.Resource, order *block.BlockOrder) *resource.ResourceList { + var cursor []byte + if p.reverse == false { + cursor = []byte(order.NextString()) + } else { + cursor = []byte(order.PrevString()) + } + return p.ResourceList(rs, cursor) +} + func (p *PageQuery) parseRequest() error { q := p.request.URL.Query() r := q.Get("reverse") diff --git a/lib/node/runner/api/transaction.go b/lib/node/runner/api/transaction.go index 88aaf4ec5..f9f51f7d0 100644 --- a/lib/node/runner/api/transaction.go +++ b/lib/node/runner/api/transaction.go @@ -8,8 +8,8 @@ import ( "github.com/gorilla/mux" "boscoin.io/sebak/lib/block" - o "boscoin.io/sebak/lib/common/observer" "boscoin.io/sebak/lib/common" + o "boscoin.io/sebak/lib/common/observer" "boscoin.io/sebak/lib/errors" "boscoin.io/sebak/lib/network/httputils" "boscoin.io/sebak/lib/node/runner/api/resource" @@ -22,7 +22,7 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht return } - options, err := p.PageCursorListOptions(common.BlockTransactionPrefixHeight) + options, err := p.PageCursorListOptions(common.BlockTransactionPrefixAll) if err != nil { //TODO: more correct err for it httputils.WriteJSONError(w, err) @@ -30,8 +30,8 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht } var ( - prevCursor string - nextCursor + prevOrder *block.BlockOrder + nextOrder *block.BlockOrder ) readFunc := func() []resource.Resource { @@ -42,11 +42,10 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht if !hasNext { break } - cursor = append([]byte{}, c...) - if prevCursor == "" { - prevCursor = t.BlockOrder().String() + if prevOrder == nil { + prevOrder = t.BlockOrder() } - cursor = t.BlockOrder().String() + nextOrder = t.BlockOrder() txs = append(txs, resource.NewTransaction(&t)) } closeFunc() @@ -54,8 +53,7 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht } txs := readFunc() - - list := p.ResourceList(txs, prevCursor, nextCursor) + list := p.ResourceListWithOrder(txs, prevOrder, nextOrder) httputils.MustWriteJSON(w, 200, list) } diff --git a/lib/storage/index.go b/lib/storage/index.go index def388fb0..369717093 100644 --- a/lib/storage/index.go +++ b/lib/storage/index.go @@ -1,6 +1,8 @@ package storage -import "strings" +import ( + "strings" +) const ( IndexPrefixOrderDelimiter = "/" From edc7242bb7e879d724eefbce95f074f536698ffa Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 19:39:02 +0900 Subject: [PATCH 10/25] Remove Prev,NextOrder in BlockOrder and change operation handelrs --- lib/block/operation.go | 60 ++++++++++++++++++++++++---- lib/block/order.go | 39 ------------------ lib/node/runner/api/operation.go | 37 ++++++++++++----- lib/node/runner/api/pagequery.go | 7 +--- lib/node/runner/api/transaction.go | 25 +++++++----- lib/node/runner/api/tx_operations.go | 28 ++++++++----- 6 files changed, 113 insertions(+), 83 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index 028e04177..5b9ca6d2c 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -64,7 +64,7 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans linked = createAccount.Linked } } - order := NewBlockOpOrder(blockHeight, txIndex, index) + order := NewBlockOpOrder(blockHeight, txIndex, uint64(opIndex)) return BlockOperation{ Hash: NewBlockOperationKey(opHash, txHash, uint64(opIndex)), @@ -167,6 +167,50 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) { return nil } +func key(hash string) string { + return fmt.Sprintf("%s%s", common.BlockOperationPrefixHash, hash) +} + +func keyPrefixFrozenLinked(hash string) string { + return fmt.Sprintf( + "%s%s", + common.BlockOperationPrefixFrozenLinked, + hash, + ) +} + +func keyPrefixTxHash(txHash string) string { + return fmt.Sprintf("%s%s-", common.BlockOperationPrefixTxHash, txHash) +} + +func keyPrefixSource(source string) string { + return fmt.Sprintf("%s%s-", common.BlockOperationPrefixSource, source) +} + +func keyPrefixSourceAndType(source string, ty operation.OperationType) string { + return fmt.Sprintf("%s%s%s-", common.BlockOperationPrefixTypeSource, string(ty), source) +} + +func keyPrefixBlockHeight(height uint64) string { + return fmt.Sprintf("%s%s-", common.BlockOperationPrefixBlockHeight, common.EncodeUint64ToByteSlice(height)) +} + +func keyPrefixTarget(target string) string { + return fmt.Sprintf("%s%s-", common.BlockOperationPrefixTarget, target) +} + +func keyPrefixTargetAndType(target string, ty operation.OperationType) string { + return fmt.Sprintf("%s%s%s-", common.BlockOperationPrefixTypeTarget, string(ty), target) +} + +func keyPrefixPeers(addr string) string { + return fmt.Sprintf("%s%s-", common.BlockOperationPrefixPeers, addr) +} + +func keyPrefixPeersAndType(addr string, ty operation.OperationType) string { + return fmt.Sprintf("%s%s%s-", common.BlockOperationPrefixTypePeers, string(ty), addr) +} + func GetBlockOperationKey(hash string) string { idx := storage.NewIndex() idx.WritePrefix(common.BlockOperationPrefixHash, hash) @@ -305,12 +349,14 @@ func (bo BlockOperation) NewBlockOperationPeersAndTypeKey(addr string) string { ) } func (bo BlockOperation) NewBlockOperationBlockHeightKey() string { - return fmt.Sprintf( - "%s%s%s", - keyPrefixBlockHeight(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixBlockHeight(bo.Height)) + bo.order.Index(idx) + return idx.String() +} + +func (bo BlockOperation) BlockOrder() *BlockOrder { + return bo.order } func ExistsBlockOperation(st *storage.LevelDBBackend, hash string) (bool, error) { diff --git a/lib/block/order.go b/lib/block/order.go index 8bbf85e92..bf4b0f98d 100644 --- a/lib/block/order.go +++ b/lib/block/order.go @@ -51,42 +51,3 @@ func (o *BlockOrder) formatText(xs []uint64) string { func (o *BlockOrder) String() string { return o.formatText(o.parts) } - -func (o *BlockOrder) NextString() string { - return o.formatText(o.NextOrder()) -} - -func (o *BlockOrder) PrevString() string { - return o.formatText(o.PrevOrder()) -} - -func (o *BlockOrder) NextOrder() (xs []uint64) { - if o == nil || len(o.parts) <= 0 { - return - } - for i, x := range o.parts { - if i == len(xs)-1 { - x++ - } - xs = append(xs, x) - } - return -} - -func (o *BlockOrder) PrevOrder() []uint64 { - if o == nil || len(o.parts) <= 0 { - return []uint64{} - } - return blockOrderPrevOrder(o.parts, len(o.parts)-1) -} - -func blockOrderPrevOrder(xs []uint64, pos int) []uint64 { - if pos == 0 || len(xs)-1 < pos { - return xs - } - if xs[pos] > 0 { - xs[pos]-- - return xs - } - return blockOrderPrevOrder(xs, pos-1) -} diff --git a/lib/node/runner/api/operation.go b/lib/node/runner/api/operation.go index 718ba7056..4aa3aedb4 100644 --- a/lib/node/runner/api/operation.go +++ b/lib/node/runner/api/operation.go @@ -59,8 +59,6 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter return } - options := p.ListOptions() - oTypeStr := r.URL.Query().Get("type") if len(oTypeStr) > 0 && !operation.IsValidOperationType(oTypeStr) { httputils.WriteJSONError(w, errors.InvalidQueryString) @@ -75,12 +73,27 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter return } - var txs []resource.Resource blockCache := map[ /* block.Height */ uint64]*block.Block{} oType := operation.OperationType(oTypeStr) - var firstCursor []byte - var lastCursor []byte - { + + prefix := block.GetBlockOperationKeyPrefixSourceAndType(address, oType) + if len(oType) > 0 { + prefix = block.GetBlockOperationKeyPrefixSource(address) + } + + options, err := p.PageCursorListOptions(prefix) + if err != nil { + httputils.WriteJSONError(w, err) + return + } + + var ( + pOrder *block.BlockOrder + nOrder *block.BlockOrder + ) + + readFunc := func() []resource.Resource { + var txs []resource.Resource var iterFunc func() (block.BlockOperation, bool, []byte) var closeFunc func() @@ -90,14 +103,14 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter iterFunc, closeFunc = block.GetBlockOperationsByPeers(api.storage, address, options) } for { - t, hasNext, c := iterFunc() + t, hasNext, _ := iterFunc() if !hasNext { break } - if len(firstCursor) == 0 { - firstCursor = append(firstCursor, c...) + if pOrder == nil { + pOrder = t.BlockOrder() } - lastCursor = append([]byte{}, c...) + nOrder = t.BlockOrder() var blk *block.Block var ok bool @@ -123,10 +136,12 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter r := resource.NewOperation(&t, opIndex) r.Block = blk txs = append(txs, r) + order = t.BlockOrder() } closeFunc() } - list := p.ResourceList(txs, firstCursor, lastCursor) + txs := readFunc() + list := p.ResourceListWithOrder(txs, pOrder, nOrder) httputils.MustWriteJSON(w, 200, list) } diff --git a/lib/node/runner/api/pagequery.go b/lib/node/runner/api/pagequery.go index 3253e2ddd..d4360bd7f 100644 --- a/lib/node/runner/api/pagequery.go +++ b/lib/node/runner/api/pagequery.go @@ -111,12 +111,7 @@ func (p *PageQuery) ResourceList(rs []resource.Resource, firstCursor, lastCursor } func (p *PageQuery) ResourceListWithOrder(rs []resource.Resource, order *block.BlockOrder) *resource.ResourceList { - var cursor []byte - if p.reverse == false { - cursor = []byte(order.NextString()) - } else { - cursor = []byte(order.PrevString()) - } + cursor := []byte(order.String()) return p.ResourceList(rs, cursor) } diff --git a/lib/node/runner/api/transaction.go b/lib/node/runner/api/transaction.go index f9f51f7d0..25792cb23 100644 --- a/lib/node/runner/api/transaction.go +++ b/lib/node/runner/api/transaction.go @@ -95,22 +95,27 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit return } - var options = p.ListOptions() - var firstCursor []byte - var cursor []byte + options, err := p.PageCursorListOptions(block.GetBlockTransactionKeyPrefixAccount(address)) + if err != nil { + httputils.WriteJSONError(w, err) + return + } + var ( + pOrder *block.BlockOrder + nOrder + ) readFunc := func() []resource.Resource { var txs []resource.Resource iterFunc, closeFunc := block.GetBlockTransactionsByAccount(api.storage, address, options) for { - t, hasNext, c := iterFunc() + t, hasNext, _ := iterFunc() if !hasNext { break } - cursor = append([]byte{}, c...) - if len(firstCursor) == 0 { - firstCursor = append(firstCursor, c...) - } - + if pOrder == nil { + pOrder = t.BlockOrder() + } + nOrder = t.BlockOrder() txs = append(txs, resource.NewTransaction(&t)) } closeFunc() @@ -118,7 +123,7 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit } txs := readFunc() - list := p.ResourceList(txs, firstCursor, cursor) + list := p.ResourceListWithOrder(txs, pOrder,nOrder) httputils.MustWriteJSON(w, 200, list) } diff --git a/lib/node/runner/api/tx_operations.go b/lib/node/runner/api/tx_operations.go index 7dcfd0469..ae73463ef 100644 --- a/lib/node/runner/api/tx_operations.go +++ b/lib/node/runner/api/tx_operations.go @@ -27,7 +27,11 @@ func (api NetworkHandlerAPI) GetOperationsByTxHandler(w http.ResponseWriter, r * return } - options := p.ListOptions() + options, err := p.PageCursorListOptions(block.GetBlockOperationKeyPrefixTxHash(hash)) + if err != nil { + httputils.WriteJSONError(w, err) + return + } var blk *block.Block if blk, err = api.getBlockByTx(hash); err != nil { @@ -35,31 +39,35 @@ func (api NetworkHandlerAPI) GetOperationsByTxHandler(w http.ResponseWriter, r * return } - ops, firstCursor, cursor := api.getOperationsByTx(hash, blk, options) + ops, pOrder, nOrder := api.getOperationsByTxHash(hash, blk, options) + if len(ops) < 1 { httputils.WriteJSONError(w, errors.BlockTransactionDoesNotExists) return } - list := p.ResourceList(ops, firstCursor, cursor) + list := p.ResourceListWithOrder(ops, pOrder, nOrder) httputils.MustWriteJSON(w, 200, list) } -func (api NetworkHandlerAPI) getOperationsByTx(txHash string, blk *block.Block, options storage.ListOptions) (txs []resource.Resource, firstCursor, cursor []byte) { - iterFunc, closeFunc := block.GetBlockOperationsByTx(api.storage, txHash, options) - for idx := 0; ; idx++ { - o, hasNext, c := iterFunc() +func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, blk *block.Block, options storage.ListOptions) (txs []resource.Resource, pOrder *block.BlockOrder, nOrder *block.BlockOrder) { + iterFunc, closeFunc := block.GetBlockOperationsByTxHash(api.storage, txHash, options) + for { + o, hasNext, _ := iterFunc() if !hasNext { break } - cursor = append([]byte{}, c...) - if len(firstCursor) == 0 { - firstCursor = append(firstCursor, c...) + + if pOrder == nil { + pOrder = t.BlockOrder() } + nOrder = t.BlockOrder() rs := resource.NewOperation(&o, idx) rs.Block = blk txs = append(txs, rs) + order = o.BlockOrder() + txs = append(txs, resource.NewOperation(&o)) } closeFunc() return From 709996323d6c272099e42e4eb9bfd3208c410df6 Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 21:48:18 +0900 Subject: [PATCH 11/25] Fix test failure --- lib/block/order.go | 3 +++ lib/node/runner/api/operation.go | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/block/order.go b/lib/block/order.go index bf4b0f98d..35e293e07 100644 --- a/lib/block/order.go +++ b/lib/block/order.go @@ -49,5 +49,8 @@ func (o *BlockOrder) formatText(xs []uint64) string { } func (o *BlockOrder) String() string { + if o == nil || o.parts == nil { + return "" + } return o.formatText(o.parts) } diff --git a/lib/node/runner/api/operation.go b/lib/node/runner/api/operation.go index 4aa3aedb4..6af8e3248 100644 --- a/lib/node/runner/api/operation.go +++ b/lib/node/runner/api/operation.go @@ -76,9 +76,9 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter blockCache := map[ /* block.Height */ uint64]*block.Block{} oType := operation.OperationType(oTypeStr) - prefix := block.GetBlockOperationKeyPrefixSourceAndType(address, oType) + prefix := block.GetBlockOperationKeyPrefixSource(address) if len(oType) > 0 { - prefix = block.GetBlockOperationKeyPrefixSource(address) + prefix = block.GetBlockOperationKeyPrefixSourceAndType(address, oType) } options, err := p.PageCursorListOptions(prefix) From fb90a1dd80db7410cc3602e8f8a5cdc61470c4ef Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 21 Nov 2018 23:46:18 +0900 Subject: [PATCH 12/25] Fix paging next,prev and fix rebase miss --- lib/block/operation.go | 2 +- lib/block/transaction.go | 2 +- lib/node/runner/api/operation.go | 4 +++- lib/node/runner/api/transaction.go | 3 +++ lib/node/runner/api/tx_operations.go | 5 +++-- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index 5b9ca6d2c..53a2b55c9 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -406,7 +406,7 @@ func LoadBlockOperationsInsideIterator( return (func() (BlockOperation, bool, []byte) { item, hasNext := iterFunc() - if !hasNext { + if !hasNext && (item.Key == nil || item.Value == nil) { return BlockOperation{}, false, item.Key } diff --git a/lib/block/transaction.go b/lib/block/transaction.go index a3aefa7dd..49e09510f 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -348,7 +348,7 @@ func LoadBlockTransactionsInsideIterator( return (func() (BlockTransaction, bool, []byte) { item, hasNext := iterFunc() - if !hasNext { + if !hasNext && (item.Key == nil || item.Value == nil) { return BlockTransaction{}, false, item.Key } diff --git a/lib/node/runner/api/operation.go b/lib/node/runner/api/operation.go index 6af8e3248..1af40f508 100644 --- a/lib/node/runner/api/operation.go +++ b/lib/node/runner/api/operation.go @@ -104,6 +104,9 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter } for { t, hasNext, _ := iterFunc() + if t.BlockOrder() != nil { + order = t.BlockOrder() + } if !hasNext { break } @@ -136,7 +139,6 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter r := resource.NewOperation(&t, opIndex) r.Block = blk txs = append(txs, r) - order = t.BlockOrder() } closeFunc() } diff --git a/lib/node/runner/api/transaction.go b/lib/node/runner/api/transaction.go index 25792cb23..c63c44ced 100644 --- a/lib/node/runner/api/transaction.go +++ b/lib/node/runner/api/transaction.go @@ -109,6 +109,9 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit iterFunc, closeFunc := block.GetBlockTransactionsByAccount(api.storage, address, options) for { t, hasNext, _ := iterFunc() + if t.BlockOrder() != nil { + order = t.BlockOrder() + } if !hasNext { break } diff --git a/lib/node/runner/api/tx_operations.go b/lib/node/runner/api/tx_operations.go index ae73463ef..8235c4fa0 100644 --- a/lib/node/runner/api/tx_operations.go +++ b/lib/node/runner/api/tx_operations.go @@ -54,6 +54,9 @@ func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, blk *block.Blo iterFunc, closeFunc := block.GetBlockOperationsByTxHash(api.storage, txHash, options) for { o, hasNext, _ := iterFunc() + if o.BlockOrder() != nil { + order = o.BlockOrder() + } if !hasNext { break } @@ -66,8 +69,6 @@ func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, blk *block.Blo rs := resource.NewOperation(&o, idx) rs.Block = blk txs = append(txs, rs) - order = o.BlockOrder() - txs = append(txs, resource.NewOperation(&o)) } closeFunc() return From 1857601a3f058ebf87a1c21fd09d42fc7931d426 Mon Sep 17 00:00:00 2001 From: anarcher Date: Thu, 22 Nov 2018 12:24:30 +0900 Subject: [PATCH 13/25] Remove commnets for index --- lib/block/transaction.go | 42 ---------------------------------------- 1 file changed, 42 deletions(-) diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 49e09510f..59c54734b 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -76,16 +76,6 @@ func (bt BlockTransaction) NewBlockTransactionKeySource() string { idx.WritePrefix(GetBlockTransactionKeyPrefixSource(bt.Source)) bt.order.Index(idx) return idx.String() - - /* - return fmt.Sprintf( - "%s%s%s%s", - GetBlockTransactionKeyPrefixSource(bt.Source), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.SequenceID), - common.EncodeUint64ToByteSlice(bt.Index), - ) - */ } func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { @@ -93,14 +83,6 @@ func (bt BlockTransaction) NewBlockTransactionKeyConfirmed() string { idx.WritePrefix(GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed)) bt.order.Index(idx) return idx.String() - /* - return fmt.Sprintf( - "%s%s%s", - GetBlockTransactionKeyPrefixConfirmed(bt.Confirmed), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), - ) - */ } func (bt BlockTransaction) NewBlockTransactionKeyAll() string { @@ -108,13 +90,6 @@ func (bt BlockTransaction) NewBlockTransactionKeyAll() string { idx.WritePrefix(common.BlockTransactionPrefixAll) bt.order.Index(idx) return idx.String() - /* - return fmt.Sprintf( - "%s%s", - GetBlockTransactionKeyPrefixHeight(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), - ) - */ } func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string) string { @@ -123,15 +98,6 @@ func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string bt.order.Index(idx) idx.WriteOrder(common.GetUniqueIDFromUUID()) return idx.String() - /* - return fmt.Sprintf( - "%s%s%s%s", - GetBlockTransactionKeyPrefixAccount(accountAddress), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), - common.GetUniqueIDFromUUID(), - ) - */ } func (bt BlockTransaction) NewBlockTransactionKeyByBlock(hash string) string { @@ -139,14 +105,6 @@ func (bt BlockTransaction) NewBlockTransactionKeyByBlock(hash string) string { idx.WritePrefix(GetBlockTransactionKeyPrefixBlock(hash)) bt.order.Index(idx) return idx.String() - /* - return fmt.Sprintf( - "%s%s%s", - GetBlockTransactionKeyPrefixBlock(hash), - common.EncodeUint64ToByteSlice(bt.blockHeight), - common.EncodeUint64ToByteSlice(bt.Index), - ) - */ } func (bt *BlockTransaction) Save(st *storage.LevelDBBackend) (err error) { From 2fd753521885efef0187357c99198093ad092bb2 Mon Sep 17 00:00:00 2001 From: anarcher Date: Fri, 23 Nov 2018 13:14:56 +0900 Subject: [PATCH 14/25] Add NewBlockOperationHeightKey --- lib/block/operation_test.go | 12 ++++++------ lib/block/test.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index f14ca02c0..e1c60d0f2 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -15,7 +15,7 @@ func TestNewBlockOperationFromOperation(t *testing.T) { conf := common.NewTestConfig() _, tx := transaction.TestMakeTransaction(conf.NetworkID, 1) - var index uint64 = 0 + var index int = 0 var txIndex uint64 = 1 op := tx.B.Operations[0] @@ -35,7 +35,7 @@ func TestBlockOperationSaveAndGet(t *testing.T) { conf := common.NewTestConfig() st := storage.NewTestStorage() - bos := TestMakeNewBlockOperation(conf.NetworkID, 1) + bos := TestMakeNewBlockOperation(conf.NetworkID, 1, 0) bo := bos[0] bos[0].MustSave(st) @@ -53,7 +53,7 @@ func TestBlockOperationSaveExisting(t *testing.T) { conf := common.NewTestConfig() st := storage.NewTestStorage() - bos := TestMakeNewBlockOperation(conf.NetworkID, 1) + bos := TestMakeNewBlockOperation(conf.NetworkID, 1, 0) bo := bos[0] bo.MustSave(st) @@ -73,8 +73,8 @@ func TestGetSortedBlockOperationsByTxHash(t *testing.T) { // create 30 `BlockOperation` var txHashes []string createdOrder := map[string][]string{} - for _ = range [3]int{0, 0, 0} { - bos := TestMakeNewBlockOperation(conf.NetworkID, 10) + for _, i := range [3]uint64{0, 1, 2} { + bos := TestMakeNewBlockOperation(conf.NetworkID, 10, i) txHashes = append(txHashes, bos[0].TxHash) for _, bo := range bos { @@ -143,7 +143,7 @@ func TestBlockOperationsByBlockHeight(t *testing.T) { heights := []uint64{1, 2, 3} created := map[uint64][]string{} for _, height := range heights { - bos := TestMakeNewBlockOperation(conf.NetworkID, 10) + bos := TestMakeNewBlockOperation(conf.NetworkID, 10, 1) for _, bo := range bos { bo.Height = height diff --git a/lib/block/test.go b/lib/block/test.go index 5eb7f9b9a..3e0100892 100644 --- a/lib/block/test.go +++ b/lib/block/test.go @@ -129,11 +129,11 @@ func TestMakeNewBlockWithPrevBlock(prevBlock Block, txs []string) Block { ) } -func TestMakeNewBlockOperation(networkID []byte, n int) (bos []BlockOperation) { +func TestMakeNewBlockOperation(networkID []byte, n int, txindex uint64) (bos []BlockOperation) { _, tx := transaction.TestMakeTransaction(networkID, n) for i, op := range tx.B.Operations { - bo, err := NewBlockOperationFromOperation(op, tx, 0, 1, i) + bo, err := NewBlockOperationFromOperation(op, tx, 0, txindex, i) if err != nil { panic(err) } From 14386c839a414c8a19ee74100dfd7f67342cf573 Mon Sep 17 00:00:00 2001 From: anarcher Date: Fri, 23 Nov 2018 17:44:55 +0900 Subject: [PATCH 15/25] Fix wrong rebase... --- lib/block/operation_test.go | 2 +- lib/node/runner/api/base_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index e1c60d0f2..ff10b3d45 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -143,7 +143,7 @@ func TestBlockOperationsByBlockHeight(t *testing.T) { heights := []uint64{1, 2, 3} created := map[uint64][]string{} for _, height := range heights { - bos := TestMakeNewBlockOperation(conf.NetworkID, 10, 1) + bos := TestMakeNewBlockOperation(conf.NetworkID, 10, 0) for _, bo := range bos { bo.Height = height diff --git a/lib/node/runner/api/base_test.go b/lib/node/runner/api/base_test.go index fe88e7148..4f7cf006d 100644 --- a/lib/node/runner/api/base_test.go +++ b/lib/node/runner/api/base_test.go @@ -99,10 +99,10 @@ func prepareBlkTxOpWithoutSave(st *storage.LevelDBBackend) (*keypair.Full, block tx := transaction.TestMakeTransactionWithKeypair(networkID, 1, kp) txHashes = append(txHashes, tx.GetHash()) theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(st), txHashes) - bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx) + bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx, 0) op := tx.B.Operations[0] - bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, 0) + bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, 0, 0) if err != nil { panic(err) } From 9a18874ddc9c0795a85cecca70c87e0da106ce4a Mon Sep 17 00:00:00 2001 From: anarcher Date: Fri, 23 Nov 2018 18:23:33 +0900 Subject: [PATCH 16/25] Fix trigger event for block operation index --- lib/node/runner/api/api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/node/runner/api/api.go b/lib/node/runner/api/api.go index 391acf177..3a6af2bfc 100644 --- a/lib/node/runner/api/api.go +++ b/lib/node/runner/api/api.go @@ -85,6 +85,8 @@ func TriggerEvent(st *storage.LevelDBBackend, transactions []*transaction.Transa t(event(cond(obs.Tx, obs.TxHash, txHash)), &bt) for _, op := range tx.B.Operations { + opHash := block.NewBlockOperationKey(op.MakeHashString(), txHash, uint64(i)) + bo, err := block.GetBlockOperation(st, opHash) if err != nil { return } From d74d789610e32b8dbfd7494bf5090d0673aa557d Mon Sep 17 00:00:00 2001 From: anarcher Date: Fri, 23 Nov 2018 21:29:55 +0900 Subject: [PATCH 17/25] Fix a bug is raised after merged #791 --- lib/node/runner/api/pagequery.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/node/runner/api/pagequery.go b/lib/node/runner/api/pagequery.go index d4360bd7f..f35a76068 100644 --- a/lib/node/runner/api/pagequery.go +++ b/lib/node/runner/api/pagequery.go @@ -90,10 +90,14 @@ func (p *PageQuery) ListOptions() storage.ListOptions { } func (p *PageQuery) PageCursorListOptions(prefix string) (storage.ListOptions, error) { - c := NewPageCursor(p.Cursor(), prefix) - indexCursor, err := c.IndexKey() - if err != nil { - return nil, err + var indexCursor []byte + var err error + if p.Cursor() != nil { + c := NewPageCursor(p.Cursor(), prefix) + indexCursor, err = c.IndexKey() + if err != nil { + return nil, err + } } return storage.NewDefaultListOptions(p.Reverse(), indexCursor, p.Limit()), nil } From 6f7a0759cb25c7549f00ded656a39f9a54223a12 Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 27 Nov 2018 19:46:04 +0900 Subject: [PATCH 18/25] Fix rebase wrong... --- lib/node/runner/api/api.go | 2 +- lib/node/runner/api/operation.go | 3 --- lib/node/runner/api/pagequery.go | 7 ++++--- lib/node/runner/api/transaction.go | 11 ++++------- lib/node/runner/api/tx_operations.go | 7 ++----- 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/node/runner/api/api.go b/lib/node/runner/api/api.go index 3a6af2bfc..830b08464 100644 --- a/lib/node/runner/api/api.go +++ b/lib/node/runner/api/api.go @@ -84,7 +84,7 @@ func TriggerEvent(st *storage.LevelDBBackend, transactions []*transaction.Transa t(event(cond(obs.Tx, obs.Source, source)), &bt) t(event(cond(obs.Tx, obs.TxHash, txHash)), &bt) - for _, op := range tx.B.Operations { + for i, op := range tx.B.Operations { opHash := block.NewBlockOperationKey(op.MakeHashString(), txHash, uint64(i)) bo, err := block.GetBlockOperation(st, opHash) if err != nil { diff --git a/lib/node/runner/api/operation.go b/lib/node/runner/api/operation.go index 1af40f508..ecee6f471 100644 --- a/lib/node/runner/api/operation.go +++ b/lib/node/runner/api/operation.go @@ -104,9 +104,6 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter } for { t, hasNext, _ := iterFunc() - if t.BlockOrder() != nil { - order = t.BlockOrder() - } if !hasNext { break } diff --git a/lib/node/runner/api/pagequery.go b/lib/node/runner/api/pagequery.go index f35a76068..1ee071361 100644 --- a/lib/node/runner/api/pagequery.go +++ b/lib/node/runner/api/pagequery.go @@ -114,9 +114,10 @@ func (p *PageQuery) ResourceList(rs []resource.Resource, firstCursor, lastCursor } } -func (p *PageQuery) ResourceListWithOrder(rs []resource.Resource, order *block.BlockOrder) *resource.ResourceList { - cursor := []byte(order.String()) - return p.ResourceList(rs, cursor) +func (p *PageQuery) ResourceListWithOrder(rs []resource.Resource, prevOrder *block.BlockOrder, nextOrder *block.BlockOrder) *resource.ResourceList { + pcursor := []byte(prevOrder.String()) + ncursor := []byte(nextOrder.String()) + return p.ResourceList(rs, pcursor, ncursor) } func (p *PageQuery) parseRequest() error { diff --git a/lib/node/runner/api/transaction.go b/lib/node/runner/api/transaction.go index c63c44ced..a7a1f61c6 100644 --- a/lib/node/runner/api/transaction.go +++ b/lib/node/runner/api/transaction.go @@ -38,7 +38,7 @@ func (api NetworkHandlerAPI) GetTransactionsHandler(w http.ResponseWriter, r *ht var txs []resource.Resource iterFunc, closeFunc := block.GetBlockTransactions(api.storage, options) for { - t, hasNext, c := iterFunc() + t, hasNext, _ := iterFunc() if !hasNext { break } @@ -102,22 +102,19 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit } var ( pOrder *block.BlockOrder - nOrder + nOrder *block.BlockOrder ) readFunc := func() []resource.Resource { var txs []resource.Resource iterFunc, closeFunc := block.GetBlockTransactionsByAccount(api.storage, address, options) for { t, hasNext, _ := iterFunc() - if t.BlockOrder() != nil { - order = t.BlockOrder() - } if !hasNext { break } if pOrder == nil { pOrder = t.BlockOrder() - } + } nOrder = t.BlockOrder() txs = append(txs, resource.NewTransaction(&t)) } @@ -126,7 +123,7 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit } txs := readFunc() - list := p.ResourceListWithOrder(txs, pOrder,nOrder) + list := p.ResourceListWithOrder(txs, pOrder, nOrder) httputils.MustWriteJSON(w, 200, list) } diff --git a/lib/node/runner/api/tx_operations.go b/lib/node/runner/api/tx_operations.go index 8235c4fa0..4be03b4a0 100644 --- a/lib/node/runner/api/tx_operations.go +++ b/lib/node/runner/api/tx_operations.go @@ -54,17 +54,14 @@ func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, blk *block.Blo iterFunc, closeFunc := block.GetBlockOperationsByTxHash(api.storage, txHash, options) for { o, hasNext, _ := iterFunc() - if o.BlockOrder() != nil { - order = o.BlockOrder() - } if !hasNext { break } if pOrder == nil { - pOrder = t.BlockOrder() + pOrder = o.BlockOrder() } - nOrder = t.BlockOrder() + nOrder = o.BlockOrder() rs := resource.NewOperation(&o, idx) rs.Block = blk From 78f0d4579930bd307f959113dc3a42ca50668693 Mon Sep 17 00:00:00 2001 From: kfangw Date: Tue, 27 Nov 2018 21:13:39 +0900 Subject: [PATCH 19/25] rebase fix --- lib/block/operation.go | 57 +++++++++++++------------------- lib/node/runner/api/operation.go | 4 +-- lib/storage/index.go | 10 +++++- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index 53a2b55c9..19401cfa2 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -131,7 +131,7 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) { if err = st.New(bo.NewBlockOperationPeersKey(bo.Source), bo.Hash); err != nil { return } - if err = st.New(bo.NewBlockOperationPeersAndTypeKey(bo.Source), bo.Hash); err != nil { + if err = st.New(bo.NewBlockOperationPeersAndTypeKey(bo.Source, bo.Type), bo.Hash); err != nil { return } if err = st.New(bo.NewBlockOperationBlockHeightKey(), bo.Hash); err != nil { @@ -142,13 +142,13 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) { if err = st.New(bo.NewBlockOperationTargetKey(bo.Target), bo.Hash); err != nil { return } - if err = st.New(bo.NewBlockOperationTargetAndTypeKey(bo.Target), bo.Hash); err != nil { + if err = st.New(bo.NewBlockOperationTargetAndTypeKey(bo.Target, bo.Type), bo.Hash); err != nil { return } if err = st.New(bo.NewBlockOperationPeersKey(bo.Target), bo.Hash); err != nil { return } - if err = st.New(bo.NewBlockOperationPeersAndTypeKey(bo.Target), bo.Hash); err != nil { + if err = st.New(bo.NewBlockOperationPeersAndTypeKey(bo.Target, bo.Type), bo.Hash); err != nil { return } } @@ -244,7 +244,7 @@ func GetBlockOperationKeyPrefixSource(source string) string { func GetBlockOperationKeyPrefixSourceAndType(source string, ty operation.OperationType) string { idx := storage.NewIndex() - idx.WritePrefix(common.BlockOperationPrefixSource, string(ty), source) + idx.WritePrefix(common.BlockOperationPrefixTypeSource, string(ty), source) return idx.String() } @@ -310,44 +310,33 @@ func (bo BlockOperation) NewBlockOperationSourceAndTypeKey() string { } func (bo BlockOperation) NewBlockOperationTargetKey(target string) string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixTarget(target), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixTarget(target)) + bo.order.Index(idx) + return idx.String() } -func (bo BlockOperation) NewBlockOperationTargetAndTypeKey(target string) string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixTargetAndType(target, bo.Type), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), - ) +func (bo BlockOperation) NewBlockOperationTargetAndTypeKey(target string, ty operation.OperationType) string { + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixTargetAndType(target, ty)) + bo.order.Index(idx) + return idx.String() } func (bo BlockOperation) NewBlockOperationPeersKey(addr string) string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixPeers(addr), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), - ) + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixPeers(addr)) + bo.order.Index(idx) + return idx.String() } -func (bo BlockOperation) NewBlockOperationPeersAndTypeKey(addr string) string { - return fmt.Sprintf( - "%s%s%s%s", - keyPrefixPeersAndType(addr, bo.Type), - common.EncodeUint64ToByteSlice(bo.Height), - common.EncodeUint64ToByteSlice(bo.transaction.B.SequenceID), - common.GetUniqueIDFromUUID(), - ) +func (bo BlockOperation) NewBlockOperationPeersAndTypeKey(addr string, ty operation.OperationType) string { + idx := storage.NewIndex() + idx.WritePrefix(GetBlockOperationKeyPrefixPeersAndType(addr, ty)) + bo.order.Index(idx) + return idx.String() } + func (bo BlockOperation) NewBlockOperationBlockHeightKey() string { idx := storage.NewIndex() idx.WritePrefix(GetBlockOperationKeyPrefixBlockHeight(bo.Height)) diff --git a/lib/node/runner/api/operation.go b/lib/node/runner/api/operation.go index ecee6f471..bdefb8318 100644 --- a/lib/node/runner/api/operation.go +++ b/lib/node/runner/api/operation.go @@ -76,9 +76,9 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter blockCache := map[ /* block.Height */ uint64]*block.Block{} oType := operation.OperationType(oTypeStr) - prefix := block.GetBlockOperationKeyPrefixSource(address) + prefix := block.GetBlockOperationKeyPrefixPeers(address) if len(oType) > 0 { - prefix = block.GetBlockOperationKeyPrefixSourceAndType(address, oType) + prefix = block.GetBlockOperationKeyPrefixPeersAndType(address, oType) } options, err := p.PageCursorListOptions(prefix) diff --git a/lib/storage/index.go b/lib/storage/index.go index 369717093..8727ae86b 100644 --- a/lib/storage/index.go +++ b/lib/storage/index.go @@ -29,7 +29,15 @@ func (idx Index) Bytes() []byte { func (idx Index) String() string { prefix := strings.Join(idx.prefix, IndexElementDelimiter) order := strings.Join(idx.order, IndexElementDelimiter) - index := strings.Join([]string{prefix, order}, IndexPrefixOrderDelimiter) + var strs []string + if len(prefix) != 0 { + strs = append(strs, prefix) + } + if len(order) != 0 { + strs = append(strs, order) + } + + index := strings.Join(strs, IndexPrefixOrderDelimiter) return index } From 1c65f0027cd221da78266a67f7aebd831c402b05 Mon Sep 17 00:00:00 2001 From: kfangw Date: Tue, 27 Nov 2018 22:17:34 +0900 Subject: [PATCH 20/25] more fix --- lib/block/block.go | 2 +- lib/block/transaction.go | 39 +++++++++++++-------------- lib/common/prefix.go | 1 + lib/common/util.go | 14 +++------- lib/node/runner/api/operation_test.go | 16 +++++++++++ 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/block/block.go b/lib/block/block.go index 3b99310ba..261968773 100644 --- a/lib/block/block.go +++ b/lib/block/block.go @@ -78,7 +78,7 @@ func (b Block) NewBlockKeyConfirmed() string { return fmt.Sprintf( "%s%s-%s%s", common.BlockPrefixConfirmed, b.ProposedTime, - common.EncodeUint64ToByteSlice(b.Height), + common.EncodeUint64ToString(b.Height), common.GetUniqueIDFromUUID(), ) } diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 59c54734b..878e24341 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -100,6 +100,14 @@ func (bt BlockTransaction) NewBlockTransactionKeyByAccount(accountAddress string return idx.String() } +func (bt BlockTransaction) NewBlockTransactionKeyByTarget(accountAddress string) string { + idx := storage.NewIndex() + idx.WritePrefix(GetBlockTransactionKeyPrefixTarget(accountAddress)) + bt.order.Index(idx) + idx.WriteOrder(common.GetUniqueIDFromUUID()) + return idx.String() +} + func (bt BlockTransaction) NewBlockTransactionKeyByBlock(hash string) string { idx := storage.NewIndex() idx.WritePrefix(GetBlockTransactionKeyPrefixBlock(hash)) @@ -217,6 +225,10 @@ func (bt *BlockTransaction) SaveBlockOperation(st *storage.LevelDBBackend, op op if err != nil { return } + err = st.New(bt.NewBlockTransactionKeyByTarget(pop.TargetAddress()), bt.Hash) + if err != nil { + return + } } return nil @@ -258,6 +270,12 @@ func GetBlockTransactionKeyPrefixAccount(accountAddress string) string { //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixAccount, accountAddress) } +func GetBlockTransactionKeyPrefixTarget(accountAddress string) string { + idx := storage.NewIndex() + return idx.WritePrefix(common.BlockTransactionPrefixTarget, accountAddress).String() + //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixAccount, accountAddress) +} + func GetBlockTransactionKeyPrefixBlock(hash string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixBlock, hash).String() @@ -270,17 +288,6 @@ func GetBlockTransactionKey(hash string) string { //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHash, hash) } -/* -func GetBlockTransactionKeyPrefixHeight(height uint64) string { - idx := storage.NewIndex() - idx.WritePrefix( - common.BlockTransactionPrefixHeight, - ) - return idx.WriteOrder(common.EncodeUint64ToString(height)).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHeight, common.EncodeUint64ToByteSlice(height)) -} -*/ - func GetBlockTransaction(st *storage.LevelDBBackend, hash string) (bt BlockTransaction, err error) { if err = st.Get(GetBlockTransactionKey(hash), &bt); err != nil { return @@ -358,16 +365,6 @@ func GetBlockTransactionsByBlock(st *storage.LevelDBBackend, hash string, option return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) } -/* -func GetBlockTransactionsByHeight(st *storage.LevelDBBackend, height uint64, options storage.ListOptions) ( - func() (BlockTransaction, bool, []byte), - func(), -) { - iterFunc, closeFunc := st.GetIterator(GetBlockTransactionKeyPrefixHeight(height), options) - return LoadBlockTransactionsInsideIterator(st, iterFunc, closeFunc) -} -*/ - func GetBlockTransactions(st *storage.LevelDBBackend, options storage.ListOptions) ( func() (BlockTransaction, bool, []byte), func(), diff --git a/lib/common/prefix.go b/lib/common/prefix.go index b3a38059a..6d57ef7b0 100644 --- a/lib/common/prefix.go +++ b/lib/common/prefix.go @@ -11,6 +11,7 @@ const ( BlockTransactionPrefixAccount = string(0x13) BlockTransactionPrefixBlock = string(0x14) BlockTransactionPrefixAll = string(0x15) + BlockTransactionPrefixTarget = string(0x16) BlockOperationPrefixHash = string(0x20) BlockOperationPrefixTxHash = string(0x21) BlockOperationPrefixSource = string(0x22) diff --git a/lib/common/util.go b/lib/common/util.go index 888de2ad6..903b37120 100644 --- a/lib/common/util.go +++ b/lib/common/util.go @@ -2,14 +2,13 @@ package common import ( "bytes" - "encoding/binary" "encoding/json" + "fmt" + "github.com/satori/go.uuid" "io" "net/url" "os" "sort" - - uuid "github.com/satori/go.uuid" ) const MaxUintEncodeByte = 8 @@ -154,15 +153,8 @@ func IsStringMapEqualWithHash(a, b map[string]bool) bool { return bytes.Equal(aHash, bHash) } -func EncodeUint64ToByteSlice(i uint64) [MaxUintEncodeByte]byte { - var b [MaxUintEncodeByte]byte - binary.BigEndian.PutUint64(b[:], i) - return b -} - func EncodeUint64ToString(i uint64) string { - bs := EncodeUint64ToByteSlice(i) - return string(bs[:]) + return fmt.Sprintf("%020d", i) } type KV struct { diff --git a/lib/node/runner/api/operation_test.go b/lib/node/runner/api/operation_test.go index 7aa25ba4a..9eb259e8d 100644 --- a/lib/node/runner/api/operation_test.go +++ b/lib/node/runner/api/operation_test.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/stretchr/testify/require" "io/ioutil" + "strconv" "strings" "testing" ) @@ -334,3 +335,18 @@ func TestGetOperationsByAccountHandlerPage(t *testing.T) { } } } + +func TestStr(t *testing.T) { + var i uint64 = 123 + a := strconv.FormatUint(i, 10) + t.Log(a) + + x := ^uint64(0) + b := strconv.FormatUint(x, 10) + t.Log(b) + + t.Log(len(b)) + + ttt := fmt.Sprintf("%020d", i) + t.Log(ttt) +} From a3018ed27c5b1e445d5111d6bcfbedc70386ea15 Mon Sep 17 00:00:00 2001 From: kfangw Date: Tue, 27 Nov 2018 22:20:10 +0900 Subject: [PATCH 21/25] remove test code --- lib/node/runner/api/operation_test.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/node/runner/api/operation_test.go b/lib/node/runner/api/operation_test.go index 9eb259e8d..149e335f5 100644 --- a/lib/node/runner/api/operation_test.go +++ b/lib/node/runner/api/operation_test.go @@ -5,10 +5,8 @@ import ( "boscoin.io/sebak/lib/common" "boscoin.io/sebak/lib/transaction/operation" "bufio" - "fmt" "github.com/stretchr/testify/require" "io/ioutil" - "strconv" "strings" "testing" ) @@ -335,18 +333,3 @@ func TestGetOperationsByAccountHandlerPage(t *testing.T) { } } } - -func TestStr(t *testing.T) { - var i uint64 = 123 - a := strconv.FormatUint(i, 10) - t.Log(a) - - x := ^uint64(0) - b := strconv.FormatUint(x, 10) - t.Log(b) - - t.Log(len(b)) - - ttt := fmt.Sprintf("%020d", i) - t.Log(ttt) -} From 846f063b4b09d826149c1e5c907b9907bcb4f4c0 Mon Sep 17 00:00:00 2001 From: anarcher Date: Fri, 30 Nov 2018 14:23:28 +0900 Subject: [PATCH 22/25] Operation.Hash is hashing with opHash,txHash,Index --- lib/block/operation.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index 19401cfa2..912ad69a4 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -3,6 +3,7 @@ package block import ( "encoding/json" "fmt" + "strconv" "boscoin.io/sebak/lib/common" "boscoin.io/sebak/lib/errors" @@ -41,7 +42,7 @@ type BlockOperation struct { } func NewBlockOperationKey(opHash, txHash string, index uint64) string { - return fmt.Sprintf("%s-%s-%d", opHash, txHash, index) + return common.MustMakeObjectHashString([]string{opHash, txHash, strconv.FormatUint(index, 10)}) } func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, txIndex uint64, opIndex int) (BlockOperation, error) { @@ -192,7 +193,7 @@ func keyPrefixSourceAndType(source string, ty operation.OperationType) string { } func keyPrefixBlockHeight(height uint64) string { - return fmt.Sprintf("%s%s-", common.BlockOperationPrefixBlockHeight, common.EncodeUint64ToByteSlice(height)) + return fmt.Sprintf("%s%s-", common.BlockOperationPrefixBlockHeight, common.EncodeUint64ToString(height)) } func keyPrefixTarget(target string) string { From d9465ace0467027a5a3f26c40a148a94f227f139 Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 5 Dec 2018 15:08:28 +0900 Subject: [PATCH 23/25] Fix build failure --- lib/block/operation_test.go | 2 +- lib/node/runner/api/api.go | 8 +------- lib/node/runner/api/base_test.go | 4 ++-- lib/node/runner/api/operation.go | 6 ++---- lib/node/runner/api/operation_test.go | 11 +++++++---- lib/node/runner/api/tx_operations.go | 9 ++++----- 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/block/operation_test.go b/lib/block/operation_test.go index ff10b3d45..f9d237741 100644 --- a/lib/block/operation_test.go +++ b/lib/block/operation_test.go @@ -27,7 +27,7 @@ func TestNewBlockOperationFromOperation(t *testing.T) { require.Equal(t, bo.Source, tx.B.Source) encoded := common.MustMarshalJSON(op.B) require.Equal(t, bo.Body, encoded) - require.Equal(t, bo.Index, index) + require.Equal(t, bo.Index, uint64(index)) require.Equal(t, bo.TxIndex, txIndex) } diff --git a/lib/node/runner/api/api.go b/lib/node/runner/api/api.go index 830b08464..4e9a963ea 100644 --- a/lib/node/runner/api/api.go +++ b/lib/node/runner/api/api.go @@ -84,13 +84,7 @@ func TriggerEvent(st *storage.LevelDBBackend, transactions []*transaction.Transa t(event(cond(obs.Tx, obs.Source, source)), &bt) t(event(cond(obs.Tx, obs.TxHash, txHash)), &bt) - for i, op := range tx.B.Operations { - opHash := block.NewBlockOperationKey(op.MakeHashString(), txHash, uint64(i)) - bo, err := block.GetBlockOperation(st, opHash) - if err != nil { - return - } - + for _, op := range tx.B.Operations { if pop, ok := op.B.(operation.Targetable); ok { target := pop.TargetAddress() accountMap[target] = struct{}{} diff --git a/lib/node/runner/api/base_test.go b/lib/node/runner/api/base_test.go index 4f7cf006d..07b30892c 100644 --- a/lib/node/runner/api/base_test.go +++ b/lib/node/runner/api/base_test.go @@ -82,7 +82,7 @@ func prepareOpsWithoutSave(count int, st *storage.LevelDBBackend) (*keypair.Full theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(st), txHashes) for i, tx := range txs { for j, op := range tx.B.Operations { - bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, uint64(i), uint64(j)) + bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, uint64(i), j) if err != nil { panic(err) } @@ -150,7 +150,7 @@ func prepareTxWithOperations(storage *storage.LevelDBBackend, count int) (*keypa theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(storage), []string{tx.GetHash()}) theBlock.MustSave(storage) - bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx) + bt := block.NewBlockTransactionFromTransaction(theBlock.Hash, theBlock.Height, theBlock.ProposedTime, tx, 1) bt.Save(storage) if err := bt.SaveBlockOperations(storage); err != nil { panic(err) diff --git a/lib/node/runner/api/operation.go b/lib/node/runner/api/operation.go index bdefb8318..86a4f1223 100644 --- a/lib/node/runner/api/operation.go +++ b/lib/node/runner/api/operation.go @@ -92,9 +92,8 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter nOrder *block.BlockOrder ) - readFunc := func() []resource.Resource { - var txs []resource.Resource - + var txs []resource.Resource + { var iterFunc func() (block.BlockOperation, bool, []byte) var closeFunc func() if len(oType) > 0 { @@ -140,7 +139,6 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter closeFunc() } - txs := readFunc() list := p.ResourceListWithOrder(txs, pOrder, nOrder) httputils.MustWriteJSON(w, 200, list) } diff --git a/lib/node/runner/api/operation_test.go b/lib/node/runner/api/operation_test.go index 149e335f5..f75f51383 100644 --- a/lib/node/runner/api/operation_test.go +++ b/lib/node/runner/api/operation_test.go @@ -1,14 +1,17 @@ package api import ( - "boscoin.io/sebak/lib/block" - "boscoin.io/sebak/lib/common" - "boscoin.io/sebak/lib/transaction/operation" "bufio" - "github.com/stretchr/testify/require" + "fmt" "io/ioutil" "strings" "testing" + + "boscoin.io/sebak/lib/block" + "boscoin.io/sebak/lib/common" + "boscoin.io/sebak/lib/transaction/operation" + + "github.com/stretchr/testify/require" ) func TestGetOperationsByAccountHandler(t *testing.T) { diff --git a/lib/node/runner/api/tx_operations.go b/lib/node/runner/api/tx_operations.go index 4be03b4a0..029962e7a 100644 --- a/lib/node/runner/api/tx_operations.go +++ b/lib/node/runner/api/tx_operations.go @@ -39,7 +39,7 @@ func (api NetworkHandlerAPI) GetOperationsByTxHandler(w http.ResponseWriter, r * return } - ops, pOrder, nOrder := api.getOperationsByTxHash(hash, blk, options) + ops, pOrder, nOrder := api.getOperationsByTx(hash, blk, options) if len(ops) < 1 { httputils.WriteJSONError(w, errors.BlockTransactionDoesNotExists) @@ -50,14 +50,13 @@ func (api NetworkHandlerAPI) GetOperationsByTxHandler(w http.ResponseWriter, r * httputils.MustWriteJSON(w, 200, list) } -func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, blk *block.Block, options storage.ListOptions) (txs []resource.Resource, pOrder *block.BlockOrder, nOrder *block.BlockOrder) { - iterFunc, closeFunc := block.GetBlockOperationsByTxHash(api.storage, txHash, options) - for { +func (api NetworkHandlerAPI) getOperationsByTx(txHash string, blk *block.Block, options storage.ListOptions) (txs []resource.Resource, pOrder *block.BlockOrder, nOrder *block.BlockOrder) { + iterFunc, closeFunc := block.GetBlockOperationsByTx(api.storage, txHash, options) + for idx := 0; ; idx++ { o, hasNext, _ := iterFunc() if !hasNext { break } - if pOrder == nil { pOrder = o.BlockOrder() } From 522e52d409833dba9002090aa45e2fb79104cf83 Mon Sep 17 00:00:00 2001 From: anarcher Date: Wed, 5 Dec 2018 15:52:48 +0900 Subject: [PATCH 24/25] Fix rebase bug --- lib/block/operation.go | 18 +++++++++--------- lib/node/runner/api/resource/resource_test.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/block/operation.go b/lib/block/operation.go index 912ad69a4..5e533ddab 100644 --- a/lib/block/operation.go +++ b/lib/block/operation.go @@ -418,7 +418,7 @@ func GetBlockOperationsByTx(st *storage.LevelDBBackend, txHash string, options s func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixTxHash(txHash), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixTxHash(txHash), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -427,7 +427,7 @@ func GetBlockOperationsBySource(st *storage.LevelDBBackend, source string, optio func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixSource(source), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixSource(source), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -446,7 +446,7 @@ func GetBlockOperationsByLinked(st *storage.LevelDBBackend, hash string, options func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixFrozenLinked(hash), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixFrozenLinked(hash), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -454,7 +454,7 @@ func GetBlockOperationsBySourceAndType(st *storage.LevelDBBackend, source string func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixSourceAndType(source, ty), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixSourceAndType(source, ty), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -462,7 +462,7 @@ func GetBlockOperationsByTarget(st *storage.LevelDBBackend, target string, optio func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixTarget(target), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixTarget(target), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -471,7 +471,7 @@ func GetBlockOperationsByTargetAndType(st *storage.LevelDBBackend, target string func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixTargetAndType(target, ty), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixTargetAndType(target, ty), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -479,7 +479,7 @@ func GetBlockOperationsByPeers(st *storage.LevelDBBackend, addr string, options func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixPeers(addr), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixPeers(addr), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -488,7 +488,7 @@ func GetBlockOperationsByPeersAndType(st *storage.LevelDBBackend, addr string, t func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixPeersAndType(addr, ty), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixPeersAndType(addr, ty), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } @@ -496,6 +496,6 @@ func GetBlockOperationsByBlockHeight(st *storage.LevelDBBackend, height uint64, func() (BlockOperation, bool, []byte), func(), ) { - iterFunc, closeFunc := st.GetIterator(keyPrefixBlockHeight(height), options) + iterFunc, closeFunc := st.GetIterator(GetBlockOperationKeyPrefixBlockHeight(height), options) return LoadBlockOperationsInsideIterator(st, iterFunc, closeFunc) } diff --git a/lib/node/runner/api/resource/resource_test.go b/lib/node/runner/api/resource/resource_test.go index af1ed64bd..b0d0593d8 100644 --- a/lib/node/runner/api/resource/resource_test.go +++ b/lib/node/runner/api/resource/resource_test.go @@ -73,7 +73,7 @@ func TestResourceAccount(t *testing.T) { // Operation { _, tx := transaction.TestMakeTransaction([]byte{0x00}, 1) - bt := block.NewBlockTransactionFromTransaction(common.GetUniqueIDFromUUID(), 0, common.NowISO8601(), tx, 1) + bt := block.NewBlockTransactionFromTransaction(blk.Hash, blk.Height, common.NowISO8601(), tx, 1) bt.MustSave(storage) err := bt.SaveBlockOperations(storage) From 185a0a681a2b4bf68883722daff6ca6b0af52f4d Mon Sep 17 00:00:00 2001 From: anarcher Date: Tue, 18 Dec 2018 17:29:09 +0900 Subject: [PATCH 25/25] Remove comments --- lib/block/transaction.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/block/transaction.go b/lib/block/transaction.go index 878e24341..ea0616152 100644 --- a/lib/block/transaction.go +++ b/lib/block/transaction.go @@ -255,37 +255,31 @@ func (bt BlockTransaction) BlockOrder() *BlockOrder { func GetBlockTransactionKeyPrefixSource(source string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixSource, source).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixSource, source) } func GetBlockTransactionKeyPrefixConfirmed(confirmed string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixConfirmed, confirmed).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixConfirmed, confirmed) } func GetBlockTransactionKeyPrefixAccount(accountAddress string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixAccount, accountAddress).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixAccount, accountAddress) } func GetBlockTransactionKeyPrefixTarget(accountAddress string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixTarget, accountAddress).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixAccount, accountAddress) } func GetBlockTransactionKeyPrefixBlock(hash string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixBlock, hash).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixBlock, hash) } func GetBlockTransactionKey(hash string) string { idx := storage.NewIndex() return idx.WritePrefix(common.BlockTransactionPrefixHash, hash).String() - //return fmt.Sprintf("%s%s-", common.BlockTransactionPrefixHash, hash) } func GetBlockTransaction(st *storage.LevelDBBackend, hash string) (bt BlockTransaction, err error) {