github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/vectors/vectors_v2.go (about)

     1  package vectors
     2  
     3  import (
     4  	"github.com/0xPolygon/supernets2-node/merkletree"
     5  	"github.com/0xPolygon/supernets2-node/state"
     6  )
     7  
     8  // StateTransitionTestCaseV2 holds the metadata needed to run a state transition test
     9  type StateTransitionTestCaseV2 struct {
    10  	Description          string          `json:"Description"`
    11  	Genesis              []GenesisEntity `json:"genesis"`
    12  	ExpectedOldStateRoot string          `json:"expectedOldStateRoot"`
    13  	ExpectedNewStateRoot string          `json:"expectedNewStateRoot"`
    14  	ExpectedNewLeafs     []LeafV2        `json:"expectedNewLeafs"`
    15  	Receipts             []TestReceipt   `json:"receipts"`
    16  	GlobalExitRoot       string          `json:"globalExitRoot"`
    17  	BatchL2Data          string          `json:"batchL2Data"`
    18  }
    19  
    20  // LeafV2 represents the state of a leaf in the merkle tree
    21  type LeafV2 struct {
    22  	Address         string            `json:"address"`
    23  	Balance         argBigInt         `json:"balance"`
    24  	Nonce           string            `json:"nonce"`
    25  	Storage         map[string]string `json:"storage"`
    26  	IsSmartContract bool              `json:"isSmartContract"`
    27  	Bytecode        string            `json:"bytecode"`
    28  	HashBytecode    string            `json:"hashBytecode"`
    29  	BytecodeLength  int               `json:"bytecodeLength"`
    30  }
    31  
    32  // GenesisEntity represents the state of an account or smart contract when the network
    33  // starts
    34  type GenesisEntity struct {
    35  	Address         string            `json:"address"`
    36  	PvtKey          *string           `json:"pvtKey"`
    37  	Balance         argBigInt         `json:"balance"`
    38  	Nonce           string            `json:"nonce"`
    39  	Storage         map[string]string `json:"storage"`
    40  	IsSmartContract bool              `json:"isSmartContract"`
    41  	Bytecode        *string           `json:"bytecode"`
    42  }
    43  
    44  func GenerateGenesisActions(genesis []GenesisEntity) []*state.GenesisAction {
    45  	var genesisActions []*state.GenesisAction
    46  	for _, genesisEntity := range genesis {
    47  
    48  		if genesisEntity.Balance.String() != "0" {
    49  			action := &state.GenesisAction{
    50  				Address: genesisEntity.Address,
    51  				Type:    int(merkletree.LeafTypeBalance),
    52  				Value:   genesisEntity.Balance.String(),
    53  			}
    54  			genesisActions = append(genesisActions, action)
    55  		}
    56  
    57  		if genesisEntity.Nonce != "" && genesisEntity.Nonce != "0" {
    58  			action := &state.GenesisAction{
    59  				Address: genesisEntity.Address,
    60  				Type:    int(merkletree.LeafTypeNonce),
    61  				Value:   genesisEntity.Nonce,
    62  			}
    63  			genesisActions = append(genesisActions, action)
    64  		}
    65  
    66  		if genesisEntity.IsSmartContract && genesisEntity.Bytecode != nil && *genesisEntity.Bytecode != "0x" {
    67  			action := &state.GenesisAction{
    68  				Address:  genesisEntity.Address,
    69  				Type:     int(merkletree.LeafTypeCode),
    70  				Bytecode: *genesisEntity.Bytecode,
    71  			}
    72  			genesisActions = append(genesisActions, action)
    73  		}
    74  
    75  		if genesisEntity.IsSmartContract && len(genesisEntity.Storage) > 0 {
    76  			for storageKey, storageValue := range genesisEntity.Storage {
    77  				action := &state.GenesisAction{
    78  					Address:         genesisEntity.Address,
    79  					Type:            int(merkletree.LeafTypeStorage),
    80  					StoragePosition: storageKey,
    81  					Value:           storageValue,
    82  				}
    83  				genesisActions = append(genesisActions, action)
    84  			}
    85  		}
    86  	}
    87  
    88  	return genesisActions
    89  }