parent
27c5aa468f
commit
132ea783b6
@ -0,0 +1,19 @@ |
||||
package common |
||||
|
||||
import ( |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/harmony-one/harmony/core/types" |
||||
) |
||||
|
||||
// BlockFactory is the interface of factory for RPC block data
|
||||
type BlockFactory interface { |
||||
NewBlock(b *types.Block, args *BlockArgs) (interface{}, error) |
||||
} |
||||
|
||||
// BlockDataProvider helps with providing data for RPC blocks
|
||||
type BlockDataProvider interface { |
||||
GetLeader(b *types.Block) string |
||||
GetSigners(b *types.Block) ([]string, error) |
||||
GetStakingTxs(b *types.Block) (interface{}, error) |
||||
GetStakingTxHashes(b *types.Block) []common.Hash |
||||
} |
@ -0,0 +1,71 @@ |
||||
package eth |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/core/types" |
||||
internal_common "github.com/harmony-one/harmony/internal/common" |
||||
rpc_common "github.com/harmony-one/harmony/rpc/common" |
||||
"github.com/pkg/errors" |
||||
) |
||||
|
||||
// BlockFactory is the factory for v1 rpc block
|
||||
type BlockFactory struct { |
||||
provider rpc_common.BlockDataProvider |
||||
} |
||||
|
||||
// NewBlockFactory return the block factory with the given provider
|
||||
func NewBlockFactory(provider rpc_common.BlockDataProvider) *BlockFactory { |
||||
return &BlockFactory{provider} |
||||
} |
||||
|
||||
// NewBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
|
||||
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
|
||||
// transaction hashes.
|
||||
func (fac *BlockFactory) NewBlock(b *types.Block, args *rpc_common.BlockArgs) (interface{}, error) { |
||||
if args.FullTx { |
||||
return fac.newBlockWithFullTx(b, args) |
||||
} |
||||
return fac.newBlockWithTxHash(b, args) |
||||
} |
||||
|
||||
func (fac *BlockFactory) newBlockWithTxHash(b *types.Block, args *rpc_common.BlockArgs) (*BlockWithTxHash, error) { |
||||
blk := blockWithTxHashFromBlock(b) |
||||
|
||||
leader := fac.provider.GetLeader(b) |
||||
ethLeader, err := internal_common.ParseAddr(leader) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "parse leader address") |
||||
} |
||||
blk.Block.Miner = ethLeader |
||||
|
||||
if args.WithSigners { |
||||
signers, err := fac.provider.GetSigners(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetSigners") |
||||
} |
||||
blk.Signers = signers |
||||
} |
||||
return blk, nil |
||||
} |
||||
|
||||
func (fac *BlockFactory) newBlockWithFullTx(b *types.Block, args *rpc_common.BlockArgs) (*BlockWithFullTx, error) { |
||||
blk, err := blockWithFullTxFromBlock(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "blockWithFullTxFromBlock") |
||||
} |
||||
|
||||
leader := fac.provider.GetLeader(b) |
||||
ethLeader, err := internal_common.ParseAddr(leader) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "parse leader address") |
||||
} |
||||
blk.Block.Miner = ethLeader |
||||
|
||||
if args.WithSigners { |
||||
signers, err := fac.provider.GetSigners(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetSigners") |
||||
} |
||||
blk.Signers = signers |
||||
} |
||||
return blk, nil |
||||
} |
@ -0,0 +1,68 @@ |
||||
package v1 |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/core/types" |
||||
rpc_common "github.com/harmony-one/harmony/rpc/common" |
||||
"github.com/pkg/errors" |
||||
) |
||||
|
||||
// BlockFactory is the factory for v1 rpc block
|
||||
type BlockFactory struct { |
||||
provider rpc_common.BlockDataProvider |
||||
} |
||||
|
||||
// NewBlockFactory return the block factory with the given provider
|
||||
func NewBlockFactory(provider rpc_common.BlockDataProvider) *BlockFactory { |
||||
return &BlockFactory{provider} |
||||
} |
||||
|
||||
// NewBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
|
||||
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
|
||||
// transaction hashes.
|
||||
func (fac *BlockFactory) NewBlock(b *types.Block, args *rpc_common.BlockArgs) (interface{}, error) { |
||||
if args.FullTx { |
||||
return fac.newBlockWithFullTx(b, args) |
||||
} |
||||
return fac.newBlockWithTxHash(b, args) |
||||
} |
||||
|
||||
func (fac *BlockFactory) newBlockWithTxHash(b *types.Block, args *rpc_common.BlockArgs) (*BlockWithTxHash, error) { |
||||
blk := blockWithTxHashFromBlock(b) |
||||
|
||||
blk.Miner = fac.provider.GetLeader(b) |
||||
if args.InclStaking { |
||||
blk.StakingTxs = fac.provider.GetStakingTxHashes(b) |
||||
} |
||||
if args.WithSigners { |
||||
signers, err := fac.provider.GetSigners(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetSigners") |
||||
} |
||||
blk.Signers = signers |
||||
} |
||||
return blk, nil |
||||
} |
||||
|
||||
func (fac *BlockFactory) newBlockWithFullTx(b *types.Block, args *rpc_common.BlockArgs) (*BlockWithFullTx, error) { |
||||
blk, err := blockWithFullTxFromBlock(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "blockWithFullTxFromBlock") |
||||
} |
||||
|
||||
blk.Miner = fac.provider.GetLeader(b) |
||||
if args.InclStaking { |
||||
txs, err := fac.provider.GetStakingTxs(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetStakingTxs") |
||||
} |
||||
blk.StakingTxs = txs.([]*StakingTransaction) |
||||
} |
||||
if args.WithSigners { |
||||
signers, err := fac.provider.GetSigners(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetSigners") |
||||
} |
||||
blk.Signers = signers |
||||
} |
||||
return blk, nil |
||||
} |
@ -0,0 +1,68 @@ |
||||
package v2 |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/core/types" |
||||
rpc_common "github.com/harmony-one/harmony/rpc/common" |
||||
"github.com/pkg/errors" |
||||
) |
||||
|
||||
// BlockFactory is the factory for v1 rpc block
|
||||
type BlockFactory struct { |
||||
provider rpc_common.BlockDataProvider |
||||
} |
||||
|
||||
// NewBlockFactory return the block factory with the given provider
|
||||
func NewBlockFactory(provider rpc_common.BlockDataProvider) *BlockFactory { |
||||
return &BlockFactory{provider} |
||||
} |
||||
|
||||
// NewBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
|
||||
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
|
||||
// transaction hashes.
|
||||
func (fac *BlockFactory) NewBlock(b *types.Block, args *rpc_common.BlockArgs) (interface{}, error) { |
||||
if args.FullTx { |
||||
return fac.newBlockWithFullTx(b, args) |
||||
} |
||||
return fac.newBlockWithTxHash(b, args) |
||||
} |
||||
|
||||
func (fac *BlockFactory) newBlockWithTxHash(b *types.Block, args *rpc_common.BlockArgs) (*BlockWithTxHash, error) { |
||||
blk := blockWithTxHashFromBlock(b) |
||||
|
||||
blk.Miner = fac.provider.GetLeader(b) |
||||
if args.InclStaking { |
||||
blk.StakingTxs = fac.provider.GetStakingTxHashes(b) |
||||
} |
||||
if args.WithSigners { |
||||
signers, err := fac.provider.GetSigners(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetSigners") |
||||
} |
||||
blk.Signers = signers |
||||
} |
||||
return blk, nil |
||||
} |
||||
|
||||
func (fac *BlockFactory) newBlockWithFullTx(b *types.Block, args *rpc_common.BlockArgs) (*BlockWithFullTx, error) { |
||||
blk, err := blockWithFullTxFromBlock(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "blockWithFullTxFromBlock") |
||||
} |
||||
|
||||
blk.Miner = fac.provider.GetLeader(b) |
||||
if args.InclStaking { |
||||
txs, err := fac.provider.GetStakingTxs(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetStakingTxs") |
||||
} |
||||
blk.StakingTxs = txs.([]*StakingTransaction) |
||||
} |
||||
if args.WithSigners { |
||||
signers, err := fac.provider.GetSigners(b) |
||||
if err != nil { |
||||
return nil, errors.Wrap(err, "GetSigners") |
||||
} |
||||
blk.Signers = signers |
||||
} |
||||
return blk, nil |
||||
} |
Loading…
Reference in new issue