This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
consensus.go
78 lines (69 loc) · 2.73 KB
/
consensus.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
// Copyright (c) 2017-2018 The qitmeer developers
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2015-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txscript
import (
"fmt"
)
const (
// LockTimeThreshold is the number below which a lock time is
// interpreted to be a block number. Since an average of one block
// is generated per 10 minutes, this allows blocks for about 9,512
// years.
LockTimeThreshold = 5e8 // Tue Nov 5 00:53:20 1985 UTC
// maxUniqueCoinbaseNullDataSize is the maximum number of bytes allowed
// in the pushed data output of the coinbase output that is used to
// ensure the coinbase has a unique hash.
maxUniqueCoinbaseNullDataSize = 256
)
// ExtractCoinbaseNullData ensures the passed script is a nulldata script as
// required by the consensus rules for the coinbase output that is used to
// ensure the coinbase has a unique hash and returns the data it pushes.
func ExtractCoinbaseNullData(pkScript []byte) ([]byte, error) {
pops, err := parseScript(pkScript)
if err != nil {
return nil, fmt.Errorf("script parse failure")
}
// The nulldata in the coinbase must be a single OP_RETURN followed by a
// data push up to maxUniqueCoinbaseNullDataSize bytes.
//
// NOTE: This is intentionally not using GetScriptClass and the related
// functions because those are specifically for standardness checks which
// can change over time and this function is specifically intended to be
// used by the consensus rules.
//
// Also of note is that technically normal nulldata scripts support encoding
// numbers via small opcodes, however the consensus rules require the block
// height to be encoded as a 4-byte little-endian uint32 pushed via a normal
// data push, as opposed to using the normal number handling semantics of
// scripts, so this is specialized to accommodate that.
if len(pops) == 1 && pops[0].opcode.value == OP_RETURN {
return nil, nil
}
if len(pops) == 2 && pops[0].opcode.value == OP_RETURN &&
pops[1].opcode.value <= OP_PUSHDATA4 && len(pops[1].data) <=
maxUniqueCoinbaseNullDataSize {
return pops[1].data, nil
}
return nil, fmt.Errorf("not a properly-formed nulldata script")
}
// ExtractCoinbaseData
func ExtractCoinbaseData(pkScript []byte) ([][]byte, error) {
pops, err := parseScript(pkScript)
if err != nil {
return nil, fmt.Errorf("script parse failure")
}
result := [][]byte{}
for _, v := range pops {
if v.opcode.value == OP_0 {
result = append(result, []byte{0})
} else if v.opcode.value >= OP_1 && v.opcode.value <= OP_16 {
result = append(result, []byte{v.opcode.value - (OP_1 - 1)})
} else {
result = append(result, v.GetData())
}
}
return result, nil
}