Skip to content

store as system contract in erigon db#4700

Closed
envestcc wants to merge 27 commits into
iotexproject:masterfrom
envestcc:erigon-store-storage
Closed

store as system contract in erigon db#4700
envestcc wants to merge 27 commits into
iotexproject:masterfrom
envestcc:erigon-store-storage

Conversation

@envestcc

Copy link
Copy Markdown
Member

Description

The erigon database, in addition to storing account and contract data, now also supports storing data that satisfies the ContractStorage interface in the form of system contracts in erigon.

The main changes are as follows:

  • Introduced system contracts for erigon db to support the storage of arbitrary data.
  • The workingsetstore interface has been adjusted for object-oriented read and write operations.

base #4699

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@envestcc
envestcc requested a review from a team as a code owner August 21, 2025 09:14
@envestcc envestcc changed the title store as contract in erigon db store as system contract in erigon db Aug 21, 2025
Comment on lines +769 to +774
if len(ret) < 4 {
return hex.EncodeToString(ret)
}
if !bytes.Equal(ret[:4], _revertSelector) {
return hex.EncodeToString(ret)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these happen?

Comment thread state/contract_storage.go Outdated
Comment thread state/contract_storage_namespaced.go Outdated
Comment thread systemcontracts/NamespaceStorage.sol Outdated
Comment on lines +108 to +115
for (uint256 i = 0; i < keys.length; i++) {
if (keccak256(keys[i]) == keccak256(key)) {
// Move last element to current position and pop
keys[i] = keys[keys.length - 1];
keys.pop();
break;
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may have efficiency problem here

Comment thread systemcontracts/NamespaceStorage.sol Outdated
bytes auxiliaryData; // Additional data field for flexibility
}

// Nested mapping: namespace => key => value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyMap: namespace => key => index (start from 1)
keys: namespace => []bytes
values: namespace => []bytes

Delete: if index is not 0, delete it from map, update the keys and values

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure in the KeyMap, we should store hash(namespace), hash(key) or the raw data.

Comment thread systemcontracts/GenericStorage.sol Outdated
Comment on lines +70 to +71
uint256 index = keyIndex_[key] - 1;
values_[index] = value;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

values_[keyIndex_[key] - 1] = value;

Comment thread systemcontracts/GenericStorage.sol Outdated
Comment on lines +90 to +91
bytes memory lastKey = keys_[lastIndex];
GenericValue memory lastValue = values_[lastIndex];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why copy to memory?

@envestcc
envestcc force-pushed the erigon-store-storage branch from c102388 to 791c488 Compare August 31, 2025 23:14
Comment on lines +74 to +89
func (store *stateDBWorkingSetStore) PutObject(ns string, key []byte, obj any) error {
store.lock.Lock()
defer store.lock.Unlock()
value, err := state.Serialize(obj)
if err != nil {
return errors.Wrapf(err, "failed to serialize object of ns = %x and key = %x", ns, key)
}
return store.putKV(ns, key, value)
}

func (store *stateDBWorkingSetStore) Put(ns string, key []byte, value []byte) error {
store.lock.Lock()
defer store.lock.Unlock()
return store.putKV(ns, key, value)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we merge them? e.g.,

Put(ns string, key []byte, value any) error {
  switch v := value.(type) {
  case []byte:
    ...
  default:
    ...
  }

)

type (
contractBacked struct {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

}
)

func NewContractBackend(intraBlockState *erigonstate.IntraBlockState, org erigonstate.StateReader, height uint64, timestamp time.Time, g *genesis.Genesis, evmNetworkID uint32) *contractBacked {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment

return nil, errors.Errorf("unknown account type %v for address %x", pbAcc.Type, addr.Bytes())
}

if ch := backend.intraBlockState.GetCodeHash(addr); !accounts.IsEmptyCodeHash(ch) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty?

)
return
},
Coinbase: erigonComm.Address{},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why zero address?

Comment thread state/factory/erigonstore/workingsetstore_erigon.go
Comment on lines +71 to +74
contract, err := cs.storageContract(ns, nil, backend)
if err != nil {
return nil, nil, err
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx = genesis.WithGenesisContext(ctx, *backend.g)
ctx = protocol.WithBlockchainCtx(ctx, protocol.BlockchainCtx{
GetBlockTime: func(u uint64) (time.Time, error) {
interval := 2500 * time.Millisecond

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot hardcode the block interval in this way.

Comment on lines +17 to +19
if ns == "" {
return nil, errors.New("namespace cannot be empty")
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may not be true

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
5.1% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@envestcc envestcc closed this Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants