github.com/annchain/OG@v0.0.9/og/archive/archive.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 archive
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	types2 "github.com/annchain/OG/arefactor/og/types"
    20  	"github.com/annchain/OG/common"
    21  	"github.com/annchain/OG/common/byteutil"
    22  	"github.com/annchain/OG/consensus/campaign"
    23  	"github.com/annchain/OG/og/types"
    24  	"github.com/annchain/OG/og/types/archive"
    25  
    26  	"github.com/annchain/OG/types"
    27  	"golang.org/x/crypto/sha3"
    28  	"math/rand"
    29  	"strings"
    30  )
    31  
    32  //go:generate msgp
    33  
    34  //msgp:tuple Archive
    35  type Archive struct {
    36  	types.TxBase
    37  	Data []byte `json:"data"`
    38  }
    39  
    40  //msgp:tuple ArchiveJson
    41  type ArchiveJson struct {
    42  	types.TxBaseJson
    43  	Data []byte `json:"data"`
    44  }
    45  
    46  func (a *Archive) ToSmallCaseJson() ([]byte, error) {
    47  	if a == nil {
    48  		return nil, nil
    49  	}
    50  	j := ArchiveJson{
    51  		TxBaseJson: *a.TxBase.ToSmallCase(),
    52  		Data:       a.Data,
    53  	}
    54  	return json.Marshal(&j)
    55  }
    56  
    57  //msgp:tuple Campaigns
    58  type Archives []*Archive
    59  
    60  func (a *Archive) GetBase() *types.TxBase {
    61  	return &a.TxBase
    62  }
    63  
    64  func (a *Archive) Sender() common.Address {
    65  	panic("not implemented")
    66  	return common.Address{}
    67  	//return  &Address{}
    68  }
    69  
    70  func (tc *Archive) GetSender() *common.Address {
    71  	panic("not implemented")
    72  	return nil
    73  }
    74  
    75  func (c *Archive) Compare(tx types.Txi) bool {
    76  	switch tx := tx.(type) {
    77  	case *campaign.Campaign:
    78  		if c.GetHash().Cmp(tx.GetHash()) == 0 {
    79  			return true
    80  		}
    81  		return false
    82  	default:
    83  		return false
    84  	}
    85  }
    86  
    87  func (c *Archive) Dump() string {
    88  	var phashes []string
    89  	for _, p := range c.ParentsHash {
    90  		phashes = append(phashes, p.Hex())
    91  	}
    92  	return fmt.Sprintf("hash: %s, pHash: [%s] , nonce: %d  ,Data: %x", c.Hash.Hex(),
    93  		strings.Join(phashes, " ,"), c.AccountNonce, c.Data)
    94  }
    95  
    96  func (a *Archive) SignatureTargets() []byte {
    97  	// add parents infornmation.
    98  	panic("not inplemented")
    99  }
   100  
   101  func (a *Archive) String() string {
   102  	return fmt.Sprintf("%s-%d-Ac", a.TxBase.String(), a.AccountNonce)
   103  }
   104  
   105  func (as Archives) String() string {
   106  	var strs []string
   107  	for _, v := range as {
   108  		strs = append(strs, v.String())
   109  	}
   110  	return strings.Join(strs, ", ")
   111  }
   112  
   113  func (a *Archive) RawArchive() *RawArchive {
   114  	if a == nil {
   115  		return nil
   116  	}
   117  	ra := RawArchive{
   118  		Archive: *a,
   119  	}
   120  	return &ra
   121  }
   122  
   123  func (cs Archives) RawArchives() RawArchives {
   124  	if len(cs) == 0 {
   125  		return nil
   126  	}
   127  	var rawCps RawArchives
   128  	for _, v := range cs {
   129  		rasSeq := v.RawArchive()
   130  		rawCps = append(rawCps, rasSeq)
   131  	}
   132  	return rawCps
   133  }
   134  
   135  func (c *Archive) RawTxi() archive.RawTxi {
   136  	return c.RawArchive()
   137  }
   138  
   139  func RandomArchive() *Archive {
   140  	return &Archive{TxBase: types.TxBase{
   141  		Hash:        types2.RandomHash(),
   142  		Height:      uint64(rand.Int63n(1000)),
   143  		ParentsHash: types2.Hashes{types2.RandomHash(), types2.RandomHash()},
   144  		Type:        types.TxBaseTypeArchive,
   145  		//AccountNonce: uint64(rand.Int63n(50000)),
   146  		Weight: uint64(rand.Int31n(2000)),
   147  	},
   148  		Data: types2.RandomHash().ToBytes(),
   149  	}
   150  }
   151  
   152  func (t *Archive) CalcTxHash() (hash types2.Hash) {
   153  	w := byteutil.NewBinaryWriter()
   154  
   155  	for _, ancestor := range t.ParentsHash {
   156  		w.Write(ancestor.Bytes)
   157  	}
   158  	// do not use Height to calculate tx hash.
   159  	w.Write(t.Weight, t.Data, t.CalcMinedHash().Bytes)
   160  	result := sha3.Sum256(w.Bytes())
   161  	hash.MustSetBytes(result[0:], types2.PaddingNone)
   162  	return
   163  }
   164  
   165  func (t *Archive) SetSender(address common.Address) {
   166  	return
   167  }