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

Function BlockSig

protocol/validation/validation.go:29–79  ·  view source on GitHub ↗

BlockSig checks the predicate against b.

(b *bc.Block, predicate *bc.Predicate)

Source from the content-addressed store, hash-verified

27
28// BlockSig checks the predicate against b.
29func BlockSig(b *bc.Block, predicate *bc.Predicate) error {
30 if predicate.Version != 1 {
31 return errors.WithDetailf(errBadPredicate, "predicate version %d", predicate.Version)
32 }
33 if predicate.Quorum < 0 {
34 return errors.WithDetailf(errBadPredicate, "predicate quorum %d", predicate.Quorum)
35 }
36 if int(predicate.Quorum) > len(predicate.Pubkeys) {
37 return errors.WithDetailf(errBadPredicate, "predicate quorum %d, pubkeys %d", predicate.Quorum, len(predicate.Pubkeys))
38 }
39 if len(b.Arguments) != len(predicate.Pubkeys) {
40 return errors.WithDetailf(errBadArguments, "pubkeys %d, signatures %d", len(predicate.Pubkeys), len(b.Arguments))
41 }
42
43 var (
44 sigCount int32
45 hash = b.Hash()
46 )
47
48 for i := 0; i < len(b.Arguments); i++ {
49 pk := predicate.Pubkeys[i]
50 if len(pk) != ed25519.PublicKeySize {
51 return errors.WithDetailf(errBadPredicate, "public key length %d", len(pk))
52 }
53
54 sig, ok := b.Arguments[i].([]byte)
55 if !ok {
56 return errors.WithDetailf(errBadArguments, "invalid signature type %T", b.Arguments[i])
57 }
58
59 if len(sig) == 0 {
60 continue
61 }
62
63 if len(sig) != ed25519.SignatureSize {
64 return errors.WithDetailf(errBadArguments, "invalid signature length %d", len(sig))
65 }
66
67 if !ed25519.Verify(pk, hash.Bytes(), sig) {
68 return errors.WithDetailf(errBadArguments, "message %x, public key %x, signature %x", hash.Bytes(), pk, sig)
69 }
70
71 sigCount++
72 }
73
74 if sigCount != predicate.Quorum {
75 return errors.WithDetail(errBadArguments, "insufficient signatures for quorum")
76 }
77
78 return nil
79}
80
81// Block validates a block and the transactions within.
82// It does not check the predicate; for that, see ValidateBlockSig.

Callers 2

validateFunction · 0.92
TestBlockSigFunction · 0.85

Calls 5

WithDetailfFunction · 0.92
VerifyFunction · 0.92
WithDetailFunction · 0.92
HashMethod · 0.45
BytesMethod · 0.45

Tested by 1

TestBlockSigFunction · 0.68