github.com/annchain/OG@v0.0.9/wserver/convertor.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package wserver
    15  
    16  import (
    17  	"github.com/annchain/OG/og/types"
    18  )
    19  
    20  //type Tx struct {
    21  //	TxBase
    22  //	From  Address
    23  //	To    Address
    24  //	Value *math.BigInt
    25  //}
    26  //type TxBase struct {
    27  //	Type         TxBaseType
    28  //	Hash         Hash
    29  //	ParentsHash  Hashes
    30  //	AccountNonce uint64
    31  //	Height       uint64
    32  //	PublicKey    []byte
    33  //	Signature    []byte
    34  //}
    35  
    36  type NodeData struct {
    37  	Unit   string `json:"unit"`
    38  	Unit_s string `json:"unit_s"`
    39  	//Tx    types.TxiSmallCaseMarshal `json:"tx"`
    40  }
    41  
    42  type Node struct {
    43  	Data NodeData `json:"data"`
    44  	Type string   `json:"type"`
    45  }
    46  
    47  type Edge struct {
    48  	Id     string `json:"id"`
    49  	Source string `json:"source"`
    50  	Target string `json:"target"`
    51  }
    52  
    53  type UIData struct {
    54  	Type  string `json:"type"`
    55  	Nodes []Node `json:"nodes"`
    56  	Edges []Edge `json:"edges"`
    57  }
    58  
    59  type BlockDbUIData struct {
    60  	Nodes []types.TxiSmallCaseMarshal `json:"nodes"`
    61  	//Type  string `json:"type"`
    62  	//Edges []Edge `json:"edges"`
    63  }
    64  
    65  func (u *UIData) AddToBatch(tx types.Txi, includingEdge bool) {
    66  	nodeData := NodeData{
    67  		Unit:   tx.GetHash().Hex(),
    68  		Unit_s: tx.String(),
    69  	}
    70  	node := Node{
    71  		Data: nodeData,
    72  	}
    73  
    74  	switch tx.GetBase().Type {
    75  	case types.TxBaseTypeSequencer:
    76  		node.Type = "sequencer_unit"
    77  	default:
    78  		node.Type = ""
    79  		//default:
    80  		//	node.Type = "Unknown"
    81  	}
    82  
    83  	u.Nodes = append(u.Nodes, node)
    84  
    85  	if includingEdge {
    86  		for _, parent := range tx.GetParents() {
    87  			edge := Edge{
    88  				Id:     tx.String() + "_" + parent.String(),
    89  				Source: tx.GetHash().Hex(),
    90  				Target: parent.Hex(),
    91  			}
    92  			u.Edges = append(u.Edges, edge)
    93  		}
    94  	}
    95  
    96  }