github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/state/mpt_root_test.go (about)

     1  package state
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/nspcc-dev/neo-go/internal/random"
     9  	"github.com/nspcc-dev/neo-go/internal/testserdes"
    10  	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
    11  	"github.com/nspcc-dev/neo-go/pkg/util"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func testStateRoot() *MPTRoot {
    16  	return &MPTRoot{
    17  		Version: byte(rand.Uint32()),
    18  		Index:   rand.Uint32(),
    19  		Root:    random.Uint256(),
    20  		Witness: []transaction.Witness{},
    21  	}
    22  }
    23  
    24  func TestStateRoot_Serializable(t *testing.T) {
    25  	r := testStateRoot()
    26  	testserdes.EncodeDecodeBinary(t, r, new(MPTRoot))
    27  
    28  	t.Run("WithWitness", func(t *testing.T) {
    29  		r.Witness = []transaction.Witness{{
    30  			InvocationScript:   random.Bytes(10),
    31  			VerificationScript: random.Bytes(11),
    32  		}}
    33  		testserdes.EncodeDecodeBinary(t, r, new(MPTRoot))
    34  	})
    35  }
    36  
    37  func TestMPTRoot_MarshalJSON(t *testing.T) {
    38  	t.Run("Good", func(t *testing.T) {
    39  		r := testStateRoot()
    40  		testserdes.MarshalUnmarshalJSON(t, r, new(MPTRoot))
    41  	})
    42  
    43  	t.Run("Compatibility", func(t *testing.T) {
    44  		js := []byte(`{
    45              "version": 1,
    46              "index": 3000000,
    47              "roothash": "0xb2fd7e368a848ef70d27cf44940a35237333ed05f1d971c9408f0eb285e0b6f3"
    48          }`)
    49  
    50  		rs := new(MPTRoot)
    51  		require.NoError(t, json.Unmarshal(js, &rs))
    52  
    53  		require.EqualValues(t, 1, rs.Version)
    54  		require.EqualValues(t, 3000000, rs.Index)
    55  		require.Equal(t, 0, len(rs.Witness))
    56  
    57  		u, err := util.Uint256DecodeStringLE("b2fd7e368a848ef70d27cf44940a35237333ed05f1d971c9408f0eb285e0b6f3")
    58  		require.NoError(t, err)
    59  		require.Equal(t, u, rs.Root)
    60  	})
    61  }