Skip to content

Commit

Permalink
Updating all sources of block.Transactions and do the corresponding w…
Browse files Browse the repository at this point in the history
…ork for block staking txns
  • Loading branch information
denniswon committed Apr 6, 2020
1 parent 054c3cb commit d12c329
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ func (consensus *Consensus) Start(

consensus.getLogger().Debug().
Int("numTxs", len(newBlock.Transactions())).
Int("numStakingTxs", len(newBlock.StakingTransactions())).
Time("startTime", startTime).
Int64("publicKeys", consensus.Decider.ParticipantsCount()).
Msg("[ConsensusMainLoop] STARTING CONSENSUS")
Expand Down
30 changes: 26 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,12 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error {
signer := types.MakeSigner(config, block.Epoch())

transactions, logIndex := block.Transactions(), uint(0)
if len(transactions) != len(receipts) {
return errors.New("transaction and receipt count mismatch")
transactions, stakingTransactions, logIndex := block.Transactions(), block.StakingTransactions(), uint(0)
if len(transactions)+len(stakingTransactions) != len(receipts) {
return errors.New("transaction+stakingTransactions and receipt count mismatch")
}

for j := 0; j < len(receipts); j++ {
for j := 0; j < len(transactions); j++ {
// The transaction hash can be retrieved from the transaction itself
receipts[j].TxHash = transactions[j].Hash()

Expand All @@ -942,6 +942,27 @@ func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts ty
logIndex++
}
}
// in a block, txns are processed before staking txns
for j := len(transactions); j < len(transactions)+len(stakingTransactions); j++ {
// The transaction hash can be retrieved from the staking transaction itself
receipts[j].TxHash = stakingTransactions[j].Hash()

// The used gas can be calculated based on previous receipts
if j == 0 {
receipts[j].GasUsed = receipts[j].CumulativeGasUsed
} else {
receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed
}
// The derived log fields can simply be set from the block and transaction
for k := 0; k < len(receipts[j].Logs); k++ {
receipts[j].Logs[k].BlockNumber = block.NumberU64()
receipts[j].Logs[k].BlockHash = block.Hash()
receipts[j].Logs[k].TxHash = receipts[j].TxHash
receipts[j].Logs[k].TxIndex = uint(j) + uint(len(transactions))
receipts[j].Logs[k].Index = logIndex
logIndex++
}
}
return nil
}

Expand Down Expand Up @@ -1360,6 +1381,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifyHeaders bool) (int,
Str("hash", block.Hash().Hex()).
Int("uncles", len(block.Uncles())).
Int("txs", len(block.Transactions())).
Int("stakingTxs", len(block.StakingTransactions())).
Uint64("gas", block.GasUsed()).
Str("elapsed", common.PrettyDuration(time.Since(bstart)).String()).
Logger()
Expand Down
5 changes: 4 additions & 1 deletion core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
blockfactory "github.com/harmony-one/harmony/block/factory"
mock "github.com/harmony-one/harmony/core/rawdb/mock"
"github.com/harmony-one/harmony/core/types"
staking "github.com/harmony-one/harmony/staking/types"
)

// Tests block header storage and retrieval operations.
Expand Down Expand Up @@ -142,7 +143,9 @@ func TestBlockStorage(t *testing.T) {
}
if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
t.Fatalf("Stored body not found")
} else if types.DeriveSha(types.Transactions(entry.Transactions())) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles()) != types.CalcUncleHash(block.Uncles()) {
} else if types.DeriveSha(types.Transactions(entry.Transactions())) != types.DeriveSha(block.Transactions()) ||
types.DeriveSha(staking.StakingTransactions(entry.StakingTransactions())) != types.DeriveSha(block.StakingTransactions()) ||
types.CalcUncleHash(entry.Uncles()) != types.CalcUncleHash(block.Uncles()) {
t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
}
//if actual, err := ReadEpochBlockNumber(db, big.NewInt(0)); err != nil {
Expand Down
1 change: 1 addition & 0 deletions node/node_cross_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (node *Node) VerifyBlockCrossLinks(block *types.Block) error {
"crossLinkShard", crossLink.ShardID(),
"crossLinkBlock", crossLink.BlockNum(),
"numTx", len(block.Transactions()),
"numStakingTx", len(block.StakingTransactions()),
).WithCause(err)
}
}
Expand Down
1 change: 1 addition & 0 deletions node/node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error {
return ctxerror.New("[VerifyNewBlock] Cannot Verify New Block!!!",
"blockHash", newBlock.Hash(),
"numTx", len(newBlock.Transactions()),
"numStakingTx", len(newBlock.StakingTransactions()),
).WithCause(err)
}

Expand Down

0 comments on commit d12c329

Please sign in to comment.