forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_input.go
50 lines (45 loc) · 1.16 KB
/
tx_input.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
package payload
import (
"fmt"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/state"
"github.com/hyperledger/burrow/execution/errors"
)
func (input *TxInput) String() string {
return fmt.Sprintf("TxInput{%s, Amount: %v, Sequence:%v}", input.Address, input.Amount, input.Sequence)
}
func (input *TxInput) Validate(acc acm.Account) error {
if input.Address != acc.Address() {
return fmt.Errorf("trying to validate input from address %v but passed account %v", input.Address,
acc.Address())
}
// Check sequences
if acc.Sequence()+1 != uint64(input.Sequence) {
return ErrTxInvalidSequence{
Input: input,
Account: acc,
}
}
// Check amount
if acc.Balance() < uint64(input.Amount) {
return errors.ErrorCodeInsufficientFunds
}
return nil
}
func ValidateInputs(getter state.AccountGetter, ins []*TxInput) error {
for _, in := range ins {
acc, err := getter.GetAccount(in.Address)
if err != nil {
return err
}
if acc == nil {
return fmt.Errorf("validateInputs() expects to be able to retrive accoutn %v but it was not found",
in.Address)
}
err = in.Validate(acc)
if err != nil {
return err
}
}
return nil
}