Merge pull request #2015 from flicker-harmony/pr_add_timestamp

Add timestamp to transactions json
pull/2024/head
flicker-harmony 5 years ago committed by GitHub
commit 9ffa6e1be4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      internal/hmyapi/transactionpool.go
  2. 32
      internal/hmyapi/types.go

@ -110,8 +110,12 @@ func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context
func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction { func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction {
// Try to return an already finalized transaction // Try to return an already finalized transaction
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash) tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
block, _ := s.b.GetBlock(ctx, blockHash)
if block == nil {
return nil
}
if tx != nil { if tx != nil {
return newRPCTransaction(tx, blockHash, blockNumber, index) return newRPCTransaction(tx, blockHash, blockNumber, block.Time().Uint64(), index)
} }
// No finalized transaction, try to retrieve it from the pool // No finalized transaction, try to retrieve it from the pool
if tx = s.b.GetPoolTransaction(hash); tx != nil { if tx = s.b.GetPoolTransaction(hash); tx != nil {
@ -125,8 +129,12 @@ func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, has
func (s *PublicTransactionPoolAPI) GetStakingTransactionByHash(ctx context.Context, hash common.Hash) *RPCStakingTransaction { func (s *PublicTransactionPoolAPI) GetStakingTransactionByHash(ctx context.Context, hash common.Hash) *RPCStakingTransaction {
// Try to return an already finalized transaction // Try to return an already finalized transaction
stx, blockHash, blockNumber, index := rawdb.ReadStakingTransaction(s.b.ChainDb(), hash) stx, blockHash, blockNumber, index := rawdb.ReadStakingTransaction(s.b.ChainDb(), hash)
block, _ := s.b.GetBlock(ctx, blockHash)
if block == nil {
return nil
}
if stx != nil { if stx != nil {
return newRPCStakingTransaction(stx, blockHash, blockNumber, index) return newRPCStakingTransaction(stx, blockHash, blockNumber, block.Time().Uint64(), index)
} }
return nil return nil
} }

@ -23,6 +23,7 @@ type RPCTransaction struct {
BlockHash common.Hash `json:"blockHash"` BlockHash common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"` BlockNumber *hexutil.Big `json:"blockNumber"`
From string `json:"from"` From string `json:"from"`
Timestamp hexutil.Uint64 `json:"timestamp"`
Gas hexutil.Uint64 `json:"gas"` Gas hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"` GasPrice *hexutil.Big `json:"gasPrice"`
Hash common.Hash `json:"hash"` Hash common.Hash `json:"hash"`
@ -43,6 +44,7 @@ type RPCStakingTransaction struct {
BlockHash common.Hash `json:"blockHash"` BlockHash common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"` BlockNumber *hexutil.Big `json:"blockNumber"`
From string `json:"from"` From string `json:"from"`
Timestamp hexutil.Uint64 `json:"timestamp"`
Gas hexutil.Uint64 `json:"gas"` Gas hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"` GasPrice *hexutil.Big `json:"gasPrice"`
Hash common.Hash `json:"hash"` Hash common.Hash `json:"hash"`
@ -192,7 +194,7 @@ func newRPCValidator(validator *types2.Validator) *RPCValidator {
// newRPCTransaction returns a transaction that will serialize to the RPC // newRPCTransaction returns a transaction that will serialize to the RPC
// representation, with the given location metadata set (if available). // representation, with the given location metadata set (if available).
func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCTransaction { func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, timestamp uint64, index uint64) *RPCTransaction {
var signer types.Signer = types.FrontierSigner{} var signer types.Signer = types.FrontierSigner{}
if tx.Protected() { if tx.Protected() {
signer = types.NewEIP155Signer(tx.ChainID()) signer = types.NewEIP155Signer(tx.ChainID())
@ -208,6 +210,7 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
Value: (*hexutil.Big)(tx.Value()), Value: (*hexutil.Big)(tx.Value()),
ShardID: tx.ShardID(), ShardID: tx.ShardID(),
ToShardID: tx.ToShardID(), ToShardID: tx.ToShardID(),
Timestamp: hexutil.Uint64(timestamp),
V: (*hexutil.Big)(v), V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r), R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s), S: (*hexutil.Big)(s),
@ -239,7 +242,7 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
// newRPCStakingTransaction returns a transaction that will serialize to the RPC // newRPCStakingTransaction returns a transaction that will serialize to the RPC
// representation, with the given location metadata set (if available). // representation, with the given location metadata set (if available).
func newRPCStakingTransaction(tx *types2.StakingTransaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCStakingTransaction { func newRPCStakingTransaction(tx *types2.StakingTransaction, blockHash common.Hash, blockNumber uint64, timestamp uint64, index uint64) *RPCStakingTransaction {
from, _ := tx.SenderAddress() from, _ := tx.SenderAddress()
v, r, s := tx.RawSignatureValues() v, r, s := tx.RawSignatureValues()
@ -302,15 +305,16 @@ func newRPCStakingTransaction(tx *types2.StakingTransaction, blockHash common.Ha
} }
result := &RPCStakingTransaction{ result := &RPCStakingTransaction{
Gas: hexutil.Uint64(tx.Gas()), Gas: hexutil.Uint64(tx.Gas()),
GasPrice: (*hexutil.Big)(tx.Price()), GasPrice: (*hexutil.Big)(tx.Price()),
Hash: tx.Hash(), Hash: tx.Hash(),
Nonce: hexutil.Uint64(tx.Nonce()), Nonce: hexutil.Uint64(tx.Nonce()),
V: (*hexutil.Big)(v), Timestamp: hexutil.Uint64(timestamp),
R: (*hexutil.Big)(r), V: (*hexutil.Big)(v),
S: (*hexutil.Big)(s), R: (*hexutil.Big)(r),
Type: stakingTxType, S: (*hexutil.Big)(s),
Msg: fields, Type: stakingTxType,
Msg: fields,
} }
if blockHash != (common.Hash{}) { if blockHash != (common.Hash{}) {
result.BlockHash = blockHash result.BlockHash = blockHash
@ -329,7 +333,7 @@ func newRPCStakingTransaction(tx *types2.StakingTransaction, blockHash common.Ha
// newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation // newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation
func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction { func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction {
return newRPCTransaction(tx, common.Hash{}, 0, 0) return newRPCTransaction(tx, common.Hash{}, 0, 0, 0)
} }
// RPCBlock represents a block that will serialize to the RPC representation of a block // RPCBlock represents a block that will serialize to the RPC representation of a block
@ -448,7 +452,7 @@ func newRPCTransactionFromBlockIndex(b *types.Block, index uint64) *RPCTransacti
if index >= uint64(len(txs)) { if index >= uint64(len(txs)) {
return nil return nil
} }
return newRPCTransaction(txs[index], b.Hash(), b.NumberU64(), index) return newRPCTransaction(txs[index], b.Hash(), b.NumberU64(), b.Time().Uint64(), index)
} }
// newRPCStakingTransactionFromBlockHash returns a transaction that will serialize to the RPC representation. // newRPCStakingTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
@ -467,7 +471,7 @@ func newRPCStakingTransactionFromBlockIndex(b *types.Block, index uint64) *RPCSt
if index >= uint64(len(txs)) { if index >= uint64(len(txs)) {
return nil return nil
} }
return newRPCStakingTransaction(txs[index], b.Hash(), b.NumberU64(), index) return newRPCStakingTransaction(txs[index], b.Hash(), b.NumberU64(), b.Time().Uint64(), index)
} }
// CallArgs represents the arguments for a call. // CallArgs represents the arguments for a call.

Loading…
Cancel
Save