Tx submission epoch checks (#3421)

* [node] Add Cx epoch check

* Report invalid epoch for staking tx

Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>

* [rosetta] Add Cx epoch check in Metadata request

Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>

* [node] Fix lint

Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
pull/3425/head
Daniel Van Der Maden 4 years ago committed by GitHub
parent 13051c4cf8
commit 2ead9d7d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      node/node.go
  2. 14
      rosetta/services/construction_check.go

@ -204,10 +204,16 @@ func (node *Node) tryBroadcastStaking(stakingTx *staking.StakingTransaction) {
// Add new transactions to the pending transaction list.
func (node *Node) addPendingTransactions(newTxs types.Transactions) []error {
poolTxs := types.PoolTransactions{}
errs := []error{}
acceptCx := node.Blockchain().Config().AcceptsCrossTx(node.Blockchain().CurrentHeader().Epoch())
for _, tx := range newTxs {
if tx.ShardID() != tx.ToShardID() && !acceptCx {
errs = append(errs, errors.WithMessage(errInvalidEpoch, "cross-shard tx not accepted yet"))
continue
}
poolTxs = append(poolTxs, tx)
}
errs := node.TxPool.AddRemotes(poolTxs)
errs = append(errs, node.TxPool.AddRemotes(poolTxs)...)
pendingCount, queueCount := node.TxPool.Stats()
utils.Logger().Info().
@ -221,8 +227,8 @@ func (node *Node) addPendingTransactions(newTxs types.Transactions) []error {
// Add new staking transactions to the pending staking transaction list.
func (node *Node) addPendingStakingTransactions(newStakingTxs staking.StakingTransactions) []error {
if node.NodeConfig.ShardID == shard.BeaconChainShardID &&
node.Blockchain().Config().IsPreStaking(node.Blockchain().CurrentHeader().Epoch()) {
if node.NodeConfig.ShardID == shard.BeaconChainShardID {
if node.Blockchain().Config().IsPreStaking(node.Blockchain().CurrentHeader().Epoch()) {
poolTxs := types.PoolTransactions{}
for _, tx := range newStakingTxs {
poolTxs = append(poolTxs, tx)
@ -236,7 +242,13 @@ func (node *Node) addPendingStakingTransactions(newStakingTxs staking.StakingTra
Msg("Got more staking transactions")
return errs
}
return make([]error, len(newStakingTxs))
return []error{
errors.WithMessage(errInvalidEpoch, "staking txs not accepted yet"),
}
}
return []error{
errors.WithMessage(errInvalidShard, fmt.Sprintf("txs only valid on shard %v", shard.BeaconChainShardID)),
}
}
// AddPendingStakingTransaction staking transactions
@ -248,13 +260,17 @@ func (node *Node) AddPendingStakingTransaction(
var err error
for i := range errs {
if errs[i] != nil {
utils.Logger().Info().Err(errs[i]).Msg("[AddPendingStakingTransaction] Failed adding new staking transaction")
utils.Logger().Info().
Err(errs[i]).
Msg("[AddPendingStakingTransaction] Failed adding new staking transaction")
err = errs[i]
break
}
}
if err == nil || node.BroadcastInvalidTx {
utils.Logger().Info().Str("Hash", newStakingTx.Hash().Hex()).Msg("Broadcasting Staking Tx")
utils.Logger().Info().
Str("Hash", newStakingTx.Hash().Hex()).
Msg("Broadcasting Staking Tx")
node.tryBroadcastStaking(newStakingTx)
}
return err
@ -361,6 +377,8 @@ var (
errWrongShardID = errors.New("wrong shard id")
errInvalidNodeMsg = errors.New("invalid node message")
errIgnoreBeaconMsg = errors.New("ignore beacon sync block")
errInvalidEpoch = errors.New("invalid epoch for transaction")
errInvalidShard = errors.New("invalid shard")
)
// validateNodeMessage validate node message

@ -172,6 +172,20 @@ func (s *ConstructAPI) ConstructionMetadata(
})
}
currBlock, err := s.hmy.BlockByNumber(ctx, ethRpc.LatestBlockNumber)
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
if options.OperationType == common.NativeCrossShardTransferOperation &&
!s.hmy.BlockChain.Config().AcceptsCrossTx(currBlock.Epoch()) {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "cross-shard transaction is not accepted yet",
})
}
data := hexutil.Bytes{}
if options.TransactionMetadata.Data != nil {
var err error

Loading…
Cancel
Save