Andrea Franz 874a3e8151
Feature/mailserver registry smart contract (#1135)
* add verifier and test using simulated backend

* add ContractCaller

* commit simulated backend after deploy and after smart contract writes

* use bind.NewKeyedTransactor for all transactions in tests

* rename RegistryVerifier to Verifier

* initialize contract verifier if MailServerRegistryAddress config is set

* use contractAddress.Hash()

* refactoring

* use fmt.Sprintf to format contract address in logs

* fix test and lint warnings

* update Gopkg.lock

* update Gopkg.lock once more
2018-08-20 15:55:43 +02:00

45 lines
1.3 KiB
Go

package registry
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
)
var logger = log.New("package", "mailserver/registry")
// Verifier verifies nodes based on a smart contract.
type Verifier struct {
rc *RegistryCaller
}
// NewVerifier returns a new Verifier instance.
func NewVerifier(contractCaller bind.ContractCaller, contractAddress common.Address) (*Verifier, error) {
logger.Debug("initializing mailserver registry verifier", "address", fmt.Sprintf("0x%x", contractAddress))
rc, err := NewRegistryCaller(contractAddress, contractCaller)
if err != nil {
logger.Debug("error initializing mailserver registry verifier", "address", fmt.Sprintf("0x%x", contractAddress))
return nil, err
}
return &Verifier{
rc: rc,
}, nil
}
// VerifyNode checks if a given node is trusted using a smart contract.
func (v *Verifier) VerifyNode(ctx context.Context, nodeID discover.NodeID) bool {
res, err := v.rc.Exists(&bind.CallOpts{Context: ctx}, nodeID.Bytes())
logger.Debug("verifying node", "id", nodeID, "verified", res)
if err != nil {
logger.Error("error verifying node", "id", nodeID, "error", err)
return false
}
return res
}