-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from ElrondNetwork/fix-json-vmoutput
Wrap VMOutput in a JSON-friendly structure
- Loading branch information
Showing
7 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package common | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ElrondNetwork/arwen-wasm-vm/ipc/marshaling" | ||
vmcommon "github.com/ElrondNetwork/elrond-vm-common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMessageContractResponse_IsConsistentlySerializable(t *testing.T) { | ||
vmOutput := &vmcommon.VMOutput{OutputAccounts: make(map[string]*vmcommon.OutputAccount)} | ||
vmOutput.OutputAccounts["alice"] = &vmcommon.OutputAccount{StorageUpdates: make(map[string]*vmcommon.StorageUpdate)} | ||
vmOutput.OutputAccounts["alice"].StorageUpdates["foo"] = &vmcommon.StorageUpdate{} | ||
vmOutput.OutputAccounts["alice"].StorageUpdates["bar"] = &vmcommon.StorageUpdate{} | ||
message := NewMessageContractResponse(vmOutput, nil) | ||
requireSerializationConsistency(t, message, &MessageContractResponse{}) | ||
|
||
// Non text as output account keys | ||
vmOutput = &vmcommon.VMOutput{OutputAccounts: make(map[string]*vmcommon.OutputAccount)} | ||
vmOutput.OutputAccounts[string([]byte{0})] = &vmcommon.OutputAccount{StorageUpdates: make(map[string]*vmcommon.StorageUpdate)} | ||
vmOutput.OutputAccounts[string([]byte{0})].StorageUpdates["foo"] = &vmcommon.StorageUpdate{} | ||
vmOutput.OutputAccounts[string([]byte{0})].StorageUpdates["bar"] = &vmcommon.StorageUpdate{} | ||
message = NewMessageContractResponse(vmOutput, nil) | ||
requireSerializationConsistency(t, message, &MessageContractResponse{}) | ||
|
||
// Non UTF-8 as output account keys | ||
vmOutput = &vmcommon.VMOutput{OutputAccounts: make(map[string]*vmcommon.OutputAccount)} | ||
vmOutput.OutputAccounts[string([]byte{128})] = &vmcommon.OutputAccount{StorageUpdates: make(map[string]*vmcommon.StorageUpdate)} | ||
vmOutput.OutputAccounts[string([]byte{128})].StorageUpdates["foo"] = &vmcommon.StorageUpdate{} | ||
vmOutput.OutputAccounts[string([]byte{128})].StorageUpdates["bar"] = &vmcommon.StorageUpdate{} | ||
message = NewMessageContractResponse(vmOutput, nil) | ||
requireSerializationConsistency(t, message, &MessageContractResponse{}) | ||
|
||
// Non UTF-8 as storage update keys | ||
vmOutput = &vmcommon.VMOutput{OutputAccounts: make(map[string]*vmcommon.OutputAccount)} | ||
vmOutput.OutputAccounts["alice"] = &vmcommon.OutputAccount{StorageUpdates: make(map[string]*vmcommon.StorageUpdate)} | ||
vmOutput.OutputAccounts["alice"].StorageUpdates[string([]byte{128})] = &vmcommon.StorageUpdate{} | ||
vmOutput.OutputAccounts["alice"].StorageUpdates[string([]byte{129})] = &vmcommon.StorageUpdate{} | ||
message = NewMessageContractResponse(vmOutput, nil) | ||
requireSerializationConsistency(t, message, &MessageContractResponse{}) | ||
} | ||
|
||
func TestMessageContractResponse_CanWrapNilVMOutput(t *testing.T) { | ||
message := NewMessageContractResponse(nil, nil) | ||
expectedEmptyVMOutput := vmcommon.VMOutput{OutputAccounts: make(map[string]*vmcommon.OutputAccount)} | ||
actualVMOutput := *message.SerializableVMOutput.ConvertToVMOutput() | ||
|
||
require.True(t, reflect.DeepEqual(expectedEmptyVMOutput, actualVMOutput)) | ||
requireSerializationConsistency(t, message, &MessageContractResponse{}) | ||
} | ||
|
||
func TestMessageBlockchainGetAllStateResponse_IsConsistentlySerializable(t *testing.T) { | ||
t.Skip("GetAllState isn't used at this moment") | ||
|
||
allState := make(map[string][]byte) | ||
allState["foo"] = []byte{0} | ||
allState[string([]byte{0})] = []byte{0} | ||
allState[string([]byte{128})] = []byte{0} | ||
message := NewMessageBlockchainGetAllStateResponse(allState, nil) | ||
requireSerializationConsistency(t, message, &MessageBlockchainGetAllStateResponse{}) | ||
} | ||
|
||
func requireSerializationConsistency(t *testing.T, message interface{}, intoMessage interface{}) { | ||
marshalizer := marshaling.CreateMarshalizer(marshaling.JSON) | ||
|
||
serialized, err := marshalizer.Marshal(message) | ||
require.Nil(t, err) | ||
err = marshalizer.Unmarshal(intoMessage, serialized) | ||
require.Nil(t, err) | ||
|
||
areEqual := reflect.DeepEqual(message, intoMessage) | ||
if !areEqual { | ||
require.FailNow(t, "Serialization is not consistent.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package common | ||
|
||
import ( | ||
"math/big" | ||
|
||
vmcommon "github.com/ElrondNetwork/elrond-vm-common" | ||
) | ||
|
||
type SerializableVMOutput struct { | ||
ReturnData [][]byte | ||
ReturnCode vmcommon.ReturnCode | ||
ReturnMessage string | ||
GasRemaining uint64 | ||
GasRefund *big.Int | ||
CorrectedOutputAccounts []*SerializableOutputAccount | ||
DeletedAccounts [][]byte | ||
TouchedAccounts [][]byte | ||
Logs []*vmcommon.LogEntry | ||
} | ||
|
||
func NewSerializableVMOutput(vmOutput *vmcommon.VMOutput) *SerializableVMOutput { | ||
if vmOutput == nil { | ||
return &SerializableVMOutput{} | ||
} | ||
|
||
o := &SerializableVMOutput{ | ||
ReturnData: vmOutput.ReturnData, | ||
ReturnCode: vmOutput.ReturnCode, | ||
ReturnMessage: vmOutput.ReturnMessage, | ||
GasRemaining: vmOutput.GasRemaining, | ||
GasRefund: vmOutput.GasRefund, | ||
CorrectedOutputAccounts: make([]*SerializableOutputAccount, 0, len(vmOutput.OutputAccounts)), | ||
DeletedAccounts: vmOutput.DeletedAccounts, | ||
TouchedAccounts: vmOutput.TouchedAccounts, | ||
Logs: vmOutput.Logs, | ||
} | ||
|
||
for _, account := range vmOutput.OutputAccounts { | ||
o.CorrectedOutputAccounts = append(o.CorrectedOutputAccounts, NewSerializableOutputAccount(account)) | ||
} | ||
|
||
return o | ||
} | ||
|
||
func (o *SerializableVMOutput) ConvertToVMOutput() *vmcommon.VMOutput { | ||
accountsMap := make(map[string]*vmcommon.OutputAccount) | ||
|
||
for _, item := range o.CorrectedOutputAccounts { | ||
accountsMap[string(item.Address)] = item.ConvertToOutputAccount() | ||
} | ||
|
||
return &vmcommon.VMOutput{ | ||
ReturnData: o.ReturnData, | ||
ReturnCode: o.ReturnCode, | ||
ReturnMessage: o.ReturnMessage, | ||
GasRemaining: o.GasRemaining, | ||
GasRefund: o.GasRefund, | ||
OutputAccounts: accountsMap, | ||
DeletedAccounts: o.DeletedAccounts, | ||
TouchedAccounts: o.TouchedAccounts, | ||
Logs: o.Logs, | ||
} | ||
} | ||
|
||
type SerializableOutputAccount struct { | ||
Address []byte | ||
Nonce uint64 | ||
Balance *big.Int | ||
BalanceDelta *big.Int | ||
StorageUpdates []*vmcommon.StorageUpdate | ||
Code []byte | ||
CodeMetadata []byte | ||
Data []byte | ||
GasLimit uint64 | ||
CallType vmcommon.CallType | ||
} | ||
|
||
func NewSerializableOutputAccount(account *vmcommon.OutputAccount) *SerializableOutputAccount { | ||
a := &SerializableOutputAccount{ | ||
Address: account.Address, | ||
Nonce: account.Nonce, | ||
Balance: account.Balance, | ||
BalanceDelta: account.BalanceDelta, | ||
StorageUpdates: make([]*vmcommon.StorageUpdate, 0, len(account.StorageUpdates)), | ||
Code: account.Code, | ||
CodeMetadata: account.CodeMetadata, | ||
Data: account.Data, | ||
GasLimit: account.GasLimit, | ||
CallType: account.CallType, | ||
} | ||
|
||
for _, storageUpdate := range account.StorageUpdates { | ||
a.StorageUpdates = append(a.StorageUpdates, storageUpdate) | ||
} | ||
|
||
return a | ||
} | ||
|
||
func (a *SerializableOutputAccount) ConvertToOutputAccount() *vmcommon.OutputAccount { | ||
updatesMap := make(map[string]*vmcommon.StorageUpdate) | ||
|
||
for _, item := range a.StorageUpdates { | ||
updatesMap[string(item.Offset)] = item | ||
} | ||
|
||
return &vmcommon.OutputAccount{ | ||
Address: a.Address, | ||
Nonce: a.Nonce, | ||
Balance: a.Balance, | ||
BalanceDelta: a.BalanceDelta, | ||
StorageUpdates: updatesMap, | ||
Code: a.Code, | ||
CodeMetadata: a.CodeMetadata, | ||
Data: a.Data, | ||
GasLimit: a.GasLimit, | ||
CallType: a.CallType, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters