Skip to content

Commit

Permalink
noto: propagate errors back through domain handler
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <[email protected]>
  • Loading branch information
awrichar committed Aug 19, 2024
1 parent bc48206 commit 4c46096
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions domains/noto/internal/noto/noto.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,30 @@ func (d *Noto) handler(ctx context.Context) {
log.L(ctx).Errorf("Error receiving message - terminating handler loop: %v", err)
return
}
err = d.handleMessage(ctx, in)
reply, err := d.handleMessage(ctx, in)
if err != nil {
log.L(ctx).Errorf("Error handling message - terminating handler loop: %v", err)
return
reply = &pb.DomainAPIError{ErrorMessage: err.Error()}
err = nil
}
if reply != nil {
if err = d.sendReply(ctx, in, reply); err != nil {
log.L(ctx).Errorf("Error sending message reply - terminating handler loop: %v", err)
return
}
}
}
}

func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) error {
func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) (reply proto.Message, err error) {
body, err := message.Body.UnmarshalNew()
if err != nil {
return err
return nil, err
}

inflight := d.replies.getInflight(message.CorrelationId)
if inflight != nil {
inflight.done <- message
return nil
return nil, nil
}

switch m := body.(type) {
Expand All @@ -284,75 +290,73 @@ func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) error {
var config Config
err := yaml.Unmarshal([]byte(m.ConfigYaml), &config)
if err != nil {
return err
return nil, err
}
factoryJSON, err := json.Marshal(d.Factory.ABI)
if err != nil {
return err
return nil, err
}
notoJSON, err := json.Marshal(d.Contract.ABI)
if err != nil {
return err
return nil, err
}
constructorJSON, err := json.Marshal(NotoConstructorABI)
if err != nil {
return err
return nil, err
}
schemaJSON, err := json.Marshal(NotoCoinABI)
if err != nil {
return err
return nil, err
}

response := &pb.ConfigureDomainResponse{
return &pb.ConfigureDomainResponse{
DomainConfig: &pb.DomainConfig{
FactoryContractAddress: config.FactoryAddress,
FactoryContractAbiJson: string(factoryJSON),
PrivateContractAbiJson: string(notoJSON),
ConstructorAbiJson: string(constructorJSON),
AbiStateSchemasJson: []string{string(schemaJSON)},
},
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.InitDomainRequest:
log.L(ctx).Infof("Received InitDomainRequest")
d.domainID = m.DomainUuid
d.coinSchema = m.AbiStateSchemas[0]
return d.sendReply(ctx, message, &pb.InitDomainResponse{})
return &pb.InitDomainResponse{}, nil

case *pb.InitDeployTransactionRequest:
log.L(ctx).Infof("Received InitDeployTransactionRequest")

var params NotoConstructorParams
err := yaml.Unmarshal([]byte(m.Transaction.ConstructorParamsJson), &params)
if err != nil {
return err
return nil, err
}
log.L(ctx).Infof("Deployment parameters: %+v", params)

response := &pb.InitDeployTransactionResponse{
return &pb.InitDeployTransactionResponse{
RequiredVerifiers: []*pb.ResolveVerifierRequest{
{
Lookup: params.Notary,
Algorithm: signer.Algorithm_ECDSA_SECP256K1_PLAINBYTES,
},
},
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.PrepareDeployTransactionRequest:
log.L(ctx).Infof("Received PrepareDeployTransactionRequest")

var params NotoConstructorParams
err := yaml.Unmarshal([]byte(m.Transaction.ConstructorParamsJson), &params)
if err != nil {
return err
return nil, err
}
log.L(ctx).Infof("Deployment parameters: %+v", params)
log.L(ctx).Infof("Resolved verifiers: %+v", m.ResolvedVerifiers)
notary := m.ResolvedVerifiers[0].Verifier

response := &pb.PrepareDeployTransactionResponse{
return &pb.PrepareDeployTransactionResponse{
Transaction: &pb.BaseLedgerTransaction{
FunctionName: "deploy",
ParamsJson: fmt.Sprintf(`{
Expand All @@ -361,24 +365,22 @@ func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) error {
}`, m.Transaction.TransactionId, notary),
},
SigningAddress: params.Notary,
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.InitTransactionRequest:
log.L(ctx).Infof("Received InitTransactionRequest")

response := &pb.InitTransactionResponse{
return &pb.InitTransactionResponse{
RequiredVerifiers: []*pb.ResolveVerifierRequest{},
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.AssembleTransactionRequest:
log.L(ctx).Infof("Received AssembleTransactionRequest")

var functionABI abi.Entry
err := json.Unmarshal([]byte(m.Transaction.FunctionAbiJson), &functionABI)
if err != nil {
return err
return nil, err
}

var assembled *pb.AssembledTransaction
Expand All @@ -389,16 +391,16 @@ func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) error {
err = fmt.Errorf("Unsupported method: %s", functionABI.Name)
}
if err != nil {
return err
return nil, err
}

_, err = d.decodeDomainConfig(ctx, m.Transaction.ContractConfig)
if err != nil {
return err
return nil, err
}
// TODO: use config.Notary instead of hard-coding below

response := &pb.AssembleTransactionResponse{
return &pb.AssembleTransactionResponse{
AssemblyResult: pb.AssembleTransactionResponse_OK,
AssembledTransaction: assembled,
AttestationPlan: []*pb.AttestationRequest{
Expand All @@ -411,16 +413,14 @@ func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) error {
},
},
},
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.EndorseTransactionRequest:
log.L(ctx).Infof("Received EndorseTransactionRequest")

response := &pb.EndorseTransactionResponse{
return &pb.EndorseTransactionResponse{
EndorsementResult: pb.EndorseTransactionResponse_ENDORSER_SUBMIT,
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.PrepareTransactionRequest:
log.L(ctx).Infof("Received PrepareTransactionRequest")
Expand All @@ -442,24 +442,23 @@ func (d *Noto) handleMessage(ctx context.Context, message *pb.Message) error {
}
paramsJSON, err := json.Marshal(params)
if err != nil {
return err
return nil, err
}

response := &pb.PrepareTransactionResponse{
return &pb.PrepareTransactionResponse{
Transaction: &pb.BaseLedgerTransaction{
FunctionName: "transfer", // TODO: can we have more than one method on base ledger?
ParamsJson: string(paramsJSON),
},
}
return d.sendReply(ctx, message, response)
}, nil

case *pb.DomainAPIError:
log.L(ctx).Errorf("Received error: %s", m.ErrorMessage)
return nil
return nil, nil

default:
log.L(ctx).Errorf("Unknown type: %s", reflect.TypeOf(m))
return nil
log.L(ctx).Warnf("Unhandled message type: %s", reflect.TypeOf(m))
return nil, nil
}
}

Expand Down

0 comments on commit 4c46096

Please sign in to comment.