Add Deposit Contract Methods to DB Refactor (#3264)

* new interface methods

* support proposer slashings

* add in the new buckets

* all crud for propoer slashings

* attester slashings complete

* all slashings crud done

* right comment

* deposit contract tests pass

* delete out of scope methods

* conform old beacon DB

* comment

* deprecations

* pass lint

* Update deposit_contract.go
This commit is contained in:
Raul Jordan
2019-08-21 16:11:50 -05:00
committed by GitHub
parent 01de412956
commit 3ca4d6fd91
8 changed files with 114 additions and 10 deletions

View File

@@ -11,42 +11,50 @@ import (
"go.opencensus.io/trace"
)
// ProposerSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// ProposerSlashing retrieval from the db.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) ProposerSlashing(ctx context.Context, slashingRoot [32]byte) (*ethpb.ProposerSlashing, error) {
return nil, errors.New("unimplemented")
}
// AttesterSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// AttesterSlashing retrieval from the db.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) AttesterSlashing(ctx context.Context, slashingRoot [32]byte) (*ethpb.AttesterSlashing, error) {
return nil, errors.New("unimplemented")
}
// SaveProposerSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// SaveProposerSlashing to the db.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) SaveProposerSlashing(ctx context.Context, slashing *ethpb.ProposerSlashing) error {
return errors.New("unimplemented")
}
// SaveAttesterSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// SaveAttesterSlashing to the db.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) SaveAttesterSlashing(ctx context.Context, slashing *ethpb.AttesterSlashing) error {
return errors.New("unimplemented")
}
// HasProposerSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// HasProposerSlashing by root.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) HasProposerSlashing(ctx context.Context, slashingRoot [32]byte) bool {
return false
}
// HasAttesterSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// HasAttesterSlashing by root.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) HasAttesterSlashing(ctx context.Context, slashingRoot [32]byte) bool {
return false
}
// DeleteProposerSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// DeleteProposerSlashing by root.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) DeleteProposerSlashing(ctx context.Context, slashingRoot [32]byte) error {
return errors.New("unimplemented")
}
// DeleteAttesterSlashing is deprecated - use the kv store in beacon-chain/db/kv instead.
// DeleteAttesterSlashing by root.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) DeleteAttesterSlashing(ctx context.Context, slashingRoot [32]byte) error {
return errors.New("unimplemented")
}

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/boltdb/bolt"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
@@ -63,6 +64,9 @@ type Database interface {
HasAttesterSlashing(ctx context.Context, slashingRoot [32]byte) bool
DeleteProposerSlashing(ctx context.Context, slashingRoot [32]byte) error
DeleteAttesterSlashing(ctx context.Context, slashingRoot [32]byte) error
// Deposit contract related handlers.
DepositContractAddress(ctx context.Context) ([]byte, error)
SaveDepositContractAddress(ctx context.Context, addr common.Address) error
}
var _ = Database(&BeaconDB{})

View File

@@ -7,6 +7,7 @@ import (
"github.com/boltdb/bolt"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"go.opencensus.io/trace"
)
@@ -14,6 +15,7 @@ var depositContractAddressKey = []byte("deposit-contract")
// DepositContractAddress returns contract address is the address of
// the deposit contract on the proof of work chain.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) DepositContractAddress(ctx context.Context) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "BeaconDB.DepositContractAddress")
defer span.End()
@@ -30,10 +32,17 @@ func (db *BeaconDB) DepositContractAddress(ctx context.Context) ([]byte, error)
return addr, nil
}
// SaveDepositContractAddress to the db.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) SaveDepositContractAddress(ctx context.Context, addr common.Address) error {
return errors.New("unimplemented")
}
// VerifyContractAddress that represents the data in this database. The
// contract address is the address of the deposit contract on the proof of work
// Ethereum chain. This value will never change or all of the data in the
// database would be made invalid.
// DEPRECATED: Use the kv store in beacon-chain/db/kv instead.
func (db *BeaconDB) VerifyContractAddress(ctx context.Context, addr common.Address) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.VerifyContractAddress")
defer span.End()

View File

@@ -5,6 +5,7 @@ go_library(
srcs = [
"attestations.go",
"blocks.go",
"deposit_contract.go",
"kv.go",
"schema.go",
"slashings.go",
@@ -21,6 +22,7 @@ go_library(
"//shared/bytesutil:go_default_library",
"//shared/sliceutil:go_default_library",
"@com_github_boltdb_bolt//:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
@@ -33,6 +35,7 @@ go_test(
srcs = [
"attestations_test.go",
"blocks_test.go",
"deposit_contract_test.go",
"kv_test.go",
"slashings_test.go",
"state_test.go",
@@ -44,6 +47,7 @@ go_test(
"//proto/beacon/p2p/v1:go_default_library",
"//proto/eth/v1alpha1:go_default_library",
"//shared/testutil:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
],

View File

@@ -0,0 +1,40 @@
package kv
import (
"context"
"fmt"
"github.com/boltdb/bolt"
"github.com/ethereum/go-ethereum/common"
"go.opencensus.io/trace"
)
// DepositContractAddress returns contract address is the address of
// the deposit contract on the proof of work chain.
func (k *Store) DepositContractAddress(ctx context.Context) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "BeaconDB.DepositContractAddress")
defer span.End()
var addr []byte
// #nosec G104. Always returns nil.
k.db.View(func(tx *bolt.Tx) error {
chainInfo := tx.Bucket(chainMetadataBucket)
addr = chainInfo.Get(depositContractAddressKey)
return nil
})
return addr, nil
}
// SaveDepositContractAddress to the db. It returns an error if an address has been previously saved.
func (k *Store) SaveDepositContractAddress(ctx context.Context, addr common.Address) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.VerifyContractAddress")
defer span.End()
return k.db.Update(func(tx *bolt.Tx) error {
chainInfo := tx.Bucket(chainMetadataBucket)
expectedAddress := chainInfo.Get(depositContractAddressKey)
if expectedAddress != nil {
return fmt.Errorf("cannot override deposit contract address: %v", expectedAddress)
}
return chainInfo.Put(depositContractAddressKey, addr.Bytes())
})
}

View File

@@ -0,0 +1,36 @@
package kv
import (
"context"
"testing"
"github.com/ethereum/go-ethereum/common"
)
func TestStore_DepositContract(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()
contractAddress := common.Address{1, 2, 3}
retrieved, err := db.DepositContractAddress(ctx)
if err != nil {
t.Fatal(err)
}
if retrieved != nil {
t.Errorf("Expected nil contract address, received %v", retrieved)
}
if err := db.SaveDepositContractAddress(ctx, contractAddress); err != nil {
t.Fatal(err)
}
retrieved, err = db.DepositContractAddress(ctx)
if err != nil {
t.Fatal(err)
}
if common.BytesToAddress(retrieved) != contractAddress {
t.Errorf("Expected address %#x, received %#x", contractAddress, retrieved)
}
otherAddress := common.Address{4, 5, 6}
if err := db.SaveDepositContractAddress(ctx, otherAddress); err == nil {
t.Error("Should not have been able to override old deposit contract address")
}
}

View File

@@ -58,6 +58,7 @@ func NewKVStore(dirPath string) (*Store, error) {
proposerSlashingsBucket,
attesterSlashingsBucket,
voluntaryExitsBucket,
chainMetadataBucket,
// Indices buckets.
attestationShardIndicesBucket,
attestationParentRootIndicesBucket,

View File

@@ -14,6 +14,7 @@ var (
proposerSlashingsBucket = []byte("proposer-slashings")
attesterSlashingsBucket = []byte("attester-slashings")
voluntaryExitsBucket = []byte("voluntary-exits")
chainMetadataBucket = []byte("chain-metadata")
// Key indices buckets.
blockParentRootIndicesBucket = []byte("block-parent-root-indices")
@@ -23,6 +24,7 @@ var (
attestationStartEpochIndicesBucket = []byte("attestation-start-epoch-indices")
attestationEndEpochIndicesBucket = []byte("attestation-end-epoch-indices")
// Block keys.
headBlockRootKey = []byte("head-root")
// Specific item keys.
headBlockRootKey = []byte("head-root")
depositContractAddressKey = []byte("deposit-contract")
)