OSDN Git Service

contract_trace (#2099)
authorPoseidon <shenao.78@163.com>
Wed, 1 Sep 2021 06:34:00 +0000 (14:34 +0800)
committerGitHub <noreply@github.com>
Wed, 1 Sep 2021 06:34:00 +0000 (14:34 +0800)
* contract_trace

* fix new instance table

* fix calc utxo key

* add get instance by tx hash

* remove unconfirmed utxos from instance

* opt instance

* remove tx hash from instance

* add remove instance for table

* fix remove by trace id

* fix trade service interface

* add new instance func

* contract trace

* import fmt

* remove utxo key

contract/infrastructure.go [new file with mode: 0644]
contract/instance.go [new file with mode: 0644]
contract/trace_scheduler.go [new file with mode: 0644]
contract/trace_service.go [new file with mode: 0644]
contract/tracer.go [new file with mode: 0644]

diff --git a/contract/infrastructure.go b/contract/infrastructure.go
new file mode 100644 (file)
index 0000000..c56df6c
--- /dev/null
@@ -0,0 +1,26 @@
+package contract
+
+import (
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+)
+
+type Infrastructure struct {
+       Chain      ChainService
+       Repository Repository
+}
+
+func NewInfrastructure(chain ChainService, repository Repository) *Infrastructure {
+       return &Infrastructure{Chain: chain, Repository: repository}
+}
+
+type ChainService interface {
+       BestChain() (uint64, bc.Hash)
+       GetBlock(hash bc.Hash) (*types.Block, error)
+}
+
+type Repository interface {
+       LoadInstances() ([]*Instance, error)
+       SaveInstances(instances []*Instance) error
+       RemoveInstance(id string) error
+}
diff --git a/contract/instance.go b/contract/instance.go
new file mode 100644 (file)
index 0000000..643b678
--- /dev/null
@@ -0,0 +1,93 @@
+package contract
+
+import (
+       "github.com/google/uuid"
+
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+)
+
+type Instance struct {
+       TraceID   string
+       UTXOs     []*UTXO
+       Finalized bool
+       InSync    bool
+}
+
+func NewInstance(inUTXOs, outUTXOs []*UTXO) *Instance {
+       inst := &Instance{
+               TraceID:   uuid.New().String(),
+               UTXOs:     outUTXOs,
+               Finalized: len(outUTXOs) == 0,
+       }
+       if inst.Finalized {
+               inst.UTXOs = inUTXOs
+       }
+       return inst
+}
+
+type InstanceTable struct {
+       traceIdToInst  map[string]*Instance
+       utxoHashToInst map[string]*Instance
+}
+
+func NewInstanceTable() *InstanceTable {
+       return &InstanceTable{
+               traceIdToInst:  make(map[string]*Instance),
+               utxoHashToInst: make(map[string]*Instance),
+       }
+}
+
+func (i *InstanceTable) GetByID(id string) *Instance {
+       return i.traceIdToInst[id]
+}
+
+func (i *InstanceTable) GetByUTXO(utxoHash string) *Instance {
+       return i.utxoHashToInst[utxoHash]
+}
+
+func (i *InstanceTable) Put(instance *Instance) {
+       i.traceIdToInst[instance.TraceID] = instance
+       for _, utxo := range instance.UTXOs {
+               i.utxoHashToInst[utxo.hash.String()] = instance
+       }
+       // TODO must remove prev key of utxos
+}
+
+func (i *InstanceTable) Remove(id string) {
+       if inst, ok := i.traceIdToInst[id]; ok {
+               delete(i.traceIdToInst, id)
+               for _, utxo := range inst.UTXOs {
+                       delete(i.utxoHashToInst, utxo.hash.String())
+               }
+       }
+}
+
+type UTXO struct {
+       assetID   bc.AssetID
+       sourceID  uint64
+       sourcePos uint64
+       stateData []byte
+       amount    uint64
+       hash      bc.Hash
+       program   []byte
+}
+
+func inputToUTXO(input *types.TxInput) *UTXO {
+       outputID, _ := input.SpentOutputID()
+       return &UTXO{
+               assetID: input.AssetID(),
+               amount:  input.Amount(),
+               hash:    outputID,
+               program: input.ControlProgram(),
+       }
+}
+
+func outputToUTXO(output *types.TxOutput, outputID bc.Hash) *UTXO {
+       return &UTXO{
+               assetID: *output.AssetId,
+               amount:  output.Amount,
+               hash:    outputID,
+               program: output.ControlProgram,
+       }
+}
diff --git a/contract/trace_scheduler.go b/contract/trace_scheduler.go
new file mode 100644 (file)
index 0000000..c7845a0
--- /dev/null
@@ -0,0 +1,7 @@
+package contract
+
+type TraceScheduler struct {
+       infra *Infrastructure
+}
+
+func (t *TraceScheduler) AddNewJob(instance *Instance) {}
diff --git a/contract/trace_service.go b/contract/trace_service.go
new file mode 100644 (file)
index 0000000..38ba30b
--- /dev/null
@@ -0,0 +1,13 @@
+package contract
+
+import (
+       "github.com/bytom/bytom/protocol/bc"
+)
+
+type TraceService interface {
+       CreateInstance(txHash, blockHash bc.Hash) (string, error)
+
+       RemoveInstance(traceID string) error
+
+       GetInstance(traceID string) (*Instance, error)
+}
diff --git a/contract/tracer.go b/contract/tracer.go
new file mode 100644 (file)
index 0000000..1c8ccc1
--- /dev/null
@@ -0,0 +1,39 @@
+package contract
+
+import (
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+)
+
+type Tracer struct {
+       table *InstanceTable
+       infra *Infrastructure
+}
+
+func (t *Tracer) ApplyBlock(block *types.Block) error {
+       return nil
+}
+
+func (t *Tracer) DetachBlock(block *types.Block) error {
+       return nil
+}
+
+func (t *Tracer) AddUnconfirmedTx(tx *types.Tx) error {
+       return nil
+}
+
+func (t *Tracer) CreateInstance(txHash, blockHash bc.Hash) (string, error) {
+       return "", nil
+}
+
+func (t *Tracer) RemoveInstance(traceID string) error {
+       return nil
+}
+
+func (t *Tracer) GetInstance(traceID string) (*Instance, error) {
+       return nil, nil
+}
+
+func (t *Tracer) takeOverInstance(instance *Instance) bool {
+       return false
+}