Official Go implementation of the Bytom protocol
Revision | 00a439ffc0a6733882d4aca077314cecb37083f1 (tree) |
---|---|
Zeit | 2021-09-01 18:15:43 |
Autor | shenao78 <shenao.78@163....> |
Commiter | shenao78 |
opt code
@@ -12,8 +12,9 @@ type Instance struct { | ||
12 | 12 | InSync bool |
13 | 13 | } |
14 | 14 | |
15 | -func NewInstance(inUTXOs, outUTXOs []*UTXO) *Instance { | |
15 | +func NewInstance(traceID string, inUTXOs, outUTXOs []*UTXO) *Instance { | |
16 | 16 | inst := &Instance{ |
17 | + TraceID: traceID, | |
17 | 18 | UTXOs: outUTXOs, |
18 | 19 | Finalized: len(outUTXOs) == 0, |
19 | 20 | } |
@@ -48,7 +49,6 @@ func (i *InstanceTable) Put(instance *Instance) { | ||
48 | 49 | for _, utxo := range instance.UTXOs { |
49 | 50 | i.utxoHashToInst[utxo.hash] = instance |
50 | 51 | } |
51 | - // TODO must remove prev key of utxos | |
52 | 52 | } |
53 | 53 | |
54 | 54 | func (i *InstanceTable) Remove(id string) { |
@@ -40,7 +40,7 @@ func (t *Tracer) ApplyBlock(block *types.Block) error { | ||
40 | 40 | t.Lock() |
41 | 41 | defer t.Unlock() |
42 | 42 | |
43 | - var instances []*Instance | |
43 | + var newInstances, oldInstances []*Instance | |
44 | 44 | for _, tx := range block.Transactions { |
45 | 45 | inUTXOs, outUTXOs := t.parseTransfer(tx) |
46 | 46 | if len(inUTXOs) == 0 { |
@@ -48,21 +48,22 @@ func (t *Tracer) ApplyBlock(block *types.Block) error { | ||
48 | 48 | } |
49 | 49 | |
50 | 50 | if inst := t.table.GetByUTXO(inUTXOs[0].hash); inst != nil { |
51 | - newInst := NewInstance(inUTXOs, outUTXOs) | |
52 | - newInst.TraceID = inst.TraceID | |
51 | + newInst := NewInstance(inst.TraceID, inUTXOs, outUTXOs) | |
53 | 52 | newInst.InSync = true |
54 | - instances = append(instances, newInst) | |
53 | + newInstances = append(newInstances, newInst) | |
54 | + oldInstances = append(oldInstances, inst) | |
55 | 55 | } |
56 | 56 | } |
57 | - return t.saveInstances(instances) | |
57 | + return t.saveInstances(newInstances, oldInstances) | |
58 | 58 | } |
59 | 59 | |
60 | 60 | func (t *Tracer) DetachBlock(block *types.Block) error { |
61 | 61 | t.Lock() |
62 | 62 | defer t.Unlock() |
63 | 63 | |
64 | - var instances []*Instance | |
65 | - for _, tx := range block.Transactions { | |
64 | + var newInstances, oldInstances []*Instance | |
65 | + for i := len(block.Transactions); i >= 0; i-- { | |
66 | + tx := block.Transactions[i] | |
66 | 67 | inUTXOs, outUTXOs := t.parseTransfer(tx) |
67 | 68 | utxos := append(outUTXOs, inUTXOs...) |
68 | 69 | if len(utxos) == 0 { |
@@ -70,15 +71,13 @@ func (t *Tracer) DetachBlock(block *types.Block) error { | ||
70 | 71 | } |
71 | 72 | |
72 | 73 | if inst := t.table.GetByUTXO(utxos[0].hash); inst != nil { |
73 | - instances = append(instances, &Instance{ | |
74 | - TraceID: inst.TraceID, | |
75 | - UTXOs: inUTXOs, | |
76 | - Finalized: false, | |
77 | - InSync: true, | |
78 | - }) | |
74 | + newInst := NewInstance(inst.TraceID, outUTXOs, inUTXOs) | |
75 | + newInst.InSync = true | |
76 | + newInstances = append(newInstances, newInst) | |
77 | + oldInstances = append(oldInstances, inst) | |
79 | 78 | } |
80 | 79 | } |
81 | - return t.saveInstances(instances) | |
80 | + return t.saveInstances(newInstances, oldInstances) | |
82 | 81 | } |
83 | 82 | |
84 | 83 | func (t *Tracer) AddUnconfirmedTx(tx *types.Tx) error { |
@@ -98,8 +97,7 @@ func (t *Tracer) CreateInstance(txHash, blockHash bc.Hash) (string, error) { | ||
98 | 97 | return "", errors.New("input of tx has not contract") |
99 | 98 | } |
100 | 99 | |
101 | - inst := NewInstance(inUTXOs, outUTXOs) | |
102 | - inst.TraceID = uuid.New().String() | |
100 | + inst := NewInstance(uuid.New().String(), inUTXOs, outUTXOs) | |
103 | 101 | if err := t.infra.Repository.SaveInstances([]*Instance{inst}); err != nil { |
104 | 102 | return "", err |
105 | 103 | } |
@@ -141,11 +139,15 @@ func (t *Tracer) parseTransfer(tx *types.Tx) ([]*UTXO, []*UTXO) { | ||
141 | 139 | return inUTXOs, outUTXOs |
142 | 140 | } |
143 | 141 | |
144 | -func (t *Tracer) saveInstances(instances []*Instance) error { | |
142 | +func (t *Tracer) saveInstances(instances, oldInstances []*Instance) error { | |
145 | 143 | if err := t.infra.Repository.SaveInstances(instances); err != nil { |
146 | 144 | return err |
147 | 145 | } |
148 | 146 | |
147 | + for _, inst := range oldInstances { | |
148 | + t.table.Remove(inst.TraceID) | |
149 | + } | |
150 | + | |
149 | 151 | for _, inst := range instances { |
150 | 152 | t.table.Put(inst) |
151 | 153 | } |