修訂 | ee7ed32352229f4e847a996da9b3378267583464 (tree) |
---|---|
時間 | 2019-07-22 19:20:49 |
作者 | mars <mars@byto...> |
Commiter | mars |
fix review
@@ -3,9 +3,8 @@ package service | ||
3 | 3 | import ( |
4 | 4 | "encoding/json" |
5 | 5 | |
6 | - "github.com/vapor/api" | |
7 | 6 | "github.com/vapor/errors" |
8 | - "github.com/vapor/protocol/bc/types" | |
7 | + "github.com/vapor/protocol/bc" | |
9 | 8 | "github.com/vapor/toolbar/common" |
10 | 9 | ) |
11 | 10 |
@@ -14,16 +13,16 @@ type Node struct { | ||
14 | 13 | hostPort string |
15 | 14 | } |
16 | 15 | |
17 | -// NewNode create a api client with target server | |
16 | +// Node create a api client with target server | |
18 | 17 | func NewNode(hostPort string) *Node { |
19 | 18 | return &Node{hostPort: hostPort} |
20 | 19 | } |
21 | 20 | |
22 | -func (n *Node) GetBlockByHash(hash string) (*types.Block, error) { | |
21 | +func (n *Node) GetBlockByHash(hash string) (string, *bc.TransactionStatus, error) { | |
23 | 22 | return n.getRawBlock(&getRawBlockReq{BlockHash: hash}) |
24 | 23 | } |
25 | 24 | |
26 | -func (n *Node) GetBlockByHeight(height uint64) (*types.Block, error) { | |
25 | +func (n *Node) GetBlockByHeight(height uint64) (string, *bc.TransactionStatus, error) { | |
27 | 26 | return n.getRawBlock(&getRawBlockReq{BlockHeight: height}) |
28 | 27 | } |
29 | 28 |
@@ -42,15 +41,21 @@ type getRawBlockReq struct { | ||
42 | 41 | BlockHash string `json:"block_hash"` |
43 | 42 | } |
44 | 43 | |
45 | -func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) { | |
44 | +type getRawBlockResp struct { | |
45 | + RawBlock string `json:"raw_block"` | |
46 | + // TransactionStatus has same marshalling rule for both bytom and vapor | |
47 | + TransactionStatus *bc.TransactionStatus `json:"transaction_status"` | |
48 | +} | |
49 | + | |
50 | +func (n *Node) getRawBlock(req *getRawBlockReq) (string, *bc.TransactionStatus, error) { | |
46 | 51 | url := "/get-raw-block" |
47 | 52 | payload, err := json.Marshal(req) |
48 | 53 | if err != nil { |
49 | - return nil, errors.Wrap(err, "json marshal") | |
54 | + return "", nil, errors.Wrap(err, "json marshal") | |
50 | 55 | } |
51 | 56 | |
52 | - resp := &api.GetRawBlockResp{} | |
53 | - return resp.RawBlock, n.request(url, payload, resp) | |
57 | + res := &getRawBlockResp{} | |
58 | + return res.RawBlock, res.TransactionStatus, n.request(url, payload, res) | |
54 | 59 | } |
55 | 60 | |
56 | 61 | type response struct { |
@@ -17,11 +17,11 @@ import ( | ||
17 | 17 | "github.com/vapor/consensus" |
18 | 18 | "github.com/vapor/errors" |
19 | 19 | "github.com/vapor/protocol/bc" |
20 | - "github.com/vapor/toolbar/common/service" | |
21 | 20 | "github.com/vapor/toolbar/federation/common" |
22 | 21 | "github.com/vapor/toolbar/federation/config" |
23 | 22 | "github.com/vapor/toolbar/federation/database" |
24 | 23 | "github.com/vapor/toolbar/federation/database/orm" |
24 | + "github.com/vapor/toolbar/federation/service" | |
25 | 25 | ) |
26 | 26 | |
27 | 27 | type mainchainKeeper struct { |
@@ -13,11 +13,11 @@ import ( | ||
13 | 13 | "github.com/vapor/errors" |
14 | 14 | "github.com/vapor/protocol/bc" |
15 | 15 | "github.com/vapor/protocol/bc/types" |
16 | - "github.com/vapor/toolbar/common/service" | |
17 | 16 | "github.com/vapor/toolbar/federation/common" |
18 | 17 | "github.com/vapor/toolbar/federation/config" |
19 | 18 | "github.com/vapor/toolbar/federation/database" |
20 | 19 | "github.com/vapor/toolbar/federation/database/orm" |
20 | + "github.com/vapor/toolbar/federation/service" | |
21 | 21 | ) |
22 | 22 | |
23 | 23 | type sidechainKeeper struct { |
@@ -0,0 +1,73 @@ | ||
1 | +package service | |
2 | + | |
3 | +import ( | |
4 | + "encoding/json" | |
5 | + | |
6 | + "github.com/vapor/api" | |
7 | + "github.com/vapor/errors" | |
8 | + "github.com/vapor/protocol/bc/types" | |
9 | + "github.com/vapor/toolbar/common" | |
10 | +) | |
11 | + | |
12 | +// Node can invoke the api which provide by the full node server | |
13 | +type Node struct { | |
14 | + hostPort string | |
15 | +} | |
16 | + | |
17 | +// NewNode create a api client with target server | |
18 | +func NewNode(hostPort string) *Node { | |
19 | + return &Node{hostPort: hostPort} | |
20 | +} | |
21 | + | |
22 | +func (n *Node) GetBlockByHash(hash string) (*types.Block, error) { | |
23 | + return n.getRawBlock(&getRawBlockReq{BlockHash: hash}) | |
24 | +} | |
25 | + | |
26 | +func (n *Node) GetBlockByHeight(height uint64) (*types.Block, error) { | |
27 | + return n.getRawBlock(&getRawBlockReq{BlockHeight: height}) | |
28 | +} | |
29 | + | |
30 | +type getBlockCountResp struct { | |
31 | + BlockCount uint64 `json:"block_count"` | |
32 | +} | |
33 | + | |
34 | +func (n *Node) GetBlockCount() (uint64, error) { | |
35 | + url := "/get-block-count" | |
36 | + res := &getBlockCountResp{} | |
37 | + return res.BlockCount, n.request(url, nil, res) | |
38 | +} | |
39 | + | |
40 | +type getRawBlockReq struct { | |
41 | + BlockHeight uint64 `json:"block_height"` | |
42 | + BlockHash string `json:"block_hash"` | |
43 | +} | |
44 | + | |
45 | +func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) { | |
46 | + url := "/get-raw-block" | |
47 | + payload, err := json.Marshal(req) | |
48 | + if err != nil { | |
49 | + return nil, errors.Wrap(err, "json marshal") | |
50 | + } | |
51 | + | |
52 | + resp := &api.GetRawBlockResp{} | |
53 | + return resp.RawBlock, n.request(url, payload, resp) | |
54 | +} | |
55 | + | |
56 | +type response struct { | |
57 | + Status string `json:"status"` | |
58 | + Data json.RawMessage `json:"data"` | |
59 | + ErrDetail string `json:"error_detail"` | |
60 | +} | |
61 | + | |
62 | +func (n *Node) request(path string, payload []byte, respData interface{}) error { | |
63 | + resp := &response{} | |
64 | + if err := common.Post(n.hostPort+path, payload, resp); err != nil { | |
65 | + return err | |
66 | + } | |
67 | + | |
68 | + if resp.Status != "success" { | |
69 | + return errors.New(resp.ErrDetail) | |
70 | + } | |
71 | + | |
72 | + return json.Unmarshal(resp.Data, respData) | |
73 | +} |
@@ -9,9 +9,9 @@ import ( | ||
9 | 9 | "github.com/vapor/errors" |
10 | 10 | "github.com/vapor/protocol/bc/types" |
11 | 11 | "github.com/vapor/toolbar/common" |
12 | - "github.com/vapor/toolbar/common/service" | |
13 | 12 | "github.com/vapor/toolbar/reward/config" |
14 | 13 | "github.com/vapor/toolbar/reward/database/orm" |
14 | + "github.com/vapor/toolbar/reward/service" | |
15 | 15 | ) |
16 | 16 | |
17 | 17 | type ChainKeeper struct { |
@@ -30,27 +30,32 @@ func NewChainKeeper(db *gorm.DB, cfg *config.Config, syncHeight uint64) (*ChainK | ||
30 | 30 | } |
31 | 31 | |
32 | 32 | blockState := &orm.BlockState{} |
33 | - if err := db.First(blockState).Error; err == gorm.ErrRecordNotFound { | |
34 | - block, err := keeper.node.GetBlockByHeight(0) | |
35 | - if err != nil { | |
36 | - return nil, errors.Wrap(err, "Failed to get genenis block") | |
37 | - } | |
33 | + err := db.First(blockState).Error | |
34 | + if err == nil { | |
35 | + return keeper, nil | |
36 | + } | |
38 | 37 | |
39 | - if err := keeper.initBlockState(db, block); err != nil { | |
40 | - return nil, errors.Wrap(err, "Failed to insert blockState") | |
41 | - } | |
42 | - } else if err != nil { | |
38 | + if err != gorm.ErrRecordNotFound { | |
43 | 39 | return nil, errors.Wrap(err, "Failed to get blockState") |
44 | 40 | } |
45 | 41 | |
42 | + block, err := keeper.node.GetBlockByHeight(0) | |
43 | + if err != nil { | |
44 | + return nil, errors.Wrap(err, "Failed to get genenis block") | |
45 | + } | |
46 | + | |
47 | + if err := keeper.initBlockState(db, block); err != nil { | |
48 | + return nil, errors.Wrap(err, "Failed to insert blockState") | |
49 | + } | |
50 | + | |
46 | 51 | return keeper, nil |
47 | 52 | } |
48 | 53 | |
49 | -func (c *ChainKeeper) Start() error { | |
54 | +func (c *ChainKeeper) SyncBlock() error { | |
50 | 55 | for { |
51 | 56 | blockState := &orm.BlockState{} |
52 | - if c.db.First(blockState).RecordNotFound() { | |
53 | - return errors.New("The query blockState record is empty empty on process block") | |
57 | + if err := c.db.First(blockState).Error; err != nil { | |
58 | + return errors.Wrap(err, "The query blockState record is empty empty on process block") | |
54 | 59 | } |
55 | 60 | |
56 | 61 | if blockState.Height >= c.syncHeight { |
@@ -102,8 +107,7 @@ func (c *ChainKeeper) syncBlock(ormDB *gorm.DB, blockState *orm.BlockState) erro | ||
102 | 107 | func (c *ChainKeeper) AttachBlock(ormDB *gorm.DB, block *types.Block) error { |
103 | 108 | for _, tx := range block.Transactions { |
104 | 109 | for _, input := range tx.Inputs { |
105 | - vetoInput, ok := input.TypedInput.(*types.VetoInput) | |
106 | - if !ok { | |
110 | + if _, ok := input.TypedInput.(*types.VetoInput); !ok { | |
107 | 111 | continue |
108 | 112 | } |
109 | 113 |
@@ -112,8 +116,7 @@ func (c *ChainKeeper) AttachBlock(ormDB *gorm.DB, block *types.Block) error { | ||
112 | 116 | return err |
113 | 117 | } |
114 | 118 | utxo := &orm.Utxo{ |
115 | - VoterAddress: common.GetAddressFromControlProgram(vetoInput.ControlProgram), | |
116 | - OutputID: outputID.String(), | |
119 | + OutputID: outputID.String(), | |
117 | 120 | } |
118 | 121 | // update data |
119 | 122 | db := ormDB.Model(&orm.Utxo{}).Where(utxo).Update("veto_height", block.Height) |
@@ -155,11 +158,7 @@ func (c *ChainKeeper) AttachBlock(ormDB *gorm.DB, block *types.Block) error { | ||
155 | 158 | BlockHash: blockHash.String(), |
156 | 159 | } |
157 | 160 | |
158 | - if err := c.updateBlockState(ormDB, blockState); err != nil { | |
159 | - return err | |
160 | - } | |
161 | - | |
162 | - return nil | |
161 | + return c.updateBlockState(ormDB, blockState) | |
163 | 162 | } |
164 | 163 | |
165 | 164 | func (c *ChainKeeper) DetachBlock(ormDB *gorm.DB, block *types.Block) error { |
@@ -185,11 +184,7 @@ func (c *ChainKeeper) DetachBlock(ormDB *gorm.DB, block *types.Block) error { | ||
185 | 184 | BlockHash: block.PreviousBlockHash.String(), |
186 | 185 | } |
187 | 186 | |
188 | - if err := c.updateBlockState(ormDB, blockState); err != nil { | |
189 | - return err | |
190 | - } | |
191 | - | |
192 | - return nil | |
187 | + return c.updateBlockState(ormDB, blockState) | |
193 | 188 | } |
194 | 189 | |
195 | 190 | func (c *ChainKeeper) initBlockState(db *gorm.DB, block *types.Block) error { |