2019-08-21 21:11:50 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2019-08-21 21:11:50 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DepositContractAddress returns contract address is the address of
|
|
|
|
// the deposit contract on the proof of work chain.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) DepositContractAddress(ctx context.Context) ([]byte, error) {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.DepositContractAddress")
|
2019-08-21 21:11:50 +00:00
|
|
|
defer span.End()
|
|
|
|
var addr []byte
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
2019-08-21 21:11:50 +00:00
|
|
|
chainInfo := tx.Bucket(chainMetadataBucket)
|
|
|
|
addr = chainInfo.Get(depositContractAddressKey)
|
|
|
|
return nil
|
2020-04-13 04:11:09 +00:00
|
|
|
}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-21 21:11:50 +00:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveDepositContractAddress to the db. It returns an error if an address has been previously saved.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) SaveDepositContractAddress(ctx context.Context, addr common.Address) error {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.VerifyContractAddress")
|
2019-08-21 21:11:50 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
2019-08-21 21:11:50 +00:00
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|