laconicd/x/evm/types/storage_test.go
Federico Kunze 9cbb4dcf6d
stargate: migrate types (#670)
* changelog v0.4.0

* stargate: types changes

* msg and handler changes

* validation

* fixes

* more fixes

* more test fixes

* changelog

* changelog

* lint

* rm comment

* lint

* redundant if condition
2021-01-06 17:56:40 -03:00

86 lines
1.7 KiB
Go

package types
import (
"testing"
"github.com/stretchr/testify/require"
ethcmn "github.com/ethereum/go-ethereum/common"
)
func TestStorageValidate(t *testing.T) {
testCases := []struct {
name string
storage Storage
expPass bool
}{
{
"valid storage",
Storage{
NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
},
true,
},
{
"empty storage key bytes",
Storage{
{Key: ethcmn.Hash{}.String()},
},
false,
},
{
"duplicated storage key",
Storage{
{Key: ethcmn.BytesToHash([]byte{1, 2, 3}).String()},
{Key: ethcmn.BytesToHash([]byte{1, 2, 3}).String()},
},
false,
},
}
for _, tc := range testCases {
tc := tc
err := tc.storage.Validate()
if tc.expPass {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
}
}
}
func TestStorageCopy(t *testing.T) {
testCases := []struct {
name string
storage Storage
}{
{
"single storage",
Storage{
NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
},
},
{
"empty storage key value bytes",
Storage{
{Key: ethcmn.Hash{}.String(), Value: ethcmn.Hash{}.String()},
},
},
{
"empty storage",
Storage{},
},
}
for _, tc := range testCases {
tc := tc
require.Equal(t, tc.storage, tc.storage.Copy(), tc.name)
}
}
func TestStorageString(t *testing.T) {
storage := Storage{NewState(ethcmn.BytesToHash([]byte("key")), ethcmn.BytesToHash([]byte("value")))}
str := "0x00000000000000000000000000000000000000000000000000000000006b6579: 0x00000000000000000000000000000000000000000000000000000076616c7565\n"
require.Equal(t, str, storage.String())
}