Golang implemented sidechain for Bytom
Revision | 97ea1a541a3013c09d35e146abc567febce694f9 (tree) |
---|---|
Time | 2020-02-26 10:43:36 |
Author | ipqhjjybj <250657661@qq.c...> |
Commiter | ipqhjjybj |
add examples
@@ -1,6 +1,8 @@ | ||
1 | 1 | package types |
2 | 2 | |
3 | 3 | import ( |
4 | + "fmt" | |
5 | + | |
4 | 6 | log "github.com/sirupsen/logrus" |
5 | 7 | "golang.org/x/crypto/sha3" |
6 | 8 |
@@ -263,7 +265,9 @@ func MapBlock(old *Block) *bc.Block { | ||
263 | 265 | |
264 | 266 | b := new(bc.Block) |
265 | 267 | b.ID, b.BlockHeader = mapBlockHeader(&old.BlockHeader) |
268 | + fmt.Println("[old.Transactions]", old.Transactions) | |
266 | 269 | for _, oldTx := range old.Transactions { |
270 | + fmt.Println("[oldTx]:", oldTx, oldTx.Tx) | |
267 | 271 | b.Transactions = append(b.Transactions, oldTx.Tx) |
268 | 272 | } |
269 | 273 | return b |
@@ -148,11 +148,13 @@ func (c *Chain) connectBlock(block *types.Block) (err error) { | ||
148 | 148 | func (c *Chain) detachBlock(detachBlockHeader *types.BlockHeader, consensusResult *state.ConsensusResult, utxoView *state.UtxoViewpoint) (*types.Block, error) { |
149 | 149 | detachHash := detachBlockHeader.Hash() |
150 | 150 | block, err := c.store.GetBlock(&detachHash) |
151 | + fmt.Println("[detachBlock] block ", block.Height, " transactions", block.Transactions) | |
151 | 152 | if err != nil { |
152 | 153 | return block, err |
153 | 154 | } |
154 | 155 | |
155 | 156 | detachBlock := types.MapBlock(block) |
157 | + fmt.Println("[detachBlock mapBlock]", detachBlock.Height, "transactions", detachBlock.Transactions) | |
156 | 158 | fmt.Println("detachBlock consensusResult", consensusResult) |
157 | 159 | if err := consensusResult.DetachBlock(block); err != nil { |
158 | 160 | return block, err |
@@ -168,6 +170,7 @@ func (c *Chain) detachBlock(detachBlockHeader *types.BlockHeader, consensusResul | ||
168 | 170 | return block, err |
169 | 171 | } |
170 | 172 | |
173 | + fmt.Println("detachBlock", detachBlock) | |
171 | 174 | if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil { |
172 | 175 | return block, err |
173 | 176 | } |
@@ -228,7 +231,9 @@ func (c *Chain) Rollback(targetHeight uint64) error { | ||
228 | 231 | for _, deletedBlockHeader := range deletedBlockHeaders { |
229 | 232 | hashDeleted := deletedBlockHeader.Hash() |
230 | 233 | fmt.Println("deletedBlockHeader", deletedBlockHeader.Height, hashDeleted.String()) |
234 | + fmt.Println("[Rollback] go to start") | |
231 | 235 | block, err := c.detachBlock(deletedBlockHeader, consensusResult, utxoView) |
236 | + fmt.Println("[Rollback] c.detachBlock") | |
232 | 237 | if err != nil { |
233 | 238 | return err |
234 | 239 | } |
@@ -109,6 +109,7 @@ func TestRollbackMock(t *testing.T) { | ||
109 | 109 | cases := []struct { |
110 | 110 | desc string |
111 | 111 | bestBlockHeader *types.BlockHeader |
112 | + lastIrrBlockHeader *types.BlockHeader | |
112 | 113 | storedBlocks []*types.Block |
113 | 114 | beforeBestConsensusResult *state.ConsensusResult |
114 | 115 | afterBestConsensusResult *state.ConsensusResult |
@@ -120,6 +121,10 @@ func TestRollbackMock(t *testing.T) { | ||
120 | 121 | Height: 1, |
121 | 122 | PreviousBlockHash: testutil.MustDecodeHash("39dee75363127a2857f554d2ad2706eb876407a2e09fbe0338683ca4ad4c2f90"), |
122 | 123 | }, |
124 | + lastIrrBlockHeader: &types.BlockHeader{ | |
125 | + Height: 1, | |
126 | + PreviousBlockHash: testutil.MustDecodeHash("39dee75363127a2857f554d2ad2706eb876407a2e09fbe0338683ca4ad4c2f90"), | |
127 | + }, | |
123 | 128 | targetHeight: 0, |
124 | 129 | |
125 | 130 | storedBlocks: []*types.Block{ |
@@ -191,10 +196,14 @@ func TestRollbackMock(t *testing.T) { | ||
191 | 196 | // } |
192 | 197 | |
193 | 198 | for _, block := range c.storedBlocks { |
199 | + newTrans := []*types.Tx{} | |
194 | 200 | status := bc.NewTransactionStatus() |
195 | - for index, _ := range block.Transactions { | |
201 | + for index, tx := range block.Transactions { | |
196 | 202 | status.SetStatus(index, false) |
203 | + tx := &types.Tx{TxData: tx.TxData, Tx: types.MapTx(&tx.TxData)} | |
204 | + newTrans = append(newTrans, tx) | |
197 | 205 | } |
206 | + block.Transactions = newTrans | |
198 | 207 | fmt.Println("block", block) |
199 | 208 | fmt.Println("what start:", block.Transactions, block.Transactions[0]) |
200 | 209 | mockStore.SaveBlock(block, status) |
@@ -203,9 +212,10 @@ func TestRollbackMock(t *testing.T) { | ||
203 | 212 | mockStore.SetConsensusResult(c.beforeBestConsensusResult) |
204 | 213 | |
205 | 214 | chain := &Chain{ |
206 | - store: mockStore, | |
207 | - subProtocols: []Protocoler{mockProtocoler}, | |
208 | - bestBlockHeader: c.bestBlockHeader, | |
215 | + store: mockStore, | |
216 | + subProtocols: []Protocoler{mockProtocoler}, | |
217 | + bestBlockHeader: c.bestBlockHeader, | |
218 | + lastIrrBlockHeader: c.lastIrrBlockHeader, | |
209 | 219 | } |
210 | 220 | |
211 | 221 | chain.cond.L = new(sync.Mutex) |
@@ -72,7 +72,7 @@ func (view *UtxoViewpoint) DetachBlock(block *bc.Block, txStatus *bc.Transaction | ||
72 | 72 | return err |
73 | 73 | } |
74 | 74 | |
75 | - fmt.Println("block.Transactions", block.Transactions) | |
75 | + fmt.Println("[DetachBlock]", "block.Height", block.Height, "block.Transactions", block.Transactions) | |
76 | 76 | if err := view.DetachTransaction(block.Transactions[i], statusFail); err != nil { |
77 | 77 | return err |
78 | 78 | } |