/
build_cache.go
165 lines (138 loc) · 4.26 KB
/
build_cache.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package deployer
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
log "github.com/xlab/suplog"
"github.com/InjectiveLabs/evm-deploy-contract/sol"
)
var ErrNoCache = errors.New("no cached version")
type BuildCache interface {
StoreContract(absSolPath string, contract *sol.Contract) error
LoadContract(absSolPath, contractName string, coverage bool) (contract *sol.Contract, err error)
Clear() error
}
type BuildCacheEntry struct {
Timestamp time.Time `json:"timestamp"`
CodeHash string `json:"codeHash"`
AllPaths []string `json:"allPaths"`
ContractName string `json:"contractName"`
CompilerVersion string `json:"compilerVersion"`
Coverage bool `json:"coverage"`
Statements [][]int `json:"statements"`
ABI json.RawMessage `json:"abi"`
Bin string `json:"bin"`
}
type buildCache struct {
prefix string
}
func NewBuildCache(prefix string) (BuildCache, error) {
if err := os.MkdirAll(prefix, 0755); err != nil {
err = errors.Wrap(err, "failed to prepare build cache dir")
return nil, err
}
c := &buildCache{
prefix: prefix,
}
return c, nil
}
func (b *buildCache) StoreContract(absSolPath string, contract *sol.Contract) error {
hash, err := sha3file(absSolPath)
if err != nil {
err = errors.Wrap(err, "failed to hash source")
return err
}
entry := &BuildCacheEntry{
Timestamp: time.Now().UTC(),
CodeHash: hash,
AllPaths: contract.AllPaths,
ContractName: contract.Name,
CompilerVersion: contract.CompilerVersion,
Coverage: contract.Coverage,
Statements: contract.Statements,
ABI: json.RawMessage(contract.ABI),
Bin: contract.Bin,
}
entryContents, _ := json.MarshalIndent(entry, "", "\t")
entryFileName := fmt.Sprintf("sol_%s_%s.json", strings.ToLower(contract.Name), hash)
if contract.Coverage {
entryFileName = fmt.Sprintf("sol_%s_%s_coverage.json", strings.ToLower(contract.Name), hash)
}
err = ioutil.WriteFile(filepath.Join(b.prefix, entryFileName), entryContents, 0655)
if err != nil {
err = errors.Wrap(err, "failed write cache entry file")
return err
}
return nil
}
func (b *buildCache) LoadContract(absSolPath, contractName string, coverage bool) (contract *sol.Contract, err error) {
hash, err := sha3file(absSolPath)
if err != nil {
err = errors.Wrap(err, "failed to hash source")
return nil, err
}
entryFileName := fmt.Sprintf("sol_%s_%s.json", strings.ToLower(contractName), hash)
if coverage {
entryFileName = fmt.Sprintf("sol_%s_%s_coverage.json", strings.ToLower(contractName), hash)
}
entryContents, err := ioutil.ReadFile(filepath.Join(b.prefix, entryFileName))
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNoCache
}
err = errors.Wrap(err, "failed read cache entry file")
return nil, err
}
var entry BuildCacheEntry
if err := json.Unmarshal(entryContents, &entry); err != nil {
err = errors.Wrap(err, "failed to unmarshal cache entry")
return nil, err
} else if entry.ContractName != contractName {
err = errors.Wrap(err, "cache entry contract name mismatch")
return nil, err
}
contract = &sol.Contract{
SourcePath: absSolPath,
AllPaths: entry.AllPaths,
Name: entry.ContractName,
CompilerVersion: entry.CompilerVersion,
Coverage: entry.Coverage,
Statements: entry.Statements,
ABI: []byte(entry.ABI),
Bin: entry.Bin,
}
return contract, nil
}
func (b *buildCache) Clear() error {
return filepath.Walk(b.prefix, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if path == b.prefix {
return nil
} else if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == ".json" {
if err := os.Remove(path); err != nil {
log.WithError(err).Warningln("failed to cleanup", path)
}
}
return nil
})
}
func sha3file(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
err = errors.Wrap(err, "failed to read .sol file")
return "", err
}
hashBytes := crypto.Keccak256(contents)
return hex.EncodeToString(hashBytes), nil
}