This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
send_context.go
85 lines (72 loc) · 1.91 KB
/
send_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package contexts
import (
"fmt"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/permission"
"github.com/hyperledger/burrow/txs/payload"
)
type SendContext struct {
StateWriter acmstate.ReaderWriter
Logger *logging.Logger
tx *payload.SendTx
}
func (ctx *SendContext) Execute(txe *exec.TxExecution, p payload.Payload) error {
var ok bool
ctx.tx, ok = p.(*payload.SendTx)
if !ok {
return fmt.Errorf("payload must be NameTx, but is: %v", txe.Envelope.Tx.Payload)
}
accounts, inTotal, err := getInputs(ctx.StateWriter, ctx.tx.Inputs)
if err != nil {
return err
}
// ensure all inputs have send permissions
err = allHavePermission(ctx.StateWriter, permission.Send, accounts, ctx.Logger)
if err != nil {
return errors.Wrap(err, "at least one input lacks permission for SendTx")
}
// add outputs to accounts map
// if any outputs don't exist, all inputs must have CreateAccount perm
accounts, err = getOrMakeOutputs(ctx.StateWriter, accounts, ctx.tx.Outputs, ctx.Logger)
if err != nil {
return err
}
outTotal, err := validateOutputs(ctx.tx.Outputs)
if err != nil {
return err
}
if outTotal > inTotal {
return errors.ErrorCodeInsufficientFunds
}
if outTotal < inTotal {
return errors.ErrorCodeOverpayment
}
if outTotal == 0 {
return errors.ErrorCodeZeroPayment
}
// Good! Adjust accounts
err = adjustByInputs(accounts, ctx.tx.Inputs, ctx.Logger)
if err != nil {
return err
}
err = adjustByOutputs(accounts, ctx.tx.Outputs)
if err != nil {
return err
}
for _, acc := range accounts {
err = ctx.StateWriter.UpdateAccount(acc)
if err != nil {
return err
}
}
for _, i := range ctx.tx.Inputs {
txe.Input(i.Address, nil)
}
for _, o := range ctx.tx.Outputs {
txe.Output(o.Address, nil)
}
return nil
}