Skip to content

Commit

Permalink
modified per review feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
trung committed Jan 15, 2021
1 parent a6123d5 commit 6eed243
Show file tree
Hide file tree
Showing 28 changed files with 390 additions and 259 deletions.
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.Matche
panic("not supported")
}

func (fb *filterBackend) AccountExtraDataStateReaderByNumber(context.Context, rpc.BlockNumber) (vm.AccountExtraDataStateReader, error) {
func (fb *filterBackend) AccountExtraDataStateGetterByNumber(context.Context, rpc.BlockNumber) (vm.AccountExtraDataStateGetter, error) {
panic("not supported")
}

Expand Down
54 changes: 54 additions & 0 deletions common/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package common

// ContainsAll returns true if all elements in the target are in the source,
// false otherwise.
func ContainsAll(source, target []string) bool {
mark := make(map[string]bool, len(source))
for _, str := range source {
mark[str] = true
}
for _, str := range target {
if _, found := mark[str]; !found {
return false
}
}
return true
}

// ContainsAll returns true if all elements in the target are NOT in the source,
// false otherwise.
func NotContainsAll(source, target []string) bool {
return !ContainsAll(source, target)
}

// AppendSkipDuplicates appends source with elements with a condition
// that those elemments must NOT already exist in the source
func AppendSkipDuplicates(slice []string, elems ... string) (result []string) {
mark := make(map[string]bool, len(slice))
for _, val := range slice {
mark[val] = true
}
result = slice
for _, val := range elems {
if _, ok := mark[val]; !ok {
result = append(result, val)
}
}
return result
}
93 changes: 93 additions & 0 deletions common/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package common

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestContainsAll_whenTypical(t *testing.T) {
source := []string{"1", "2"}
target := []string{"1", "2"}

assert.True(t, ContainsAll(source, target))
}

func TestContainsAll_whenNot(t *testing.T) {
source := []string{"1", "2"}
target := []string{"3", "4"}

assert.False(t, ContainsAll(source, target))
}

func TestContainsAll_whenTargetIsSubset(t *testing.T) {
source := []string{"1", "2"}
target := []string{"1"}

assert.True(t, ContainsAll(source, target))
}

func TestContainsAll_whenTargetIsSuperSet(t *testing.T) {
source := []string{"2"}
target := []string{"1", "2"}

assert.False(t, ContainsAll(source, target))
}

func TestContainsAll_whenSourceIsEmpty(t *testing.T) {
var source []string
target := []string{"1", "2"}

assert.False(t, ContainsAll(source, target))
}

func TestContainsAll_whenSourceIsNil(t *testing.T) {
target := []string{"1", "2"}

assert.False(t, ContainsAll(nil, target))
}

func TestContainsAll_whenTargetIsEmpty(t *testing.T) {
source := []string{"1", "2"}

assert.True(t, ContainsAll(source, []string{}))
}

func TestContainsAll_whenTargetIsNil(t *testing.T) {
source := []string{"1", "2"}

assert.True(t, ContainsAll(source, nil))
}

func TestAppendSkipDuplicates_whenTypical(t *testing.T) {
source := []string{"1", "2"}
additional := []string{"1", "3"}

assert.Equal(t, []string{"1", "2", "3"}, AppendSkipDuplicates(source, additional...))
}

func TestAppendSkipDuplicates_whenSourceIsNil(t *testing.T) {
additional := []string{"1", "3"}

assert.Equal(t, []string{"1", "3"}, AppendSkipDuplicates(nil, additional...))
}

func TestAppendSkipDuplicates_whenElementIsNil(t *testing.T) {
assert.Equal(t, []string{"1", "3"}, AppendSkipDuplicates([]string{"1", "3"}, nil...))
}
6 changes: 3 additions & 3 deletions core/rawdb/database_quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func GetPrivateBlockBloom(db ethdb.Database, number uint64) (bloom types.Bloom)
// AccountExtraDataLinker maintains mapping between root hash of the state trie
// and root hash of state.AccountExtraData trie
type AccountExtraDataLinker interface {
// Find returns the root hash of the state.AccountExtraData trie from
// GetAccountExtraDataRoot returns the root hash of the state.AccountExtraData trie from
// the given root hash of the state trie.
//
// It returns an empty hash if not found.
Find(stateRoot common.Hash) common.Hash
GetAccountExtraDataRoot(stateRoot common.Hash) common.Hash
// Link saves the mapping between root hash of the state trie and
// root hash of state.AccountExtraData trie to the persistent storage.
// Don't write the mapping if extraDataRoot is an emptyRoot
Expand All @@ -112,7 +112,7 @@ func NewAccountExtraDataLinker(db ethdb.Database) AccountExtraDataLinker {
}
}

func (pml *ethdbAccountExtraDataLinker) Find(stateRoot common.Hash) common.Hash {
func (pml *ethdbAccountExtraDataLinker) GetAccountExtraDataRoot(stateRoot common.Hash) common.Hash {
return GetAccountExtraDataRoot(pml.db, stateRoot)
}

Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/database_quorum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestAccountExtraDataLinker_whenFinding(t *testing.T) {

pml := NewAccountExtraDataLinker(db)

pmrRetrieved := pml.Find(psr)
pmrRetrieved := pml.GetAccountExtraDataRoot(psr)

if pmrRetrieved != pmr {
t.Fatal("the mapping should have been retrieved")
Expand All @@ -145,7 +145,7 @@ func TestAccountExtraDataLinker_whenNotFound(t *testing.T) {

pml := NewAccountExtraDataLinker(db)

pmrRetrieved := pml.Find(psr)
pmrRetrieved := pml.GetAccountExtraDataRoot(psr)

if !common.EmptyHash(pmrRetrieved) {
t.Fatal("the retrieved privacy metadata root should be the empty hash")
Expand Down
10 changes: 5 additions & 5 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func New(root common.Hash, db Database) (*StateDB, error) {
}

// Quorum - Privacy Enhancements - retrieve the privacy metadata root corresponding to the account state root
extraDataRoot := db.AccountExtraDataLinker().Find(root)
extraDataRoot := db.AccountExtraDataLinker().GetAccountExtraDataRoot(root)
log.Debug("Account Extra Data root", "hash", extraDataRoot)
accountExtraDataTrie, err := db.OpenTrie(extraDataRoot)
if err != nil {
Expand Down Expand Up @@ -253,7 +253,7 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
return 0
}

func (self *StateDB) ReadPrivacyMetadata(addr common.Address) (*PrivacyMetadata, error) {
func (self *StateDB) GetPrivacyMetadata(addr common.Address) (*PrivacyMetadata, error) {
stateObject := self.getStateObject(addr)
if stateObject != nil {
return stateObject.PrivacyMetadata()
Expand All @@ -269,7 +269,7 @@ func (self *StateDB) GetCommittedStatePrivacyMetadata(addr common.Address) (*Pri
return nil, nil
}

func (self *StateDB) ReadManagedParties(addr common.Address) ([]string, error) {
func (self *StateDB) GetManagedParties(addr common.Address) ([]string, error) {
stateObject := self.getStateObject(addr)
if stateObject != nil {
return stateObject.ManagedParties()
Expand Down Expand Up @@ -437,14 +437,14 @@ func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
}
}

func (self *StateDB) WritePrivacyMetadata(addr common.Address, metadata *PrivacyMetadata) {
func (self *StateDB) SetPrivacyMetadata(addr common.Address, metadata *PrivacyMetadata) {
stateObject := self.GetOrNewStateObject(addr)
if stateObject != nil {
stateObject.SetStatePrivacyMetadata(metadata)
}
}

func (self *StateDB) WriteManagedParties(addr common.Address, managedParties []string) {
func (self *StateDB) SetManagedParties(addr common.Address, managedParties []string) {
stateObject := self.GetOrNewStateObject(addr)
if stateObject != nil && len(managedParties) > 0 {
stateObject.SetManagedParties(managedParties)
Expand Down
30 changes: 15 additions & 15 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
args: make([]int64, 2),
},
{
name: "WritePrivacyMetadata",
name: "SetPrivacyMetadata",
fn: func(a testAction, s *StateDB) {

privFlag := engine.PrivacyFlagType((uint64(a.args[0])%2)*2 + 1) // the only possible values should be 1 and 3
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(a.args[1]))
hash := common.BytesToEncryptedPayloadHash(b)

s.WritePrivacyMetadata(addr, &PrivacyMetadata{
s.SetPrivacyMetadata(addr, &PrivacyMetadata{
CreationTxHash: hash,
PrivacyFlag: privFlag,
})
Expand Down Expand Up @@ -482,9 +482,9 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
checkeq("GetCode", state.GetCode(addr), checkstate.GetCode(addr))
checkeq("GetCodeHash", state.GetCodeHash(addr), checkstate.GetCodeHash(addr))
checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr))
statePM, _ := state.ReadPrivacyMetadata(addr)
checkStatePM, _ := checkstate.ReadPrivacyMetadata(addr)
checkeq("ReadPrivacyMetadata", statePM, checkStatePM)
statePM, _ := state.GetPrivacyMetadata(addr)
checkStatePM, _ := checkstate.GetPrivacyMetadata(addr)
checkeq("GetPrivacyMetadata", statePM, checkStatePM)
// Check storage.
if obj := state.getStateObject(addr); obj != nil {
state.ForEachStorage(addr, func(key, value common.Hash) bool {
Expand Down Expand Up @@ -744,7 +744,7 @@ func TestPrivacyMetadataIsSavedOnStateDbCommit(t *testing.T) {
state.CreateAccount(addr)

state.SetNonce(addr, uint64(1))
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagPartyProtection,
CreationTxHash: common.EncryptedPayloadHash{1},
})
Expand All @@ -771,7 +771,7 @@ func TestPrivacyMetadataIsUpdatedOnAccountReCreateWithDifferentPrivacyMetadata(t
state.CreateAccount(addr)

state.SetNonce(addr, uint64(1))
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagPartyProtection,
CreationTxHash: common.EncryptedPayloadHash{1},
})
Expand All @@ -784,7 +784,7 @@ func TestPrivacyMetadataIsUpdatedOnAccountReCreateWithDifferentPrivacyMetadata(t

state.CreateAccount(addr)
state.SetNonce(addr, uint64(1))
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagStateValidation,
CreationTxHash: common.EncryptedPayloadHash{1},
})
Expand All @@ -808,7 +808,7 @@ func TestPrivacyMetadataIsRemovedOnAccountSuicide(t *testing.T) {
state.CreateAccount(addr)

state.SetNonce(addr, uint64(1))
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagPartyProtection,
CreationTxHash: common.EncryptedPayloadHash{1},
})
Expand Down Expand Up @@ -837,7 +837,7 @@ func TestPrivacyMetadataChangesAreRolledBackOnRevert(t *testing.T) {
state.CreateAccount(addr)

state.SetNonce(addr, uint64(1))
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagPartyProtection,
CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("one")),
})
Expand All @@ -849,34 +849,34 @@ func TestPrivacyMetadataChangesAreRolledBackOnRevert(t *testing.T) {
}

// update privacy metadata
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagStateValidation,
CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("two")),
})

// record the snapshot
snapshot := state.Snapshot()

privMetaData, _ = state.ReadPrivacyMetadata(addr)
privMetaData, _ = state.GetPrivacyMetadata(addr)
if privMetaData.CreationTxHash != common.BytesToEncryptedPayloadHash([]byte("two")) {
t.Errorf("current privacy metadata creation tx hash does not match the expected value")
}

// update the metadata
state.WritePrivacyMetadata(addr, &PrivacyMetadata{
state.SetPrivacyMetadata(addr, &PrivacyMetadata{
PrivacyFlag: engine.PrivacyFlagStateValidation,
CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("three")),
})

privMetaData, _ = state.ReadPrivacyMetadata(addr)
privMetaData, _ = state.GetPrivacyMetadata(addr)
if privMetaData.CreationTxHash != common.BytesToEncryptedPayloadHash([]byte("three")) {
t.Errorf("current privacy metadata creation tx hash does not match the expected value")
}

// revert to snapshot
state.RevertToSnapshot(snapshot)

privMetaData, _ = state.ReadPrivacyMetadata(addr)
privMetaData, _ = state.GetPrivacyMetadata(addr)
if privMetaData.CreationTxHash != common.BytesToEncryptedPayloadHash([]byte("two")) {
t.Errorf("current privacy metadata creation tx hash does not match the expected value")
}
Expand Down
Loading

0 comments on commit 6eed243

Please sign in to comment.