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-06-23 10:15:01 +00:00
|
|
|
func (kv *Store) DepositContractAddress(ctx context.Context) ([]byte, error) {
|
2019-08-21 21:11:50 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.DepositContractAddress")
|
|
|
|
defer span.End()
|
|
|
|
var addr []byte
|
2020-06-23 10:15:01 +00:00
|
|
|
if err := kv.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-06-23 10:15:01 +00:00
|
|
|
func (kv *Store) SaveDepositContractAddress(ctx context.Context, addr common.Address) error {
|
2019-08-21 21:11:50 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.VerifyContractAddress")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-06-23 10:15:01 +00:00
|
|
|
return kv.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())
|
|
|
|
})
|
|
|
|
}
|