Skip to content

support sstoragePisa.RemoveKVData #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: tm_w3q
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/crypto/bls12381"
"github.com/ethereum/go-ethereum/crypto/bn256"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/sstorage"

//lint:ignore SA1019 Needed for precompile
"golang.org/x/crypto/ripemd160"
Expand Down Expand Up @@ -298,9 +299,10 @@ var (
// modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198
//
// def mult_complexity(x):
// if x <= 64: return x ** 2
// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
// else: return x ** 2 // 16 + 480 * x - 199680
//
// if x <= 64: return x ** 2
// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
// else: return x ** 2 // 16 + 480 * x - 199680
//
// where is x is max(length_of_MODULUS, length_of_BASE)
func modexpMultComplexity(x *big.Int) *big.Int {
Expand Down Expand Up @@ -715,8 +717,9 @@ func (l *sstoragePisa) RequiredGas(input []byte) uint64 {
return params.SstoreResetGasEIP2200
} else if bytes.Equal(input[0:4], getRawMethodId) {
return params.SloadGasEIP2200
} else if bytes.Equal(input[0:4], getRawMethodId) {
return params.SstoreResetGasEIP2200
} else {
// TODO: remove is not supported yet
return 0
}
}
Expand Down Expand Up @@ -774,8 +777,35 @@ func (l *sstoragePisa) RunWith(env *PrecompiledContractCallEnv, input []byte) ([
binary.BigEndian.PutUint64(pb[32-8:32], 32)
binary.BigEndian.PutUint64(pb[64-8:64], uint64(len(b)))
return append(pb, b...), nil
} else if bytes.Equal(input[0:4], removeRawMethodId) {
if evm.interpreter.readOnly {
return nil, ErrWriteProtection
}

// The execution of remove should not affect the result when validator running without data node.

// The remove operation consists of two steps.
// First, replace the data corresponding to removeKvIdx selected by the operator with data of lastKvIdx
// Second, set the data space of the original lastKvIdx to 0
lastKvIdx := new(big.Int).SetBytes(getData(input, 4, 32))
updateKvIdx := new(big.Int).SetBytes(getData(input, 4+32, 32))

if lastKvIdx.Cmp(updateKvIdx) == 0 {
// Delete the data corresponding to lastKvIdx
evm.StateDB.SstorageWrite(caller, lastKvIdx.Uint64(), make([]byte, sstorage.ShardInfos[0].KVSize))
} else {
// Read the data corresponding to lastKvIdx
replaceData, _, _ := evm.StateDB.SstorageRead(caller, lastKvIdx.Uint64(), int(sstorage.ShardInfos[0].KVSize), common.Hash{})
Copy link

Choose a reason for hiding this comment

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

What if the replaceData does not exist locally?

Copy link

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

I took an approach that only allows deletion of KV entries in the last shard

Copy link
Collaborator

@blockchaindevsh blockchaindevsh Nov 30, 2022

Choose a reason for hiding this comment

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

What will happen in this case: the shard is still synchronizing, so the SstorageRead may be stale. If we move the last kv into a kv index past synchronizing, the stale data will be there forever. If we add verification logic when SstorageRead, we can't even guarantee the execution result is the same across nodes.


// Delete the data corresponding to lastKvIdx
evm.StateDB.SstorageWrite(caller, lastKvIdx.Uint64(), make([]byte, sstorage.ShardInfos[0].KVSize))

// Replace the data corresponding to kvIdx with the data corresponding to lastKvIdx
evm.StateDB.SstorageWrite(caller, updateKvIdx.Uint64(), replaceData)
}

return nil, nil
}
// TODO: remove is not supported yet
return nil, errors.New("unsupported method")
}

Expand Down
Loading