From 774931ac7c254f369058846fbafa404df6f0b77f Mon Sep 17 00:00:00 2001 From: Alexander Cristurean Date: Mon, 15 Jul 2024 17:47:08 +0300 Subject: [PATCH] added v2 for shard conversion. --- .../hyperOutportBlocks/grpcBlockService.pb.go | 2 +- process/outportBlockConverter.go | 325 ++++++++++++------ process/outportBlockConverter_test.go | 289 +++++++++++++++- 3 files changed, 506 insertions(+), 110 deletions(-) diff --git a/data/hyperOutportBlocks/grpcBlockService.pb.go b/data/hyperOutportBlocks/grpcBlockService.pb.go index b46e69a..002bedc 100644 --- a/data/hyperOutportBlocks/grpcBlockService.pb.go +++ b/data/hyperOutportBlocks/grpcBlockService.pb.go @@ -328,7 +328,7 @@ func file_grpcBlockService_proto_init() { if File_grpcBlockService_proto != nil { return } - file_data_hyperOutportBlocks_hyperOutportBlock_proto_init() + file_hyperOutportBlock_proto_init() if !protoimpl.UnsafeEnabled { file_grpcBlockService_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*BlockHashRequest); i { diff --git a/process/outportBlockConverter.go b/process/outportBlockConverter.go index dc1d4c2..99cacac 100644 --- a/process/outportBlockConverter.go +++ b/process/outportBlockConverter.go @@ -7,6 +7,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" coreData "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/alteredAccount" "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/outport" "github.com/multiversx/mx-chain-core-go/marshal" @@ -48,22 +49,18 @@ func (o *outportBlockConverter) HandleShardOutportBlockV2(outportBlock *outport. return nil, fmt.Errorf("cannot convert to shard outport block. header type: %s not supported", headerType) } - shardOutportBlock := &hyperOutportBlocks.ShardOutportBlockV2{ - BlockData: &hyperOutportBlocks.BlockData{}, - } + shardOutportBlock := &hyperOutportBlocks.ShardOutportBlockV2{} shardOutportBlock.ShardID = outportBlock.ShardID - blockData, err := o.handleBlockData(outportBlock.BlockData, shardOutportBlock.BlockData) + err := o.handleBlockData(outportBlock.BlockData, shardOutportBlock) if err != nil { return nil, fmt.Errorf("failed to migrate block data: %w", err) } - err = o.handleTransactionPool(outportBlock.TransactionPool, shardOutportBlock.TransactionPool) + err = o.handleTransactionPool(outportBlock.TransactionPool, shardOutportBlock) if err != nil { return nil, fmt.Errorf("failed to migrate transacion pool: %w", err) } - //TODO: add these - //handleHeaderGasConsumption() - //handleAlteredAccounts() - shardOutportBlock.BlockData = blockData + handleHeaderGasConsumption(outportBlock.HeaderGasConsumption, shardOutportBlock) + handleAlteredAccounts(outportBlock.AlteredAccounts, shardOutportBlock) shardOutportBlock.NotarizedHeadersHashes = outportBlock.NotarizedHeadersHashes shardOutportBlock.NumberOfShards = outportBlock.NumberOfShards @@ -74,30 +71,39 @@ func (o *outportBlockConverter) HandleShardOutportBlockV2(outportBlock *outport. return shardOutportBlock, nil } -func (o *outportBlockConverter) handleBlockData(blockData *outport.BlockData, shardBlockData *hyperOutportBlocks.BlockData) (*hyperOutportBlocks.BlockData, error) { - shardBlockData.ShardID = blockData.ShardID - shardBlockData.HeaderType = blockData.HeaderType - shardBlockData.HeaderHash = blockData.HeaderHash +func (o *outportBlockConverter) handleBlockData(blockData *outport.BlockData, shardOutportBlock *hyperOutportBlocks.ShardOutportBlockV2) error { + if blockData == nil { + return nil + } + + shardOutportBlock.BlockData = &hyperOutportBlocks.BlockData{ + ShardID: blockData.ShardID, + Header: nil, + HeaderType: blockData.HeaderType, + HeaderHash: blockData.HeaderHash, + Body: nil, + IntraShardMiniBlocks: nil, + } var err error switch blockData.HeaderType { case string(core.ShardHeaderV1): - err = o.handleHeaderV1(blockData.HeaderBytes, shardBlockData) + err = o.handleHeaderV1(blockData.HeaderBytes, shardOutportBlock.BlockData) if err != nil { - return nil, err + return fmt.Errorf("failed to handle header v1: %w", err) } case string(core.ShardHeaderV2): - err = o.handleHeaderV2(blockData.HeaderBytes, shardBlockData) + err = o.handleHeaderV2(blockData.HeaderBytes, shardOutportBlock.BlockData) if err != nil { - return nil, err + return fmt.Errorf("failed to handle header v2: %w", err) } default: - return nil, fmt.Errorf("unknown header type [%s]", blockData.HeaderType) + return fmt.Errorf("unknown header type [%s]", blockData.HeaderType) } - return shardBlockData, nil + return nil } func (o *outportBlockConverter) handleHeaderV1(headerBytes []byte, shardBlockData *hyperOutportBlocks.BlockData) error { @@ -107,6 +113,7 @@ func (o *outportBlockConverter) handleHeaderV1(headerBytes []byte, shardBlockDat return fmt.Errorf("failed to unmarshal: %w", err) } + shardBlockData.Header = &hyperOutportBlocks.Header{} header := shardBlockData.Header header.Nonce = blockHeader.Nonce header.PrevHash = blockHeader.PrevHash @@ -253,104 +260,117 @@ func (o *outportBlockConverter) handleHeaderV2(headerBytes []byte, shardBlockDat return nil } -func (o *outportBlockConverter) handleTransactionPool(outportTxPool *outport.TransactionPool, shardTxPool *hyperOutportBlocks.TransactionPoolV2) error { - transactions := make(map[string]*hyperOutportBlocks.TxInfoV2, 0) - var err error - for txHash, tx := range outportTxPool.Transactions { - txInfo := &hyperOutportBlocks.TxInfoV2{} +func (o *outportBlockConverter) handleTransactionPool(outportTxPool *outport.TransactionPool, shardOutportBlockV2 *hyperOutportBlocks.ShardOutportBlockV2) error { + if outportTxPool == nil { + return nil + } - // TxInfo - Transaction - txInfo.Transaction.Nonce = tx.Transaction.Nonce - txInfo.Transaction.Value, err = o.castBigInt(tx.Transaction.Value) - if err != nil { - return fmt.Errorf("failed to cast transaction [%s] value: %w", txHash, err) - } - txInfo.Transaction.RcvAddr = tx.Transaction.RcvAddr - txInfo.Transaction.RcvUserName = tx.Transaction.RcvUserName - txInfo.Transaction.SndAddr = tx.Transaction.SndAddr - txInfo.Transaction.SndUserName = tx.Transaction.SndUserName - txInfo.Transaction.GasPrice = tx.Transaction.GasPrice - txInfo.Transaction.GasLimit = tx.Transaction.GasLimit - txInfo.Transaction.Data = tx.Transaction.Data - txInfo.Transaction.ChainID = tx.Transaction.ChainID - txInfo.Transaction.Version = tx.Transaction.Version - txInfo.Transaction.Signature = tx.Transaction.Signature - txInfo.Transaction.Options = tx.Transaction.Options - txInfo.Transaction.GuardianAddr = tx.Transaction.GuardianAddr - txInfo.Transaction.GuardianSignature = tx.Transaction.GuardianSignature - - // TxInfo - FeeInfo - txInfo.FeeInfo.GasUsed = tx.FeeInfo.GasUsed - txInfo.FeeInfo.Fee, err = o.castBigInt(tx.FeeInfo.Fee) - if err != nil { - return fmt.Errorf("failed to cast transaction [%s] fee: %w", txHash, err) - } + shardOutportBlockV2.TransactionPool = &hyperOutportBlocks.TransactionPoolV2{} - txInfo.FeeInfo.InitialPaidFee, err = o.castBigInt(tx.FeeInfo.InitialPaidFee) - if err != nil { - return fmt.Errorf("failed to cast transaction [%s] initial paid fee: %w", txHash, err) - } + if outportTxPool.Transactions != nil { + shardOutportBlockV2.TransactionPool.Transactions = make(map[string]*hyperOutportBlocks.TxInfoV2, len(outportTxPool.Transactions)) + } - txInfo.ExecutionOrder = tx.ExecutionOrder + if outportTxPool.Rewards != nil { + shardOutportBlockV2.TransactionPool.Rewards = make(map[string]*hyperOutportBlocks.RewardInfo, len(outportTxPool.Rewards)) + } + + if outportTxPool.Receipts != nil { + shardOutportBlockV2.TransactionPool.Receipts = make(map[string]*hyperOutportBlocks.Receipt, len(outportTxPool.Receipts)) + } + + if outportTxPool.InvalidTxs != nil { + shardOutportBlockV2.TransactionPool.InvalidTxs = make(map[string]*hyperOutportBlocks.TxInfo, len(outportTxPool.InvalidTxs)) + } + + shardOutportBlockV2.TransactionPool.ScheduledExecutedSCRSHashesPrevBlock = outportTxPool.ScheduledExecutedSCRSHashesPrevBlock + shardOutportBlockV2.TransactionPool.ScheduledExecutedInvalidTxsHashesPrevBlock = outportTxPool.ScheduledExecutedInvalidTxsHashesPrevBlock + + for txHash, tx := range outportTxPool.Transactions { + txInfo := &hyperOutportBlocks.TxInfoV2{} - if sc, ok := outportTxPool.SmartContractResults[txHash]; ok { - txInfo.SmartContractResults.SmartContractResult.Nonce = sc.SmartContractResult.Nonce - txInfo.SmartContractResults.SmartContractResult.Value, err = o.castBigInt(sc.SmartContractResult.Value) + // TxInfo - Transaction + if tx.Transaction != nil { + value, err := o.castBigInt(tx.Transaction.Value) if err != nil { - return fmt.Errorf("failed to cast transaction [%s] smart contract result value: %s", txHash, sc.SmartContractResult.Value) + return fmt.Errorf("failed to cast transaction [%s] value: %w", txHash, err) } - txInfo.SmartContractResults.SmartContractResult.RcvAddr = sc.SmartContractResult.RcvAddr - txInfo.SmartContractResults.SmartContractResult.SndAddr = sc.SmartContractResult.SndAddr - txInfo.SmartContractResults.SmartContractResult.RelayerAddr = sc.SmartContractResult.RelayerAddr - txInfo.SmartContractResults.SmartContractResult.RelayedValue, err = o.castBigInt(sc.SmartContractResult.RelayedValue) - if err != nil { - return fmt.Errorf("failed to cast transaction [%s] smart contract result relayed value: %s", txHash, sc.SmartContractResult.Value) + + txInfo.Transaction = &hyperOutportBlocks.Transaction{ + Nonce: tx.Transaction.Nonce, + Value: value, + RcvAddr: tx.Transaction.RcvAddr, + RcvUserName: tx.Transaction.RcvUserName, + SndAddr: tx.Transaction.SndAddr, + SndUserName: tx.Transaction.SndUserName, + GasPrice: tx.Transaction.GasPrice, + GasLimit: tx.Transaction.GasLimit, + Data: tx.Transaction.Data, + ChainID: tx.Transaction.ChainID, + Version: tx.Transaction.Version, + Signature: tx.Transaction.Signature, + Options: tx.Transaction.Options, + GuardianAddr: tx.Transaction.GuardianAddr, + GuardianSignature: tx.Transaction.GuardianSignature, } - txInfo.SmartContractResults.SmartContractResult.Code = sc.SmartContractResult.Code - txInfo.SmartContractResults.SmartContractResult.Data = sc.SmartContractResult.Data - txInfo.SmartContractResults.SmartContractResult.PrevTxHash = sc.SmartContractResult.PrevTxHash - txInfo.SmartContractResults.SmartContractResult.OriginalTxHash = sc.SmartContractResult.OriginalTxHash - txInfo.SmartContractResults.SmartContractResult.GasLimit = sc.SmartContractResult.GasLimit - txInfo.SmartContractResults.SmartContractResult.GasPrice = sc.SmartContractResult.GasPrice - txInfo.SmartContractResults.SmartContractResult.CallType = int64(sc.SmartContractResult.CallType) - txInfo.SmartContractResults.SmartContractResult.CodeMetadata = sc.SmartContractResult.CodeMetadata - txInfo.SmartContractResults.SmartContractResult.ReturnMessage = sc.SmartContractResult.ReturnMessage - txInfo.SmartContractResults.SmartContractResult.OriginalSender = sc.SmartContractResult.OriginalSender - } - // Logs - for _, logData := range outportTxPool.Logs { - events := make([]*hyperOutportBlocks.Event, len(logData.Log.Events)) - for i, event := range logData.Log.Events { - e := &hyperOutportBlocks.Event{} + // TxInfo - FeeInfo + if tx.FeeInfo != nil { + fee, err := o.castBigInt(tx.FeeInfo.Fee) + if err != nil { + return fmt.Errorf("failed to cast transaction [%s] fee: %w", txHash, err) + } - e.Address = event.Address - e.Identifier = event.Identifier - e.Topics = event.Topics - e.Data = event.Data - e.AdditionalData = event.AdditionalData + initialPaidFee, err := o.castBigInt(tx.FeeInfo.InitialPaidFee) + if err != nil { + return fmt.Errorf("failed to cast transaction [%s] initial paid fee: %w", txHash, err) + } - events[i] = e + txInfo.FeeInfo = &hyperOutportBlocks.FeeInfo{ + GasUsed: tx.FeeInfo.GasUsed, + Fee: fee, + InitialPaidFee: initialPaidFee, + } } - ll := &hyperOutportBlocks.Log{ - Address: logData.Log.Address, - Events: events, - } + txInfo.ExecutionOrder = tx.ExecutionOrder - if transactions[logData.TxHash].Logs == nil { - transactions[logData.TxHash].Logs = make([]*hyperOutportBlocks.Log, 0) + if sc, ok := outportTxPool.SmartContractResults[txHash]; ok { + txInfo.SmartContractResults = &hyperOutportBlocks.SCRInfo{ + SmartContractResult: &hyperOutportBlocks.SmartContractResult{}, + } + txInfo.SmartContractResults.SmartContractResult.Nonce = sc.SmartContractResult.Nonce + txInfo.SmartContractResults.SmartContractResult.Value, err = o.castBigInt(sc.SmartContractResult.Value) + if err != nil { + return fmt.Errorf("failed to cast transaction [%s] smart contract result value: %s", txHash, sc.SmartContractResult.Value) + } + txInfo.SmartContractResults.SmartContractResult.RcvAddr = sc.SmartContractResult.RcvAddr + txInfo.SmartContractResults.SmartContractResult.SndAddr = sc.SmartContractResult.SndAddr + txInfo.SmartContractResults.SmartContractResult.RelayerAddr = sc.SmartContractResult.RelayerAddr + txInfo.SmartContractResults.SmartContractResult.RelayedValue, err = o.castBigInt(sc.SmartContractResult.RelayedValue) + if err != nil { + return fmt.Errorf("failed to cast transaction [%s] smart contract result relayed value: %s", txHash, sc.SmartContractResult.Value) + } + txInfo.SmartContractResults.SmartContractResult.Code = sc.SmartContractResult.Code + txInfo.SmartContractResults.SmartContractResult.Data = sc.SmartContractResult.Data + txInfo.SmartContractResults.SmartContractResult.PrevTxHash = sc.SmartContractResult.PrevTxHash + txInfo.SmartContractResults.SmartContractResult.OriginalTxHash = sc.SmartContractResult.OriginalTxHash + txInfo.SmartContractResults.SmartContractResult.GasLimit = sc.SmartContractResult.GasLimit + txInfo.SmartContractResults.SmartContractResult.GasPrice = sc.SmartContractResult.GasPrice + txInfo.SmartContractResults.SmartContractResult.CallType = int64(sc.SmartContractResult.CallType) + txInfo.SmartContractResults.SmartContractResult.CodeMetadata = sc.SmartContractResult.CodeMetadata + txInfo.SmartContractResults.SmartContractResult.ReturnMessage = sc.SmartContractResult.ReturnMessage + txInfo.SmartContractResults.SmartContractResult.OriginalSender = sc.SmartContractResult.OriginalSender } - transactions[logData.TxHash].Logs = append(transactions[logData.TxHash].Logs, ll) - } - - transactions[txHash] = txInfo + if outportTxPool.Logs != nil { + txInfo.Logs = make([]*hyperOutportBlocks.Log, len(outportTxPool.Logs)) + } - for _, l := range outportTxPool.Logs { - if _, ok := transactions[l.TxHash]; ok { - events := make([]*hyperOutportBlocks.Event, len(l.Log.Events)) - for i, event := range l.Log.Events { + // Logs + for i, logData := range outportTxPool.Logs { + events := make([]*hyperOutportBlocks.Event, len(logData.Log.Events)) + for i, event := range logData.Log.Events { e := &hyperOutportBlocks.Event{} e.Address = event.Address @@ -363,19 +383,116 @@ func (o *outportBlockConverter) handleTransactionPool(outportTxPool *outport.Tra } ll := &hyperOutportBlocks.Log{ - Address: l.Log.Address, + Address: logData.Log.Address, Events: events, } - transactions[l.TxHash].Logs = append(transactions[l.TxHash].Logs, ll) + txInfo.Logs[i] = ll } + + shardOutportBlockV2.TransactionPool.Transactions[txHash] = txInfo } } - shardTxPool.Transactions = transactions + for txHash, reward := range outportTxPool.Rewards { + value, err := o.castBigInt(reward.Reward.Value) + if err != nil { + return fmt.Errorf("failed to cast reward tx value: %w", err) + } + + shardOutportBlockV2.TransactionPool.Rewards[txHash] = &hyperOutportBlocks.RewardInfo{ + Reward: &hyperOutportBlocks.RewardTx{ + Round: reward.Reward.Round, + Value: value, + RcvAddr: reward.Reward.RcvAddr, + Epoch: reward.Reward.Epoch, + }, + ExecutionOrder: reward.ExecutionOrder, + } + } + + for txHash, receipt := range outportTxPool.Receipts { + value, err := o.castBigInt(receipt.Value) + if err != nil { + return fmt.Errorf("failed to cast receipt tx value: %w", err) + } + shardOutportBlockV2.TransactionPool.Receipts[txHash] = &hyperOutportBlocks.Receipt{ + Value: value, + SndAddr: receipt.SndAddr, + Data: receipt.Data, + TxHash: receipt.TxHash, + } + } + + //TODO: add invalid txs + //shardOutportBlockV2.TransactionPool.InvalidTxs = outportTxPool.InvalidTxs + return nil } +func handleAlteredAccounts(alteredAccounts map[string]*alteredAccount.AlteredAccount, shardOutportBlock *hyperOutportBlocks.ShardOutportBlockV2) { + if alteredAccounts == nil { + return + } + + shardAlteredAccounts := make(map[string]*hyperOutportBlocks.AlteredAccount, len(alteredAccounts)) + for key, alteredAcc := range alteredAccounts { + tokens := make([]*hyperOutportBlocks.AccountTokenData, len(alteredAcc.Tokens)) + for i, tokenData := range alteredAcc.Tokens { + tokens[i] = &hyperOutportBlocks.AccountTokenData{ + Nonce: tokenData.Nonce, + Identifier: tokenData.Identifier, + Balance: tokenData.Balance, + Properties: tokenData.Properties, + MetaData: &hyperOutportBlocks.TokenMetaData{ + Nonce: tokenData.MetaData.Nonce, + Name: tokenData.MetaData.Name, + Creator: tokenData.MetaData.Creator, + Royalties: tokenData.MetaData.Royalties, + Hash: tokenData.MetaData.Hash, + URIs: tokenData.MetaData.URIs, + Attributes: tokenData.MetaData.Attributes, + }, + AdditionalData: &hyperOutportBlocks.AdditionalAccountTokenData{ + IsNFTCreate: tokenData.AdditionalData.IsNFTCreate, + }, + } + } + + shardAlteredAccounts[key] = &hyperOutportBlocks.AlteredAccount{ + Address: alteredAcc.Address, + Nonce: alteredAcc.Nonce, + Balance: alteredAcc.Balance, + Tokens: tokens, + AdditionalData: &hyperOutportBlocks.AdditionalAccountData{ + IsSender: alteredAcc.AdditionalData.IsSender, + BalanceChanged: alteredAcc.AdditionalData.BalanceChanged, + CurrentOwner: alteredAcc.AdditionalData.CurrentOwner, + UserName: alteredAcc.AdditionalData.UserName, + DeveloperRewards: alteredAcc.AdditionalData.DeveloperRewards, + CodeHash: alteredAcc.AdditionalData.CodeHash, + RootHash: alteredAcc.AdditionalData.RootHash, + CodeMetadata: alteredAcc.AdditionalData.CodeMetadata, + }, + } + } + + shardOutportBlock.AlteredAccounts = shardAlteredAccounts +} + +func handleHeaderGasConsumption(consumption *outport.HeaderGasConsumption, shardOutportBlock *hyperOutportBlocks.ShardOutportBlockV2) { + if consumption == nil { + return + } + + shardOutportBlock.HeaderGasConsumption = &hyperOutportBlocks.HeaderGasConsumption{ + GasProvided: consumption.GasProvided, + GasRefunded: consumption.GasRefunded, + GasPenalized: consumption.GasPenalized, + MaxGasPerBlock: consumption.MaxGasPerBlock, + } +} + // HandleShardOutportBlock will convert an outport.OutportBlock to data.ShardOutportBlock. func (o *outportBlockConverter) HandleShardOutportBlock(outportBlock *outport.OutportBlock) (*hyperOutportBlocks.ShardOutportBlock, error) { headerType := outportBlock.BlockData.HeaderType diff --git a/process/outportBlockConverter_test.go b/process/outportBlockConverter_test.go index 21d30c2..f16f6af 100644 --- a/process/outportBlockConverter_test.go +++ b/process/outportBlockConverter_test.go @@ -16,7 +16,6 @@ import ( type fieldsGetter interface { GetShardID() uint32 - GetTransactionPool() *data.TransactionPool GetHeaderGasConsumption() *data.HeaderGasConsumption GetAlteredAccounts() map[string]*data.AlteredAccount GetNotarizedHeadersHashes() []string @@ -26,6 +25,16 @@ type fieldsGetter interface { GetHighestFinalBlockHash() []byte } +type fieldsGetterV1 interface { + fieldsGetter + GetTransactionPool() *data.TransactionPool +} + +type fieldsGetterV2 interface { + fieldsGetter + GetTransactionPool() *data.TransactionPoolV2 +} + type blockDataGetter interface { GetShardID() uint32 GetHeaderType() string @@ -46,6 +55,43 @@ const ( outportBlockMetaBlockJSONPath = "../testscommon/testdata/outportBlockMetaBlock.json" ) +func TestOutportBlockConverter_HandleShardOutportBlockV2(t *testing.T) { + t.Parallel() + + jsonBytes, err := os.ReadFile(outportBlockHeaderV1JSONPath) + require.NoError(t, err, "failed to read test data") + + ob := &outport.OutportBlock{} + err = json.Unmarshal(jsonBytes, ob) + require.NoError(t, err, "failed to unmarshal test block") + + converter, err := process.NewOutportBlockConverter(gogoProtoMarshaller, protoMarshaller) + require.Nil(t, err) + + shardOutportBlock, err := converter.HandleShardOutportBlockV2(ob) + if err != nil { + panic(err) + } + + header := &block.Header{} + err = gogoProtoMarshaller.Unmarshal(header, ob.BlockData.HeaderBytes) + require.NoError(t, err, "failed to unmarshall outport block header bytes") + + checkHeaderV1ShardV2(t, header, shardOutportBlock) + checkFieldsV2(t, ob, shardOutportBlock) + checkBlockData(t, ob.BlockData, shardOutportBlock.BlockData) + + j, err := json.Marshal(shardOutportBlock) + if err != nil { + panic(err) + } + + err = os.WriteFile("./shardoutport.json", j, 0655) + if err != nil { + panic(err) + } +} + func TestHeaderConverter(t *testing.T) { t.Parallel() @@ -66,7 +112,7 @@ func TestHeaderConverter(t *testing.T) { require.NoError(t, err, "failed to unmarshall outport block header bytes") checkHeaderV1(t, header, shardOutportBlock) - checkFields(t, ob, shardOutportBlock) + checkFieldsV1(t, ob, shardOutportBlock) checkBlockData(t, ob.BlockData, shardOutportBlock.BlockData) } @@ -91,7 +137,7 @@ func TestHeaderV2Converter(t *testing.T) { require.NoError(t, err, "failed to unmarshall outport block header bytes") checkHeaderV2(t, &header, shardOutportBlock) - checkFields(t, &ob, shardOutportBlock) + checkFieldsV1(t, &ob, shardOutportBlock) checkBlockData(t, ob.BlockData, shardOutportBlock.BlockData) } @@ -114,10 +160,51 @@ func TestMetaBlockConverter(t *testing.T) { require.NoError(t, err, "failed to unmarshall outport block header bytes") checkHeaderMeta(t, &header, metaOutportBlock) - checkFields(t, &ob, metaOutportBlock) + checkFieldsV1(t, &ob, metaOutportBlock) checkBlockData(t, ob.BlockData, metaOutportBlock.BlockData) } +func checkHeaderV1ShardV2(t *testing.T, header *block.Header, shardOutportBlock *data.ShardOutportBlockV2) { + require.Equal(t, header.Nonce, shardOutportBlock.BlockData.Header.Nonce) + require.Equal(t, header.PrevHash, shardOutportBlock.BlockData.Header.PrevHash) + require.Equal(t, header.PrevRandSeed, shardOutportBlock.BlockData.Header.PrevRandSeed) + require.Equal(t, header.RandSeed, shardOutportBlock.BlockData.Header.RandSeed) + require.Equal(t, header.PubKeysBitmap, shardOutportBlock.BlockData.Header.PubKeysBitmap) + require.Equal(t, header.ShardID, shardOutportBlock.BlockData.Header.ShardID) + require.Equal(t, header.TimeStamp, shardOutportBlock.BlockData.Header.TimeStamp) + require.Equal(t, header.Round, shardOutportBlock.BlockData.Header.Round) + require.Equal(t, header.Epoch, shardOutportBlock.BlockData.Header.Epoch) + require.Equal(t, header.BlockBodyType.String(), shardOutportBlock.BlockData.Header.BlockBodyType.String()) + require.Equal(t, header.Signature, shardOutportBlock.BlockData.Header.Signature) + require.Equal(t, header.LeaderSignature, shardOutportBlock.BlockData.Header.LeaderSignature) + require.Equal(t, header.RootHash, shardOutportBlock.BlockData.Header.RootHash) + require.Equal(t, header.MetaBlockHashes, shardOutportBlock.BlockData.Header.MetaBlockHashes) + require.Equal(t, header.TxCount, shardOutportBlock.BlockData.Header.TxCount) + require.Equal(t, header.EpochStartMetaHash, shardOutportBlock.BlockData.Header.EpochStartMetaHash) + require.Equal(t, header.ReceiptsHash, shardOutportBlock.BlockData.Header.ReceiptsHash) + require.Equal(t, header.ChainID, shardOutportBlock.BlockData.Header.ChainID) + require.Equal(t, header.SoftwareVersion, shardOutportBlock.BlockData.Header.SoftwareVersion) + require.Equal(t, header.Reserved, shardOutportBlock.BlockData.Header.Reserved) + require.Equal(t, mustCastBigInt(t, header.AccumulatedFees), shardOutportBlock.BlockData.Header.AccumulatedFees) + require.Equal(t, mustCastBigInt(t, header.DeveloperFees), shardOutportBlock.BlockData.Header.DeveloperFees) + + // Block data - Header - Mini block headers. + for i, miniBlockHeader := range header.MiniBlockHeaders { + require.Equal(t, miniBlockHeader.Hash, shardOutportBlock.BlockData.Header.MiniBlockHeaders[i].Hash) + require.Equal(t, miniBlockHeader.SenderShardID, shardOutportBlock.BlockData.Header.MiniBlockHeaders[i].SenderShardID) + require.Equal(t, miniBlockHeader.ReceiverShardID, shardOutportBlock.BlockData.Header.MiniBlockHeaders[i].ReceiverShardID) + require.Equal(t, miniBlockHeader.TxCount, shardOutportBlock.BlockData.Header.MiniBlockHeaders[i].TxCount) + require.Equal(t, miniBlockHeader.Type, shardOutportBlock.BlockData.Header.MiniBlockHeaders[i].Type) + require.Equal(t, miniBlockHeader.Reserved, shardOutportBlock.BlockData.Header.MiniBlockHeaders[i].Reserved) + } + + // Block data - Header - Peer changes. + for i, peerChange := range header.PeerChanges { + require.Equal(t, peerChange.PubKey, shardOutportBlock.BlockData.Header.PeerChanges[i].PubKey) + require.Equal(t, peerChange.ShardIdDest, shardOutportBlock.BlockData.Header.PeerChanges[i].ShardIdDest) + } +} + func checkHeaderV1(t *testing.T, header *block.Header, fireOutportBlock *data.ShardOutportBlock) { // Block data - Header. require.Equal(t, header.Nonce, fireOutportBlock.BlockData.Header.Nonce) @@ -312,7 +399,7 @@ func checkHeaderMeta(t *testing.T, header *block.MetaBlock, fireOutportBlock *da require.Equal(t, header.Reserved, fireOutportBlock.BlockData.Header.Reserved) } -func checkFields(t *testing.T, outportBlock *outport.OutportBlock, fireOutportBlock fieldsGetter) { +func checkFieldsV1(t *testing.T, outportBlock *outport.OutportBlock, fireOutportBlock fieldsGetterV1) { // Asserting values. require.Equal(t, outportBlock.ShardID, fireOutportBlock.GetShardID()) require.Equal(t, outportBlock.NotarizedHeadersHashes, fireOutportBlock.GetNotarizedHeadersHashes()) @@ -502,6 +589,198 @@ func checkFields(t *testing.T, outportBlock *outport.OutportBlock, fireOutportBl } } +func checkFieldsV2(t *testing.T, outportBlock *outport.OutportBlock, fireOutportBlock fieldsGetterV2) { + // Asserting values. + require.Equal(t, outportBlock.ShardID, fireOutportBlock.GetShardID()) + require.Equal(t, outportBlock.NotarizedHeadersHashes, fireOutportBlock.GetNotarizedHeadersHashes()) + require.Equal(t, outportBlock.NumberOfShards, fireOutportBlock.GetNumberOfShards()) + require.Equal(t, outportBlock.SignersIndexes, fireOutportBlock.GetSignersIndexes()) + require.Equal(t, outportBlock.HighestFinalBlockNonce, fireOutportBlock.GetHighestFinalBlockNonce()) + require.Equal(t, outportBlock.HighestFinalBlockHash, fireOutportBlock.GetHighestFinalBlockHash()) + + // Transaction pool - Transactions. + for k, v := range outportBlock.TransactionPool.Transactions { + // Transaction pool - Transactions. - TxInfo. + require.Equal(t, v.Transaction.Nonce, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.Nonce) + require.Equal(t, mustCastBigInt(t, v.Transaction.Value), fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.Value) + require.Equal(t, v.Transaction.RcvAddr, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.RcvAddr) + require.Equal(t, v.Transaction.RcvUserName, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.RcvUserName) + require.Equal(t, v.Transaction.SndAddr, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.SndAddr) + require.Equal(t, v.Transaction.GasPrice, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.GasPrice) + require.Equal(t, v.Transaction.GasLimit, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.GasLimit) + require.Equal(t, v.Transaction.Data, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.Data) + require.Equal(t, v.Transaction.ChainID, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.ChainID) + require.Equal(t, v.Transaction.Version, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.Version) + require.Equal(t, v.Transaction.Signature, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.Signature) + require.Equal(t, v.Transaction.Options, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.Options) + require.Equal(t, v.Transaction.GuardianAddr, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.GuardianAddr) + require.Equal(t, v.Transaction.GuardianSignature, fireOutportBlock.GetTransactionPool().Transactions[k].Transaction.GuardianSignature) + + // Transaction pool - Transactions - Tx Info - Fee info. + require.Equal(t, v.FeeInfo.GasUsed, fireOutportBlock.GetTransactionPool().Transactions[k].FeeInfo.GasUsed) + require.Equal(t, mustCastBigInt(t, v.FeeInfo.Fee), fireOutportBlock.GetTransactionPool().Transactions[k].FeeInfo.Fee) + require.Equal(t, mustCastBigInt(t, v.FeeInfo.InitialPaidFee), fireOutportBlock.GetTransactionPool().Transactions[k].FeeInfo.InitialPaidFee) + + require.Equal(t, v.ExecutionOrder, fireOutportBlock.GetTransactionPool().Transactions[k].ExecutionOrder) + } + + // Transaction pool - Smart Contract results. + for k, v := range outportBlock.TransactionPool.SmartContractResults { + tx := fireOutportBlock.GetTransactionPool().Transactions[k] + // Transaction pool - Smart Contract results - SmartContractResult. + require.Equal(t, v.SmartContractResult.Nonce, tx.SmartContractResults.SmartContractResult.Nonce) + require.Equal(t, mustCastBigInt(t, v.SmartContractResult.Value), tx.SmartContractResults.SmartContractResult.Value) + require.Equal(t, v.SmartContractResult.RcvAddr, tx.SmartContractResults.SmartContractResult.RcvAddr) + require.Equal(t, v.SmartContractResult.SndAddr, tx.SmartContractResults.SmartContractResult.SndAddr) + require.Equal(t, v.SmartContractResult.RelayerAddr, tx.SmartContractResults.SmartContractResult.RelayerAddr) + require.Equal(t, mustCastBigInt(t, v.SmartContractResult.RelayedValue), tx.SmartContractResults.SmartContractResult.RelayedValue) + require.Equal(t, v.SmartContractResult.Code, tx.SmartContractResults.SmartContractResult.Code) + require.Equal(t, v.SmartContractResult.Data, tx.SmartContractResults.SmartContractResult.Data) + require.Equal(t, v.SmartContractResult.PrevTxHash, tx.SmartContractResults.SmartContractResult.PrevTxHash) + require.Equal(t, v.SmartContractResult.OriginalTxHash, tx.SmartContractResults.SmartContractResult.OriginalTxHash) + require.Equal(t, v.SmartContractResult.GasLimit, tx.SmartContractResults.SmartContractResult.GasLimit) + require.Equal(t, v.SmartContractResult.GasPrice, tx.SmartContractResults.SmartContractResult.GasPrice) + require.Equal(t, int64(v.SmartContractResult.CallType), tx.SmartContractResults.SmartContractResult.CallType) + require.Equal(t, v.SmartContractResult.CodeMetadata, tx.SmartContractResults.SmartContractResult.CodeMetadata) + require.Equal(t, v.SmartContractResult.ReturnMessage, tx.SmartContractResults.SmartContractResult.ReturnMessage) + require.Equal(t, v.SmartContractResult.OriginalSender, tx.SmartContractResults.SmartContractResult.OriginalSender) + + //TODO: check if these needs to be added too. + // Transaction pool - Smart Contract results - Fee info. + //require.Equal(t, v.FeeInfo.GasUsed, tx.SmartContractResults.SmartContractResult.GasUsed) + //require.Equal(t, mustCastBigInt(t, v.FeeInfo.Fee), tx.SmartContractResults.SmartContractResult.Fee) + //require.Equal(t, mustCastBigInt(t, v.FeeInfo.InitialPaidFee), tx.SmartContractResults.SmartContractResult.FeeInfo.InitialPaidFee) + // + //// Transaction pool - Smart Contract results - Execution Order. + //require.Equal(t, v.ExecutionOrder, tx.SmartContractResults.SmartContractResult.ExecutionOrder) + } + + // Transaction Pool - Rewards + for k, v := range outportBlock.TransactionPool.Rewards { + // Transaction Pool - Rewards - Reward info + require.Equal(t, v.Reward.Round, fireOutportBlock.GetTransactionPool().Rewards[k].Reward.Round) + require.Equal(t, mustCastBigInt(t, v.Reward.Value), fireOutportBlock.GetTransactionPool().Rewards[k].Reward.Value) + require.Equal(t, v.Reward.RcvAddr, fireOutportBlock.GetTransactionPool().Rewards[k].Reward.RcvAddr) + require.Equal(t, v.Reward.Epoch, fireOutportBlock.GetTransactionPool().Rewards[k].Reward.Epoch) + + // Transaction Pool - Rewards - Execution Order + require.Equal(t, v.ExecutionOrder, fireOutportBlock.GetTransactionPool().Rewards[k].ExecutionOrder) + } + + // Transaction Pool - Receipts + for k, v := range outportBlock.TransactionPool.Receipts { + // Transaction Pool - Receipts - Receipt info + require.Equal(t, mustCastBigInt(t, v.Value), fireOutportBlock.GetTransactionPool().Receipts[k].Value) + require.Equal(t, v.SndAddr, fireOutportBlock.GetTransactionPool().Receipts[k].SndAddr) + require.Equal(t, v.Data, fireOutportBlock.GetTransactionPool().Receipts[k].Data) + require.Equal(t, v.TxHash, fireOutportBlock.GetTransactionPool().Receipts[k].TxHash) + } + + // Transaction Pool - Invalid Txs + for k, v := range outportBlock.TransactionPool.InvalidTxs { + // Transaction Pool - Invalid Txs - Tx Info + require.Equal(t, v.Transaction.Nonce, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.Nonce) + require.Equal(t, mustCastBigInt(t, v.Transaction.Value), fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.Value) + require.Equal(t, v.Transaction.RcvAddr, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.RcvAddr) + require.Equal(t, v.Transaction.RcvUserName, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.RcvUserName) + require.Equal(t, v.Transaction.SndAddr, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.SndAddr) + require.Equal(t, v.Transaction.GasPrice, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.GasPrice) + require.Equal(t, v.Transaction.GasLimit, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.GasLimit) + require.Equal(t, v.Transaction.Data, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.Data) + require.Equal(t, v.Transaction.ChainID, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.ChainID) + require.Equal(t, v.Transaction.Version, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.Version) + require.Equal(t, v.Transaction.Signature, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.Signature) + require.Equal(t, v.Transaction.Options, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.Options) + require.Equal(t, v.Transaction.GuardianAddr, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.GuardianAddr) + require.Equal(t, v.Transaction.GuardianSignature, fireOutportBlock.GetTransactionPool().InvalidTxs[k].Transaction.GuardianSignature) + require.Equal(t, v.ExecutionOrder, fireOutportBlock.GetTransactionPool().InvalidTxs[k].ExecutionOrder) + + // Transaction pool - Invalid Txs - Fee info. + require.Equal(t, v.FeeInfo.GasUsed, fireOutportBlock.GetTransactionPool().InvalidTxs[k].FeeInfo.GasUsed) + require.Equal(t, mustCastBigInt(t, v.FeeInfo.Fee), fireOutportBlock.GetTransactionPool().InvalidTxs[k].FeeInfo.Fee) + require.Equal(t, mustCastBigInt(t, v.FeeInfo.InitialPaidFee), fireOutportBlock.GetTransactionPool().InvalidTxs[k].FeeInfo.InitialPaidFee) + + require.Equal(t, v.ExecutionOrder, fireOutportBlock.GetTransactionPool().InvalidTxs[k].ExecutionOrder) + } + + // Transaction Pool - Logs + for i, l := range outportBlock.TransactionPool.Logs { + // Transaction Pool - Logs - Log Data + require.Equal(t, l.TxHash, fireOutportBlock.GetTransactionPool().Logs[i].TxHash) + + // Transaction Pool - Logs - Log data - Log + require.Equal(t, l.Log.Address, fireOutportBlock.GetTransactionPool().Logs[i].Log.Address) + + for k, e := range outportBlock.TransactionPool.Logs[i].Log.Events { + require.Equal(t, e.Address, fireOutportBlock.GetTransactionPool().Logs[i].Log.Events[k].Address) + require.Equal(t, e.Identifier, fireOutportBlock.GetTransactionPool().Logs[i].Log.Events[k].Identifier) + require.Equal(t, e.Topics, fireOutportBlock.GetTransactionPool().Logs[i].Log.Events[k].Topics) + require.Equal(t, e.Data, fireOutportBlock.GetTransactionPool().Logs[i].Log.Events[k].Data) + require.Equal(t, e.AdditionalData, fireOutportBlock.GetTransactionPool().Logs[i].Log.Events[k].AdditionalData) + } + } + + // Transaction Pool - ScheduledExecutedSCRSHashesPrevBlock + for i, s := range outportBlock.TransactionPool.ScheduledExecutedSCRSHashesPrevBlock { + require.Equal(t, s, fireOutportBlock.GetTransactionPool().ScheduledExecutedSCRSHashesPrevBlock[i]) + } + + // Transaction Pool - ScheduledExecutedInvalidTxsHashesPrevBlock + for i, s := range outportBlock.TransactionPool.ScheduledExecutedInvalidTxsHashesPrevBlock { + require.Equal(t, s, fireOutportBlock.GetTransactionPool().ScheduledExecutedInvalidTxsHashesPrevBlock[i]) + } + + // Header gas consumption. + require.Equal(t, outportBlock.HeaderGasConsumption.GasProvided, fireOutportBlock.GetHeaderGasConsumption().GasProvided) + require.Equal(t, outportBlock.HeaderGasConsumption.GasRefunded, fireOutportBlock.GetHeaderGasConsumption().GasRefunded) + require.Equal(t, outportBlock.HeaderGasConsumption.GasPenalized, fireOutportBlock.GetHeaderGasConsumption().GasPenalized) + require.Equal(t, outportBlock.HeaderGasConsumption.MaxGasPerBlock, fireOutportBlock.GetHeaderGasConsumption().MaxGasPerBlock) + + // Altered accounts. + for key, account := range outportBlock.AlteredAccounts { + acc := fireOutportBlock.GetAlteredAccounts()[key] + + require.Equal(t, account.Address, acc.Address) + require.Equal(t, account.Nonce, acc.Nonce) + require.Equal(t, account.Balance, acc.Balance) + require.Equal(t, account.Balance, acc.Balance) + + // Altered accounts - Account token data. + for i, token := range account.Tokens { + require.Equal(t, token.Nonce, acc.Tokens[i].Nonce) + require.Equal(t, token.Identifier, acc.Tokens[i].Identifier) + require.Equal(t, token.Balance, acc.Tokens[i].Balance) + require.Equal(t, token.Properties, acc.Tokens[i].Properties) + + // Altered accounts - Account token data - Metadata. + if token.MetaData != nil { + require.Equal(t, token.MetaData.Nonce, acc.Tokens[i].MetaData.Nonce) + require.Equal(t, token.MetaData.Name, acc.Tokens[i].MetaData.Name) + require.Equal(t, token.MetaData.Creator, acc.Tokens[i].MetaData.Creator) + require.Equal(t, token.MetaData.Royalties, acc.Tokens[i].MetaData.Royalties) + require.Equal(t, token.MetaData.Hash, acc.Tokens[i].MetaData.Hash) + require.Equal(t, token.MetaData.URIs, acc.Tokens[i].MetaData.URIs) + require.Equal(t, token.MetaData.Attributes, acc.Tokens[i].MetaData.Attributes) + + // Altered accounts - Account token data - Additional data. + require.Equal(t, token.AdditionalData.IsNFTCreate, acc.Tokens[i].AdditionalData.IsNFTCreate) + } + } + + // Altered accounts - Additional account data. + if account.AdditionalData != nil { + require.Equal(t, account.AdditionalData.IsSender, acc.AdditionalData.IsSender) + require.Equal(t, account.AdditionalData.BalanceChanged, acc.AdditionalData.BalanceChanged) + require.Equal(t, account.AdditionalData.CurrentOwner, acc.AdditionalData.CurrentOwner) + require.Equal(t, account.AdditionalData.UserName, acc.AdditionalData.UserName) + require.Equal(t, account.AdditionalData.DeveloperRewards, acc.AdditionalData.DeveloperRewards) + require.Equal(t, account.AdditionalData.CodeHash, acc.AdditionalData.CodeHash) + require.Equal(t, account.AdditionalData.RootHash, acc.AdditionalData.RootHash) + require.Equal(t, account.AdditionalData.CodeMetadata, acc.AdditionalData.CodeMetadata) + } + } +} + func checkBlockData(t *testing.T, blockData *outport.BlockData, getter blockDataGetter) { require.Equal(t, blockData.ShardID, getter.GetShardID()) require.Equal(t, blockData.HeaderType, getter.GetHeaderType())