The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/rpc/transaction.go

753 lines
23 KiB

Node API Refactor - pt2 (Stage 2.2 of Node API Overhaul) (#3259) * [rpc] Refactor - expose RPC functions in rpc package Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt1 * Move apiv1 into rpc package as baseline * Create version enum * Refactor API creation to use version enum Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rejoin GetBlockByNumber & GetBlockByHash * Both RPC versions are supported in 1 function * Created types directory for v1 & v2 response types * Added framework to make rpc layer more clear and legible Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt2 * Rejoin blockchain.go functions * Create legacy service * Improve inline documentation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Use StructuredResponse when returning an exposed type from RPC * Remove addrlock.go * Add RPCBlock creation with exposed struct for easy utility from consumers * Update blockchain.go to reflect changes of RPCBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Create types for RPCs that are returned. * Reorganize types to appropriate v1 & v2 package. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt4 * Add context to all RPCs * Add response switches based on different versions of the API * Add debug logs for RPC that don't return errors Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt5 * Add versions switches to all transaction related RPCs * Integrate response with newly defined structures * Add debug messages for RPCs that don't return errors * Add inline documentation for all transaction related RPCs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt6 * Remove apiv2 * Move StructuredResponse & TxHistoryArgs to main rpc types.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt7 * Create contract, pool, staking & transaction services * Move apis into correct services * Remove util.go (functions are now baked into RPCs in pool.go) * Remove transaction.go (functions/methods are distributed among created services) * Update changes for StructuredResponse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Add rpc server start/stop to pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Refactor - use new RPC start / stop for servers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Refactor - rename network.go to net.go to match rpc * Change names for consistency with rpc pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * fix go lint & go imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Fix GetTransactionReceipt for staking txs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rename v1 & v2 struct to not have leading 'RPC' * Update comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - make defaultBlocksPeriod be the num blocks per epoch Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
package rpc
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/harmony/core/rawdb"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/hmy"
internal_common "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils"
v1 "github.com/harmony-one/harmony/rpc/v1"
v2 "github.com/harmony-one/harmony/rpc/v2"
staking "github.com/harmony-one/harmony/staking/types"
)
const (
defaultPageSize = uint32(100)
)
// PublicTransactionService provides an API to access Harmony's transaction service.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicTransactionService struct {
hmy *hmy.Harmony
version Version
}
// NewPublicTransactionAPI creates a new API for the RPC interface
func NewPublicTransactionAPI(hmy *hmy.Harmony, version Version) rpc.API {
return rpc.API{
Namespace: version.Namespace(),
Version: APIVersion,
Service: &PublicTransactionService{hmy, version},
Public: true,
}
}
// GetAccountNonce returns the nonce value of the given address for the given block number
func (s *PublicTransactionService) GetAccountNonce(
ctx context.Context, address string, blockNumber BlockNumber,
) (uint64, error) {
// Process number based on version
blockNum := blockNumber.EthBlockNumber()
// Response output is the same for all versions
addr := internal_common.ParseAddr(address)
return s.hmy.GetAccountNonce(ctx, addr, blockNum)
}
// GetTransactionCount returns the number of transactions the given address has sent for the given block number.
// Legacy for apiv1. For apiv2, please use getAccountNonce/getPoolNonce/getTransactionsCount/getStakingTransactionsCount apis for
// more granular transaction counts queries
// Note that the return type is an interface to account for the different versions
func (s *PublicTransactionService) GetTransactionCount(
ctx context.Context, addr string, blockNumber BlockNumber,
) (response interface{}, err error) {
// Process arguments based on version
blockNum := blockNumber.EthBlockNumber()
address := internal_common.ParseAddr(addr)
// Fetch transaction count
var nonce uint64
if blockNum == rpc.PendingBlockNumber {
// Ask transaction pool for the nonce which includes pending transactions
nonce, err = s.hmy.GetPoolNonce(ctx, address)
if err != nil {
return nil, err
}
} else {
// Resolve block number and use its state to ask for the nonce
state, _, err := s.hmy.StateAndHeaderByNumber(ctx, blockNum)
if err != nil {
return nil, err
}
if state == nil {
return nil, fmt.Errorf("state not found")
}
if state.Error() != nil {
return nil, state.Error()
}
nonce = state.GetNonce(address)
}
// Format response according to version
switch s.version {
case V1:
return (hexutil.Uint64)(nonce), nil
case V2:
return nonce, nil
default:
return nil, ErrUnknownRPCVersion
}
}
// GetTransactionsCount returns the number of regular transactions from genesis of input type ("SENT", "RECEIVED", "ALL")
func (s *PublicTransactionService) GetTransactionsCount(
ctx context.Context, address, txType string,
) (count uint64, err error) {
if !strings.HasPrefix(address, "one1") {
// Handle hex address
addr := internal_common.ParseAddr(address)
address, err = internal_common.AddressToBech32(addr)
if err != nil {
return 0, err
}
}
// Response output is the same for all versions
return s.hmy.GetTransactionsCount(address, txType)
}
// GetStakingTransactionsCount returns the number of staking transactions from genesis of input type ("SENT", "RECEIVED", "ALL")
func (s *PublicTransactionService) GetStakingTransactionsCount(
ctx context.Context, address, txType string,
) (count uint64, err error) {
if !strings.HasPrefix(address, "one1") {
// Handle hex address
addr := internal_common.ParseAddr(address)
address, err = internal_common.AddressToBech32(addr)
if err != nil {
return 0, err
}
}
// Response output is the same for all versions
return s.hmy.GetStakingTransactionsCount(address, txType)
}
// EstimateGas returns an estimate of the amount of gas needed to execute the
// given transaction against the current pending block.
func (s *PublicTransactionService) EstimateGas(
ctx context.Context, args CallArgs,
) (hexutil.Uint64, error) {
Rosetta Implementation - pt4 (Stage 3.4 of Node API Overhaul) (#3380) * [rosetta] Add /construct function framework Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add network ID check to all construct endpoints Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add UnsupportedCurveTypeError Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement ConstructionDerive & add ConstructAPI router Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make recover middleware the outermost middleware Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Correct error for block not found Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Expose EstimateGas Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add InvalidTransactionConstructionError * Add GetValidConstructionOperations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make TransactionMetadata optional ptr * Add UnmarshalFromInterface for TransactionMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add InvalidTransactionConstructionError to network response Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add ConstructionPreprocess & ConstructionMetadata skeleton * Add Helper methods for said functions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add GasPrice to ConstructMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make suggested gas fee & price its own fn Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Nit - fix comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add To & From Account ID in tx metadata * remove getAmountFromUndelegateMessage * make contract address a valid account identifier * remove needless metadata in non-staking operations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add error report for b32 error in newAccountIdentifier Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Simplify assertValidNetworkIdentifier Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Use sdk MapMarshall instead of rpc structured response * remove unused GetValidConstructionOperations * Add CurrencyHash Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement assertValidTransferOperationsAndGetTxAccounts * refactor usages of rpc structured response for sdk map marshall * add account identifier checks for transaction metadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Max related operation check more general for tx op check * refactor names for readability Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add & Expose OperationComponents * Remove txAccounts & add OperationComponents to metadata for quicker processing * Remove From & To acc ID from TransactionMetadata as it's in OperationComponents * Update tests for changes Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add tx type to OperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add logs to TransactionMetadata * Enforce Operation type uniqueness invariant for each transaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add error for 3+ operations in getOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Expose GetOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add CrossShardTransactionOperationMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add staking operation medata type declaration Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix error messages & update comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement getCrossShardOperationComponents * rename txAccs to components for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement getContractCreationOperationComponents * Fix some edge case optional checks for operation validation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [go.mod] Bump rosetta version to 0.4.4 & dependent libs * Update broken rosetta structs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add ChainID to ConstructMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename PreStakingBlockRewardOperation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Init operation_components.go & move respective code Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Refactor formatTransaction for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Move maxNumOfConstructionOps to operation_components.go * Clarify unsupported type err msg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement framework for ConstructionPayloads * Remove currying OperationComponents in metadata as PublicKeys is now part of the request in the necessary endpoints * Add respective checks for added PublicKeys * Make getAddressFromPublicKey more general & update tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add getAddress and update respective functions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Create transaction_construction.go & impl ConstructTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add unmarshal check for ConstructMetadata & ConstructMetadataOptions * Remove constructTransaction * Wrap unsigned transaction with UnsignedTransaction to include intended signer * Add framework for ConstructionParse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add getSigningPayload method to construction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal/commonm] Add MustGeneratePrivateKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement parseUnsignedTransaction * Add unpackWrappedTransactionFromHexString & refactor rlp encoding of transactions * Make getSigningPayload return a list Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add TestUnpackWrappedTransactionFromHexString Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement parseSignedTransaction * Move unpackWrappedTransactionFromHexString to ConstructionParse before parsing transactions bases on if it's signed or not Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Nit - fix WrappedTransaction comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add extra negative tests for unpackWrappedTransactionFromHexString Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Finish initial Construct implementation * Implement ConstructionCombine * Implement ConstructionHash * Implement ConstructionSubmit * Ensure RLPbytes & From is present in WrappedTransaction when unpackWrappedTransactionFromHexString is called Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Refactor construction into separate files * Move tests into appropriate files Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement construction_check_test.go * Update edge case for getSuggestedFeeAndPrice found during testing Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement construction_parse_test.go * Clarify names for construction_create_test.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix ConstructionParse to not use crypto.PubkeyToAddress Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix UnmarshalFromInterface for CrossShardTransactionOperationMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add operation_components_test.go * Implement TestGetContractCreationOperationComponents * Implement TestGetCrossShardOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Finish operation_components_test.go * Implement TestGetTransferOperationComponents * Implement TestGetOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix big number checks for getTransferOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement transaction_construction_test.go * Implement TestConstructPlainTransaction * Implement TestConstructCrossShardTransaction * Implement TestConstructContractCreationTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add shard ID checks for constructCrossShardTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add bad curve test for TestGetAddressFromPublicKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add nil error catches for Construction API * Fix nits in messages as seen during pass * Add DefaultGasLimit Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add trace message with version to all err msgs * Add NewError unit test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add TestGetAddressFromKnownPublicKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Fix possible nil ptr crash for GetBlockSigners Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Edge case bug fixes * Create empty TransactionMetdata if no metadata is provided in operations. Also update tests to correctly account for the behavior. * In pre-staking era make current block one less than the absolute latest to guarentee calculation of pres-taking eara block rewards. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix unpackWrappedTransactionFromHexString * Rename function to unpackWrappedTransactionFromString * Update respective tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make parsed transaction statuses empty Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Remove needles list initialization Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add SignatureType & SignedPayloadLength with checks Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix genesis network status crash Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Correct error message dumps for transaction_construction.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add OperationType to ConstructMetadataOptions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix rosetta service rebase Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Fix estimate gas fail check Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add formatNegativeValue to ensure there's only 1 zero * Correct regression found in constructCrossShardTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add contract creation estimate gas hack Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add DefaultSenderAddress for transaction formatter * Update Construction API parser to reflect change Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Address PR comments * Add GetCallStackInfo to internal utils * Add EstimateGas TODO in RPC package * Remove DefaultGasLimit to use param gas limit Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add checks to tx formatter for nil to addr * Remove needless block check for genesisBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Remove EstimatedGasUsed from WrappedTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
gas, err := EstimateGas(ctx, s.hmy, args, nil)
Node API Refactor - pt2 (Stage 2.2 of Node API Overhaul) (#3259) * [rpc] Refactor - expose RPC functions in rpc package Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt1 * Move apiv1 into rpc package as baseline * Create version enum * Refactor API creation to use version enum Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rejoin GetBlockByNumber & GetBlockByHash * Both RPC versions are supported in 1 function * Created types directory for v1 & v2 response types * Added framework to make rpc layer more clear and legible Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt2 * Rejoin blockchain.go functions * Create legacy service * Improve inline documentation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Use StructuredResponse when returning an exposed type from RPC * Remove addrlock.go * Add RPCBlock creation with exposed struct for easy utility from consumers * Update blockchain.go to reflect changes of RPCBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Create types for RPCs that are returned. * Reorganize types to appropriate v1 & v2 package. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt4 * Add context to all RPCs * Add response switches based on different versions of the API * Add debug logs for RPC that don't return errors Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt5 * Add versions switches to all transaction related RPCs * Integrate response with newly defined structures * Add debug messages for RPCs that don't return errors * Add inline documentation for all transaction related RPCs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt6 * Remove apiv2 * Move StructuredResponse & TxHistoryArgs to main rpc types.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt7 * Create contract, pool, staking & transaction services * Move apis into correct services * Remove util.go (functions are now baked into RPCs in pool.go) * Remove transaction.go (functions/methods are distributed among created services) * Update changes for StructuredResponse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Add rpc server start/stop to pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Refactor - use new RPC start / stop for servers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Refactor - rename network.go to net.go to match rpc * Change names for consistency with rpc pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * fix go lint & go imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Fix GetTransactionReceipt for staking txs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rename v1 & v2 struct to not have leading 'RPC' * Update comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - make defaultBlocksPeriod be the num blocks per epoch Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
if err != nil {
return 0, err
}
// Response output is the same for all versions
return (hexutil.Uint64)(gas), nil
}
// GetTransactionByHash returns the plain transaction for the given hash
func (s *PublicTransactionService) GetTransactionByHash(
ctx context.Context, hash common.Hash,
) (StructuredResponse, error) {
// Try to return an already finalized transaction
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.hmy.ChainDb(), hash)
if tx == nil {
utils.Logger().Debug().
Err(errors.Wrapf(ErrTransactionNotFound, "hash %v", hash.String())).
Msgf("%v error at %v", LogTag, "GetTransactionByHash")
// Legacy behavior is to not return RPC errors
return nil, nil
}
block, err := s.hmy.GetBlock(ctx, blockHash)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetTransactionByHash")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format the response according to the version
switch s.version {
case V1:
tx, err := v1.NewTransaction(tx, blockHash, blockNumber, block.Time().Uint64(), index)
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewTransaction(tx, blockHash, blockNumber, block.Time().Uint64(), index)
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetStakingTransactionByHash returns the staking transaction for the given hash
func (s *PublicTransactionService) GetStakingTransactionByHash(
ctx context.Context, hash common.Hash,
) (StructuredResponse, error) {
// Try to return an already finalized transaction
stx, blockHash, blockNumber, index := rawdb.ReadStakingTransaction(s.hmy.ChainDb(), hash)
if stx == nil {
utils.Logger().Debug().
Err(errors.Wrapf(ErrTransactionNotFound, "hash %v", hash.String())).
Msgf("%v error at %v", LogTag, "GetStakingTransactionByHash")
// Legacy behavior is to not return RPC errors
return nil, nil
}
block, err := s.hmy.GetBlock(ctx, blockHash)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetStakingTransactionByHash")
// Legacy behavior is to not return RPC errors
return nil, nil
}
switch s.version {
case V1:
tx, err := v1.NewStakingTransaction(stx, blockHash, blockNumber, block.Time().Uint64(), index)
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewStakingTransaction(stx, blockHash, blockNumber, block.Time().Uint64(), index)
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetTransactionsHistory returns the list of transactions hashes that involve a particular address.
func (s *PublicTransactionService) GetTransactionsHistory(
ctx context.Context, args TxHistoryArgs,
) (StructuredResponse, error) {
// Fetch transaction history
var address string
var result []common.Hash
var err error
if strings.HasPrefix(args.Address, "one1") {
address = args.Address
} else {
addr := internal_common.ParseAddr(args.Address)
address, err = internal_common.AddressToBech32(addr)
if err != nil {
return nil, err
}
}
hashes, err := s.hmy.GetTransactionsHistory(address, args.TxType, args.Order)
if err != nil {
return nil, err
}
result = returnHashesWithPagination(hashes, args.PageIndex, args.PageSize)
// Just hashes have same response format for all versions
if !args.FullTx {
return StructuredResponse{"transactions": result}, nil
}
// Full transactions have different response format
txs := []StructuredResponse{}
for _, hash := range result {
tx, err := s.GetTransactionByHash(ctx, hash)
if err == nil {
txs = append(txs, tx)
} else {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetTransactionsHistory")
// Legacy behavior is to not return RPC errors
}
}
return StructuredResponse{"transactions": txs}, nil
}
// GetStakingTransactionsHistory returns the list of transactions hashes that involve a particular address.
func (s *PublicTransactionService) GetStakingTransactionsHistory(
ctx context.Context, args TxHistoryArgs,
) (StructuredResponse, error) {
// Fetch transaction history
var address string
var result []common.Hash
var err error
if strings.HasPrefix(args.Address, "one1") {
address = args.Address
} else {
addr := internal_common.ParseAddr(args.Address)
address, err = internal_common.AddressToBech32(addr)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetStakingTransactionsHistory")
// Legacy behavior is to not return RPC errors
return nil, nil
}
}
hashes, err := s.hmy.GetStakingTransactionsHistory(address, args.TxType, args.Order)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetStakingTransactionsHistory")
// Legacy behavior is to not return RPC errors
return nil, nil
}
result = returnHashesWithPagination(hashes, args.PageIndex, args.PageSize)
// Just hashes have same response format for all versions
if !args.FullTx {
return StructuredResponse{"staking_transactions": result}, nil
}
// Full transactions have different response format
txs := []StructuredResponse{}
for _, hash := range result {
tx, err := s.GetStakingTransactionByHash(ctx, hash)
if err == nil {
txs = append(txs, tx)
} else {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetStakingTransactionsHistory")
// Legacy behavior is to not return RPC errors
}
}
return StructuredResponse{"staking_transactions": txs}, nil
}
// GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number.
// Note that the return type is an interface to account for the different versions
func (s *PublicTransactionService) GetBlockTransactionCountByNumber(
ctx context.Context, blockNumber BlockNumber,
) (interface{}, error) {
// Process arguments based on version
blockNum := blockNumber.EthBlockNumber()
// Fetch block
block, err := s.hmy.BlockByNumber(ctx, blockNum)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetBlockTransactionCountByNumber")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
return hexutil.Uint(len(block.Transactions())), nil
case V2:
return len(block.Transactions()), nil
default:
return nil, ErrUnknownRPCVersion
}
}
// GetBlockTransactionCountByHash returns the number of transactions in the block with the given hash.
// Note that the return type is an interface to account for the different versions
func (s *PublicTransactionService) GetBlockTransactionCountByHash(
ctx context.Context, blockHash common.Hash,
) (interface{}, error) {
// Fetch block
block, err := s.hmy.GetBlock(ctx, blockHash)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetBlockTransactionCountByHash")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
return hexutil.Uint(len(block.Transactions())), nil
case V2:
return len(block.Transactions()), nil
default:
return nil, ErrUnknownRPCVersion
}
}
// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index.
func (s *PublicTransactionService) GetTransactionByBlockNumberAndIndex(
ctx context.Context, blockNumber BlockNumber, index TransactionIndex,
) (StructuredResponse, error) {
// Process arguments based on version
blockNum := blockNumber.EthBlockNumber()
// Fetch Block
block, err := s.hmy.BlockByNumber(ctx, blockNum)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetTransactionByBlockNumberAndIndex")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
tx, err := v1.NewTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index.
func (s *PublicTransactionService) GetTransactionByBlockHashAndIndex(
ctx context.Context, blockHash common.Hash, index TransactionIndex,
) (StructuredResponse, error) {
// Fetch Block
block, err := s.hmy.GetBlock(ctx, blockHash)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetTransactionByBlockHashAndIndex")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
tx, err := v1.NewTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetBlockStakingTransactionCountByNumber returns the number of staking transactions in the block with the given block number.
// Note that the return type is an interface to account for the different versions
func (s *PublicTransactionService) GetBlockStakingTransactionCountByNumber(
ctx context.Context, blockNumber BlockNumber,
) (interface{}, error) {
// Process arguments based on version
blockNum := blockNumber.EthBlockNumber()
// Fetch block
block, err := s.hmy.BlockByNumber(ctx, blockNum)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetBlockStakingTransactionCountByNumber")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
return hexutil.Uint(len(block.StakingTransactions())), nil
case V2:
return len(block.StakingTransactions()), nil
default:
return nil, ErrUnknownRPCVersion
}
}
// GetBlockStakingTransactionCountByHash returns the number of staking transactions in the block with the given hash.
// Note that the return type is an interface to account for the different versions
func (s *PublicTransactionService) GetBlockStakingTransactionCountByHash(
ctx context.Context, blockHash common.Hash,
) (interface{}, error) {
// Fetch block
block, err := s.hmy.GetBlock(ctx, blockHash)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetBlockStakingTransactionCountByHash")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
return hexutil.Uint(len(block.StakingTransactions())), nil
case V2:
return len(block.StakingTransactions()), nil
default:
return nil, ErrUnknownRPCVersion
}
}
// GetStakingTransactionByBlockNumberAndIndex returns the staking transaction for the given block number and index.
func (s *PublicTransactionService) GetStakingTransactionByBlockNumberAndIndex(
ctx context.Context, blockNumber BlockNumber, index TransactionIndex,
) (StructuredResponse, error) {
// Process arguments based on version
blockNum := blockNumber.EthBlockNumber()
// Fetch Block
block, err := s.hmy.BlockByNumber(ctx, blockNum)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetStakingTransactionByBlockNumberAndIndex")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
tx, err := v1.NewStakingTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewStakingTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetStakingTransactionByBlockHashAndIndex returns the transaction for the given block hash and index.
func (s *PublicTransactionService) GetStakingTransactionByBlockHashAndIndex(
ctx context.Context, blockHash common.Hash, index TransactionIndex,
) (StructuredResponse, error) {
// Fetch Block
block, err := s.hmy.GetBlock(ctx, blockHash)
if err != nil {
utils.Logger().Debug().
Err(err).
Msgf("%v error at %v", LogTag, "GetStakingTransactionByBlockHashAndIndex")
// Legacy behavior is to not return RPC errors
return nil, nil
}
// Format response according to version
switch s.version {
case V1:
tx, err := v1.NewStakingTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewStakingTransactionFromBlockIndex(block, uint64(index))
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (s *PublicTransactionService) GetTransactionReceipt(
ctx context.Context, hash common.Hash,
) (StructuredResponse, error) {
// Fetch receipt for plain & staking transaction
var tx *types.Transaction
var stx *staking.StakingTransaction
var blockHash common.Hash
var blockNumber, index uint64
tx, blockHash, blockNumber, index = rawdb.ReadTransaction(s.hmy.ChainDb(), hash)
if tx == nil {
stx, blockHash, blockNumber, index = rawdb.ReadStakingTransaction(s.hmy.ChainDb(), hash)
if stx == nil {
return nil, nil
}
// if there both normal and staking transactions, add to index
if block, _ := s.hmy.GetBlock(ctx, blockHash); block != nil {
index = index + uint64(block.Transactions().Len())
}
Node API Refactor - pt2 (Stage 2.2 of Node API Overhaul) (#3259) * [rpc] Refactor - expose RPC functions in rpc package Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt1 * Move apiv1 into rpc package as baseline * Create version enum * Refactor API creation to use version enum Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rejoin GetBlockByNumber & GetBlockByHash * Both RPC versions are supported in 1 function * Created types directory for v1 & v2 response types * Added framework to make rpc layer more clear and legible Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt2 * Rejoin blockchain.go functions * Create legacy service * Improve inline documentation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Use StructuredResponse when returning an exposed type from RPC * Remove addrlock.go * Add RPCBlock creation with exposed struct for easy utility from consumers * Update blockchain.go to reflect changes of RPCBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Create types for RPCs that are returned. * Reorganize types to appropriate v1 & v2 package. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt4 * Add context to all RPCs * Add response switches based on different versions of the API * Add debug logs for RPC that don't return errors Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt5 * Add versions switches to all transaction related RPCs * Integrate response with newly defined structures * Add debug messages for RPCs that don't return errors * Add inline documentation for all transaction related RPCs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt6 * Remove apiv2 * Move StructuredResponse & TxHistoryArgs to main rpc types.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt7 * Create contract, pool, staking & transaction services * Move apis into correct services * Remove util.go (functions are now baked into RPCs in pool.go) * Remove transaction.go (functions/methods are distributed among created services) * Update changes for StructuredResponse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Add rpc server start/stop to pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Refactor - use new RPC start / stop for servers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Refactor - rename network.go to net.go to match rpc * Change names for consistency with rpc pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * fix go lint & go imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Fix GetTransactionReceipt for staking txs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rename v1 & v2 struct to not have leading 'RPC' * Update comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - make defaultBlocksPeriod be the num blocks per epoch Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
}
receipts, err := s.hmy.GetReceipts(ctx, blockHash)
if err != nil {
return nil, err
}
if len(receipts) <= int(index) {
return nil, nil
}
receipt := receipts[index]
// Format response according to version
var RPCReceipt interface{}
switch s.version {
case V1:
if tx == nil {
RPCReceipt, err = v1.NewReceipt(stx, blockHash, blockNumber, index, receipt)
} else {
RPCReceipt, err = v1.NewReceipt(tx, blockHash, blockNumber, index, receipt)
}
if err != nil {
return nil, err
}
return NewStructuredResponse(RPCReceipt)
case V2:
if tx == nil {
RPCReceipt, err = v2.NewReceipt(stx, blockHash, blockNumber, index, receipt)
} else {
RPCReceipt, err = v2.NewReceipt(tx, blockHash, blockNumber, index, receipt)
}
if err != nil {
return nil, err
}
return NewStructuredResponse(RPCReceipt)
default:
return nil, ErrUnknownRPCVersion
}
}
// GetCXReceiptByHash returns the transaction for the given hash
func (s *PublicTransactionService) GetCXReceiptByHash(
ctx context.Context, hash common.Hash,
) (StructuredResponse, error) {
if cx, blockHash, blockNumber, _ := rawdb.ReadCXReceipt(s.hmy.ChainDb(), hash); cx != nil {
// Format response according to version
switch s.version {
case V1:
tx, err := v1.NewCxReceipt(cx, blockHash, blockNumber)
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
case V2:
tx, err := v2.NewCxReceipt(cx, blockHash, blockNumber)
if err != nil {
return nil, err
}
return NewStructuredResponse(tx)
default:
return nil, ErrUnknownRPCVersion
}
}
utils.Logger().Debug().
Err(fmt.Errorf("unable to found CX receipt for tx %v", hash.String())).
Msgf("%v error at %v", LogTag, "GetCXReceiptByHash")
return nil, nil // Legacy behavior is to not return an error here
}
// ResendCx requests that the egress receipt for the given cross-shard
// transaction be sent to the destination shard for credit. This is used for
// unblocking a half-complete cross-shard transaction whose fund has been
// withdrawn already from the source shard but not credited yet in the
// destination account due to transient failures.
func (s *PublicTransactionService) ResendCx(ctx context.Context, txID common.Hash) (bool, error) {
_, success := s.hmy.ResendCx(ctx, txID)
// Response output is the same for all versions
return success, nil
}
// returnHashesWithPagination returns result with pagination (offset, page in TxHistoryArgs).
func returnHashesWithPagination(hashes []common.Hash, pageIndex uint32, pageSize uint32) []common.Hash {
size := defaultPageSize
if pageSize > 0 {
size = pageSize
}
if uint64(size)*uint64(pageIndex) >= uint64(len(hashes)) {
return make([]common.Hash, 0)
}
if uint64(size)*uint64(pageIndex)+uint64(size) > uint64(len(hashes)) {
return hashes[size*pageIndex:]
}
return hashes[size*pageIndex : size*pageIndex+size]
}
Rosetta Implementation - pt4 (Stage 3.4 of Node API Overhaul) (#3380) * [rosetta] Add /construct function framework Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add network ID check to all construct endpoints Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add UnsupportedCurveTypeError Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement ConstructionDerive & add ConstructAPI router Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make recover middleware the outermost middleware Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Correct error for block not found Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Expose EstimateGas Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add InvalidTransactionConstructionError * Add GetValidConstructionOperations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make TransactionMetadata optional ptr * Add UnmarshalFromInterface for TransactionMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add InvalidTransactionConstructionError to network response Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add ConstructionPreprocess & ConstructionMetadata skeleton * Add Helper methods for said functions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add GasPrice to ConstructMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make suggested gas fee & price its own fn Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Nit - fix comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add To & From Account ID in tx metadata * remove getAmountFromUndelegateMessage * make contract address a valid account identifier * remove needless metadata in non-staking operations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add error report for b32 error in newAccountIdentifier Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Simplify assertValidNetworkIdentifier Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Use sdk MapMarshall instead of rpc structured response * remove unused GetValidConstructionOperations * Add CurrencyHash Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement assertValidTransferOperationsAndGetTxAccounts * refactor usages of rpc structured response for sdk map marshall * add account identifier checks for transaction metadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Max related operation check more general for tx op check * refactor names for readability Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add & Expose OperationComponents * Remove txAccounts & add OperationComponents to metadata for quicker processing * Remove From & To acc ID from TransactionMetadata as it's in OperationComponents * Update tests for changes Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add tx type to OperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add logs to TransactionMetadata * Enforce Operation type uniqueness invariant for each transaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add error for 3+ operations in getOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Expose GetOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add CrossShardTransactionOperationMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add staking operation medata type declaration Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix error messages & update comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement getCrossShardOperationComponents * rename txAccs to components for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement getContractCreationOperationComponents * Fix some edge case optional checks for operation validation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [go.mod] Bump rosetta version to 0.4.4 & dependent libs * Update broken rosetta structs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add ChainID to ConstructMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename PreStakingBlockRewardOperation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Init operation_components.go & move respective code Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Refactor formatTransaction for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Move maxNumOfConstructionOps to operation_components.go * Clarify unsupported type err msg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement framework for ConstructionPayloads * Remove currying OperationComponents in metadata as PublicKeys is now part of the request in the necessary endpoints * Add respective checks for added PublicKeys * Make getAddressFromPublicKey more general & update tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add getAddress and update respective functions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Create transaction_construction.go & impl ConstructTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add unmarshal check for ConstructMetadata & ConstructMetadataOptions * Remove constructTransaction * Wrap unsigned transaction with UnsignedTransaction to include intended signer * Add framework for ConstructionParse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add getSigningPayload method to construction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal/commonm] Add MustGeneratePrivateKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement parseUnsignedTransaction * Add unpackWrappedTransactionFromHexString & refactor rlp encoding of transactions * Make getSigningPayload return a list Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add TestUnpackWrappedTransactionFromHexString Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement parseSignedTransaction * Move unpackWrappedTransactionFromHexString to ConstructionParse before parsing transactions bases on if it's signed or not Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Nit - fix WrappedTransaction comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add extra negative tests for unpackWrappedTransactionFromHexString Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Finish initial Construct implementation * Implement ConstructionCombine * Implement ConstructionHash * Implement ConstructionSubmit * Ensure RLPbytes & From is present in WrappedTransaction when unpackWrappedTransactionFromHexString is called Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Refactor construction into separate files * Move tests into appropriate files Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement construction_check_test.go * Update edge case for getSuggestedFeeAndPrice found during testing Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement construction_parse_test.go * Clarify names for construction_create_test.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix ConstructionParse to not use crypto.PubkeyToAddress Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix UnmarshalFromInterface for CrossShardTransactionOperationMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add operation_components_test.go * Implement TestGetContractCreationOperationComponents * Implement TestGetCrossShardOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Finish operation_components_test.go * Implement TestGetTransferOperationComponents * Implement TestGetOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix big number checks for getTransferOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement transaction_construction_test.go * Implement TestConstructPlainTransaction * Implement TestConstructCrossShardTransaction * Implement TestConstructContractCreationTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add shard ID checks for constructCrossShardTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add bad curve test for TestGetAddressFromPublicKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add nil error catches for Construction API * Fix nits in messages as seen during pass * Add DefaultGasLimit Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add trace message with version to all err msgs * Add NewError unit test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add TestGetAddressFromKnownPublicKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Fix possible nil ptr crash for GetBlockSigners Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Edge case bug fixes * Create empty TransactionMetdata if no metadata is provided in operations. Also update tests to correctly account for the behavior. * In pre-staking era make current block one less than the absolute latest to guarentee calculation of pres-taking eara block rewards. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix unpackWrappedTransactionFromHexString * Rename function to unpackWrappedTransactionFromString * Update respective tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make parsed transaction statuses empty Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Remove needles list initialization Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add SignatureType & SignedPayloadLength with checks Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix genesis network status crash Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Correct error message dumps for transaction_construction.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add OperationType to ConstructMetadataOptions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix rosetta service rebase Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Fix estimate gas fail check Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add formatNegativeValue to ensure there's only 1 zero * Correct regression found in constructCrossShardTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add contract creation estimate gas hack Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add DefaultSenderAddress for transaction formatter * Update Construction API parser to reflect change Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Address PR comments * Add GetCallStackInfo to internal utils * Add EstimateGas TODO in RPC package * Remove DefaultGasLimit to use param gas limit Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add checks to tx formatter for nil to addr * Remove needless block check for genesisBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Remove EstimatedGasUsed from WrappedTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
// EstimateGas ..
// TODO: fix contract creation gas estimation, it currently underestimates.
func EstimateGas(
Node API Refactor - pt2 (Stage 2.2 of Node API Overhaul) (#3259) * [rpc] Refactor - expose RPC functions in rpc package Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt1 * Move apiv1 into rpc package as baseline * Create version enum * Refactor API creation to use version enum Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rejoin GetBlockByNumber & GetBlockByHash * Both RPC versions are supported in 1 function * Created types directory for v1 & v2 response types * Added framework to make rpc layer more clear and legible Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt2 * Rejoin blockchain.go functions * Create legacy service * Improve inline documentation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Use StructuredResponse when returning an exposed type from RPC * Remove addrlock.go * Add RPCBlock creation with exposed struct for easy utility from consumers * Update blockchain.go to reflect changes of RPCBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Create types for RPCs that are returned. * Reorganize types to appropriate v1 & v2 package. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt4 * Add context to all RPCs * Add response switches based on different versions of the API * Add debug logs for RPC that don't return errors Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt5 * Add versions switches to all transaction related RPCs * Integrate response with newly defined structures * Add debug messages for RPCs that don't return errors * Add inline documentation for all transaction related RPCs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt6 * Remove apiv2 * Move StructuredResponse & TxHistoryArgs to main rpc types.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt7 * Create contract, pool, staking & transaction services * Move apis into correct services * Remove util.go (functions are now baked into RPCs in pool.go) * Remove transaction.go (functions/methods are distributed among created services) * Update changes for StructuredResponse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Add rpc server start/stop to pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Refactor - use new RPC start / stop for servers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Refactor - rename network.go to net.go to match rpc * Change names for consistency with rpc pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * fix go lint & go imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Fix GetTransactionReceipt for staking txs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rename v1 & v2 struct to not have leading 'RPC' * Update comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - make defaultBlocksPeriod be the num blocks per epoch Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
ctx context.Context, hmy *hmy.Harmony, args CallArgs, gasCap *big.Int,
) (uint64, error) {
// Binary search the gas requirement, as it may be higher than the amount used
var (
lo = params.TxGas - 1
hi uint64
max uint64
)
blockNum := rpc.LatestBlockNumber
if args.Gas != nil && uint64(*args.Gas) >= params.TxGas {
hi = uint64(*args.Gas)
} else {
// Retrieve the blk to act as the gas ceiling
blk, err := hmy.BlockByNumber(ctx, blockNum)
if err != nil {
return 0, err
}
hi = blk.GasLimit()
}
if gasCap != nil && hi > gasCap.Uint64() {
hi = gasCap.Uint64()
}
max = hi
// Use zero-address if none other is available
if args.From == nil {
args.From = &common.Address{}
}
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) bool {
args.Gas = (*hexutil.Uint64)(&gas)
result, err := doCall(ctx, hmy, args, blockNum, vm.Config{}, 0, big.NewInt(int64(max)))
Rosetta Implementation - pt4 (Stage 3.4 of Node API Overhaul) (#3380) * [rosetta] Add /construct function framework Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add network ID check to all construct endpoints Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add UnsupportedCurveTypeError Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement ConstructionDerive & add ConstructAPI router Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make recover middleware the outermost middleware Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Correct error for block not found Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Expose EstimateGas Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add InvalidTransactionConstructionError * Add GetValidConstructionOperations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make TransactionMetadata optional ptr * Add UnmarshalFromInterface for TransactionMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add InvalidTransactionConstructionError to network response Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add ConstructionPreprocess & ConstructionMetadata skeleton * Add Helper methods for said functions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add GasPrice to ConstructMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make suggested gas fee & price its own fn Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Nit - fix comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add To & From Account ID in tx metadata * remove getAmountFromUndelegateMessage * make contract address a valid account identifier * remove needless metadata in non-staking operations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add error report for b32 error in newAccountIdentifier Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Simplify assertValidNetworkIdentifier Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Use sdk MapMarshall instead of rpc structured response * remove unused GetValidConstructionOperations * Add CurrencyHash Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement assertValidTransferOperationsAndGetTxAccounts * refactor usages of rpc structured response for sdk map marshall * add account identifier checks for transaction metadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Max related operation check more general for tx op check * refactor names for readability Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add & Expose OperationComponents * Remove txAccounts & add OperationComponents to metadata for quicker processing * Remove From & To acc ID from TransactionMetadata as it's in OperationComponents * Update tests for changes Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add tx type to OperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add logs to TransactionMetadata * Enforce Operation type uniqueness invariant for each transaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add error for 3+ operations in getOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Expose GetOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add CrossShardTransactionOperationMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add staking operation medata type declaration Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix error messages & update comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement getCrossShardOperationComponents * rename txAccs to components for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement getContractCreationOperationComponents * Fix some edge case optional checks for operation validation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [go.mod] Bump rosetta version to 0.4.4 & dependent libs * Update broken rosetta structs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add ChainID to ConstructMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Rename PreStakingBlockRewardOperation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Init operation_components.go & move respective code Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Refactor formatTransaction for clarity Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Move maxNumOfConstructionOps to operation_components.go * Clarify unsupported type err msg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Implement framework for ConstructionPayloads * Remove currying OperationComponents in metadata as PublicKeys is now part of the request in the necessary endpoints * Add respective checks for added PublicKeys * Make getAddressFromPublicKey more general & update tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add getAddress and update respective functions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Create transaction_construction.go & impl ConstructTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add unmarshal check for ConstructMetadata & ConstructMetadataOptions * Remove constructTransaction * Wrap unsigned transaction with UnsignedTransaction to include intended signer * Add framework for ConstructionParse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add getSigningPayload method to construction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal/commonm] Add MustGeneratePrivateKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement parseUnsignedTransaction * Add unpackWrappedTransactionFromHexString & refactor rlp encoding of transactions * Make getSigningPayload return a list Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add TestUnpackWrappedTransactionFromHexString Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement parseSignedTransaction * Move unpackWrappedTransactionFromHexString to ConstructionParse before parsing transactions bases on if it's signed or not Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Nit - fix WrappedTransaction comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add extra negative tests for unpackWrappedTransactionFromHexString Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Finish initial Construct implementation * Implement ConstructionCombine * Implement ConstructionHash * Implement ConstructionSubmit * Ensure RLPbytes & From is present in WrappedTransaction when unpackWrappedTransactionFromHexString is called Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Refactor construction into separate files * Move tests into appropriate files Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement construction_check_test.go * Update edge case for getSuggestedFeeAndPrice found during testing Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement construction_parse_test.go * Clarify names for construction_create_test.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix ConstructionParse to not use crypto.PubkeyToAddress Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix UnmarshalFromInterface for CrossShardTransactionOperationMetadata Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add operation_components_test.go * Implement TestGetContractCreationOperationComponents * Implement TestGetCrossShardOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Finish operation_components_test.go * Implement TestGetTransferOperationComponents * Implement TestGetOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix big number checks for getTransferOperationComponents Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement transaction_construction_test.go * Implement TestConstructPlainTransaction * Implement TestConstructCrossShardTransaction * Implement TestConstructContractCreationTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add shard ID checks for constructCrossShardTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add bad curve test for TestGetAddressFromPublicKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add nil error catches for Construction API * Fix nits in messages as seen during pass * Add DefaultGasLimit Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add trace message with version to all err msgs * Add NewError unit test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add TestGetAddressFromKnownPublicKey Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Fix possible nil ptr crash for GetBlockSigners Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Edge case bug fixes * Create empty TransactionMetdata if no metadata is provided in operations. Also update tests to correctly account for the behavior. * In pre-staking era make current block one less than the absolute latest to guarentee calculation of pres-taking eara block rewards. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix unpackWrappedTransactionFromHexString * Rename function to unpackWrappedTransactionFromString * Update respective tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Make parsed transaction statuses empty Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Remove needles list initialization Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add SignatureType & SignedPayloadLength with checks Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix genesis network status crash Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Correct error message dumps for transaction_construction.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add OperationType to ConstructMetadataOptions Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Fix rosetta service rebase Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Fix estimate gas fail check Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add formatNegativeValue to ensure there's only 1 zero * Correct regression found in constructCrossShardTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> [rosetta] Add contract creation estimate gas hack Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add DefaultSenderAddress for transaction formatter * Update Construction API parser to reflect change Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Address PR comments * Add GetCallStackInfo to internal utils * Add EstimateGas TODO in RPC package * Remove DefaultGasLimit to use param gas limit Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add checks to tx formatter for nil to addr * Remove needless block check for genesisBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Remove EstimatedGasUsed from WrappedTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
if err != nil || result.VMErr == vm.ErrCodeStoreOutOfGas || result.VMErr == vm.ErrOutOfGas {
Node API Refactor - pt2 (Stage 2.2 of Node API Overhaul) (#3259) * [rpc] Refactor - expose RPC functions in rpc package Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt1 * Move apiv1 into rpc package as baseline * Create version enum * Refactor API creation to use version enum Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rejoin GetBlockByNumber & GetBlockByHash * Both RPC versions are supported in 1 function * Created types directory for v1 & v2 response types * Added framework to make rpc layer more clear and legible Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt2 * Rejoin blockchain.go functions * Create legacy service * Improve inline documentation Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Use StructuredResponse when returning an exposed type from RPC * Remove addrlock.go * Add RPCBlock creation with exposed struct for easy utility from consumers * Update blockchain.go to reflect changes of RPCBlock Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt3 * Create types for RPCs that are returned. * Reorganize types to appropriate v1 & v2 package. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt4 * Add context to all RPCs * Add response switches based on different versions of the API * Add debug logs for RPC that don't return errors Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt5 * Add versions switches to all transaction related RPCs * Integrate response with newly defined structures * Add debug messages for RPCs that don't return errors * Add inline documentation for all transaction related RPCs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt6 * Remove apiv2 * Move StructuredResponse & TxHistoryArgs to main rpc types.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Undo v1 & v2 code duplication pt7 * Create contract, pool, staking & transaction services * Move apis into correct services * Remove util.go (functions are now baked into RPCs in pool.go) * Remove transaction.go (functions/methods are distributed among created services) * Update changes for StructuredResponse Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Add rpc server start/stop to pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Refactor - use new RPC start / stop for servers Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [hmy] Refactor - rename network.go to net.go to match rpc * Change names for consistency with rpc pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * fix go lint & go imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Fix GetTransactionReceipt for staking txs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - Rename v1 & v2 struct to not have leading 'RPC' * Update comments Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Refactor - make defaultBlocksPeriod be the num blocks per epoch Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
return false
}
return true
}
// Execute the binary search and hone in on an executable gas limit
for lo+1 < hi {
mid := (hi + lo) / 2
if !executable(mid) {
lo = mid
} else {
hi = mid
}
}
// Reject the transaction as invalid if it still fails at the highest allowance
if hi == max {
if !executable(hi) {
return 0, fmt.Errorf("gas required exceeds allowance (%d) or always failing transaction", max)
}
}
return hi, nil
}