github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/context.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package nvm
    20  
    21  import (
    22  	"math/rand"
    23  	"unsafe"
    24  
    25  	"github.com/gogo/protobuf/proto"
    26  	"github.com/nebulasio/go-nebulas/core"
    27  	"github.com/nebulasio/go-nebulas/core/pb"
    28  )
    29  
    30  // SerializableAccount serializable account state
    31  type SerializableAccount struct {
    32  	Nonce   uint64 `json:"nonce"`
    33  	Balance string `json:"balance"`
    34  }
    35  
    36  // SerializableBlock serializable block
    37  type SerializableBlock struct {
    38  	Timestamp int64  `json:"timestamp"`
    39  	Hash      string `json:"hash"`
    40  	Height    uint64 `json:"height"`
    41  	Seed      string `json:"seed,omitempty"`
    42  }
    43  
    44  // SerializableTransaction serializable transaction
    45  type SerializableTransaction struct {
    46  	Hash      string `json:"hash"`
    47  	From      string `json:"from"`
    48  	To        string `json:"to"`
    49  	Value     string `json:"value"`
    50  	Nonce     uint64 `json:"nonce"`
    51  	Timestamp int64  `json:"timestamp"`
    52  	GasPrice  string `json:"gasPrice"`
    53  	GasLimit  string `json:"gasLimit"`
    54  }
    55  
    56  // ContextRand ..
    57  type ContextRand struct {
    58  	rand *rand.Rand
    59  }
    60  
    61  // Context nvm engine context
    62  type Context struct {
    63  	block       Block
    64  	tx          Transaction
    65  	contract    Account
    66  	state       WorldState
    67  	head        unsafe.Pointer
    68  	index       uint32
    69  	contextRand *ContextRand
    70  }
    71  
    72  // NewContext create a engine context
    73  func NewContext(block Block, tx Transaction, contract Account, state WorldState) (*Context, error) {
    74  	if block == nil || tx == nil || contract == nil || state == nil {
    75  		return nil, ErrContextConstructArrEmpty
    76  	}
    77  	ctx := &Context{
    78  		block:       block,
    79  		tx:          tx,
    80  		contract:    contract,
    81  		state:       state,
    82  		contextRand: &ContextRand{},
    83  	}
    84  	return ctx, nil
    85  }
    86  
    87  // NewInnerContext create a child engine context
    88  func NewInnerContext(block Block, tx Transaction, contract Account, state WorldState, head unsafe.Pointer, index uint32, ctxRand *ContextRand) (*Context, error) {
    89  	if block == nil || tx == nil || contract == nil || state == nil || head == nil {
    90  		return nil, ErrContextConstructArrEmpty
    91  	}
    92  	ctx := &Context{
    93  		block:       block,
    94  		tx:          tx,
    95  		contract:    contract,
    96  		state:       state,
    97  		head:        head,
    98  		index:       index,
    99  		contextRand: ctxRand,
   100  	}
   101  	return ctx, nil
   102  }
   103  func toSerializableAccount(acc Account) *SerializableAccount {
   104  	sAcc := &SerializableAccount{
   105  		Nonce:   acc.Nonce(),
   106  		Balance: acc.Balance().String(),
   107  	}
   108  	return sAcc
   109  }
   110  
   111  func toSerializableBlock(block Block) *SerializableBlock {
   112  	sBlock := &SerializableBlock{
   113  		Timestamp: block.Timestamp(),
   114  		Hash:      "",
   115  		Height:    block.Height(),
   116  	}
   117  	if core.V8BlockSeedAvailableAtHeight(block.Height()) {
   118  		sBlock.Seed = block.RandomSeed()
   119  	}
   120  	return sBlock
   121  }
   122  
   123  func toSerializableTransaction(tx Transaction) *SerializableTransaction {
   124  	return &SerializableTransaction{
   125  		From:      tx.From().String(),
   126  		To:        tx.To().String(),
   127  		Value:     tx.Value().String(),
   128  		Timestamp: tx.Timestamp(),
   129  		Nonce:     tx.Nonce(),
   130  		Hash:      tx.Hash().String(),
   131  		GasPrice:  tx.GasPrice().String(),
   132  		GasLimit:  tx.GasLimit().String(),
   133  	}
   134  }
   135  
   136  func toSerializableTransactionFromBytes(txBytes []byte) (*SerializableTransaction, error) {
   137  	pbTx := new(corepb.Transaction)
   138  	if err := proto.Unmarshal(txBytes, pbTx); err != nil {
   139  		return nil, err
   140  	}
   141  	tx := new(core.Transaction)
   142  	if err := tx.FromProto(pbTx); err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	return &SerializableTransaction{
   147  		From:      tx.From().String(),
   148  		To:        tx.To().String(),
   149  		Value:     tx.Value().String(),
   150  		Timestamp: tx.Timestamp(),
   151  		Nonce:     tx.Nonce(),
   152  		Hash:      tx.Hash().String(),
   153  		GasPrice:  tx.GasPrice().String(),
   154  		GasLimit:  tx.GasLimit().String(),
   155  	}, nil
   156  }