github.com/amazechain/amc@v0.1.3/common/block/body.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package block
    18  
    19  import (
    20  	"fmt"
    21  	"github.com/amazechain/amc/api/protocol/types_pb"
    22  	"github.com/amazechain/amc/common/transaction"
    23  	"github.com/amazechain/amc/common/types"
    24  	"github.com/amazechain/amc/utils"
    25  	"google.golang.org/protobuf/proto"
    26  )
    27  
    28  type Body struct {
    29  	Txs       []*transaction.Transaction
    30  	Verifiers []*Verify
    31  	Rewards   []*Reward
    32  }
    33  
    34  func (b *Body) ToProtoMessage() proto.Message {
    35  	var pbTxs []*types_pb.Transaction
    36  	var pbVerifiers []*types_pb.Verifier
    37  	var pbRewards []*types_pb.Reward
    38  
    39  	for _, v := range b.Txs {
    40  		pbTx := v.ToProtoMessage()
    41  		if pbTx != nil {
    42  			pbTxs = append(pbTxs, pbTx.(*types_pb.Transaction))
    43  		}
    44  	}
    45  
    46  	for _, reward := range b.Rewards {
    47  		pbReward := reward.ToProtoMessage()
    48  		if pbReward != nil {
    49  			pbRewards = append(pbRewards, pbReward.(*types_pb.Reward))
    50  		}
    51  	}
    52  
    53  	for _, verifier := range b.Verifiers {
    54  		pbVerifier := verifier.ToProtoMessage()
    55  		if pbVerifier != nil {
    56  			pbVerifiers = append(pbVerifiers, pbVerifier.(*types_pb.Verifier))
    57  		}
    58  	}
    59  
    60  	pBody := types_pb.Body{
    61  		Txs:       pbTxs,
    62  		Verifiers: pbVerifiers,
    63  		Rewards:   pbRewards,
    64  	}
    65  
    66  	return &pBody
    67  }
    68  
    69  func (b *Body) FromProtoMessage(message proto.Message) error {
    70  	var (
    71  		pBody *types_pb.Body
    72  		ok    bool
    73  	)
    74  
    75  	if pBody, ok = message.(*types_pb.Body); !ok {
    76  		return fmt.Errorf("type conversion failure")
    77  	}
    78  
    79  	var txs []*transaction.Transaction
    80  	//
    81  	for _, v := range pBody.Txs {
    82  		tx, err := transaction.FromProtoMessage(v)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		txs = append(txs, tx)
    87  	}
    88  	//
    89  	b.Txs = txs
    90  
    91  	//verifiers
    92  	var verifiers []*Verify
    93  	for _, v := range pBody.Verifiers {
    94  		verify := new(Verify).FromProtoMessage(v)
    95  		verifiers = append(verifiers, verify)
    96  	}
    97  	b.Verifiers = verifiers
    98  
    99  	//Reward
   100  	var rewards []*Reward
   101  	for _, v := range pBody.Rewards {
   102  		reward := new(Reward).FromProtoMessage(v)
   103  		rewards = append(rewards, reward)
   104  	}
   105  	b.Rewards = rewards
   106  
   107  	return nil
   108  }
   109  
   110  func (b *Body) Transactions() []*transaction.Transaction {
   111  	return b.Txs
   112  }
   113  func (b *Body) Verifier() []*Verify {
   114  	return b.Verifiers
   115  }
   116  
   117  func (b *Body) Reward() []*Reward {
   118  	return b.Rewards
   119  }
   120  
   121  func (b *Body) reward() []*types_pb.H256 {
   122  	var rewardAmount []*types_pb.H256
   123  	if len(b.Rewards) > 0 {
   124  		for _, reward := range b.Rewards {
   125  			rewardAmount = append(rewardAmount, utils.ConvertUint256IntToH256(reward.Amount))
   126  		}
   127  	}
   128  	return rewardAmount
   129  }
   130  
   131  func (b *Body) rewardAddress() []types.Address {
   132  	var rewardAddress []types.Address
   133  	for _, reward := range b.Rewards {
   134  		rewardAddress = append(rewardAddress, reward.Address)
   135  	}
   136  	return rewardAddress
   137  }
   138  
   139  func (b *Body) SendersFromTxs() []types.Address {
   140  	senders := make([]types.Address, len(b.Transactions()))
   141  	for i, tx := range b.Transactions() {
   142  		senders[i] = *tx.From()
   143  	}
   144  	return senders
   145  }
   146  
   147  func (b *Body) SendersToTxs(senders []types.Address) {
   148  	if senders == nil {
   149  		return
   150  	}
   151  
   152  	//todo
   153  	//for i, tx := range b.Txs {
   154  	//	//tx.SetFrom(senders[i])
   155  	//}
   156  }
   157  
   158  type BodyForStorage struct {
   159  	BaseTxId uint64
   160  	TxAmount uint32
   161  }
   162  
   163  func NewBlockFromStorage(hash types.Hash, header *Header, body *Body) *Block {
   164  	b := &Block{header: header, body: body}
   165  	b.hash.Store(hash)
   166  	return b
   167  }
   168  
   169  type RawBody struct {
   170  	Transactions [][]byte
   171  }