github.com/dim4egster/coreth@v0.10.2/plugin/evm/test_tx.go (about)

     1  // (c) 2020-2021, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"math/big"
     8  	"math/rand"
     9  
    10  	"github.com/dim4egster/qmallgo/utils"
    11  
    12  	"github.com/dim4egster/qmallgo/chains/atomic"
    13  	"github.com/dim4egster/qmallgo/codec"
    14  	"github.com/dim4egster/qmallgo/codec/linearcodec"
    15  	"github.com/dim4egster/qmallgo/ids"
    16  	"github.com/dim4egster/qmallgo/snow"
    17  	"github.com/dim4egster/qmallgo/utils/wrappers"
    18  	"github.com/dim4egster/coreth/core/state"
    19  	"github.com/dim4egster/coreth/params"
    20  )
    21  
    22  type TestUnsignedTx struct {
    23  	GasUsedV                    uint64           `serialize:"true"`
    24  	AcceptRequestsBlockchainIDV ids.ID           `serialize:"true"`
    25  	AcceptRequestsV             *atomic.Requests `serialize:"true"`
    26  	VerifyV                     error
    27  	IDV                         ids.ID `serialize:"true" json:"id"`
    28  	BurnedV                     uint64 `serialize:"true"`
    29  	UnsignedBytesV              []byte
    30  	SignedBytesV                []byte
    31  	InputUTXOsV                 ids.Set
    32  	SemanticVerifyV             error
    33  	EVMStateTransferV           error
    34  }
    35  
    36  var _ UnsignedAtomicTx = &TestUnsignedTx{}
    37  
    38  // GasUsed implements the UnsignedAtomicTx interface
    39  func (t *TestUnsignedTx) GasUsed(fixedFee bool) (uint64, error) { return t.GasUsedV, nil }
    40  
    41  // Verify implements the UnsignedAtomicTx interface
    42  func (t *TestUnsignedTx) Verify(ctx *snow.Context, rules params.Rules) error { return t.VerifyV }
    43  
    44  // AtomicOps implements the UnsignedAtomicTx interface
    45  func (t *TestUnsignedTx) AtomicOps() (ids.ID, *atomic.Requests, error) {
    46  	return t.AcceptRequestsBlockchainIDV, t.AcceptRequestsV, nil
    47  }
    48  
    49  // Initialize implements the UnsignedAtomicTx interface
    50  func (t *TestUnsignedTx) Initialize(unsignedBytes, signedBytes []byte) {}
    51  
    52  // ID implements the UnsignedAtomicTx interface
    53  func (t *TestUnsignedTx) ID() ids.ID { return t.IDV }
    54  
    55  // Burned implements the UnsignedAtomicTx interface
    56  func (t *TestUnsignedTx) Burned(assetID ids.ID) (uint64, error) { return t.BurnedV, nil }
    57  
    58  // Bytes implements the UnsignedAtomicTx interface
    59  func (t *TestUnsignedTx) Bytes() []byte { return t.UnsignedBytesV }
    60  
    61  // SignedBytes implements the UnsignedAtomicTx interface
    62  func (t *TestUnsignedTx) SignedBytes() []byte { return t.SignedBytesV }
    63  
    64  // InputUTXOs implements the UnsignedAtomicTx interface
    65  func (t *TestUnsignedTx) InputUTXOs() ids.Set { return t.InputUTXOsV }
    66  
    67  // SemanticVerify implements the UnsignedAtomicTx interface
    68  func (t *TestUnsignedTx) SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules params.Rules) error {
    69  	return t.SemanticVerifyV
    70  }
    71  
    72  // EVMStateTransfer implements the UnsignedAtomicTx interface
    73  func (t *TestUnsignedTx) EVMStateTransfer(ctx *snow.Context, state *state.StateDB) error {
    74  	return t.EVMStateTransferV
    75  }
    76  
    77  func testTxCodec() codec.Manager {
    78  	codec := codec.NewDefaultManager()
    79  	c := linearcodec.NewDefault()
    80  
    81  	errs := wrappers.Errs{}
    82  	errs.Add(
    83  		c.RegisterType(&TestUnsignedTx{}),
    84  		c.RegisterType(&atomic.Element{}),
    85  		c.RegisterType(&atomic.Requests{}),
    86  		codec.RegisterCodec(codecVersion, c),
    87  	)
    88  
    89  	if errs.Errored() {
    90  		panic(errs.Err)
    91  	}
    92  	return codec
    93  }
    94  
    95  var blockChainID = ids.GenerateTestID()
    96  
    97  func testDataImportTx() *Tx {
    98  	return &Tx{
    99  		UnsignedAtomicTx: &TestUnsignedTx{
   100  			IDV:                         ids.GenerateTestID(),
   101  			AcceptRequestsBlockchainIDV: blockChainID,
   102  			AcceptRequestsV: &atomic.Requests{
   103  				RemoveRequests: [][]byte{
   104  					utils.RandomBytes(32),
   105  					utils.RandomBytes(32),
   106  				},
   107  			},
   108  		},
   109  	}
   110  }
   111  
   112  func testDataExportTx() *Tx {
   113  	return &Tx{
   114  		UnsignedAtomicTx: &TestUnsignedTx{
   115  			IDV:                         ids.GenerateTestID(),
   116  			AcceptRequestsBlockchainIDV: blockChainID,
   117  			AcceptRequestsV: &atomic.Requests{
   118  				PutRequests: []*atomic.Element{
   119  					{
   120  						Key:   utils.RandomBytes(16),
   121  						Value: utils.RandomBytes(24),
   122  						Traits: [][]byte{
   123  							utils.RandomBytes(32),
   124  							utils.RandomBytes(32),
   125  						},
   126  					},
   127  				},
   128  			},
   129  		},
   130  	}
   131  }
   132  
   133  func newTestTx() *Tx {
   134  	txType := rand.Intn(2)
   135  	switch txType {
   136  	case 0:
   137  		return testDataImportTx()
   138  	case 1:
   139  		return testDataExportTx()
   140  	default:
   141  		panic("rng generated unexpected value for tx type")
   142  	}
   143  }
   144  
   145  func newTestTxs(numTxs int) []*Tx {
   146  	txs := make([]*Tx, 0, numTxs)
   147  	for i := 0; i < numTxs; i++ {
   148  		txs = append(txs, newTestTx())
   149  	}
   150  
   151  	return txs
   152  }