prysm-pulse/beacon-chain/db/attestation.go
Raul Jordan 1065617087 Replace Attestation Type Primitive With Proto Generated Type (#1149)
* first commit, remote att types

* no more agg attestation proto

* regen mock

* only attestations

* proto

* att process

* fix att references

* more tests passing

* use att protos

* complete

* Update dependency com_github_deckarep_golang_set to v1 (#1159)

* Update dependency com_github_edsrzf_mmap_go to v1 (#1160)

* Update dependency com_github_go_stack_stack to v1 (#1161)

* Update dependency com_github_rs_cors to v1 (#1162)

* Update dependency in_gopkg_urfave_cli_v1 to v1 (#1163)

* change visibility
2018-12-22 15:30:59 -05:00

66 lines
1.6 KiB
Go

package db
import (
"fmt"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
att "github.com/prysmaticlabs/prysm/beacon-chain/core/attestations"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
// SaveAttestation puts the attestation record into the beacon chain db.
func (db *BeaconDB) SaveAttestation(attestation *pb.Attestation) error {
hash := att.Key(attestation.GetData())
encodedState, err := proto.Marshal(attestation)
if err != nil {
return err
}
return db.update(func(tx *bolt.Tx) error {
a := tx.Bucket(attestationBucket)
return a.Put(hash[:], encodedState)
})
}
// GetAttestation retrieves an attestation record from the db using its hash.
func (db *BeaconDB) GetAttestation(hash [32]byte) (*pb.Attestation, error) {
var attestation *pb.Attestation
err := db.view(func(tx *bolt.Tx) error {
a := tx.Bucket(attestationBucket)
enc := a.Get(hash[:])
if enc == nil {
return nil
}
var err error
attestation, err = createAttestation(enc)
return err
})
return attestation, err
}
// HasAttestation checks if the attestation exists.
func (db *BeaconDB) HasAttestation(hash [32]byte) bool {
exists := false
// #nosec G104
db.view(func(tx *bolt.Tx) error {
a := tx.Bucket(attestationBucket)
exists = a.Get(hash[:]) != nil
return nil
})
return exists
}
func createAttestation(enc []byte) (*pb.Attestation, error) {
protoAttestation := &pb.Attestation{}
if err := proto.Unmarshal(enc, protoAttestation); err != nil {
return nil, fmt.Errorf("failed to unmarshal encoding: %v", err)
}
return protoAttestation, nil
}