github.com/Night-mk/quorum@v21.1.0+incompatible/extension/privacyExtension/state_set_utilities_test.go (about)

     1  package privacyExtension
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"testing"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/ethereum/go-ethereum/core/rawdb"
    11  	"github.com/ethereum/go-ethereum/core/state"
    12  	"github.com/ethereum/go-ethereum/core/types"
    13  	extension "github.com/ethereum/go-ethereum/extension/extensionContracts"
    14  	"github.com/ethereum/go-ethereum/private/engine"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestLogContainsExtensionTopicWithWrongLengthReturnsFalse(t *testing.T) {
    19  	testLog := &types.Log{
    20  		Topics: []common.Hash{{}, {}},
    21  	}
    22  
    23  	contained := logContainsExtensionTopic(testLog)
    24  
    25  	if contained {
    26  		t.Errorf("expected value '%t', but got '%t'", false, contained)
    27  	}
    28  }
    29  
    30  func TestLogContainsExtensionTopicWithWrongHashReturnsFalse(t *testing.T) {
    31  	testLog := &types.Log{
    32  		Topics: []common.Hash{common.HexToHash("0xf20540914db019dd7c8d05ed165316a58d1583642772ac46f3d0c29b8644bd36")},
    33  	}
    34  
    35  	contained := logContainsExtensionTopic(testLog)
    36  
    37  	if contained {
    38  		t.Errorf("expected value '%t', but got '%t'", false, contained)
    39  	}
    40  }
    41  
    42  func TestLogContainsExtensionTopicWithCorrectHashReturnsTrue(t *testing.T) {
    43  	testLog := &types.Log{
    44  		Topics: []common.Hash{common.HexToHash("0x67a92539f3cbd7c5a9b36c23c0e2beceb27d2e1b3cd8eda02c623689267ae71e")},
    45  	}
    46  
    47  	contained := logContainsExtensionTopic(testLog)
    48  
    49  	if !contained {
    50  		t.Errorf("expected value '%t', but got '%t'", true, contained)
    51  	}
    52  }
    53  
    54  func createStateDb(t *testing.T) *state.StateDB {
    55  	input := `{"0x2222222222222222222222222222222222222222":{"state":{"balance":"22","nonce":5,"root":"56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","codeHash":"87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3","code":"03030303030303","storage":{}}}}`
    56  	statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()))
    57  
    58  	var accounts map[string]extension.AccountWithMetadata
    59  	if err := json.Unmarshal([]byte(input), &accounts); err != nil {
    60  		t.Errorf("error when unmarshalling static data: %s", err.Error())
    61  	}
    62  
    63  	success := setState(statedb, accounts, &state.PrivacyMetadata{}, nil)
    64  	if !success {
    65  		t.Errorf("unexpected error when setting state")
    66  	}
    67  
    68  	return statedb
    69  }
    70  
    71  func TestStateSetWithListedAccounts(t *testing.T) {
    72  	statedb := createStateDb(t)
    73  
    74  	address := common.HexToAddress("0x2222222222222222222222222222222222222222")
    75  	balance := statedb.GetBalance(address)
    76  	code := statedb.GetCode(address)
    77  	nonce := statedb.GetNonce(address)
    78  	storage, _ := statedb.GetStorageRoot(address)
    79  
    80  	if balance.Uint64() != 22 {
    81  		t.Errorf("expect Balance value of '%d', but got '%d'", 22, balance.Uint64())
    82  		return
    83  	}
    84  
    85  	expectedCode := []byte{3, 3, 3, 3, 3, 3, 3}
    86  	if !bytes.Equal(code, expectedCode) {
    87  		t.Errorf("expect Code value of '%d', but got '%d'", expectedCode, code)
    88  		return
    89  	}
    90  
    91  	if nonce != 5 {
    92  		t.Errorf("expect Nonce value of '%d', but got '%d'", 5, nonce)
    93  		return
    94  	}
    95  
    96  	expectedStorageHash := common.FromHex("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    97  	if !bytes.Equal(storage.Bytes(), expectedStorageHash) {
    98  		t.Errorf("expect Storage value of '%d', but got '%s'", expectedStorageHash, storage)
    99  		return
   100  	}
   101  }
   102  
   103  func TestStateSetWithListedAccountsFailsOnInvalidBalance(t *testing.T) {
   104  	input := `{"0x2222222222222222222222222222222222222222":{"state":{"balance":"invalid","nonce":5,"root":"56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","codeHash":"87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3","code":"03030303030303","storage":{}}}}`
   105  	statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()))
   106  
   107  	var accounts map[string]extension.AccountWithMetadata
   108  	if err := json.Unmarshal([]byte(input), &accounts); err != nil {
   109  		t.Errorf("error when unmarshalling static data: %s", err.Error())
   110  	}
   111  
   112  	success := setState(statedb, accounts, &state.PrivacyMetadata{}, nil)
   113  	if success {
   114  		t.Errorf("error expected when setting state")
   115  	}
   116  }
   117  
   118  func Test_setPrivacyMetadata(t *testing.T) {
   119  	statedb := createStateDb(t)
   120  	address := common.HexToAddress("0x2222222222222222222222222222222222222222")
   121  
   122  	// call setPrivacyMetaData
   123  	arbitraryBytes1 := []byte{10}
   124  	hash := common.BytesToEncryptedPayloadHash(arbitraryBytes1)
   125  	setPrivacyMetadata(statedb, address, base64.StdEncoding.EncodeToString(arbitraryBytes1))
   126  
   127  	// we don't save PrivacyMetadata if it's standardprivate
   128  	privacyMetaData, err := statedb.GetPrivacyMetadata(address)
   129  	assert.Error(t, err, common.ErrNoAccountExtraData)
   130  
   131  	privacyMetaData = &state.PrivacyMetadata{CreationTxHash: hash, PrivacyFlag: engine.PrivacyFlagPartyProtection}
   132  	statedb.SetPrivacyMetadata(address, privacyMetaData)
   133  
   134  	privacyMetaData, err = statedb.GetPrivacyMetadata(address)
   135  	if err != nil {
   136  		t.Errorf("expected error to be nil, got err %s", err)
   137  	}
   138  	assert.Equal(t, engine.PrivacyFlagPartyProtection, privacyMetaData.PrivacyFlag)
   139  	assert.Equal(t, hash, privacyMetaData.CreationTxHash)
   140  
   141  	arbitraryBytes2 := []byte{20}
   142  	newHash := common.BytesToEncryptedPayloadHash(arbitraryBytes2)
   143  	setPrivacyMetadata(statedb, address, base64.StdEncoding.EncodeToString(arbitraryBytes2))
   144  
   145  	privacyMetaData, err = statedb.GetPrivacyMetadata(address)
   146  	if err != nil {
   147  		t.Errorf("expected error to be nil, got err %s", err)
   148  	}
   149  	assert.Equal(t, engine.PrivacyFlagPartyProtection, privacyMetaData.PrivacyFlag)
   150  	assert.Equal(t, newHash, privacyMetaData.CreationTxHash)
   151  }