• R/O
  • HTTP
  • SSH
  • HTTPS

bytom: Commit

Official Go implementation of the Bytom protocol


Commit MetaInfo

Revision00a439ffc0a6733882d4aca077314cecb37083f1 (tree)
Zeit2021-09-01 18:15:43
Autorshenao78 <shenao.78@163....>
Commitershenao78

Log Message

opt code

Ändern Zusammenfassung

Diff

--- a/contract/instance.go
+++ b/contract/instance.go
@@ -12,8 +12,9 @@ type Instance struct {
1212 InSync bool
1313 }
1414
15-func NewInstance(inUTXOs, outUTXOs []*UTXO) *Instance {
15+func NewInstance(traceID string, inUTXOs, outUTXOs []*UTXO) *Instance {
1616 inst := &Instance{
17+ TraceID: traceID,
1718 UTXOs: outUTXOs,
1819 Finalized: len(outUTXOs) == 0,
1920 }
@@ -48,7 +49,6 @@ func (i *InstanceTable) Put(instance *Instance) {
4849 for _, utxo := range instance.UTXOs {
4950 i.utxoHashToInst[utxo.hash] = instance
5051 }
51- // TODO must remove prev key of utxos
5252 }
5353
5454 func (i *InstanceTable) Remove(id string) {
--- a/contract/tracer.go
+++ b/contract/tracer.go
@@ -40,7 +40,7 @@ func (t *Tracer) ApplyBlock(block *types.Block) error {
4040 t.Lock()
4141 defer t.Unlock()
4242
43- var instances []*Instance
43+ var newInstances, oldInstances []*Instance
4444 for _, tx := range block.Transactions {
4545 inUTXOs, outUTXOs := t.parseTransfer(tx)
4646 if len(inUTXOs) == 0 {
@@ -48,21 +48,22 @@ func (t *Tracer) ApplyBlock(block *types.Block) error {
4848 }
4949
5050 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)
5352 newInst.InSync = true
54- instances = append(instances, newInst)
53+ newInstances = append(newInstances, newInst)
54+ oldInstances = append(oldInstances, inst)
5555 }
5656 }
57- return t.saveInstances(instances)
57+ return t.saveInstances(newInstances, oldInstances)
5858 }
5959
6060 func (t *Tracer) DetachBlock(block *types.Block) error {
6161 t.Lock()
6262 defer t.Unlock()
6363
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]
6667 inUTXOs, outUTXOs := t.parseTransfer(tx)
6768 utxos := append(outUTXOs, inUTXOs...)
6869 if len(utxos) == 0 {
@@ -70,15 +71,13 @@ func (t *Tracer) DetachBlock(block *types.Block) error {
7071 }
7172
7273 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)
7978 }
8079 }
81- return t.saveInstances(instances)
80+ return t.saveInstances(newInstances, oldInstances)
8281 }
8382
8483 func (t *Tracer) AddUnconfirmedTx(tx *types.Tx) error {
@@ -98,8 +97,7 @@ func (t *Tracer) CreateInstance(txHash, blockHash bc.Hash) (string, error) {
9897 return "", errors.New("input of tx has not contract")
9998 }
10099
101- inst := NewInstance(inUTXOs, outUTXOs)
102- inst.TraceID = uuid.New().String()
100+ inst := NewInstance(uuid.New().String(), inUTXOs, outUTXOs)
103101 if err := t.infra.Repository.SaveInstances([]*Instance{inst}); err != nil {
104102 return "", err
105103 }
@@ -141,11 +139,15 @@ func (t *Tracer) parseTransfer(tx *types.Tx) ([]*UTXO, []*UTXO) {
141139 return inUTXOs, outUTXOs
142140 }
143141
144-func (t *Tracer) saveInstances(instances []*Instance) error {
142+func (t *Tracer) saveInstances(instances, oldInstances []*Instance) error {
145143 if err := t.infra.Repository.SaveInstances(instances); err != nil {
146144 return err
147145 }
148146
147+ for _, inst := range oldInstances {
148+ t.table.Remove(inst.TraceID)
149+ }
150+
149151 for _, inst := range instances {
150152 t.table.Put(inst)
151153 }
Show on old repository browser