Skip to content

Commit

Permalink
Add BlockTransaction.Index and BlockOperation.Index
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Nov 20, 2018
1 parent 34e7895 commit 9940a20
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/block/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion lib/block/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type BlockOperation struct {
Source string `json:"source"`
Body []byte `json:"body"`
Height uint64 `json:"block_height"`
Index uint64 `json:"index"`

// transaction will be used only for `Save` time.
transaction transaction.Transaction
Expand All @@ -39,7 +40,7 @@ func NewBlockOperationKey(opHash, txHash string) string {
return fmt.Sprintf("%s-%s", opHash, txHash)
}

func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64) (BlockOperation, error) {
func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight, index uint64) (BlockOperation, error) {
body, err := op.B.Serialize()
if err != nil {
return BlockOperation{}, err
Expand All @@ -58,6 +59,7 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans
Source: tx.B.Source,
Body: body,
Height: blockHeight,
Index: index,

transaction: tx,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions lib/block/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
bo, err := NewBlockOperationFromOperation(op, tx, 0, 0)
require.NoError(t, err)

require.Equal(t, bo.Type, op.H.Type)
Expand Down Expand Up @@ -104,7 +104,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)

Expand Down
4 changes: 2 additions & 2 deletions lib/block/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func TestMakeNewBlockWithPrevBlock(prevBlock Block, txs []string) Block {
func TestMakeNewBlockOperation(networkID []byte, n int) (bos []BlockOperation) {
_, tx := transaction.TestMakeTransaction(networkID, n)

for _, op := range tx.B.Operations {
bo, err := NewBlockOperationFromOperation(op, tx, 0)
for i, op := range tx.B.Operations {
bo, err := NewBlockOperationFromOperation(op, tx, 0, uint64(i))
if err != nil {
panic(err)
}
Expand Down
13 changes: 8 additions & 5 deletions lib/block/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,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()))
Expand All @@ -60,6 +62,7 @@ func NewBlockTransactionFromTransaction(blockHash string, blockHeight uint64, co
Amount: tx.TotalAmount(true),
Confirmed: confirmed,
Created: tx.H.Created,
Index: index,

transaction: tx,
blockHeight: blockHeight,
Expand Down Expand Up @@ -189,16 +192,16 @@ func (bt *BlockTransaction) SaveBlockOperations(st *storage.LevelDBBackend) (err
}
}

for _, op := range bt.Transaction().B.Operations {
if err = bt.SaveBlockOperation(st, op); err != nil {
for i, op := range bt.Transaction().B.Operations {
if err = bt.SaveBlockOperation(st, op, uint64(i)); err != nil {
return
}
}

return nil
}

func (bt *BlockTransaction) SaveBlockOperation(st *storage.LevelDBBackend, op operation.Operation) (err error) {
func (bt *BlockTransaction) SaveBlockOperation(st *storage.LevelDBBackend, op operation.Operation, index uint64) (err error) {
if bt.blockHeight < 1 {
var blk Block
if blk, err = GetBlock(st, bt.Block); err != nil {
Expand All @@ -209,7 +212,7 @@ func (bt *BlockTransaction) SaveBlockOperation(st *storage.LevelDBBackend, op op
}

var bo BlockOperation
bo, err = NewBlockOperationFromOperation(op, bt.Transaction(), bt.blockHeight)
bo, err = NewBlockOperationFromOperation(op, bt.Transaction(), bt.blockHeight, index)
if err != nil {
return
}
Expand Down
50 changes: 25 additions & 25 deletions lib/block/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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...)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions lib/common/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (

// DefaultTxPoolLimit is the default tx pool limit.
DefaultTxPoolLimit int = 1000000

// ProposerTransactionIndex
ProposerTransactionIndex uint64 = 0
)

var (
Expand Down
14 changes: 7 additions & 7 deletions lib/node/runner/api/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func prepareOpsWithoutSave(count int, st *storage.LevelDBBackend) (*keypair.Full

theBlock := block.TestMakeNewBlockWithPrevBlock(block.GetLatestBlock(st), txHashes)
for _, tx := range txs {
for _, op := range tx.B.Operations {
bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height)
for i, op := range tx.B.Operations {
bo, err := block.NewBlockOperationFromOperation(op, tx, theBlock.Height, uint64(i))
if err != nil {
panic(err)
}
Expand All @@ -90,8 +90,8 @@ func prepareTxs(storage *storage.LevelDBBackend, count int) (*keypair.Full, []bl

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
Expand All @@ -113,8 +113,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
Expand All @@ -125,7 +125,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
}

Expand Down
1 change: 1 addition & 0 deletions lib/node/runner/api/resource/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (o Operation) GetMap() hal.Entry {
"source": o.bo.Source,
"type": o.bo.Type,
"tx_hash": o.bo.TxHash,
"index": o.bo.Index,
"body": body,
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/node/runner/api/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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)
Expand All @@ -72,7 +72,7 @@ func TestResourceAccount(t *testing.T) {
// Operation
{
_, tx := transaction.TestMakeTransaction([]byte{0x00}, 1)
bt := block.NewBlockTransactionFromTransaction(common.GetUniqueIDFromUUID(), 0, common.NowISO8601(), tx)
bt := block.NewBlockTransactionFromTransaction(common.GetUniqueIDFromUUID(), 0, common.NowISO8601(), tx, 0)
bt.MustSave(storage)
bo, _ := block.GetBlockOperation(storage, bt.Operations[0])

Expand All @@ -96,7 +96,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)
Expand Down
1 change: 1 addition & 0 deletions lib/node/runner/api/resource/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions lib/node/runner/api/tx_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func TestGetOperationsByTxHashHandlerStream(t *testing.T) {

kp := keypair.Random()
tx := transaction.TestMakeTransactionWithKeypair(networkID, 10, kp)
bt := block.NewBlockTransactionFromTransaction("block-hash", 1, common.NowISO8601(), tx)
bt := block.NewBlockTransactionFromTransaction("block-hash", 1, common.NowISO8601(), tx, 0)

boMap := make(map[string]block.BlockOperation)
for _, op := range tx.B.Operations {
bo, err := block.NewBlockOperationFromOperation(op, tx, 0)
for i, op := range tx.B.Operations {
bo, err := block.NewBlockOperationFromOperation(op, tx, 0, uint64(i))
require.NoError(t, err)
boMap[bo.Hash] = bo
}
Expand Down
4 changes: 2 additions & 2 deletions lib/node/runner/api_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,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)
Expand Down
Loading

0 comments on commit 9940a20

Please sign in to comment.