MCPcopy Create free account
hub / github.com/chain/txvm / SignBlock

Function SignBlock

protocol/bc/block.go:39–81  ·  view source on GitHub ↗

SignBlock produces a SignedBlock from a Block. It invokes its callback once for each position in [0..N) where N is the number of pubkeys in the previous block's NextPredicate, until a quorum of signatures is obtained. Any callback returning an error will cause SignBlock to return with an error. A c

(b *UnsignedBlock, prev *BlockHeader, f func(int) (interface{}, error))

Source from the content-addressed store, hash-verified

37// skipped silently. If too many callbacks do this, SignBlock will
38// return ErrTooFewSignatures.
39func SignBlock(b *UnsignedBlock, prev *BlockHeader, f func(int) (interface{}, error)) (*Block, error) {
40 sb := &Block{UnsignedBlock: b}
41 if b.Height == 1 {
42 // Block at height 1 does not require a signature.
43 return sb, nil
44 }
45 if prev == nil {
46 return nil, errors.New("must supply previous blockheader to Sign")
47 }
48 pred := prev.NextPredicate
49 if pred == nil {
50 return nil, errors.New("no next predicate in previous blockheader")
51 }
52 if pred.Version != 1 {
53 return nil, errors.New("unknown predicate version")
54 }
55 sb.Arguments = make([]interface{}, len(pred.Pubkeys))
56 q := pred.Quorum
57 if q > 0 && f == nil {
58 return nil, errors.New("no signature function provided")
59 }
60 for i := 0; q > 0 && i < len(pred.Pubkeys); i++ {
61 arg, err := f(i)
62 if err != nil {
63 return nil, errors.Wrapf(err, "getting signature %d for block %d", i, b.Height)
64 }
65 if arg == nil {
66 continue
67 }
68 if sig, ok := arg.([]byte); ok {
69 if len(sig) > 0 {
70 sb.Arguments[i] = sig
71 q--
72 }
73 continue
74 }
75 return nil, errors.New("non-signature in block arguments")
76 }
77 if q > 0 {
78 return nil, ErrTooFewSignatures
79 }
80 return sb, nil
81}
82
83// MarshalText fulfills the json.Marshaler interface.
84// This guarantees that blocks will get deserialized correctly

Callers 6

TestPersistSnapshotFunction · 0.92
MakeBlockFunction · 0.92
signFunction · 0.92
TestSignBlockFunction · 0.85

Calls 2

NewFunction · 0.92
WrapfFunction · 0.92

Tested by 4

TestPersistSnapshotFunction · 0.74
TestSignBlockFunction · 0.68