-
Notifications
You must be signed in to change notification settings - Fork 2
/
block.go
68 lines (60 loc) · 1.98 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package v2
import (
"github.com/PositionExchange/posichain/core/types"
rpc_common "github.com/PositionExchange/posichain/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
}